-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeano.hs
78 lines (59 loc) · 1.85 KB
/
Peano.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE GADTs #-}
{-@ LIQUID "--exact-data-con" @-}
{-@ LIQUID "--higherorder" @-}
{-@ LIQUID "--ple" @-}
module Peano where
import Language.Haskell.Liquid.NewProofCombinators
--------------------------------------------------------------------------------
-- | Peano Numbers -------------------------------------------------------------
--------------------------------------------------------------------------------
{-@ data Peano [toNat] @-}
data Peano where
Z :: Peano
S :: Peano -> Peano
{-@ reflect one @-}
one = S Z
{-@ reflect two @-}
two = S (S Z)
{-@ reflect three @-}
three = S (S (S Z))
{-@ reflect four @-}
four = S (S (S (S Z)))
{-@ measure toNat @-}
{-@ toNat :: Peano -> Nat @-}
toNat :: Peano -> Int
toNat Z = 0
toNat (S n) = 1 + toNat n
{-@ reflect prev @-}
prev :: Peano -> Peano
prev Z = Z
prev (S n) = n
{-@ reflect plus @-}
plus :: Peano -> Peano -> Peano
plus Z n = n
plus (S m) n = S (plus m n)
{-@ reflect double @-}
double :: Peano -> Peano
double n = plus n n
{-@ plus_zero_r :: n:Peano -> { plus n Z = n } @-}
plus_zero_r :: Peano -> ()
plus_zero_r Z = ()
plus_zero_r (S n) = plus_zero_r n
{-@ plus_succ_r :: n:Peano -> m:Peano -> { plus n (S m) = S (plus n m)} @-}
plus_succ_r :: Peano -> Peano -> ()
plus_succ_r Z m = ()
plus_succ_r (S n) m = plus_succ_r n m
{-@ plus_comm :: n:Peano -> m:Peano -> { plus n m = plus m n } @-}
plus_comm :: Peano -> Peano -> ()
plus_comm Z m = plus_zero_r m
plus_comm (S n) m = plus_comm n m &&& plus_succ_r (S m) n
{-@ plus_assoc :: n:Peano -> m:Peano -> p:Peano ->
{ plus (plus n m) p = plus n (plus m p) } @-}
plus_assoc :: Peano -> Peano -> Peano -> ()
plus_assoc Z m p = ()
plus_assoc (S n) m p = plus_assoc n m p
{-@ reflect isLe @-}
isLe :: Peano -> Peano -> Bool
isLe Z _ = True
isLe _ Z = False
isLe (S n) (S m) = isLe n m