Kai
Kai

Reputation: 4037

SML function call doesn't parse arguments as arguments

I'm getting what seems to be a precedence issue in calling an SML function, substitute:

fun substitute v new (typevar q) = ...

And I am calling this from another function:

fun new_type_vars (typevar v) non_gens = 
  substitute v new_var() (typevar v)

But I get an error:

C:/sml/type_checker.sml:22.48-23.44 Error: operator and operand don't agree [tycon mismatch]
  operator domain: type_exp
  operand:         unit -> string
  in expression:
    (substitute v) new_var

Which seems to suggest that it's trying to call (substitute v), and then call the result of that call with argument new_var.

I've tried adding parentheses around the whole thing, but that doesn't help, and when I add parenthesis around the arguments like (v new_var...) it thinks that v is a function application on new_var. What's going on with this function call?

Upvotes: 0

Views: 812

Answers (2)

Darius Bacon
Darius Bacon

Reputation: 15134

You probably want to change

substitute v new_var() (typevar v)

to

substitute v (new_var()) (typevar v)

(boutta's answer explains why.)

Upvotes: 1

boutta
boutta

Reputation: 24639

I don't understand everything, since you don't give all the types of the different operations and variables.

But the general problem you've got is as you already guessed, SML executes the function calls (and binds the variables) from the left side. Here an example:

fun f a:int b:int c:string = ...

f is thus a function of the type int -> int -> string -> ... and implicitly adds the parentheses fun (((f a:int) b:int) c:int) = ...

This means you can use it for example like this:

var f2 = f 3;

And f2 has now the type int -> string -> ...

Upvotes: 1

Related Questions