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.

139 lignes · 2089 octetsBrut · Historique
1// Types inferred for unannotated functions.
2
3fn id :: x
4{
5 x
6}
7//~ TYPE id: fn(a) -> a
8
9fn const_ :: x y
10{
11 x
12}
13//~ TYPE const_: fn(a, b) -> a
14
15fn compose :: f g
16{
17 fn x { f (g x) }
18}
19//~ TYPE compose: fn(fn(a) -> b / <e>, fn(c) -> a / <e>) -> fn(c) -> b / <e>
20
21fn length :: xs
22{
23 match xs { [] -> 0, [_, ..rest] -> 1 + length rest }
24}
25//~ TYPE length: fn(List<a>) -> Int
26
27fn map :: xs f
28{
29 match xs { Nil -> Nil, Cons(x, rest) -> Cons(f x, map rest f) }
30}
31//~ TYPE map: fn(List<a>, fn(a) -> b / <e>) -> List<b> / <e>
32
33// Mutual recursion is inferred as one group.
34fn is_even :: n
35{
36 if n == 0
37 {
38 True
39 }
40 else
41 {
42 is_odd (n - 1)
43 }
44}
45fn is_odd :: n
46{
47 if n == 0
48 {
49 False
50 }
51 else
52 {
53 is_even (n - 1)
54 }
55}
56//~ TYPE is_even: fn(Int) -> Bool
57//~ TYPE is_odd: fn(Int) -> Bool
58
59// Operators on an unknown type become constraints of the scheme.
60fn same :: a b
61{
62 a == b
63}
64//~ TYPE same: fn(a, a) -> Bool where Eq<a>
65
66fn sum3 :: a b c
67{
68 a + b + c
69}
70//~ TYPE sum3: fn(a, a, a) -> a where Num<a>
71
72fn describe :: x
73{
74 "value: " <> show x
75}
76//~ TYPE describe: fn(a) -> String where Show<a>
77
78// `<>` is `Semigroup` (ADR 0027): generic unless a side is known.
79fn joined :: a b
80{
81 a <> b
82}
83//~ TYPE joined: fn(a, a) -> a where Semigroup<a>
84
85fn exclaim :: s
86{
87 s <> "!"
88}
89//~ TYPE exclaim: fn(String) -> String
90
91fn prepend_zero :: xs
92{
93 [0] <> xs
94}
95//~ TYPE prepend_zero: fn(List<Int>) -> List<Int>
96
97// Constraints on concrete types are solved by impls.
98fn list_eq ::
99{
100 [1, 2] == [1, 2]
101}
102//~ TYPE list_eq: fn() -> Bool
103
104fn tuple ::
105{
106 (1, "two", 3.0)
107}
108//~ TYPE tuple: fn() -> (Int, String, Float)
109
110fn first :: p
111{
112 p.0 //~ ERROR E0203 must be known
113}
114
115fn pipeline :: xs
116{
117 xs |> map fn x { x * 2 } |> length
118}
119//~ TYPE pipeline: fn(List<Int>) -> Int
120
121// `use` passes the rest of the block as a callback.
122fn with_value :: v k
123{
124 k v
125}
126fn use_it ::
127{
128 use x <- with_value 41
129 x + 1
130}
131//~ TYPE use_it: fn() -> Int
132
133// Local lets are monomorphic.
134fn mono_local ::
135{
136 let f = fn x { x }
137 let a = f 1
138 f "s" //~ ERROR E0202 expected `Int`, found `String`
139}