// GADTs and existential types (ADR 0041): a typed expression evaluator, // values that carry their `Show` dictionary, and an actor protocol whose // requests say the type of their reply. import std/actor import std/io import std/list import std/process.{type Subject, type proc} import std/random.{type random} import std/string import std/time.{type clock} type Expr { IntLit(Int) -> Expr , BoolLit(Bool) -> Expr , Add(Expr, Expr) -> Expr , Less(Expr, Expr) -> Expr , If(Expr, Expr, Expr) -> Expr , Pair(Expr, Expr) -> Expr<(b, c)> , Fst(Expr<(a, b)>) -> Expr , Lit(a) -> Expr where Show } fn eval :: e: Expr -> a { match e { IntLit(n) -> n , BoolLit(b) -> b , Add(x, y) -> eval x + eval y , Less(x, y) -> eval x < eval y , If(c, t, f) -> if eval c { eval t } else { eval f } , Pair(x, y) -> (eval x, eval y) , Fst(p) -> (eval p).0 , Lit(v) -> v } } /// Source text: `Lit` shows its value with the dictionary it stores. fn source :: e: Expr -> String { match e { IntLit(n) -> show n , BoolLit(b) -> show b , Add(x, y) -> "({source x} + {source y})" , Less(x, y) -> "({source x} < {source y})" , If(c, t, f) -> "if {source c} then {source t} else {source f}" , Pair(x, y) -> "({source x}, {source y})" , Fst(p) -> "fst {source p}" , Lit(v) -> "lit {v}" } } /// Only constructors of `Expr` need an arm. fn size :: e: Expr -> Int { match e { IntLit(_) -> 1 , Add(x, y) -> 1 + size x + size y , If(_, t, f) -> 1 + size t + size f , Fst(_) -> 1 , Lit(_) -> 1 } } type Showable { Showable(b) -> Showable where Show } fn describe :: s: Showable -> String { match s { Showable(x) -> show x } } /// A request, indexed by the type of its reply. type Request { GetName(Int) -> Request , GetAge(Int) -> Request , Ping -> Request<()> } /// A request with the subject its reply goes to: `r` is hidden. type Msg { Ask(Request, Subject) -> Msg } fn serve :: base: Int msg: Msg -> actor.Next / { match msg { Ask(req, reply) -> { match req { GetName(id) -> process.send reply "user{id}" , GetAge(id) -> process.send reply (base + id) , Ping -> process.send reply () } actor.Continue(base) } } } fn ask :: server: Subject req: Request -> r / { actor.call server fn reply { Ask(req, reply) } 1000 } pub fn main :: -> () / { let e = If( Less(IntLit(1), Add(IntLit(1), IntLit(2))) , Add(IntLit(10), Lit(5)) , IntLit(0) ) io.println "{source e} = {eval e} ({size e} nodes)" let p = Pair(Lit("x"), Pair(BoolLit(True), Lit(2.5))) let (s, (b, f)) = eval p io.println "{source p} = {s} {b} {f}" io.println (show (eval Fst(Pair(Add(IntLit(5), IntLit(5)), BoolLit(True))))) let items = [Showable(1), Showable("two"), Showable([3, 4]), Showable(Some(True))] io.println (string.join (list.map items describe) ", ") // The constructor as a function value: it takes its dictionary along. io.println (string.join (list.map (list.map [7, 8] Showable) describe) " ") let server = actor.start 40 serve let name: String = ask server GetName(7) let age: Int = ask server GetAge(2) ask server Ping io.println "{name} {age + 1}" } //> if (1 < (1 + 2)) then (10 + lit 5) else 0 = 15 (5 nodes) //> (lit x, (True, lit 2.5)) = x True 2.5 //> 10 //> 1, two, [3, 4], Some(True) //> 7 8 //> user7 43