vectors

examples/vectors.omg

examples/vectors.omg26 lines
1{-  Dependent types: vectors, lists whose length is part of their type.
2
3        omegyu normalize examples/vectors.omg
4
5    `Vec a n` is computed from `n`, so the type checker knows the length of
6    every vector: `head` is only defined on vectors that are not empty, and
7    `zip` on vectors of the same length.
8-}
9
10let Vec = std:Vec
11
12let xs : Vec.Vec Natural 3 = Vec.cons 1 (Vec.cons 2 (Vec.cons 3 Vec.nil))
13
14let ys : Vec.Vec Text 3 = Vec.cons "one" (Vec.cons "two" (Vec.cons "three" Vec.nil))
15
16-- The length of a list is not known to the type checker; a proof that it
17-- has the length we claim lets us treat it as a vector.
18let three : 1 + 2 === 3 = assert : 1 + 2 === 3
19
20in  { first = Vec.head xs
21    , rest = Vec.toList 2 (Vec.tail xs)
22    , pairs = Vec.toList 3 (Vec.zip 3 xs ys)
23    , doubled = Vec.toList 3 (Vec.map 3 (\(n : Natural) -> n * 2) xs)
24    , zeros = Vec.replicate 4 0
25    , three
26    }
Your result
Complete file: examples/vectors.omg · with imports