Quick actions

cmd+k|ctrl+k

Navigation

Languages

Monad Type Class in ATS

Snippet info

Language

Ats

Visibility

public

Author

steinwaywhw

Created

2018-05-02T03:56:14Z

Updated

2018-05-02T04:01:51Z

#include "share/atspre_staload.hats"

datatype maybe (a:t@ype) = 
| Nothing (a) of ()
| Just    (a) of a

sortdef _tycon = t@ype -> type
extern fun {m:_tycon} {a:t@ype}   monad$pure (a): m(a)
extern fun {m:_tycon} {a,b:t@ype} monad$bind (m(a), a -<cloref1> m(b)): m(b)

implement (a:t@ype) monad$pure<maybe><a> (x) = 
    Just (x)
    
implement (a,b:t@ype) monad$bind<maybe><a,b> (x, f) = 
    case+ x of 
    | Just x     => f x
    | Nothing () => Nothing ()
    
infixl >>=
macdef >>= (x, f) = monad$bind (,(x), ,(f))

implement main0 () = let 
    val x = monad$pure<maybe><int> 10
    val n = x >>= (lam x => Just (x+32)) 
              >>= (lam y => let val _ = print_int y in Just " is the answer." end) 
              >>= (lam z => let val _ = print_string z in Nothing {int} () end)
              : maybe int
              
in 
end
INFO