proofs
examples/proofs.omg
examples/proofs.omg45 lines
1{- Programs as proofs.
2
3 omegyu type examples/proofs.omg
4
5 `x === y` is the type of the proofs that `x` and `y` are equal. The type
6 checker accepts `refl` (and `assert`) when both sides compute to the same
7 thing; everything else is proved, here by induction.
8-}
9
10let Equality = std:Equal
11
12-- `0 + n` is `n` by computation...
13let plusZeroLeft : forall (n : Natural) -> 0 + n === n = \n -> refl
14
15-- ...and so is `n + 0`: the type checker knows that zero is neutral.
16let plusZeroRight : forall (n : Natural) -> n + 0 === n = \n -> refl
17
18-- A property that needs induction: doubling is adding to oneself.
19let double
20 : Natural -> Natural
21 = \n ->
22 Natural/elim (\(_ : Natural) -> Natural) 0 (\(_ : Natural) (rest : Natural) -> rest + 2) n
23
24let doubleIsPlus
25 : forall (n : Natural) -> double n === n + n
26 = \n ->
27 Natural/elim
28 (\(k : Natural) -> double k === k + k)
29 refl
30 (\(k : Natural) (hypothesis : double k === k + k) ->
31 Equality.cong (\(m : Natural) -> m + 2) hypothesis)
32 n
33
34-- Equality is symmetric and transitive.
35let example
36 : forall (a : Natural) (b : Natural) (c : Natural) -> a === b -> c === b -> a === c
37 = \a b c p q -> Equality.trans p (Equality.sym q)
38
39-- Nothing proves a false equation: the empty union has no values, and from
40-- one, anything follows.
41let Not : Type -> Type = \a -> a -> <>
42
43let exFalso : forall {a : Type} -> <> -> a = \{a} impossible -> absurd (\(_ : <>) -> a) impossible
44
45in { plusZeroLeft, plusZeroRight, doubleIsPlus, example, Not, exFalso }