Skip to content

Instantly share code, notes, and snippets.

@doublec
Created January 3, 2018 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublec/a3cc8f3431cabe9a319c8e7ba27e7890 to your computer and use it in GitHub Desktop.
Save doublec/a3cc8f3431cabe9a319c8e7ba27e7890 to your computer and use it in GitHub Desktop.
GADT example in ATS2
(*
$ patscc -o gadt gadt.dats
$ ./gadt
res1=true and res2=8
*)
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"
datatype Expr (t@ype) =
| I(int) of int
| B(bool) of bool
| Add(int) of (Expr(int), Expr(int))
| Mul(int) of (Expr(int), Expr(int))
| Eq(bool) of (Expr(int), Expr(int))
fun{a:t@ype} eval (x:Expr(a)): a =
case+ x of
| I i => i
| B b => b
| Add (t1, t2) => eval(t1) + eval(t2)
| Mul (t1, t2) => eval(t1) * eval(t2)
| Eq (t1, t2) => eval(t1) = eval(t2)
implement main0() = let
val term1 = Eq(I(5), Add(I(1), I(4)))
val term2 = Mul(I(2), I(4))
val res1 = eval(term1)
val res2 = eval(term2)
in
println!("res1=", res1, " and res2=", res2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment