SML Option Monad (binding operator does not work)

I want to implement Option Monad in SML, so I can use them the same way they can be used in haskell. What I did does not work.

infix 1 >>=
signature MONAD =
sig
    type 'a m 
    val return : 'a -> 'a m 
    val >>= : 'a m * ('a -> 'b m) -> 'b m 
end;

structure OptionM : MONAD =
struct
    type 'a m = 'a option
    val return = SOME
    fun x >>= k = Option.mapPartial k x
end;

val x = OptionM.return 3;
x (OptionM.>>=) (fn y => NONE);

Result:

stdIn:141.1-141.31 Error: operator is not a function [tycon mismatch] 
operator: int OptionM.m
in expression:
x OptionM.>>=

What can I do to do the last job?

+4
source share
1 answer

Unlike Haskell, skilled infix operators (such as A.+or Option.>>=) are not infixes in SML. You must use them as unskilled, for example. either by opening the module, or by localizing it.

Btw, you probably want to define >>=as right-associative, i.e. to use infixr.

, SML , Haskell. , >>= lambdas, fn :

foo >>= (fn x => bar >>= (fn y => baz >>= (fn z => boo)))
+6

All Articles