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.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Row evidence (ADR 0066), the typing rules. A row variable in a row |
| 2 | // argument of an effect is an evidence variable: it may stand for |
| 3 | // handlers a caller supplies, which any other row variable refuses |
| 4 | // (E0202). An arrow whose row ends in one is bundled. |
| 5 | |
| 6 | import std/io |
| 7 | import std/list |
| 8 | |
| 9 | effect ask |
| 10 | { |
| 11 | fn ask :: -> Int |
| 12 | } |
| 13 | |
| 14 | effect tell |
| 15 | { |
| 16 | fn tell :: line: String -> () |
| 17 | } |
| 18 | |
| 19 | effect later<e> |
| 20 | { |
| 21 | fn later :: render: fn() -> Int / <e> -> Int |
| 22 | } |
| 23 | |
| 24 | fn with_ask :: n: Int action: fn() -> a / <ask | e> -> a / <e> |
| 25 | { |
| 26 | handle |
| 27 | { |
| 28 | action () |
| 29 | } |
| 30 | with |
| 31 | { ask -> n } |
| 32 | } |
| 33 | |
| 34 | fn with_tell :: action: fn() -> a / <tell, io | e> -> a / <io | e> |
| 35 | { |
| 36 | handle |
| 37 | { |
| 38 | action () |
| 39 | } |
| 40 | with |
| 41 | { tell line -> io.println "told: {line}" } |
| 42 | } |
| 43 | |
| 44 | // The handler runs `render` in place, with the evidence `inline`'s |
| 45 | // caller gave for `e`. |
| 46 | fn inline :: action: fn() -> a / <later<e> | e> -> a / <e> |
| 47 | { |
| 48 | handle |
| 49 | { |
| 50 | action () |
| 51 | } |
| 52 | with |
| 53 | { later render -> render () } |
| 54 | } |
| 55 | //~ TYPE inline: fn(fn() -> a / <later<e> | e>) -> a / <e> |
| 56 | |
| 57 | // The handler re-installs `ask` around `render`; a lambda calls the |
| 58 | // bundled `render` and builds its evidence there. |
| 59 | fn rehandled :: action: fn() -> a / <later<<ask>> | e> -> a / <e> |
| 60 | { |
| 61 | handle |
| 62 | { |
| 63 | action () |
| 64 | } |
| 65 | with |
| 66 | { later render -> with_ask 100 fn { render () } } |
| 67 | } |
| 68 | //~ TYPE rehandled: fn(fn() -> a / <later<<ask>> | e>) -> a / <e> |
| 69 | |
| 70 | // The lambda receives `ask` from whoever handles `later`. |
| 71 | fn page :: -> Int / <later<<ask | r>> | r2> |
| 72 | { |
| 73 | later fn { ask () + 1 } |
| 74 | } |
| 75 | //~ TYPE page: fn() -> Int / <later<<ask | r>> | r2> |
| 76 | |
| 77 | fn chatty :: -> Int / <later<<ask, tell | r>>, tell | r2> |
| 78 | { |
| 79 | tell "before" |
| 80 | later |
| 81 | fn |
| 82 | { |
| 83 | tell "inside" |
| 84 | ask () |
| 85 | } |
| 86 | } |
| 87 | //~ TYPE chatty: fn() -> Int / <later<<ask, tell | r>>, tell | r2> |
| 88 | |
| 89 | pub fn main :: -> () / <io> |
| 90 | { |
| 91 | let a = with_ask 41 fn { inline fn { page () } } |
| 92 | io.println "{a}" |
| 93 | let b = with_tell fn { with_ask 7 fn { inline fn { chatty () } } } |
| 94 | io.println "{b}" |
| 95 | let d = with_ask 1 fn { rehandled fn { later fn { ask () } } } |
| 96 | io.println "{d}" |
| 97 | } |
| 98 | |
| 99 | fn apply :: action: fn() -> a / <e> -> a / <e> |
| 100 | { |
| 101 | action () |
| 102 | } |
| 103 | |
| 104 | // Where the caller's row ends in a plain variable, an evidence row closes |
| 105 | // there: it prints as the row it is. |
| 106 | fn run_closed :: f |
| 107 | { |
| 108 | apply fn { inline f } |
| 109 | } |
| 110 | //~ TYPE run_closed: fn(fn() -> a / <later<<>>>) -> a |
| 111 | |
| 112 | // An evidence variable is not dropped from an inferred row. |
| 113 | fn open_inline :: |
| 114 | { |
| 115 | inline fn { page () } |
| 116 | } |
| 117 | //~ TYPE open_inline: fn() -> Int / <ask | e> |
| 118 | |
| 119 | // An inferred effect argument is named as a row. |
| 120 | fn later_one :: |
| 121 | { |
| 122 | later fn { 1 } |
| 123 | } |
| 124 | //~ TYPE later_one: fn() -> Int / <later<e>> |
| 125 | |
| 126 | // A closed row written as an effect's argument is an evidence row. |
| 127 | fn closed_argument :: -> Int / <later<<ask>>> |
| 128 | { |
| 129 | later fn { ask () } |
| 130 | } |
| 131 | //~ TYPE closed_argument: fn() -> Int / <later<<ask>>> |
| 132 | |
| 133 | // A bundled closure is not passed where an unbundled arrow is declared: |
| 134 | // they receive their handlers differently. |
| 135 | fn run_plain :: f: fn() -> Int / <ask> -> Int / <ask> |
| 136 | { |
| 137 | f () |
| 138 | } |
| 139 | |
| 140 | fn bundled_where_closed :: |
| 141 | action: fn() -> a / <later<<ask>> | e> |
| 142 | -> a / <ask | e> |
| 143 | { |
| 144 | handle |
| 145 | { |
| 146 | action () |
| 147 | } |
| 148 | with |
| 149 | { later render -> run_plain render //~ ERROR E0202 bundled |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fn bundled_where_open :: action: fn() -> a / <later<<ask>> | e> -> a / <e> |
| 154 | { |
| 155 | handle |
| 156 | { |
| 157 | action () |
| 158 | } |
| 159 | with |
| 160 | { later render -> with_ask 100 render //~ ERROR E0202 bundled |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // A lambda that captured a handler is not one that receives it. |
| 165 | fn captured :: -> Int / <later<<ask | r>> | r2> |
| 166 | { |
| 167 | with_ask |
| 168 | 1 |
| 169 | fn |
| 170 | { |
| 171 | let f = fn { ask () } |
| 172 | later f //~ ERROR E0202 |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Where a function is passed its handlers one by one, a bundled one is |
| 177 | // not supported yet (ADR 0066): a lambda that calls it is. |
| 178 | fn sidebar :: -> Int / <ask> |
| 179 | { |
| 180 | ask () |
| 181 | } |
| 182 | |
| 183 | fn named_part :: -> Int / <later<<ask | r>> | r2> |
| 184 | { |
| 185 | later |
| 186 | sidebar //~ ERROR E0202 one bundled as row evidence, the other one by one |
| 187 | } |
| 188 | |
| 189 | effect stage<e> |
| 190 | { |
| 191 | fn stage :: f: fn(Int, Int) -> Int / <e> -> Int |
| 192 | } |
| 193 | |
| 194 | fn partial_stage :: action: fn() -> a / <stage<e> | e> -> a / <e> |
| 195 | { |
| 196 | handle |
| 197 | { |
| 198 | action () |
| 199 | } |
| 200 | with |
| 201 | { stage f -> (f 1) 2 //~ ERROR E0202 partially applied |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | fn inline_value :: -> () |
| 206 | { |
| 207 | let f = inline //~ ERROR E0202 a function name used as a value |
| 208 | () |
| 209 | } |
| 210 | |
| 211 | // A function with a closed row is called the plain way from a bundled |
| 212 | // lambda: its row closes there (issue #362). |
| 213 | fn twice :: n: Int -> Int |
| 214 | { |
| 215 | n * 2 |
| 216 | } |
| 217 | |
| 218 | fn shout :: line: String -> () / <tell> |
| 219 | { |
| 220 | tell line |
| 221 | } |
| 222 | |
| 223 | fn closed_callees :: -> Int / <later<<ask, tell | r>> | r2> |
| 224 | { |
| 225 | later |
| 226 | fn |
| 227 | { |
| 228 | shout (show (list.length [1, 2, 3])) |
| 229 | twice (ask ()) |
| 230 | } |
| 231 | } |
| 232 | //~ TYPE closed_callees: fn() -> Int / <later<<ask, tell | r>> | r2> |