-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewProofCombinators.hs
214 lines (152 loc) · 6.22 KB
/
NewProofCombinators.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE IncoherentInstances #-}
module NewProofCombinators (
-- ATTENTION! `Admit` and `(==!)` are UNSAFE: they should not belong the final proof term
-- * Proof is just a () alias
Proof
, toProof
-- * Proof constructors
, trivial, unreachable, (***), QED(..)
-- * Proof certificate constructors
, (?)
-- * These two operators check all intermediate equalities
, (===) -- proof of equality is implicit eg. x === y
, (==?) -- proof of equality is explicit eg. x ==? y ? p
, (=<=) -- proof of equality is implicit eg. x <= y
, (=<=?) -- proof of equality is explicit eg. x <= y
, (=>=) -- proof of equality is implicit eg. x =>= y
, (=>=?) -- proof of equality is explicit eg. x =>=? y ? p
-- Uncheck operator used only for proof debugging
, (==!) -- x ==! y always succeds
-- * Combining Proofs
, (&&&)
, withProof
, impossible
) where
-------------------------------------------------------------------------------
-- | Proof is just a () alias -------------------------------------------------
-------------------------------------------------------------------------------
type Proof = ()
toProof :: a -> Proof
toProof _ = ()
-------------------------------------------------------------------------------
-- | Proof Construction -------------------------------------------------------
-------------------------------------------------------------------------------
-- | trivial is proof by SMT
trivial :: Proof
trivial = ()
-- {-@ unreachable :: {v : Proof | False } @-}
unreachable :: Proof
unreachable = ()
-- All proof terms are deleted at runtime.
{- RULE "proofs are irrelevant" forall (p :: Proof). p = () #-}
-- | proof casting
-- | `x *** QED`: x is a proof certificate* strong enough for SMT to prove your theorem
-- | `x *** Admit`: x is an unfinished proof
infixl 3 ***
{-@ assume (***) :: a -> p:QED -> { if (isAdmit p) then false else true } @-}
(***) :: a -> QED -> Proof
_ *** _ = ()
data QED = Admit | QED
{-@ measure isAdmit :: QED -> Bool @-}
{-@ Admit :: {v:QED | isAdmit v } @-}
-------------------------------------------------------------------------------
-- | * Checked Proof Certificates ---------------------------------------------
-------------------------------------------------------------------------------
-- Any (refined) carries proof certificates.
-- For example 42 :: {v:Int | v == 42} is a certificate that
-- the value 42 is equal to 42.
-- But, this certificate will not really be used to proof any fancy theorems.
-- Below we provide a number of equational operations
-- that constuct proof certificates.
-- | Implicit equality
-- x === y returns the proof certificate that
-- result value is equal to both x and y
-- when y == x (as assumed by the operator's precondition)
infixl 3 ===
{-@ (===) :: x:a -> y:{a | y == x} -> {v:a | v == x && v == y} @-}
(===) :: a -> a -> a
_ === y = y
infixl 3 =<=
{-@ (=<=) :: x:a -> y:{a | x <= y} -> {v:a | v == y} @-}
(=<=) :: a -> a -> a
_ =<= y = y
infixl 3 =>=
{-@ (=>=) :: x:a -> y:{a | x >= y} -> {v:a | v == y} @-}
(=>=) :: a -> a -> a
_ =>= y = y
-------------------------------------------------------------------------------
-- | Explicit equality
-- `x ==? y ? p`
-- returns the proof certificate that result value is equal to both x and y
-- when y == x is explicitely asserted by the proof term p
-------------------------------------------------------------------------------
infixl 3 ==?
{-@ (==?) :: x:a -> y:a -> {v:_ | x == y} -> {v:a | v == x && v == y} @-}
(==?) :: a -> a -> b -> a
(==?) _ y _ = y
infixl 3 =<=?
{-@ (=<=?) :: x:a -> y:a -> {v:_ | x <= y} -> {v:a | v == y} @-}
(=<=?) :: a -> a -> b -> a
(=<=?) _ y _ = y
infixl 3 =>=?
{-@ (=>=?) :: x:a -> y:a -> {v:_ | x >= y} -> {v:a | v == y} @-}
(=>=?) :: a -> a -> b -> a
(=>=?) _ y _ = y
-------------------------------------------------------------------------------
-- | `?` is basically Haskell's $ and is used for the right precedence
-------------------------------------------------------------------------------
infixl 3 ?
(?) :: (proof -> a) -> proof -> a
f ? y = f y
-------------------------------------------------------------------------------
-- | Assumed equality
-- `x ==! y `
-- returns the admitted proof certificate that result value is equals x and y
-------------------------------------------------------------------------------
infixl 3 ==!
{-@ assume (==!) :: x:a -> y:a -> {v:a | v == x && v == y} @-}
(==!) :: a -> a -> a
(==!) _ y = y
-- | To summarize:
--
-- - (==!) is *only* for proof debuging
-- - (===) does not require explicit proof term
-- - (==?) requires explicit proof term
-------------------------------------------------------------------------------
-- | * Unchecked Proof Certificates -------------------------------------------
-------------------------------------------------------------------------------
-- The above operators check each intermediate proof step.
-- The operator `==.` below accepts an optional proof term
-- argument, but does not check intermediate steps.
infixl 3 ==.
class OptEq a r where
(==.) :: a -> a -> r
instance (a~b) => OptEq a (Proof -> b) where
{-@ instance OptEq a (Proof -> b) where
==. :: x:a -> y:a -> {v:Proof | x == y} -> {v:b | v ~~ x && v ~~ y}
@-}
(==.) _ y _ = y
instance (a~b) => OptEq a b where
{-@ instance OptEq a b where
==. :: x:a -> y:{a| x == y} -> {v:b | v ~~ x && v ~~ y }
@-}
(==.) _ y = y
-------------------------------------------------------------------------------
-- | * Combining Proof Certificates -------------------------------------------
-------------------------------------------------------------------------------
(&&&) :: Proof -> Proof -> Proof
x &&& _ = x
{-@ withProof :: x:a -> b -> {v:a | v = x} @-}
withProof :: a -> b -> a
withProof x y = x
{-@ impossible :: {v:a | false} -> b @-}
impossible :: a -> b
impossible _ = undefined
-------------------------------------------------------------------------------
-- | Convenient Syntax for Inductive Propositions
-------------------------------------------------------------------------------
{-@ measure prop :: a -> b @-}
{-@ type Prop E = {v:_ | prop v = E} @-}