35 lines
941 B
Text
35 lines
941 B
Text
#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
|