Search code examples
smlsmlnjfunction-composition

SML: Function composition with isSome


I have the following examples and they do not work even though the types do matchup with each other

- isSome;
val it = fn : 'a option -> bool


- SOME;
val it = fn : 'a -> 'a option
- val my_converter = (fn x => if x = 5 then SOME x else NONE);
val my_converter = fn : int -> int option

Both SOME and my_converter return an option but when I do the following

- fn x => isSome o SOME x;
stdIn:19.9-19.24 Error: operator and operand don't agree [tycon mismatch]
  operator domain: ('Z option -> bool) * ('Y -> 'Z option)
  operand:         ('Z option -> bool) * 'X option
  in expression:
    isSome o SOME x

I get a type error why?


Solution

  • The error message is telling you that o wants a function operand but what it actually gets is an option. This is because isSome o SOME x parses as isSome o (SOME x), which makes no sense.

    You can fix this by writing

    (isSome o SOME) x
    

    instead.