
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // An unboxed-float worker (ADR 0045) whose loop, built unoptimised, still |
| 2 | // calls the boxed dictionary operations: the worker reads their results |
| 3 | // raw and must drop the boxes the boundary consumed (issue #279: two |
| 4 | // leaked blocks per iteration, so `floats` reported 6,000,002 live blocks |
| 5 | // at exit). `g` is the same shape at a non-tail self-call. Natively, both |
| 6 | // ways, this ends with 0 live blocks. `add` takes one variable in two |
| 7 | // marked owned slots (issue #283: the boundary's temps, once keyed by |
| 8 | // variable, collided; each slot reads and drops its own). `total`'s |
| 9 | // worker calls `bump`'s with a raw argument whose result never escapes |
| 10 | // (issue #337): raw in, raw out, nothing boxed on the way. |
| 11 | |
| 12 | import std/io |
| 13 | |
| 14 | fn fsum :: i: Int n: Int x: Float acc: Float -> Float |
| 15 | { |
| 16 | if i > n |
| 17 | { |
| 18 | acc |
| 19 | } |
| 20 | else |
| 21 | { |
| 22 | fsum (i + 1) n (x + 0.5) (acc + x * 2.0) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn g :: n: Int x: Float -> Float |
| 27 | { |
| 28 | if n == 0 |
| 29 | { |
| 30 | x |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | let r = g (n - 1) (x * 2.0) |
| 35 | if r > 0.0 |
| 36 | { |
| 37 | r |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | r |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn add :: n: Int a: Float b: Float -> Float |
| 47 | { |
| 48 | if n == 0 |
| 49 | { |
| 50 | a |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | add (n - 1) b (a + 1.0) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // `bump` is recursive, so the simplifier keeps it, and it gets a float |
| 59 | // worker; `total`'s worker calls it with a raw argument (a borrowed |
| 60 | // parameter: no box, nothing to free), and the result never escapes, so |
| 61 | // it stays raw end to end (issue #337: no box at the call, no unbox at |
| 62 | // the use). |
| 63 | fn bump :: x: Float n: Int -> Float |
| 64 | { |
| 65 | if n == 0 |
| 66 | { |
| 67 | x |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | bump (x + 1.0) (n - 1) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn total :: i: Int n: Int acc: Float -> Float |
| 76 | { |
| 77 | if i > n |
| 78 | { |
| 79 | acc |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | total (i + 1) n (acc + bump 1.5 2) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn main :: -> () / <io> |
| 88 | { |
| 89 | io.println (show (fsum 0 1000 0.0 0.0)) |
| 90 | io.println (show (g 3 0.5)) |
| 91 | let x = fsum 0 3 0.25 1.0 |
| 92 | io.println (show (add 3 x x)) |
| 93 | io.println (show (total 1 1000 0.0)) |
| 94 | } |
| 95 | //> 500500.0 |
| 96 | //> 4.0 |
| 97 | //> 10.0 |
| 98 | //> 3500.0 |