Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Monads-Maybe-monad/ATS/monads-maybe-monad.ats
Normal file
35
Task/Monads-Maybe-monad/ATS/monads-maybe-monad.ats
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
(* There are "Option" and "Option_vt" in the ATS2 prelude, but I shall
|
||||
construct something anew. *)
|
||||
|
||||
datatype Maybe (a : t@ype+) =
|
||||
| Nothing of ()
|
||||
| Just of a
|
||||
|
||||
fn {a, b : t@ype}
|
||||
bind_Maybe {u : bool}
|
||||
(m : Maybe a,
|
||||
f : a -<cloref1> Maybe b) : Maybe b =
|
||||
case+ m of
|
||||
| Nothing {a} () => Nothing {b} ()
|
||||
| Just {a} x => f x
|
||||
|
||||
infixl 0 >>=
|
||||
overload >>= with bind_Maybe
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val f : int -<cloref1> Maybe int =
|
||||
lam i =<cloref1> if (i : int) < 0 then Nothing () else Just i
|
||||
val g : int -<cloref1> Maybe string =
|
||||
lam i =<cloref1> Just (tostring_val<int> i)
|
||||
in
|
||||
case+ Just 123 >>= f >>= g of
|
||||
| Nothing () => println! ("Nothing ()")
|
||||
| Just s => println! ("Just (\"", s : string, "\")");
|
||||
case+ Just ~123 >>= f >>= g of
|
||||
| Nothing () => println! ("Nothing ()")
|
||||
| Just s => println! ("Just (\"", s : string, "\")")
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue