tobogganSe connecter

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.

156 lignes · 3287 octetsBrut · Historique
1import std/io
2import std/list
3import std/string
4
5// Higher-kinded trait parameters (ADR 0018): `f` stands for a type
6// constructor, such as `List`, `Option` or `Result<a>`. The row variable
7// `e` makes `map` accept effectful functions.
8trait Functor<f>
9{
10 fn map :: x: f<a> g: fn(a) -> b / <e> -> f<b> / <e>
11}
12
13impl Functor<List>
14{
15 fn map :: xs g
16 {
17 list.map xs g
18 }
19}
20
21impl Functor<Option>
22{
23 fn map :: o g
24 {
25 match o { Some(x) -> Some(g x), None -> None }
26 }
27}
28
29// Partial application follows the order of parameters: `Result<a, e>`
30// lists its value type first, so `Result<a>` maps over errors.
31impl Functor<Result<a>>
32{
33 fn map :: r g
34 {
35 match r { Ok(x) -> Ok(x), Error(e) -> Error(g e) }
36 }
37}
38
39trait Monad<m>
40{
41 fn pure :: x: a -> m<a>
42 fn bind :: x: m<a> g: fn(a) -> m<b> / <e> -> m<b> / <e>
43}
44
45impl Monad<List>
46{
47 fn pure :: x
48 {
49 [x]
50 }
51 fn bind :: xs g
52 {
53 match xs { [] -> [], [x, ..rest] -> list.append (g x) (bind rest g) }
54 }
55}
56
57impl Monad<Option>
58{
59 fn pure :: x
60 {
61 Some(x)
62 }
63 fn bind :: o g
64 {
65 match o { Some(x) -> g x, None -> None }
66 }
67}
68
69// On errors: `bind` tries a recovery while the computation fails.
70impl Monad<Result<a>>
71{
72 fn pure :: e
73 {
74 Error(e)
75 }
76 fn bind :: r g
77 {
78 match r { Ok(x) -> Ok(x), Error(e) -> g e }
79 }
80}
81
82// A type with a higher-kinded parameter, and an impl that needs one.
83pub type Twice<f, a> { Twice(f<a>, f<a>) }
84
85impl Functor<Twice<f>> where Functor<f>
86{
87 fn map :: t g
88 {
89 match t { Twice(x, y) -> Twice(map x g, map y g) }
90 }
91}
92
93// Generic over any Functor.
94pub fn double_all :: x: f<Int> -> f<Int> where Functor<f>
95{
96 map x fn n { n * 2 }
97}
98
99// Inferred: `fn(a<b>, fn(b) -> b / <e>) -> a<b> / <e> where Functor<a>`.
100fn twice_map :: x g
101{
102 map (map x g) g
103}
104
105// Generic over any Monad.
106pub fn combine :: x: m<a> y: m<b> f: fn(a, b) -> c -> m<c> where Monad<m>
107{
108 bind x fn a { bind y fn b { pure (f a b) } }
109}
110
111fn parse_digit :: s: String -> Result<Int, String>
112{
113 match s { "0" -> Ok(0), "1" -> Ok(1), "2" -> Ok(2), _ -> Error(s) }
114}
115
116pub fn main :: -> () / <io>
117{
118 io.println (show (double_all [1, 2, 3]))
119 io.println (show (double_all Some(21)))
120 let failed: Result<String, Int> = Error(5)
121 io.println (show (double_all failed))
122 io.println (show (twice_map [1, 2] fn n { n + 10 }))
123 match map Twice([1], [2, 3]) fn n { n * 100 }
124 { Twice(a, b) -> io.println (show a <> " " <> show b) }
125 // An effectful function: `map` performs its effects.
126 let lengths =
127 map
128 ["a", "bcd"]
129 fn s
130 {
131 io.println ("visiting " <> s)
132 string.length s
133 }
134 io.println (show lengths)
135 io.println (show (combine [1, 2] ["x", "y"] fn n s { show n <> s }))
136 let none: Option<Int> = None
137 io.println (show (combine Some(1) none fn a b { a + b }))
138 io.println (show (combine Some(1) Some(2) fn a b { a + b }))
139 // Recover from a parse error by trying a fallback.
140 let parsed = bind (parse_digit "x") fn bad { parse_digit "2" }
141 io.println (show parsed)
142 io.println (show (map (parse_digit "y") fn bad { "not a digit: " <> bad }))
143}
144//> [2, 4, 6]
145//> Some(42)
146//> Error(10)
147//> [21, 22]
148//> [100] [200, 300]
149//> visiting a
150//> visiting bcd
151//> [1, 3]
152//> [1x, 1y, 2x, 2y]
153//> None
154//> Some(3)
155//> Ok(2)
156//> Error(not a digit: y)