0

I have the following type:

datatype pattern = Wildcard
         | Variable of string
         | UnitP
         | ConstP of int
         | TupleP of pattern list
         | ConstructorP of string * pattern

I am trying to create a function that accepts this type (simplified example):

fun check_pat p =   
     case p of
        Wildcard          => 1
      | Variable x        => 2
      | TupleP ps         => 3
      | ConstructorP(_,p) => 4
      | _                 => 5

but get an error when I call it with TupleP like so:

check_pat TupleP[Variable "x",Variable "xx"];

Error:

stdIn:3.1-3.46 Error: operator and operand do not agree [tycon mismatch]
  operator domain: pattern
  operand:         pattern list -> pattern
  in expression:
    check_pat2 TupleP

But if I set this TupleP type to a variable and call it with that variable I am able to get the intended result. Could anyone let me know why this is the case?

 val x = TupleP[Variable "x",Variable "xx"];
 check_pat x;
 val it = 3 : int
3
  • 1
    SML interprets check_pat TupleP[Variable "x",Variable "xx"]; as (check_pat TupleP) [Variable "x",Variable "xx"]; which is obviously something you do not want. But check_pat (TupleP [Variable "x",Variable "xx"]); should work fine. Commented Nov 25, 2022 at 7:44
  • 1
    The error message basically says that TupleP is something that produces a pattern from a pattern list (i.e., pattern list -> pattern) whereas check_pat expects a pattern. Commented Nov 25, 2022 at 7:48
  • 2
    Also to enhance readability, I suggest you integrate pattern matching in your function definition instead of using case, i.e., write fun check_pat Wildcard = 1 | check_pat (Variable _) = 2 | .... Commented Nov 25, 2022 at 7:51

1 Answer 1

3

As noted in comments, this:

check_pat TupleP[Variable "x",Variable "xx"]

parses as this:

(check_pat TupleP) [Variable "x",Variable "xx"]

rather than what you likely intended:

check_pat (TupleP [Variable "x",Variable "xx"])

Assigning TupleP [Variable "x",Variable "xx"] to x and then calling check_pat x also ensures that check_pat is called correctly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.