| @@ -20,21 +20,30 @@ fn core_of_main(src: &str) -> String { |
| 20 | 20 | |
| 21 | 21 | #[test] |
| 22 | 22 | fn core_of_a_small_program() { |
| 23 | | − let src = r#" |
| 24 | | −effect ask { fn ask() -> Int } |
| 23 | + let src = r#"effect ask { |
| 24 | + fn ask :: -> Int |
| 25 | +} |
| 25 | 26 | |
| 26 | | −fn same(a, b) { a == b } |
| 27 | +fn same :: a b { |
| 28 | + a == b |
| 29 | +} |
| 27 | 30 | |
| 28 | | −fn adder(n) { fn(x) { x + n } } |
| 31 | +fn adder :: n { |
| 32 | + fn x { x + n } |
| 33 | +} |
| 29 | 34 | |
| 30 | | −fn first_or(xs, d) { |
| 35 | +fn first_or :: xs d { |
| 31 | 36 | match xs { |
| 32 | | − [x, ..] if x > 0 -> x, |
| 33 | | − _ -> d, |
| 37 | + [x, ..] if x > 0 -> x |
| 38 | + , _ -> d |
| 34 | 39 | } |
| 35 | 40 | } |
| 36 | 41 | |
| 37 | | −fn asked() { handle { ask() + 1 } with { ask() -> 41 } } |
| 42 | +fn asked :: { |
| 43 | + handle { ask () + 1 } with { |
| 44 | + ask -> 41 |
| 45 | + } |
| 46 | +} |
| 38 | 47 | "#; |
| 39 | 48 | let main_only = core_of_main(src); |
| 40 | 49 | insta::assert_snapshot!(main_only); |
| @@ -42,75 +51,78 @@ fn asked() { handle { ask() + 1 } with { ask() -> 41 } } |
| 42 | 51 | |
| 43 | 52 | #[test] |
| 44 | 53 | fn decision_trees() { |
| 45 | | − let src = r#" |
| 46 | | −type Shape { Circle(r: Int) Rect(w: Int, h: Int) Dot } |
| 54 | + let src = r#"type Shape |
| 55 | + { Circle(r: Int) |
| 56 | + , Rect(w: Int, h: Int) |
| 57 | + , Dot |
| 58 | + } |
| 47 | 59 | |
| 48 | 60 | // Or-patterns, of constructors with and without fields. |
| 49 | | −fn corners(s: Shape) -> Int { |
| 61 | +fn corners :: s: Shape -> Int { |
| 50 | 62 | match s { |
| 51 | | − Circle(_) | Dot -> 0, |
| 52 | | − Rect(_, _) -> 4, |
| 63 | + Circle(_) | Dot -> 0 |
| 64 | + , Rect(_, _) -> 4 |
| 53 | 65 | } |
| 54 | 66 | } |
| 55 | 67 | |
| 56 | | −fn small(s: Option<Shape>) -> Bool { |
| 68 | +fn small :: s: Option<Shape> -> Bool { |
| 57 | 69 | match s { |
| 58 | | − Some(Circle(0)) | Some(Dot) | None -> True, |
| 59 | | − _ -> False, |
| 70 | + Some(Circle(0)) | Some(Dot) | None -> True |
| 71 | + , _ -> False |
| 60 | 72 | } |
| 61 | 73 | } |
| 62 | 74 | |
| 63 | 75 | // Nested constructors and tuples: columns are tested left to right. |
| 64 | | −fn both(a: Option<Int>, b: Result<Int, String>) -> Int { |
| 76 | +fn both :: a: Option<Int> b: Result<Int, String> -> Int { |
| 65 | 77 | match (a, b) { |
| 66 | | − (Some(x), Ok(y)) -> x + y, |
| 67 | | − (Some(x), Error(_)) -> x, |
| 68 | | − (None, Ok(y)) -> y, |
| 69 | | − (None, Error(_)) -> 0, |
| 78 | + (Some(x), Ok(y)) -> x + y |
| 79 | + , (Some(x), Error(_)) -> x |
| 80 | + , (None, Ok(y)) -> y |
| 81 | + , (None, Error(_)) -> 0 |
| 70 | 82 | } |
| 71 | 83 | } |
| 72 | 84 | |
| 73 | 85 | // Literals: Ints, Strings and Floats test equality, then a default. |
| 74 | | −fn digit(n: Int) -> String { |
| 86 | +fn digit :: n: Int -> String { |
| 75 | 87 | match n { |
| 76 | | − 0 -> "zero", |
| 77 | | − 1 | 2 -> "small", |
| 78 | | − _ -> "many", |
| 88 | + 0 -> "zero" |
| 89 | + , 1 | 2 -> "small" |
| 90 | + , _ -> "many" |
| 79 | 91 | } |
| 80 | 92 | } |
| 81 | 93 | |
| 82 | | −fn greet(s: String) -> Int { |
| 94 | +fn greet :: s: String -> Int { |
| 83 | 95 | match s { |
| 84 | | − "hi" -> 1, |
| 85 | | − "hello" -> 2, |
| 86 | | − _ -> 0, |
| 96 | + "hi" -> 1 |
| 97 | + , "hello" -> 2 |
| 98 | + , _ -> 0 |
| 87 | 99 | } |
| 88 | 100 | } |
| 89 | 101 | |
| 90 | | −fn half(x: Float) -> Bool { |
| 102 | +fn half :: x: Float -> Bool { |
| 91 | 103 | match x { |
| 92 | | − 0.5 -> True, |
| 93 | | − _ -> False, |
| 104 | + 0.5 -> True |
| 105 | + , _ -> False |
| 94 | 106 | } |
| 95 | 107 | } |
| 96 | 108 | |
| 97 | 109 | // Lists, with a binding of a whole pattern and a guard. |
| 98 | | −fn second(xs: List<Int>) -> Int { |
| 110 | +fn second :: xs: List<Int> -> Int { |
| 99 | 111 | match xs { |
| 100 | | − [_, y, ..] if y > 0 -> y, |
| 101 | | − [x] as one -> x, |
| 102 | | − _ -> 0, |
| 112 | + [_, y, ..] if y > 0 -> y |
| 113 | + , [x] as one -> x |
| 114 | + , _ -> 0 |
| 103 | 115 | } |
| 104 | 116 | } |
| 105 | 117 | |
| 106 | 118 | // String literals and lists of them: the HTTP-router shape of issue |
| 107 | 119 | // #295, whose arm bodies the leaves call as lifted continuations. |
| 108 | | −fn route(method: String, segments: List<String>) -> String { |
| 120 | +fn route :: method: String segments: List<String> -> String { |
| 109 | 121 | match (method, segments) { |
| 110 | | − ("GET", ["archive"]) -> "archive", |
| 111 | | − ("GET", ["label", label]) -> "label " <> label, |
| 112 | | − ("POST", ["notes", note, verb]) -> verb <> " " <> note, |
| 113 | | − _ -> "missing", |
| 122 | + ("GET", ["archive"]) -> "archive" |
| 123 | + , ("GET", ["label", label]) -> "label " <> label |
| 124 | + , ("POST", ["notes", note, verb]) -> verb <> " " <> note |
| 125 | + , _ -> "missing" |
| 114 | 126 | } |
| 115 | 127 | } |
| 116 | 128 | "#; |
| @@ -148,24 +160,25 @@ fn nodes(e: &louise_core::ir::Expr) -> u32 { |
| 148 | 160 | /// every leaf (7 arms once made 91 MB of Core). |
| 149 | 161 | #[test] |
| 150 | 162 | fn match_core_size_is_linear_in_the_arms() { |
| 151 | | − let src = r#" |
| 152 | | −import std/io |
| 163 | + let src = r#"import std/io |
| 153 | 164 | |
| 154 | | −fn route(method: String, segments: List<String>) -> String { |
| 165 | +fn route :: method: String segments: List<String> -> String { |
| 155 | 166 | match (method, segments) { |
| 156 | | − ("GET", ["archive"]) -> "archive", |
| 157 | | − ("GET", ["search"]) -> "search", |
| 158 | | − ("GET", ["trash"]) -> "trash", |
| 159 | | − ("GET", ["label", label]) -> "label " <> label, |
| 160 | | − ("POST", ["notes", note]) -> "revise " <> note, |
| 161 | | − ("GET", ["notes", note, "edit"]) -> "edit " <> note, |
| 162 | | − ("POST", ["notes", note, "tag"]) -> "tag " <> note, |
| 163 | | − ("POST", ["notes", note, verb]) -> verb <> " " <> note, |
| 164 | | − _ -> "missing", |
| 167 | + ("GET", ["archive"]) -> "archive" |
| 168 | + , ("GET", ["search"]) -> "search" |
| 169 | + , ("GET", ["trash"]) -> "trash" |
| 170 | + , ("GET", ["label", label]) -> "label " <> label |
| 171 | + , ("POST", ["notes", note]) -> "revise " <> note |
| 172 | + , ("GET", ["notes", note, "edit"]) -> "edit " <> note |
| 173 | + , ("POST", ["notes", note, "tag"]) -> "tag " <> note |
| 174 | + , ("POST", ["notes", note, verb]) -> verb <> " " <> note |
| 175 | + , _ -> "missing" |
| 165 | 176 | } |
| 166 | 177 | } |
| 167 | 178 | |
| 168 | | −pub fn main() -> () / <io> { io.println(route("POST", ["notes", "n", "pin"])) } |
| 179 | +pub fn main :: -> () / <io> { |
| 180 | + io.println (route "POST" ["notes", "n", "pin"]) |
| 181 | +} |
| 169 | 182 | "#; |
| 170 | 183 | let program = compile("main", "main.lse", src).unwrap().program; |
| 171 | 184 | let total: u32 = program |
| @@ -186,18 +199,19 @@ fn root_capabilities_of_actors_and_entries() { |
| 186 | 199 | let program = compile( |
| 187 | 200 | "main", |
| 188 | 201 | "main.lse", |
| 189 | | − r#" |
| 190 | | −import std/process.{type proc} |
| 202 | + r#"import std/process.{type proc} |
| 191 | 203 | import std/random.{type random} |
| 192 | 204 | import std/time.{type clock} |
| 193 | 205 | |
| 194 | | −pub fn main() -> () / <io, random, proc, clock> { |
| 195 | | − let _ = random.int(2) |
| 196 | | − let _ = time.now() |
| 197 | | − process.sleep(1) |
| 206 | +pub fn main :: -> () / <io, random, proc, clock> { |
| 207 | + let _ = random.int 2 |
| 208 | + let _ = time.now () |
| 209 | + process.sleep 1 |
| 198 | 210 | } |
| 199 | 211 | |
| 200 | | −pub fn quiet() -> () / <io> { () } |
| 212 | +pub fn quiet :: -> () / <io> { |
| 213 | + () |
| 214 | +} |
| 201 | 215 | "#, |
| 202 | 216 | ) |
| 203 | 217 | .unwrap() |
| @@ -221,27 +235,36 @@ fn unbox_signatures_are_recorded() { |
| 221 | 235 | let program = louise_testkit::compile( |
| 222 | 236 | "main", |
| 223 | 237 | "main.lse", |
| 224 | | − r#" |
| 225 | | −import std/io |
| 238 | + r#"import std/io |
| 226 | 239 | |
| 227 | | −fn fsum(i: Int, n: Int, x: Float, acc: Float) -> Float { |
| 228 | | − if i > n { acc } else { fsum(i + 1, n, x + 0.5, acc + x * 2.0) } |
| 240 | +fn fsum :: i: Int n: Int x: Float acc: Float -> Float { |
| 241 | + if i > n { acc } else { fsum (i + 1) n (x + 0.5) (acc + x * 2.0) } |
| 229 | 242 | } |
| 230 | 243 | |
| 231 | | −fn logged(x: Float) -> Float / <io> { |
| 232 | | − io.println("f") |
| 244 | +fn logged :: x: Float -> Float / <io> { |
| 245 | + io.println "f" |
| 233 | 246 | x |
| 234 | 247 | } |
| 235 | 248 | |
| 236 | | −fn fib(n: Int) -> Int { if n < 2 { n } else { fib(n - 1) + fib(n - 2) } } |
| 249 | +fn fib :: n: Int -> Int { |
| 250 | + if n < 2 { n } else { fib (n - 1) + fib (n - 2) } |
| 251 | +} |
| 237 | 252 | |
| 238 | | −fn mixed(x: Float) -> Int { 1 } |
| 253 | +fn mixed :: x: Float -> Int { |
| 254 | + 1 |
| 255 | +} |
| 239 | 256 | |
| 240 | | −fn unit(x: Int) -> () { () } |
| 257 | +fn unit :: x: Int -> () { |
| 258 | + () |
| 259 | +} |
| 241 | 260 | |
| 242 | | −fn ident(x: a) -> a { x } |
| 261 | +fn ident :: x: a -> a { |
| 262 | + x |
| 263 | +} |
| 243 | 264 | |
| 244 | | −pub fn main() -> () / <io> { io.println(show(fsum(0, 1, 0.0, 0.0) + 0.0)) } |
| 265 | +pub fn main :: -> () / <io> { |
| 266 | + io.println (show (fsum 0 1 0.0 0.0 + 0.0)) |
| 267 | +} |
| 245 | 268 | "#, |
| 246 | 269 | ) |
| 247 | 270 | .unwrap() |