Quick actions

cmd+k|ctrl+k

Navigation

Languages

Functor Type Class in ATS

Snippet info

Language

Ats

Visibility

public

Author

steinwaywhw

Created

2018-05-02T03:02:30Z

Updated

2018-05-02T03:05:53Z

#include "share/atspre_staload.hats"

datatype list (a:t@ype) =
| Nil  (a) of ()
| Cons (a) of (a, list a)
#define :: Cons

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

(* type class *)
sortdef tycon = t@ype -> type
extern fun {f:tycon} {a,b:t@ype} fmap (a -<cloref1> b): f(a) -<cloref1> f(b)

(* instance list *)
implement (a,b:t@ype) fmap<list><a,b> (f) = 
    lam (xs: list a): list b => 
        case+ xs of 
        | Cons (x, xs) => Cons (f x, (fmap<list><a,b> f) xs)
        | _ => Nil ()
        
(* instance maybe *)
implement (a,b:t@ype) fmap<maybe><a,b> (f) = 
    lam (x: maybe a): maybe b => 
        case+ x of 
        | Just a => Just (f a)
        | Nothing () => Nothing ()

implement main0 () = let 
    val xs = 1 :: 2 :: 3 :: Nil() : list int
    val ys = fmap<list><int,int> (lam (x:int):int => x + 1) (xs)
    val zs  = fmap<list><int,int> (lam (x:int):int => let val _ = println! x in x end) (ys)
    
    val mx = Just 1 : maybe int
    val my = fmap<maybe><int,string> (lam (x:int):string => "hello") (mx)
    val mz = fmap<maybe><string,string> (lam (x:string):string => let val _ = println! x in x end) (my)
in 
end
INFO