tobogganSe connecter

louise

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

216 lignes · 4124 octetsBrut · Historique
1import std/io
2import std/list
3import std/ref
4import std/string
5
6// Multi-shot resumptions (ADR 0017): a `multi ctl` clause may call
7// `resume` several times; each call runs a copy of the rest of the body.
8
9effect amb
10{
11 multi ctl choose :: -> Bool
12 final ctl fail :: -> Never
13}
14
15// Every result of `action`: `choose` tries both answers, `fail` abandons
16// the current branch.
17fn all :: action: fn() -> a / <amb | e> -> List<a> / <e>
18{
19 handle
20 {
21 [action ()]
22 }
23 with
24 { choose -> list.append (resume True) (resume False), fail -> [] }
25}
26
27// The same, with `resume` kept in a list: no call is its last use, so
28// every call runs a copy, and the continuation itself is released when
29// the handler finishes.
30fn all_kept :: action: fn() -> a / <amb | e> -> List<a> / <e>
31{
32 handle
33 {
34 [action ()]
35 }
36 with
37 { choose ->
38 {
39 let kept = [resume]
40 let results = list.append (first_of kept True) (first_of kept False)
41 if list.length kept == 1
42 {
43 results
44 }
45 else
46 {
47 []
48 }
49 }
50 , fail -> []
51 }
52}
53
54fn first_of :: ks: List<fn(Bool) -> List<a> / <e>> b: Bool -> List<a> / <e>
55{
56 match ks { [k, ..] -> k b, [] -> [] }
57}
58
59fn digit :: -> Int / <amb>
60{
61 if choose ()
62 {
63 0
64 }
65 else
66 {
67 1
68 }
69}
70
71// A column between `lo` and `hi`.
72fn pick :: lo: Int hi: Int -> Int / <amb>
73{
74 if lo == hi
75 {
76 lo
77 }
78 else if choose ()
79 {
80 lo
81 }
82 else
83 {
84 pick (lo + 1) hi
85 }
86}
87
88// Whether a queen in column `col` is attacked by `queens` (the rows
89// above, nearest first).
90fn safe :: col: Int queens: List<Int> distance: Int -> Bool
91{
92 match queens
93 { [] -> True
94 , [q, ..rest] ->
95 q != col && q - col != distance && col - q != distance
96 && safe col rest (distance + 1)
97 }
98}
99
100fn place :: n: Int rows: Int queens: List<Int> -> List<Int> / <amb>
101{
102 if rows == 0
103 {
104 queens
105 }
106 else
107 {
108 let col = pick 1 n
109 if safe col queens 1
110 {
111 place n (rows - 1) [col, ..queens]
112 }
113 else
114 {
115 fail ()
116 }
117 }
118}
119
120fn show_all :: xs: List<List<Int>> -> String
121{
122 string.join (list.map xs fn x { string.join (list.map x show) "" }) " "
123}
124
125pub fn main :: -> () / <io>
126{
127 let pairs = all fn { (digit (), digit ()) }
128 io.println ("pairs " <> show (list.length pairs))
129 let sums =
130 all
131 fn
132 {
133 let a = digit ()
134 let b = digit ()
135 if a + b == 1
136 {
137 a * 10 + b
138 }
139 else
140 {
141 fail ()
142 }
143 }
144 io.println ("sums " <> show sums)
145 let four = all fn { place 4 4 [] }
146 io.println ("4 queens: " <> show_all four)
147 let six = all fn { place 6 6 [] }
148 match six
149 { [first, ..] ->
150 io.println
151 ( "6 queens: " <> show (list.length six) <> " solutions, first "
152 <> show_all [first]
153 )
154 , [] -> io.println "6 queens: none"
155 }
156 // A cleanup runs once per branch that leaves its scope.
157 let cleaned =
158 all
159 fn
160 {
161 defer io.println "branch done"
162 digit ()
163 }
164 io.println ("cleaned " <> show cleaned)
165 let kept =
166 all_kept
167 fn
168 {
169 let held = list.range 1 3
170 defer io.println "kept branch done"
171 digit () + list.length held
172 }
173 io.println ("kept " <> show kept)
174 // References are shared by the branches; the region ends after the last.
175 let shared =
176 all
177 fn
178 {
179 region
180 {
181 let r = ref.new 0
182 if choose ()
183 {
184 ref.set r 1
185 }
186 else
187 {
188 ()
189 }
190 ref.get r
191 }
192 }
193 io.println ("shared " <> show shared)
194 // A branch that is never resumed is cancelled when the handler finishes.
195 let none = handle
196 {
197 defer io.println "never resumed"
198 [digit ()]
199 }
200 with
201 { choose -> [], fail -> [] }
202 io.println ("none " <> show (list.length none))
203}
204//> pairs 4
205//> sums [1, 10]
206//> 4 queens: 3142 2413
207//> 6 queens: 4 solutions, first 531642
208//> branch done
209//> branch done
210//> cleaned [0, 1]
211//> kept branch done
212//> kept branch done
213//> kept [3, 4]
214//> shared [1, 1]
215//> never resumed
216//> none 0