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): a handler whose operation takes a closure that |
| 2 | // performs the effects of its row argument, generic or concrete. |
| 3 | |
| 4 | import std/io |
| 5 | import std/list |
| 6 | |
| 7 | effect ask |
| 8 | { |
| 9 | fn ask :: -> Int |
| 10 | } |
| 11 | |
| 12 | effect tell |
| 13 | { |
| 14 | fn tell :: line: String -> () |
| 15 | } |
| 16 | |
| 17 | effect later<e> |
| 18 | { |
| 19 | fn later :: render: fn() -> Int / <e> -> Int |
| 20 | } |
| 21 | |
| 22 | fn with_ask :: n: Int action: fn() -> a / <ask | e> -> a / <e> |
| 23 | { |
| 24 | handle |
| 25 | { |
| 26 | action () |
| 27 | } |
| 28 | with |
| 29 | { ask -> n } |
| 30 | } |
| 31 | |
| 32 | fn with_tell :: action: fn() -> a / <tell, io | e> -> a / <io | e> |
| 33 | { |
| 34 | handle |
| 35 | { |
| 36 | action () |
| 37 | } |
| 38 | with |
| 39 | { tell line -> io.println "told: {line}" } |
| 40 | } |
| 41 | |
| 42 | // Runs `render` in place, with the evidence its caller gave for `e`. |
| 43 | fn inline :: action: fn() -> a / <later<e> | e> -> a / <e> |
| 44 | { |
| 45 | handle |
| 46 | { |
| 47 | action () |
| 48 | } |
| 49 | with |
| 50 | { later render -> render () } |
| 51 | } |
| 52 | |
| 53 | // Re-installs `ask` around `render`. |
| 54 | fn rehandled :: action: fn() -> a / <later<<ask>> | e> -> a / <e> |
| 55 | { |
| 56 | handle |
| 57 | { |
| 58 | action () |
| 59 | } |
| 60 | with |
| 61 | { later render -> with_ask 100 fn { render () } } |
| 62 | } |
| 63 | |
| 64 | fn page :: -> Int / <later<<ask | r>> | r2> |
| 65 | { |
| 66 | later fn { ask () + 1 } |
| 67 | } |
| 68 | |
| 69 | fn chatty :: -> Int / <later<<ask, tell | r>>, tell | r2> |
| 70 | { |
| 71 | tell "before" |
| 72 | later |
| 73 | fn |
| 74 | { |
| 75 | tell "inside" |
| 76 | ask () |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Functions with closed rows, called from a part (issue #362). |
| 81 | fn twice :: n: Int -> Int |
| 82 | { |
| 83 | n * 2 |
| 84 | } |
| 85 | |
| 86 | fn shout :: line: String -> () / <tell> |
| 87 | { |
| 88 | tell line |
| 89 | } |
| 90 | |
| 91 | fn plain_calls :: -> Int / <later<<ask, tell | r>> | r2> |
| 92 | { |
| 93 | later |
| 94 | fn |
| 95 | { |
| 96 | shout (show (list.length [1, 2, 3])) |
| 97 | twice (ask ()) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // A part that itself defers a part. |
| 102 | fn nested :: -> Int / <later<<later<<ask | r>> | r1>> | r2> |
| 103 | { |
| 104 | later fn { later fn { ask () + 10 } } |
| 105 | } |
| 106 | |
| 107 | pub fn main :: -> () / <io> |
| 108 | { |
| 109 | let a = with_ask 41 fn { inline fn { page () } } |
| 110 | io.println "{a}" |
| 111 | let b = with_tell fn { with_ask 7 fn { inline fn { chatty () } } } |
| 112 | io.println "{b}" |
| 113 | let d = with_ask 1 fn { rehandled fn { later fn { ask () } } } |
| 114 | io.println "{d}" |
| 115 | let n = with_ask 1 fn { inline fn { inline fn { nested () } } } |
| 116 | io.println "{n}" |
| 117 | // `tell` reaches the part through `inline`'s evidence once `chatty` |
| 118 | // is called; a callback inside the part uses it there. |
| 119 | let c = |
| 120 | with_tell |
| 121 | fn |
| 122 | { |
| 123 | with_ask |
| 124 | 5 |
| 125 | fn |
| 126 | { |
| 127 | inline |
| 128 | fn |
| 129 | { |
| 130 | let v = chatty () |
| 131 | list.each [1, 2] fn x { tell "after {x}" } |
| 132 | v |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | io.println "{c}" |
| 137 | let p = with_tell fn { with_ask 4 fn { inline fn { plain_calls () } } } |
| 138 | io.println "{p}" |
| 139 | } |
| 140 | //> 42 |
| 141 | //> told: before |
| 142 | //> told: inside |
| 143 | //> 7 |
| 144 | //> 100 |
| 145 | //> 11 |
| 146 | //> told: before |
| 147 | //> told: inside |
| 148 | //> told: after 1 |
| 149 | //> told: after 2 |
| 150 | //> 5 |
| 151 | //> told: 3 |
| 152 | //> 8 |