// Types inferred for unannotated functions. fn id :: x { x } //~ TYPE id: fn(a) -> a fn const_ :: x y { x } //~ TYPE const_: fn(a, b) -> a fn compose :: f g { fn x { f (g x) } } //~ TYPE compose: fn(fn(a) -> b / , fn(c) -> a / ) -> fn(c) -> b / fn length :: xs { match xs { [] -> 0, [_, ..rest] -> 1 + length rest } } //~ TYPE length: fn(List) -> Int fn map :: xs f { match xs { Nil -> Nil, Cons(x, rest) -> Cons(f x, map rest f) } } //~ TYPE map: fn(List, fn(a) -> b / ) -> List / // Mutual recursion is inferred as one group. fn is_even :: n { if n == 0 { True } else { is_odd (n - 1) } } fn is_odd :: n { if n == 0 { False } else { is_even (n - 1) } } //~ TYPE is_even: fn(Int) -> Bool //~ TYPE is_odd: fn(Int) -> Bool // Operators on an unknown type become constraints of the scheme. fn same :: a b { a == b } //~ TYPE same: fn(a, a) -> Bool where Eq fn sum3 :: a b c { a + b + c } //~ TYPE sum3: fn(a, a, a) -> a where Num fn describe :: x { "value: " <> show x } //~ TYPE describe: fn(a) -> String where Show // `<>` is `Semigroup` (ADR 0027): generic unless a side is known. fn joined :: a b { a <> b } //~ TYPE joined: fn(a, a) -> a where Semigroup fn exclaim :: s { s <> "!" } //~ TYPE exclaim: fn(String) -> String fn prepend_zero :: xs { [0] <> xs } //~ TYPE prepend_zero: fn(List) -> List // Constraints on concrete types are solved by impls. fn list_eq :: { [1, 2] == [1, 2] } //~ TYPE list_eq: fn() -> Bool fn tuple :: { (1, "two", 3.0) } //~ TYPE tuple: fn() -> (Int, String, Float) fn first :: p { p.0 //~ ERROR E0203 must be known } fn pipeline :: xs { xs |> map fn x { x * 2 } |> length } //~ TYPE pipeline: fn(List) -> Int // `use` passes the rest of the block as a callback. fn with_value :: v k { k v } fn use_it :: { use x <- with_value 41 x + 1 } //~ TYPE use_it: fn() -> Int // Local lets are monomorphic. fn mono_local :: { let f = fn x { x } let a = f 1 f "s" //~ ERROR E0202 expected `Int`, found `String` }