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.

143 lignes · 3269 octetsBrut · Historique
1import std/io
2import std/list
3
4// Perceus v2 (ADR 0040): a matched cell of known size is reused without a
5// size check. Tuples and record updates reuse cells too, and a cell can be
6// given to a function that builds one of its size. Results must be the
7// same whether cells are unique or shared.
8
9@derive Show
10pub type State { State(count: Int, name: String, tags: List<String>) }
11
12@derive Show
13pub type Tree { Leaf, Node(Tree, Int, Tree) }
14
15effect amb
16{
17 multi ctl choose :: -> Bool
18}
19
20// A record update: `s`'s cell is updated in place.
21@fip
22fn bump :: s: State -> State
23{
24 State(..s, count: s.count + 1)
25}
26
27// A tuple's cell, reused for the swapped tuple.
28@fip
29fn swap :: p: (Int, String) -> (String, Int)
30{
31 let (a, b) = p
32 (b, a)
33}
34
35@fip
36fn mirror :: t: Tree -> Tree
37{
38 match t { Leaf -> Leaf, Node(l, k, r) -> Node(mirror r, k, mirror l) }
39}
40
41// The released pair builds nothing here: `pair` builds a cell of its
42// size, and takes it.
43fn pair :: a: Int b: Int -> (Int, Int)
44{
45 (a + b, a * b)
46}
47
48fn combine :: p: (Int, Int) -> (Int, Int)
49{
50 let (a, b) = p
51 pair a b
52}
53
54// A token given on through two calls: `combine2` gives it to `choose_pair`,
55// whose `else` path builds nothing and gives it to `pair`.
56fn choose_pair :: a: Int b: Int flip: Bool -> (Int, Int)
57{
58 if flip
59 {
60 (b, a)
61 }
62 else
63 {
64 pair a b
65 }
66}
67
68fn combine2 :: p: (Int, Int) flip: Bool -> (Int, Int)
69{
70 let (a, b) = p
71 choose_pair a b flip
72}
73
74// A token kept across a multi-shot operation: both resumptions build the
75// state, from one released cell.
76fn relabel :: s: State -> State / <amb>
77{
78 match s
79 { State(count: c, name: n, tags: t) -> if choose ()
80 {
81 State(c, n <> "!", t)
82 }
83 else
84 {
85 State(c + 10, n, t)
86 }
87 }
88}
89
90fn all :: action: fn() -> a / <amb | e> -> List<a> / <e>
91{
92 handle
93 {
94 [action ()]
95 }
96 with
97 { choose -> list.append (resume True) (resume False) }
98}
99
100fn loop :: s: State n: Int -> State
101{
102 if n == 0
103 {
104 s
105 }
106 else
107 {
108 loop (bump s) (n - 1)
109 }
110}
111
112pub fn main :: -> () / <io>
113{
114 // Unique: updated in place a million times.
115 io.println "{loop State(0, "a", ["x"]) 1000000}"
116 // Shared: the original stays intact.
117 let s = State(1, "b", ["y", "z"])
118 let t = bump (bump s)
119 io.println "{s} {t}"
120 let p = (1, "one")
121 io.println "{swap p} {p} {swap (2, "two")}"
122 let tree = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf)))
123 io.println "{mirror tree}"
124 io.println "{tree}"
125 io.println "{mirror (mirror Node(Leaf, 5, Node(Leaf, 6, Leaf)))}"
126 let q = (3, 4)
127 io.println "{combine q} {q} {combine (5, 6)}"
128 io.println
129 "{combine2 (3, 4) True} {combine2 (3, 4) False} {combine2 q False} {q}"
130 io.println "{all fn { relabel State(0, "c", ["w"]) }}"
131 let shared = State(7, "d", [])
132 io.println "{all fn { relabel shared }} {shared}"
133}
134//> State(1000000, a, [x])
135//> State(1, b, [y, z]) State(3, b, [y, z])
136//> (one, 1) (1, one) (two, 2)
137//> Node(Node(Node(Leaf, 4, Leaf), 3, Leaf), 2, Node(Leaf, 1, Leaf))
138//> Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf)))
139//> Node(Leaf, 5, Node(Leaf, 6, Leaf))
140//> (7, 12) (3, 4) (11, 30)
141//> (4, 3) (7, 12) (7, 12) (3, 4)
142//> [State(0, c!, [w]), State(10, c, [w])]
143//> [State(7, d!, []), State(17, d, [])] State(7, d, [])