0

I'm trying to do a pattern-matching function in CPN Tools using SML. I have defined a colorset EVENT: colset EVENT = product EVENTTYPE * EVENTTIME timed;

When comparing lists, I am only interested in the Event Type, so I'm trying to compare e.g. [a,b] to [ (a,0), (b,1) ] to - so I wrote the following SML-function which compares two lists:

fun pattern_match _ [] = true
  | pattern_match [] [x] = false
  | pattern_match (x::xs) (y::ys) =
    if #1 x = y
    then pattern_match xs ys
    else pattern_match xs (y::ys)

which only gives me an unspecified compiler error in evalloop.sml Since I'm fairly new to SML, my guess is the CPN-Tools #-operator is not supported by SML. Unfortunately I have no idea how to extract only the part of the tuple from the first list that I'm actually interested in. Any help on this?

sshine
  • 15,635
  • 1
  • 41
  • 66
DCH
  • 199
  • 2
  • 3
  • 13
  • Any reason you can't just `map (fn (a,_) => a) xs`, then compare the two lists directly? It doesn't look like you need the second value for anything in this function. – Inaimathi Aug 26 '15 at 16:30

1 Answers1

1

Inaimathi is probably correct that there is a simpler way to do what you want, though it is still a good exercise to fix your definition so that it works.

When I enter your fun definition in SML/NJ I get the error

stdIn:10.1-14.30 Error: unresolved flex record
   (can't tell what fields there are besides #1)

This means that SML's type inference can't resolve the type adequately. It is enough to give it a little hint:

fun pattern_match _ [] = true
|   pattern_match [] [y] = false
|   pattern_match ((x:string*int)::xs) (y::ys) = if #1 x = y
then pattern_match xs ys
else pattern_match xs (y::ys);

Now that SML knows that x is of type string*int it compiles.

The inferred type is (string * int) list -> string list -> bool and it works as expected:

- pattern_match [("a",0),("b",1)] ["a","b"];
val it = true : bool
John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Thanks a lot for your comment, John. Unfortunately, the Compiler inside CPN-Tools doesn't seem to like your solution, as I still get an unspecified error. When I remove the #1, the function compiles, but the comparison won't work that way. Even if I remove the #1 and add your solution to the body, it gives a compilation error. – DCH Aug 26 '15 at 19:02
  • @DustinH Weird. I don't know enough about it to be sure, but this sounds like a bug in CPN-Tools. It is interesting how it returns "unspecified error" as opposed to the fairly specific error message I got when I tried to compile your original definition. I've never heard of CPN-Tools before but it looks like a pretty interesting project (even if it has a bug in its SML implementation). – John Coleman Aug 26 '15 at 20:09
  • I think the problem is, that they don't give you the actual error the compiler throws, but just tells you there _was_ a compilation error. That makes debugging really hard. I just read that CPN-Tools actually uses SML/NJ - which makes this even more weird. Thanks for your input anyway! I've reached out to the developers of the tool now and will update this post in regard to their response. – DCH Aug 26 '15 at 20:51
  • 1
    +1, though rather than writing `(x:string*int)` and `#1 x`, I think it's probably better to write something like `(a, _)` and `a`. – ruakh Aug 27 '15 at 06:22