\begin{code}
{-# OPTIONS --safe --experimental-lossy-unification #-}
open import Prelude
open import Signatures
open import FinitenessConditions
open import FreeMonad.PackagedTheory
module Hoare.Calculus {β} (π― : FullTheory β) where
open import FreeMonad.Quotiented π―
open import Hoare.Definition π―
open import Hoare.Lemmas π―
open import Truth
β¨return_β© : Ξ© a β Assertion a
β¨return x β© .fst = return x
β¨return x β© .snd .fst k = refl
β¨return x β© .snd .snd k = refl
pass : β {a} β Term (Poly-β€ {a})
pass = return Poly-tt
_<<_ : Term A β Term B β Term A
xs << ys = do x β xs; ys; return x
sef : (Ξ¦ : Assertion a) β let Ο = Ξ¦ .fst in
(q : Term A)
β SEF 0 q
β
β
Ο β q >> pass β
Ο β
sef (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) q sefβ¨qβ© .proof k =
(do a β Ο
q >> return Poly-tt
b β Ο
k Poly-tt (a |β| b)) β‘β¨ a β Ο ΝΎ/ cong (_>> _) (sefβ¨qβ© _) β©
(do a β Ο
b β Ο
k Poly-tt (a |β| b)) β‘β¨ dsefβ¨Οβ© (Ξ» a b β k Poly-tt (a |β| b)) β©
(do a β Ο
b β Ο
k Poly-tt (a |β| a)) β‘β¨ a β Ο ΝΎ/ b β Ο ΝΎ/ cong (k Poly-tt) (|β|-id a) β©
(do a β Ο
b β Ο
k Poly-tt True) β‘Λβ¨ a β Ο ΝΎ/ cong (_>> _) (sefβ¨qβ© _) β©
(do a β Ο
x β q >> return Poly-tt
b β Ο
k Poly-tt True) β
stateless : (Ο : Ξ© a) (p : Term B) β β
return Ο β [ p /β¨β©] β
return Ο β
stateless Ο p .proof k = cong ([ p /β¨β©] >>_) (cong (k tt) (|β|-id Ο))
dsef : {A : Type a} β (p : Term A)
β DET (βsuc a) p
β
β
return (True {a}) β x β p β
(do y β p ; return (x |β‘| y)) β
dsef {a = aβ} p dsefβ¨pβ© .proof k =
(do a β return (True {aβ})
x β p
b β (do y β p ; return (x |β‘| y))
k x (a |β| b)) β‘β¨β©
(do x β p
b β (do y β p ; return (x |β‘| y))
k x (True {aβ} |β| b)) β‘β¨ x β p ΝΎ/ assoc p _ _ β©
(do x β p
y β p
b β return (x |β‘| y)
k x (True {aβ} |β| b)) β‘β¨β©
(do x β p
y β p
k x (True {aβ} |β| (x |β‘| y)))
β‘β¨ x β p ΝΎ/ y β p ΝΎ/ cong (k x) (|β|-idΛ‘ (x |β‘| y)) β©
(do x β p
y β p
k x (x |β‘| y)) β‘β¨ dsefβ¨pβ© _ β©
(do x β p
y β p
k x (x |β‘| x))
β‘β¨ x β p ΝΎ/ y β p ΝΎ/ cong (k x) (proves β£ refl β£) β©
(do x β p
y β p
k x (True {aβ})) β‘Λβ¨ dsefβ¨pβ© _ β©
(do x β p
y β p
k x (True {aβ})) β‘β¨β©
(do a β return (True {aβ})
x β p
y β p
k x (True {aβ})) β‘Λβ¨ x β p ΝΎ/ assoc p _ _ β©
(do a β return (True {aβ})
x β p
b β (do y β p ; return (x |β‘| y))
k x (True {aβ})) β
β¨&&&β©Λ‘ :
(Ο : Term (Ξ© b))
(Ο : A β Term (Ξ© b))
(Ο : Ξ© b)
(t : Term A) β
β
Ο β x β t β
Ο x β β
β
return Ο β¨β§β© Ο β x β t β
return Ο β¨β§β© Ο x β
β¨&&&β©Λ‘ Ο Ο Ο t h .proof k =
(do a β return Ο β¨β§β© Ο
x β t
b β return Ο β¨β§β© Ο x
k x (a |β| b)) β‘β¨ βͺassocβ« (return Ο) Ο _|β§|_ _ β©
(do a β Ο
x β t
b β return Ο β¨β§β© Ο x
k x ((Ο |β§| a) |β| b)) β‘β¨ (a β Ο ΝΎ/ x β t ΝΎ/ βͺassocβ« (return Ο) (Ο x) _|β§|_ _) β©
(do a β Ο
x β t
b β Ο x
k x ((Ο |β§| a) |β| (Ο |β§| b))) β‘β¨ a β Ο ΝΎ/ x β t ΝΎ/ b β Ο x ΝΎ/ cong (k x) ((Ξ» f x a β snd (f (x , a))) iff Ξ» { f (x , a) β x , f x a }) β©
(do a β Ο
x β t
b β Ο x
k x (Ο |β| (a |β| b))) β‘β¨ h .proof (Ξ» x r β k x (Ο |β| r)) β©
(do a β Ο
x β t
b β Ο x
k x (Ο |β| True)) β‘β¨ a β Ο ΝΎ/ x β t ΝΎ/ b β Ο x ΝΎ/ cong (k x) (|β|-annΚ³ Ο) β©
(do a β Ο
x β t
b β Ο x
k x True) β‘Λβ¨ a β Ο ΝΎ/ x β t ΝΎ/ βͺassocβ« (return Ο) (Ο x) _|β§|_ _ β©
(do a β Ο
x β t
b β return Ο β¨β§β© Ο x
k x True) β‘Λβ¨ βͺassocβ« (return Ο) Ο _|β§|_ _ β©
(do a β return Ο β¨β§β© Ο
x β t
b β return Ο β¨β§β© Ο x
k x True) β
_ΝΎ,_ : Term A β (A β Term B) β Term (A Γ B)
xs ΝΎ, ys = do
x β xs
y β ys x
return (x , y)
ΝΎ,-assoc : (xs : Term A) (ys : A β Term B) (k : A β B β Term C) β
(xs ΝΎ, ys) >>= uncurry k β‘ (do x β xs ; y β ys x ; k x y)
ΝΎ,-assoc xs ys k = assoc xs (Ξ» x β ys x >>= Ξ» y β return (x , y)) (uncurry k) ΝΎ x β xs ΝΎ/ assoc (ys x) _ _
seq : {A B : Type a}
β (Ξ¦ : Assertion a) β let Ο = Ξ¦ .fst in
(Ξ¨ : A β Assertion a) β let Ο = fst β Ξ¨ in
(Ξ§ : A Γ B β Assertion a) β let Ο = fst β Ξ§ in
{p : Term A} {q : A β Term B}
β β
Ο β x β p β
Ο x β
β (β x β β
Ο x β y β q x β
Ο (x , y) β)
β
β
Ο β xy β p ΝΎ, q β
Ο xy β
seq {a = aβ} (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) Ξ¨ Ξ§ {p = p} {q = q} lhs rhs .proof k =
let Ο = fst β Ξ§
Ο = fst β Ξ¨
in
(do a β Ο
(x , y) β p ΝΎ, q
b β Ο (x , y)
k (x , y) (a |β| b))
β‘β¨ a β Ο ΝΎ/ ΝΎ,-assoc p q _ β©
(do a β Ο
x β p
y β q x
b β Ο (x , y)
k (x , y) (a |β| b))
β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ Ξ¨ x .snd .fst _ β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) (a |β| b))
β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ i β Ο x ΝΎ/ y β q x ΝΎ/ b β Ο (x , y) ΝΎ/ cong (k (x , y)) (|β|-idΛ‘ _) β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) (True {aβ} |β| (a |β| b)))
β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ i β Ο x ΝΎ/ y β q x ΝΎ/ sef-dsef-cont (Ξ§ (x , y)) _ β©
(do a β Ο
x β p
i β Ο x
y β q x
bβ β Ο (x , y)
bβ β Ο (x , y)
k (x , y) (True {aβ} |β| (a |β| bβ)))
β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ rhs x .proof (Ξ» y r β do bβ β Ο (x , y) ; k (x , y) (r |β| a |β| bβ) ) β©
(do a β Ο
x β p
i β Ο x
y β q x
bβ β Ο (x , y)
bβ β Ο (x , y)
k (x , y) ((i |β| bβ) |β| (a |β| bβ)))
β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ i β Ο x ΝΎ/ y β q x ΝΎ/ sef-dsef-cont (Ξ§ (x , y)) _ β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) ((i |β| b) |β| (a |β| b)))
β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ i β Ο x ΝΎ/ y β q x ΝΎ/ b β Ο (x , y) ΝΎ/ cong (k (x , y)) (|β|-idΛ‘ _) β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) (True {aβ} |β| (i |β| b) |β| (a |β| b)))
β‘Λβ¨ sef-dsef-cont (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) _ β©
(do aβ β Ο
aβ β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) (True {aβ} |β| (i |β| b) |β| (aβ |β| b)))
β‘Λβ¨ aβ β Ο ΝΎ/ aβ β Ο ΝΎ/ x β p ΝΎ/ sef-dsef-cont (Ξ¨ x) _ β©
(do aβ β Ο
aβ β Ο
x β p
iβ β Ο x
iβ β Ο x
y β q x
b β Ο (x , y)
k (x , y) (True {aβ} |β| (iβ |β| b) |β| (aβ |β| b)))
β‘Λβ¨ aβ β Ο ΝΎ/ proof lhs (Ξ» x r β do iβ β Ο x ; y β q x ; b β Ο (x , y) ; k (x , y) (r |β| (iβ |β| b) |β| (aβ |β| b))) β©
(do aβ β Ο
aβ β Ο
x β p
iβ β Ο x
iβ β Ο x
y β q x
b β Ο (x , y)
k (x , y) ((aβ |β| iβ) |β| (iβ |β| b) |β| (aβ |β| b)))
β‘β¨ sef-dsef-cont (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) _ β©
(do a β Ο
x β p
iβ β Ο x
iβ β Ο x
y β q x
b β Ο (x , y)
k (x , y) ((a |β| iβ) |β| (iβ |β| b) |β| (a |β| b)))
β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ sef-dsef-cont (Ξ¨ x) _ β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) ((a |β| i) |β| (i |β| b) |β| (a |β| b)))
β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ i β Ο x ΝΎ/ y β q x ΝΎ/ b β Ο (x , y) ΝΎ/ cong (k (x , y)) (|β|-curry _ _ _) β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) ((a |β| i) |β§| (i |β| b) |β| (a |β| b)))
β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ i β Ο x ΝΎ/ y β q x ΝΎ/ b β Ο (x , y) ΝΎ/ cong (k (x , y)) (|β|-trans a i b) β©
(do a β Ο
x β p
i β Ο x
y β q x
b β Ο (x , y)
k (x , y) (True {aβ}))
β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ Ξ¨ x .snd .fst _ β©
(do a β Ο
x β p
y β q x
b β Ο (x , y)
k (x , y) True)
β‘Λβ¨ a β Ο ΝΎ/ ΝΎ,-assoc p q _ β©
(do a β Ο
(x , y) β p ΝΎ, q
b β Ο (x , y)
k (x , y) True) β
focus : {A B : Type a} {Ο : Term (Ξ© b)} {Ο : B β Term (Ξ© c)} {p : Term A}
β (sub : A β B)
β β
Ο β x β p β
Ο (sub x) β
β β
Ο β x β sub <$> p β
Ο x β
focus {Ο = Ο} {Ο = Ο} {p = p} sub hr .proof k =
(do a β Ο
x β sub <$> p
b β Ο x
k x (a |β| b)) β‘β¨ a β Ο ΝΎ/ assoc p _ _ β©
(do a β Ο
x β p
b β Ο (sub x)
k (sub x) (a |β| b)) β‘β¨ hr .proof (k β sub) β©
(do a β Ο
x β p
b β Ο (sub x)
k (sub x) True) β‘Λβ¨ a β Ο ΝΎ/ assoc p _ _ β©
(do a β Ο
x β sub <$> p
b β Ο x
k x True) β
β‘β
β‘ββ‘ : {Ο Οβ² : Term (Ξ© a)} {t tβ² : Term A} {Ο Οβ² : A β Term (Ξ© b)} β Ο β‘ Οβ² β t β‘ tβ² β (β x β Ο x β‘ Οβ² x) β β
Ο β x β t β
Ο x β β β
Οβ² β x β tβ² β
Οβ² x β
β‘β
β‘ββ‘ p q r = subst (Hoare _ _) (funExt r) β substβ (Ξ» x y β Hoare x y _) p q
subst-β
β : {Ο : Term (Ξ© b)} {p : Term A} {Ο : A β Term (Ξ© c)} {q : Term A}
β p β‘ q
β β
Ο β x β p β
Ο x β
β β
Ο β x β q β
Ο x β
subst-β
β {Ο = Ο} {Ο = Ο} = subst (flip (Hoare Ο) Ο)
seqβ² : {A B : Type a}
β (Ξ¦ : Assertion a) β let Ο = Ξ¦ .fst in
(Ξ¨ : A β Assertion a) β let Ο = fst β Ξ¨ in
(Ξ§ : B β Assertion a) β let Ο = fst β Ξ§ in
{p : Term A} {q : A β Term B}
β β
Ο β x β p β
Ο x β
β (β x β β
Ο x β y β q x β
Ο y β)
β
β
Ο β y β p >>= q β
Ο y β
seqβ² Ξ¦ Ξ¨ Ξ§ {p = p} {q = q} lhs rhs =
let h = seq Ξ¦ Ξ¨ (Ξ§ β snd) lhs rhs
hβ² = focus snd h
in subst-β
β (snd <$> (p ΝΎ, q) β‘β¨ assoc p _ _ ΝΎ x β p ΝΎ/ assoc (q x) _ _ ΝΎ >>=-idΚ³ (q x) β© (p >>= q) β) hβ²
seqβ» : {A B : Type a}
β (Ξ¦ : Assertion a) β let Ο = Ξ¦ .fst in
(Ξ¨ : A β Assertion a) β let Ο = fst β Ξ¨ in
(Ξ§ : B β Assertion a) β let Ο = fst β Ξ§ in
{p : Term A} {q : A β Term B}
β (β x β β
Ο x β y β q x β
Ο y β)
β β
Ο β x β p β
Ο x β
β
β
Ο β y β p >>= q β
Ο y β
seqβ» Ξ¦ Ξ¨ Ξ§ = flip (seqβ² Ξ¦ Ξ¨ Ξ§)
seq-<< : {A B : Type a}
β (Ξ¦ : Assertion a) β let Ο = Ξ¦ .fst in
(Ξ¨ : A β Assertion a) β let Ο = fst β Ξ¨ in
(Ξ§ : A β Assertion a) β let Ο = fst β Ξ§ in
{p : Term A} {q : Term B}
β β
Ο β x β p β
Ο x β
β (β x β β
Ο x β y β q β
Ο x β)
β
β
Ο β y β p << q β
Ο y β
seq-<< Ξ¦ Ξ¨ Ξ§ {p = p} {q = q} lhs rhs =
let h = seq Ξ¦ Ξ¨ (Ξ§ β fst) lhs rhs
hβ² = focus fst h
in subst-β
β (assoc p _ _ ΝΎ x β p ΝΎ/ assoc q _ _) hβ²
module _
{A : Type a}
(Ο : Assertion a)
(p : Term A)
(Ο Ο : A β Assertion a)
where
imply : (β x β β
Ο x .fst β pass β
Ο x .fst β)
β β
Ο .fst β x β p β
Ο x .fst β
β β
Ο .fst β x β p β
Ο x .fst β
imply imp h = subst-β
β (>>=-idΚ³ p) ( seqβ² Ο Ο Ο h Ξ» x β focus (const x) (imp x))
β
sndβ : {A : Type a} (Ξ¦ : Assertion a) (p : Term A)
(Οβ : A β Term (Ξ© a)) β
(sefβ¨Οββ© : β x β SEF (βsuc a) (Οβ x))
(Ξ¨β : A β Assertion a) β
β
Ξ¦ .fst β x β p β
Οβ x β¨β§β© Ξ¨β x .fst β β β
Ξ¦ .fst β x β p β
Ξ¨β x .fst β
β
sndβ Ξ¦ p Οβ sefβ¨Οββ© Ξ¨β prf .proof k =
let Ο = Ξ¦ .fst
Οβ = fst β Ξ¨β
in
(do a β Ο
x β p
bβ β Οβ x
k x (a |β| bβ)) β‘Λβ¨ a β Ο ΝΎ/ Ξ¦ .snd .fst _ β©
(do a β Ο
aβ² β Ο
x β p
bβ β Οβ x
k x (a |β| bβ)) β‘Λβ¨ a β Ο ΝΎ/ aβ² β Ο ΝΎ/ x β p ΝΎ/ sefβ¨Οββ© x _ β©
(do a β Ο
aβ² β Ο
x β p
bββ² β Οβ x
bβ β Οβ x
k x (a |β| bβ)) β‘Λβ¨ a β Ο ΝΎ/ aβ² β Ο ΝΎ/ x β p ΝΎ/ bββ² β Οβ x ΝΎ/ Ξ¨β x .snd .fst _ β©
(do a β Ο
aβ² β Ο
x β p
bββ² β Οβ x
bββ² β Οβ x
bβ β Οβ x
k x (a |β| bβ)) β‘Λβ¨ a β Ο ΝΎ/ aβ² β Ο ΝΎ/ x β p ΝΎ/ βͺassocβ« (Οβ x) (Οβ x) _|β§|_ _ β©
(do a β Ο
aβ² β Ο
x β p
bβ² β Οβ x β¨β§β© Οβ x
bβ β Οβ x
k x (a |β| bβ)) β‘β¨ a β Ο ΝΎ/ aβ² β Ο ΝΎ/ x β p ΝΎ/ bβ² β Οβ x β¨β§β© Οβ x ΝΎ/ bβ β Οβ x ΝΎ/ cong (k x) (cong (a |β|_) ((_, _) iff fst)) β©
(do a β Ο
aβ² β Ο
x β p
bβ² β Οβ x β¨β§β© Οβ x
bβ β Οβ x
k x (a |β| (bβ |β§| True))) β‘Λβ¨ a β Ο ΝΎ/ prf .proof (Ξ» x t β do bβ β Οβ x; k x (a |β| (bβ |β§| t))) β©
(do a β Ο
aβ² β Ο
x β p
bβ² β Οβ x β¨β§β© Οβ x
bβ β Οβ x
k x (a |β| (bβ |β§| (aβ² |β| bβ²)))) β‘β¨ sef-dsef-cont Ξ¦ _ β©
(do a β Ο
x β p
bβ² β Οβ x β¨β§β© Οβ x
bβ β Οβ x
k x (a |β| (bβ |β§| (a |β| bβ²)))) β‘β¨ (a β Ο ΝΎ/ x β p ΝΎ/ βͺassocβ« (Οβ x) (Οβ x) _|β§|_ _ ) β©
(do a β Ο
x β p
bβ β Οβ x
bββ² β Οβ x
bβ β Οβ x
k x (a |β| (bβ |β§| (a |β| (bβ |β§| bββ²))))) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ bβ β Οβ x ΝΎ/ sef-dsef-cont (Ξ¨β x) _ β©
(do a β Ο
x β p
bβ β Οβ x
bβ β Οβ x
k x (a |β| (bβ |β§| (a |β| (bβ |β§| bβ))))) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ bβ β Οβ x ΝΎ/ bβ β Οβ x ΝΎ/ cong (k x) ((Ξ» f x β f x .snd x .fst , f x .fst) iff Ξ» f x β f x .snd , const (f x)) β©
(do a β Ο
x β p
bβ β Οβ x
bβ β Οβ x
k x (a |β| (bβ |β§| bβ))) β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ βͺassocβ« (Οβ x) (Οβ x) _|β§|_ _ β©
(do a β Ο
x β p
bββ§bβ β Οβ x β¨β§β© Οβ x
k x (a |β| bββ§bβ)) β‘β¨ prf .proof k β©
(do a β Ο
x β p
bββ§bβ β Οβ x β¨β§β© Οβ x
k x True) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ βͺassocβ« (Οβ x) (Οβ x) _|β§|_ _ β©
(do a β Ο
x β p
bβ β Οβ x
bβ β Οβ x
k x True) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ sefβ¨Οββ© x _ β©
(do a β Ο
x β p
bβ β Οβ x
k x True) β
β
ββTrue : (Ο : Term (Ξ© a)) (t : Term B) β β
Ο β _ β t β
return (True {a = c}) β
β
ββTrue Ο t .proof k = a β Ο ΝΎ/ x β t ΝΎ/ cong (k x) (|β|-annΚ³ _)
module _ {A : Type a} (Ξ¦ : Assertion a) {t : Term A} {Ο : A β Term (Ξ© a)} (f : Ξ© a β Ξ© a) where
private Ο = fst Ξ¦
ββ
ββ : (i : β {x} β ProofOf (f x) β ProofOf x)
β β
Ο β x β t β
Ο x β
β β
f <$> Ο β x β t β
Ο x β
ββ
ββ i h .proof k =
(do a β f <$> Ο
x β t
b β Ο x
k x (a |β| b))
β‘β¨ assoc Ο _ _ β©
(do a β Ο
x β t
b β Ο x
k x (f a |β| b))
β‘β¨ a β Ο ΝΎ/ x β t ΝΎ/ b β Ο x ΝΎ/ cong (k x) ((Ξ» k x y β k x) iff Ξ» k x β k x (i x)) β©
(do a β Ο
x β t
b β Ο x
k x (f a |β| a |β| b))
β‘Λβ¨ sef-dsef-cont Ξ¦ _ β©
(do a β Ο
aβ² β Ο
x β t
b β Ο x
k x (f a |β| aβ² |β| b))
β‘β¨ a β Ο ΝΎ/ h .proof (Ξ» x r β k x (f a |β| r)) β©
(do a β Ο
aβ² β Ο
x β t
b β Ο x
k x (f a |β| True))
β‘β¨ sef-dsef-cont Ξ¦ _ β©
(do a β Ο
x β t
b β Ο x
k x (f a |β| True))
β‘β¨ a β Ο ΝΎ/ x β t ΝΎ/ b β Ο x ΝΎ/ cong (k x) (|β|-annΚ³ (f a)) β©
(do a β Ο
x β t
b β Ο x
k x True)
β‘Λβ¨ assoc Ο _ _ β©
(do a β f <$> Ο
x β t
b β Ο x
k x True) β
module _ {A : Type a} (Ξ¦ : Assertion a) {t : Term A} (Ξ¨ : A β Assertion a) (f : Ξ© a β Ξ© a) where
private
Ο = fst β Ξ¨
Ο = fst Ξ¦
β
βββ : (i : β {x} β ProofOf x β ProofOf (f x))
β β
Ο β x β t β
Ο x β
β β
Ο β x β t β
f <$> Ο x β
β
βββ i h =
subst-β
β (>>=-idΚ³ _) (seqβ² Ξ¦ Ξ¨ (Ξ» x β _ , sef-<$> f (Ο x) (Ξ¨ x .snd .fst) , det-<$> f (Ο x) (Ξ¨ x .snd .snd)) h
Ξ» where x .proof k β
(do a β Ο x
b β f <$> Ο x
k _ (a |β| b)) β‘β¨ a β Ο x ΝΎ/ assoc (Ο x) _ _ β©
(do a β Ο x
b β Ο x
k _ (a |β| f b)) β‘β¨ sef-dsef-cont (Ξ¨ x) _ β©
(do a β Ο x
k _ (a |β| f a)) β‘β¨ a β Ο x ΝΎ/ cong (k _) (proves i) β©
(do a β Ο x
k _ True) β‘Λβ¨ Ξ¨ x .snd .fst _ β©
(do a β Ο x
b β Ο x
k _ True) β‘Λβ¨ a β Ο x ΝΎ/ assoc (Ο x) _ _ β©
(do a β Ο x
b β f <$> Ο x
k _ True) β
)
Falseβ
ββ : (t : Term A) (Ο : A β Term (Ξ© b)) β β
return (False {a = c}) β x β t β
Ο x β
Falseβ
ββ t Ο .proof k = x β t ΝΎ/ b β Ο x ΝΎ/ cong (k x) (|β|-annΛ‘ _)
β
idβ : (Ο : Term (Ξ© a)) β DET (βsuc a) Ο β β
Ο β pass {a = a} β
Ο β
β
idβ Ο det .proof k = det (Ξ» a b β k Poly-tt (a |β| b)) ΝΎ a β Ο ΝΎ/ b β Ο ΝΎ/ cong (k Poly-tt) (|β|-id _)
β
retβ : (Ο : Ξ© a) (t : Term B) β β
return Ο β _ β t β
return Ο β
β
retβ Ο t .proof k = x β t ΝΎ/ cong (k x) (|β|-id Ο)
β‘β
βββ‘ : {Ο Οβ² : Term (Ξ© a)} {t : Term A} {Ο Οβ² : A β Term (Ξ© b)} β Ο β‘ Οβ² β (β x β Ο x β‘ Οβ² x) β β
Ο β x β t β
Ο x β β β
Οβ² β x β t β
Οβ² x β
β‘β
βββ‘ {t = t} p q = substβ (Ξ» p q β Hoare p t q) p (funExt q)
β‘β
ββ : {Ο Οβ² : Term (Ξ© a)} {t : Term A} {Ο : A β Term (Ξ© b)} β Ο β‘ Οβ² β β
Ο β x β t β
Ο x β β β
Οβ² β x β t β
Ο x β
β‘β
ββ {t = t} {Ο = Ο} = subst (Ξ» p β Hoare p t Ο)
β
βββ‘ : {Ο : Term (Ξ© a)} {t : Term A} {Ο Οβ² : A β Term (Ξ© b)} β (β x β Ο x β‘ Οβ² x) β β
Ο β x β t β
Ο x β β β
Ο β x β t β
Οβ² x β
β
βββ‘ {Ο = Ο} {t = t} e = subst (Ξ» p β Hoare Ο t p) (funExt e)
β¨&&&β©Κ³ :
(Ο : Term (Ξ© b))
(Ο : A β Term (Ξ© b))
(Ο : Ξ© b)
(t : Term A) β
β
Ο β x β t β
Ο x β β
β
Ο β¨β§β© return Ο β x β t β
Ο x β¨β§β© return Ο β
β¨&&&β©Κ³ Ο Ο Ο t h = β‘β
βββ‘ (a β Ο ΝΎ/ cong return (β§-com Ο a)) (Ξ» x β b β Ο x ΝΎ/ cong return (β§-com Ο b)) (β¨&&&β©Λ‘ Ο Ο Ο t h)
Falseβ¨β§β© : (Ο : Term (Ξ© a)) β SEF (βsuc a) Ο β return False β¨β§β© Ο β‘ return False
Falseβ¨β§β© Ο sef =
return False β¨β§β© Ο β‘β¨β©
(do f β return False; p β Ο; return (f |β§| p)) β‘β¨β©
(do p β Ο; return (False |β§| p)) β‘β¨ p β Ο ΝΎ/ cong return (fst iff Ξ» ()) β©
(do p β Ο; return False) β‘β¨ sef (return False) β©
return False β
Trueβ¨β§β© : (Ο : Term (Ξ© a)) β return True β¨β§β© Ο β‘ Ο
Trueβ¨β§β© Ο = (a β Ο ΝΎ/ cong return (β§-id a)) ΝΎ <$>-id Ο
weaken : {A : Type a}
β (Ξ¦ : Assertion a) β let Ο = Ξ¦ .fst in
(Ξ¨ : A β Assertion a) β let Ο = fst β Ξ¨ in
{p : Term A}
β (Ξ§ : Assertion a) β let Ο = Ξ§ .fst in
β
Ο β pass β
Ο β
β β
Ο β x β p β
Ο x β
β β
Ο β x β p β
Ο x β
weaken Ξ¦ Ξ¨ {p = p} Ξ§ lhs rhs =
let h = seq Ξ§ (Ξ» _ β Ξ¦) (Ξ» { (_ , x) β Ξ¨ x }) lhs (Ξ» _ β rhs)
hβ² = focus snd h
in subst-β
β (assoc (return Poly-tt) (Ξ» x β p >>= Ξ» y β return (x , y)) (return ββ² snd) ΝΎ assoc p _ _ ΝΎ >>=-idΚ³ p) hβ²
if : {A : Type a}
β (Ο : Term (Ξ© a)) (Ο : A β Term (Ξ© a))
β
(p : Term A) (q : Term A) (b : Bool) β
β
return (|T| b) β¨β§β© Ο β x β p β
Ο x β β
β
return (|T| (! b)) β¨β§β© Ο β x β q β
Ο x β β
β
Ο β x β if b then p else q β
Ο x β
if Ο Ο p q =
bool
(Ξ» _ rhs β subst (Ξ» e β β
e β x β q β
Ο x β) (β§-lemma Ο) rhs)
(Ξ» lhs _ β subst (Ξ» e β β
e β x β p β
Ο x β) (β§-lemma Ο) lhs)
where
β§-lemma : (x : Term (Ξ© a)) β (True |β§|_) <$> x β‘ x
β§-lemma x = cong (_<$> x) (funExt β§-id) ΝΎ <$>-id x
conj : {A : Type a}
β (Ξ¦ : Assertion a) β let Ο = fst Ξ¦ in
(Ο : A β Term (Ξ© a)) β (β x β SEF (βsuc a) (Ο x))
β (Ο : A β Term (Ξ© a))
β (p : Term A)
β
\end{code}
%<*conj>
\begin{code}
(β
Ο β x β p β
Ο x β) β (β
Ο β x β p β
Ο x β) β (β
Ο β x β p β
Ο x β¨β§β© Ο x β)
\end{code}
%</conj>
\begin{code}
conj {a = aβ} (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) Ο sefβ¨Οβ© Ο p lhs rhs .proof k =
(do a β Ο
x β p
b β (Ο x β¨β§β© Ο x)
k x (a |β| b)) β‘β¨β©
(do a β Ο
x β p
b β (do bβ β Ο x ; bβ β Ο x ; return (bβ |β§| bβ))
k x (a |β| b)) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ assoc (Ο x) _ _ ΝΎ bβ β Ο x ΝΎ/ assoc (Ο x) _ _ β©
(do a β Ο
x β p
bβ β Ο x
bβ β Ο x
k x (a |β| (bβ |β§| bβ)))
β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ bβ β Ο x ΝΎ/ bβ β Ο x ΝΎ/ cong (k x) (|β|-|β§| a bβ bβ) β©
(do a β Ο
x β p
bβ β Ο x
bβ β Ο x
k x ((a |β| bβ) |β§| (a |β| bβ)))
β‘Λβ¨ sef-dsef-cont (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) _ β©
(do aβ β Ο
aβ β Ο
x β p
bβ β Ο x
bβ β Ο x
k x ((aβ |β| bβ) |β§| (aβ |β| bβ)))
β‘β¨ aβ β Ο ΝΎ/ lhs .proof (Ξ» x r β do bβ β Ο x ; k x (r |β§| (aβ |β| bβ))) β©
(do aβ β Ο
aβ β Ο
x β p
Ο x
bβ β Ο x
k x (True {aβ} |β§| (aβ |β| bβ))) β‘β¨ sef-dsef-cont (Ο , sefβ¨Οβ© , dsefβ¨Οβ©) _ β©
(do a β Ο
x β p
Ο x
bβ β Ο x
k x (True {aβ} |β§| (a |β| bβ)))
β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ _ β Ο x ΝΎ/ bβ β Ο x ΝΎ/ cong (k x) (β§-id _) β©
(do a β Ο
x β p
Ο x
bβ β Ο x
k x (a |β| bβ)) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ sefβ¨Οβ© x _ β©
(do a β Ο
x β p
bβ β Ο x
k x (a |β| bβ)) β‘β¨ rhs .proof k β©
(do a β Ο
x β p
Ο x
k x True) β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ sefβ¨Οβ© x _ β©
(do a β Ο
x β p
Ο x
Ο x
k x True) β‘Λβ¨ a β Ο ΝΎ/ x β p ΝΎ/ assoc (Ο x) _ _ ΝΎ b β Ο x ΝΎ/ assoc (Ο x) _ _ β©
(do a β Ο
x β p
Ο x β¨β§β© Ο x
k x True) β
β
β§β : {A : Type a} {p : Ξ© a} (Ο : Assertion a) {t : Term A} (Ο : A β Assertion a)
β β
Ο .fst β x β t β
Ο x .fst β β β
return p β¨β§β© Ο .fst β x β t β
return p β¨β§β© Ο x .fst β
β
β§β {p = p} Ο {t = t} Ο rhs =
conj
(return p β¨β§β© Ο .fst , sef-<$> _ _ (Ο .snd .fst) , det-<$> _ _ (Ο .snd .snd))
(Ξ» _ β return p)
( Ξ» x k β refl)
(Ξ» x β Ο x .fst)
t
(Ξ» where .proof k β (do a β return p β¨β§β© Ο .fst; x β t; b β return p; k x (a |β| b)) β‘β¨ assoc (Ο .fst) _ _ β©
(do a β Ο .fst; x β t; b β return p; k x ((p |β§| a) |β| b)) β‘β¨β©
(do a β Ο .fst; x β t; k x ((p |β§| a) |β| p)) β‘β¨ a β Ο .fst ΝΎ/ x β t ΝΎ/ cong (k x) (proves fst) β©
(do a β Ο .fst; x β t; k x True) β‘Λβ¨ assoc (Ο .fst) _ _ β©
(do a β return p β¨β§β© Ο .fst; x β t; b β return p; k x True) β
)
(ββ
ββ (_ , Ο .snd .fst , Ο .snd .snd) _ snd rhs)
module _ {A : Type a} where
\end{code}
\begin{code}
disj : (Ο : Term (Ξ© a))
β (Ο : Term (Ξ© a)) β SEF (βsuc a) Ο
β (Ξ§ : A β Assertion a) β let Ο = fst β Ξ§ in
(p : Term A)
β
(β
Ο β x β p β
Ο x β) β (β
Ο β x β p β
Ο x β) β (β
Ο β¨β¨β© Ο β x β p β
Ο x β)
disj Ο Ο sefβ¨Οβ© Ξ§ p lhs rhs .proof k = let Ο = fst β Ξ§ in
(do a β Ο βͺ _|β¨|_ β« Ο
x β p
b β Ο x
k x (a |β| b)) β‘β¨ βͺassocβ« Ο Ο _|β¨|_ _ β©
(do aβ β Ο
aβ β Ο
x β p
b β Ο x
k x (aβ |β¨| aβ |β| b))
β‘β¨ aβ β Ο ΝΎ/ aβ β Ο ΝΎ/ x β p ΝΎ/ b β Ο x ΝΎ/ cong (k x) (|β¨|-|β| _ _ _) β©
(do aβ β Ο
aβ β Ο
x β p
b β Ο x
k x ((aβ |β| b) |β§| (aβ |β| b))) β‘Λβ¨ aβ β Ο ΝΎ/ aβ β Ο ΝΎ/ x β p ΝΎ/ sef-dsef-cont (Ξ§ x) _ β©
(do aβ β Ο
aβ β Ο
x β p
bβ β Ο x
bβ β Ο x
k x ((aβ |β| bβ) |β§| (aβ |β| bβ))) β‘β¨ aβ β Ο ΝΎ/ rhs .proof (Ξ» x r β Ο x >>= Ξ» bβ β k x ((aβ |β| bβ) |β§| r)) β©
(do aβ β Ο
aβ β Ο
x β p
bβ β Ο x
bβ β Ο x
k x ((aβ |β| bβ) |β§| True)) β‘β¨ aβ β Ο ΝΎ/ sefβ¨Οβ© _ β©
(do a β Ο
x β p
bβ β Ο x
bβ β Ο x
k x ((a |β| bβ) |β§| True)) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ sef-dsef-cont (Ξ§ x) _ β©
(do a β Ο
x β p
b β Ο x
k x ((a |β| b) |β§| True)) β‘β¨ a β Ο ΝΎ/ x β p ΝΎ/ b β Ο x ΝΎ/ cong (k x) (β§-com _ _ ΝΎ β§-id _) β©
(do a β Ο
x β p
b β Ο x
k x (a |β| b)) β‘β¨ lhs .proof k β©
(do a β Ο
x β p
b β Ο x
k x True) β‘Λβ¨ a β Ο ΝΎ/ sefβ¨Οβ© _ β©
(do aβ β Ο
aβ β Ο
x β p
b β Ο x
k x True) β‘Λβ¨ βͺassocβ« Ο Ο _|β¨|_ _ β©
(do a β Ο βͺ _|β¨|_ β« Ο
x β p
b β Ο x
k x True) β
\end{code}