forked from ucsd-progsys/liquidhaskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProofCombinators.hs
195 lines (142 loc) · 5.87 KB
/
ProofCombinators.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
{-# OPTIONS_GHC -fplugin=LiquidHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE IncoherentInstances #-}
module Language.Haskell.Liquid.ProofCombinators (
-- 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 implicit eg. x <= y
, (=>=) -- proof of equality is implicit eg. x =>= y
-- * This operator does not check intermediate equalities
, (==.)
-- Uncheck operator used only for proof debugging
, (==!) -- x ==! y always succeeds
-- * Combining Proofs
, (&&&)
, withProof
, impossible
-- * PLE-specific
, pleUnfold
) 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 @-}
isAdmit :: QED -> Bool
isAdmit Admit = True
isAdmit QED = False
-------------------------------------------------------------------------------
-- | * 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
-------------------------------------------------------------------------------
-- | `?` is basically Haskell's $ and is used for the right precedence
-- | `?` lets you "add" some fact into a proof term
-------------------------------------------------------------------------------
infixl 3 ?
{-@ (?) :: forall a b <pa :: a -> Bool, pb :: b -> Bool>. a<pa> -> b<pb> -> a<pa> @-}
(?) :: a -> b -> a
x ? _ = x
{-# INLINE (?) #-}
-------------------------------------------------------------------------------
-- | 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 debugging
-- - (===) does not require explicit proof term
-- - (?) lets you insert "lemmas" as other `Proof` values
-------------------------------------------------------------------------------
-- | * 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.
-- So, using `==.` the proofs are faster, but the error messages worse.
infixl 3 ==.
{-# INLINE (==.) #-}
(==.) :: a -> a -> a
_ ==. x = x
-------------------------------------------------------------------------------
-- | * Combining Proof Certificates -------------------------------------------
-------------------------------------------------------------------------------
(&&&) :: Proof -> Proof -> Proof
x &&& _ = x
{-@ withProof :: x:a -> b -> {v:a | v = x} @-}
{-@ define withProof x y = (x) @-}
withProof :: a -> b -> a
withProof x _ = 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} @-}
-------------------------------------------------------------------------------
-- PLE-specific
-------------------------------------------------------------------------------
-- | Forces PLE to unfold a function application if the body of the function
-- starts with an application of @pleUnfold@.
{-@ reflect pleUnfold @-}
pleUnfold :: a -> a
pleUnfold x = x