-
Notifications
You must be signed in to change notification settings - Fork 24
/
Lec_2_6.hs
136 lines (90 loc) · 2.87 KB
/
Lec_2_6.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{-@ LIQUID "--reflection" @-}
{- LIQUID "--diff" @-}
{-@ LIQUID "--ple" @-}
{-@ infixr ++ @-} -- TODO: Silly to have to rewrite this annotation!
{-# LANGUAGE GADTs #-}
module Lec_2_6 where
import Prelude hiding ((++))
import ProofCombinators
import Lists
import Expressions
import qualified State as S
data Peano where
Z :: Peano
S :: Peano -> Peano
-- How to define "Even"?
{-@ reflect isEven @-}
isEven :: Peano -> Bool
isEven Z = True
isEven (S n) = not (isEven n)
-- isEven (S Z) = False
-- isEven (S (S n)) = isEven n
-- (isEven k)
-- (isWellTyped p)
-- 1. define "div-by-2" or "mod-2"
-- 2. define it recursively on the peano
-- 3. "recur"
-- 4. make a NEW type
{-
data List a = Nil | Cons a (List a)
data List a where
Nil :: List a
Cons :: a -> List a -> List a
-}
----
-- | The "Prop" describing an Even number `(Ev n)`
data EvP where
Ev :: Peano -> EvP
{-@ data Ev where
EvZ :: Prop (Ev Z)
| EvS :: n:Peano -> Prop (Ev n) -> Prop (Ev (S (S n)))
@-}
data Ev where
EvZ :: Ev
EvS :: Peano -> Ev -> Ev
{-@ zero_is_Even :: Prop (Ev Z) @-}
zero_is_Even :: Ev
zero_is_Even = EvZ
{-@ two_is_Even :: Prop (Ev (S (S Z))) @-}
two_is_Even :: Ev
two_is_Even = EvS Z zero_is_Even
{-@ four_is_Even :: Prop (Ev (S (S (S (S Z))))) @-}
four_is_Even :: Ev
four_is_Even = EvS (S (S Z)) two_is_Even
{-
-- WHY IS this even Even?
EvZ && EvS (S (S Z))
but EvS takes TWO parameters so ...
EvS Z EvZ
'proof that n is Even'
-}
-- | Q: Have we really defined Ev?
{-@ lem_isEven :: n:_ -> Prop (Ev n) -> { isEven n } @-}
lem_isEven :: Peano -> Ev -> Proof
lem_isEven Z _ = ()
lem_isEven (S Z) EvZ = () -- impossible "haha"
lem_isEven (S Z) (EvS _ _) = () -- impossible "haha"
-- lem_isEven (S (S m)) EvZ = () -- undefined
lem_isEven (S (S m)) (EvS _m ev_m) = isEven (S (S m))
=== not (not (isEven m))
=== isEven m
? lem_isEven m ev_m
=== True
*** QED
-- "call 'lem_isEven m ev_m' ===> 'isEven m'
{-@ lemon :: n:{_| isEven n} -> Prop (Ev n) @-}
lemon :: Peano -> Ev
lemon Z = EvZ
lemon (S Z) = impossible "asdasdasd" -- impossible "asdasd"
-- lemon (S (S mm)) = EvS mm (lemon mm)
lemon (S m) = case m of
Z -> impossible "are you clever?"
S mm -> EvS mm (lemon mm)
{-@ lemma_ev :: n:_ -> Prop (Ev n) -> {isEven n} @-}
lemma_ev :: Peano -> Ev -> Proof
lemma_ev Z _ = ()
lemma_ev (S Z) EvZ = ()
lemma_ev (S (S n)) (EvS _ pn) = lemma_ev n pn
lemma_ev _ _ = impossible "hoho"
dummy :: Int
dummy = 10