import std/io import std/list // Calls through dictionaries of constrained impls, built from constant // ones or from other built ones (issue #278): evidence specialisation // calls their methods directly and copies them; the results must not // change. trait Describe { fn describe :: x: a -> String } pub type Box { Box(a) } impl Describe { fn describe :: x: Int -> String { show x } } impl Describe { fn describe :: x: String -> String { "'" <> x <> "'" } } impl Describe> where Describe { fn describe :: x: Box -> String { let Box(v) = x let inner = describe v "box(" <> inner <> ")" } } impl Describe> where Describe { fn describe :: xs: List -> String { match xs { [] -> "[]", [x, ..rest] -> describe x <> " :: " <> describe rest } } } impl Describe<(a, b)> where Describe, Describe { fn describe :: p: (a, b) -> String { "(" <> describe p.0 <> ", " <> describe p.1 <> ")" } } fn twice :: x: a -> String where Describe { describe x <> " " <> describe x } fn all :: xs: List -> List where Describe { list.map xs fn x { describe x } } fn main :: { io.println (twice Box(1)) //> box(1) box(1) io.println (twice Box(Box("deep"))) //> box(box('deep')) box(box('deep')) io.println (describe [1, 2, 3]) //> 1 :: 2 :: 3 :: [] io.println (describe [Box(1), Box(2)]) //> box(1) :: box(2) :: [] io.println (twice (7, "seven")) //> (7, 'seven') (7, 'seven') io.println (twice (Box(1), [Box("a")])) //> (box(1), box('a') :: []) (box(1), box('a') :: []) list.each (all [[1], [2, 3]]) io.println //> 1 :: [] //> 2 :: 3 :: [] }