tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

133 lignes · 3508 octetsBrut · Historique
1// GADTs and existential types (ADR 0041): a typed expression evaluator,
2// values that carry their `Show` dictionary, and an actor protocol whose
3// requests say the type of their reply.
4
5import std/actor
6import std/io
7import std/list
8import std/process.{type Subject, type proc}
9import std/random.{type random}
10import std/string
11import std/time.{type clock}
12
13type Expr<a>
14{ IntLit(Int) -> Expr<Int>
15, BoolLit(Bool) -> Expr<Bool>
16, Add(Expr<Int>, Expr<Int>) -> Expr<Int>
17, Less(Expr<Int>, Expr<Int>) -> Expr<Bool>
18, If(Expr<Bool>, Expr<a>, Expr<a>) -> Expr<a>
19, Pair(Expr<b>, Expr<c>) -> Expr<(b, c)>
20, Fst(Expr<(a, b)>) -> Expr<a>
21, Lit(a) -> Expr<a> where Show<a>
22}
23
24fn eval :: e: Expr<a> -> a
25{
26 match e
27 { IntLit(n) -> n
28 , BoolLit(b) -> b
29 , Add(x, y) -> eval x + eval y
30 , Less(x, y) -> eval x < eval y
31 , If(c, t, f) -> if eval c { eval t } else { eval f }
32 , Pair(x, y) -> (eval x, eval y)
33 , Fst(p) -> (eval p).0
34 , Lit(v) -> v
35 }
36}
37
38/// Source text: `Lit` shows its value with the dictionary it stores.
39fn source :: e: Expr<a> -> String
40{
41 match e
42 { IntLit(n) -> show n
43 , BoolLit(b) -> show b
44 , Add(x, y) -> "({source x} + {source y})"
45 , Less(x, y) -> "({source x} < {source y})"
46 , If(c, t, f) -> "if {source c} then {source t} else {source f}"
47 , Pair(x, y) -> "({source x}, {source y})"
48 , Fst(p) -> "fst {source p}"
49 , Lit(v) -> "lit {v}"
50 }
51}
52
53/// Only constructors of `Expr<Int>` need an arm.
54fn size :: e: Expr<Int> -> Int
55{
56 match e
57 { IntLit(_) -> 1
58 , Add(x, y) -> 1 + size x + size y
59 , If(_, t, f) -> 1 + size t + size f
60 , Fst(_) -> 1
61 , Lit(_) -> 1
62 }
63}
64
65type Showable { Showable(b) -> Showable where Show<b> }
66
67fn describe :: s: Showable -> String
68{
69 match s { Showable(x) -> show x }
70}
71
72/// A request, indexed by the type of its reply.
73type Request<r>
74{ GetName(Int) -> Request<String>
75, GetAge(Int) -> Request<Int>
76, Ping -> Request<()>
77}
78
79/// A request with the subject its reply goes to: `r` is hidden.
80type Msg { Ask(Request<r>, Subject<r>) -> Msg }
81
82fn serve ::
83 base: Int
84 msg: Msg
85 -> actor.Next<Int> / <proc, clock, random, io, ffi>
86{
87 match msg
88 { Ask(req, reply) ->
89 {
90 match req
91 { GetName(id) -> process.send reply "user{id}"
92 , GetAge(id) -> process.send reply (base + id)
93 , Ping -> process.send reply ()
94 }
95 actor.Continue(base)
96 }
97 }
98}
99
100fn ask :: server: Subject<Msg> req: Request<r> -> r / <proc>
101{
102 actor.call server fn reply { Ask(req, reply) } 1000
103}
104
105pub fn main :: -> () / <io, proc>
106{
107 let e =
108 If( Less(IntLit(1), Add(IntLit(1), IntLit(2)))
109 , Add(IntLit(10), Lit(5))
110 , IntLit(0)
111 )
112 io.println "{source e} = {eval e} ({size e} nodes)"
113 let p = Pair(Lit("x"), Pair(BoolLit(True), Lit(2.5)))
114 let (s, (b, f)) = eval p
115 io.println "{source p} = {s} {b} {f}"
116 io.println (show (eval Fst(Pair(Add(IntLit(5), IntLit(5)), BoolLit(True)))))
117 let items =
118 [Showable(1), Showable("two"), Showable([3, 4]), Showable(Some(True))]
119 io.println (string.join (list.map items describe) ", ")
120 // The constructor as a function value: it takes its dictionary along.
121 io.println (string.join (list.map (list.map [7, 8] Showable) describe) " ")
122 let server = actor.start 40 serve
123 let name: String = ask server GetName(7)
124 let age: Int = ask server GetAge(2)
125 ask server Ping
126 io.println "{name} {age + 1}"
127}
128//> if (1 < (1 + 2)) then (10 + lit 5) else 0 = 15 (5 nodes)
129//> (lit x, (True, lit 2.5)) = x True 2.5
130//> 10
131//> 1, two, [3, 4], Some(True)
132//> 7 8
133//> user7 43