Skip to content

Instantly share code, notes, and snippets.

@umazalakain
Last active January 11, 2021 10:53
Show Gist options
  • Save umazalakain/3ddc7c43d772b83d3b9b282f78ab088e to your computer and use it in GitHub Desktop.
Save umazalakain/3ddc7c43d772b83d3b9b282f78ab088e to your computer and use it in GitHub Desktop.
{-
FATA @ UoG Uma Zalakain
--------------------------------------------
Theorem Proving with Dependent Types in Agda
-}
{-
Agda
is a "pure dependently typed total functional programming language"
is a "proof assistant" / "interactive theorem prover" (like Coq and Idris)
is used for "machine verification" / "mechanisation"
has syntax Γ  la Haskell (with `:` for `is of type` instead of `::` >_>)
is interactive! program writing <-> type checking
-}
{-
Pure functional programming language
-}
data List (A : Set) : Set where -- `A : Set` reads `A is a type`
[] : List A
_∷_ : A β†’ List A β†’ List A
variable
A B : Set
P : A β†’ Set
map : (A β†’ B) β†’ List A β†’ List B
map f xs = {!!}
{-
Proof assistant
based on Martin-LΓΆf Type Theory
dependent types: term β†’ type
-}
data 𝟘 : Set where -- aka `βŠ₯` (bottom) aka `empty set` aka `initial object`
absurd : 𝟘 β†’ A
absurd b = {!!}
data πŸ™ : Set where -- aka `⊀` (top) aka `singleton` aka `void` aka `terminal object`
tt : πŸ™
data 𝟚 : Set where -- aka `boolean` aka `bit`
true : 𝟚
false : 𝟚
data β„• : Set where
zero : β„•
suc : β„• β†’ β„•
Even : β„• β†’ Set
Even n = {!!}
half : (n : β„•) β†’ Even n β†’ β„•
half n En = {!!}
{-
Totality:
every possible input must deliver a result
ensures soundness
makes type checking decidable
do we lose turing completeness? no!
-}
{-# NON_COVERING #-}
headUnsafe : List A β†’ A
headUnsafe (x ∷ xs) = x
unsound : 𝟘
unsound = headUnsafe []
{-# TERMINATING #-}
unsoundβ€² : 𝟘
unsoundβ€² = unsoundβ€²
{-
Constructive logic
through the Curry-Howard correspondence
existence is constructed / witnessed
no law of excluded middle (βˆ€ toBe β†’ toBe ⊎ Β¬ toBe)
no double negation elimination (Β¬ Β¬ A β†’ A)
showing that non-existence is impossible provides no data
-}
_β‡’_ : Set β†’ Set β†’ Set -- aka `function abstraction` aka `arrow`
A β‡’ B = A β†’ B
Β¬_ : Set β†’ Set
Β¬ A = A β‡’ 𝟘
data Ξ  (A : Set) (P : A β†’ Set) : Set where
βˆ€Ξ  : (βˆ€ a β†’ P a) β†’ Ξ  A P
data Ξ£ (A : Set) (P : A β†’ Set) : Set where
_,_ : βˆ€ a β†’ P a β†’ Ξ£ A P
_Γ—_ : Set β†’ Set β†’ Set
A Γ— B = Ξ£ A (Ξ» _ β†’ B)
infix 5 _⊎_
data _⊎_ (A : Set) (B : Set) : Set where
inl : A β†’ A ⊎ B
inr : B β†’ A ⊎ B
doubleneg-intro : A β†’ Β¬ Β¬ A
doubleneg-intro a = {!!}
not-all-β„•s-are-even : Β¬ Ξ  β„• Even
not-all-β„•s-are-even p = {!!}
there-exists-an-even-β„• : Ξ£ β„• Even
there-exists-an-even-β„• = {!!}
all-β„•-are-either-even-or-not-even : βˆ€ n β†’ Even n ⊎ Β¬ Even n
all-β„•-are-either-even-or-not-even n = {!!}
{-
Propositional (intensional) equality
-}
infix 10 _≑_
data _≑_ : A β†’ A β†’ Set where
refl : {x : A} β†’ x ≑ x
_β‰’_ : A β†’ A β†’ Set
x β‰’ y = Β¬ (x ≑ y)
sym : {x y : A} β†’ x ≑ y β†’ y ≑ x
sym eq = {!!}
trans : {x y z : A} β†’ x ≑ y β†’ y ≑ z β†’ x ≑ z
trans eq qe = {!!}
cong : {x y : A} (f : A β†’ B) β†’ x ≑ y β†’ f x ≑ f y
cong f eq = {!!}
subst : {x y : A} β†’ x ≑ y β†’ P x β†’ P y
subst eq px = {!!}
-- Constructors are disjoint
zero-suc : βˆ€ {n} β†’ zero β‰’ suc n
zero-suc eq = {!!}
-- Constructors are injective
suc-injective : βˆ€ {n m} β†’ suc n ≑ suc m β†’ n ≑ m
suc-injective eq = {!!}
{-
Definitional equality
-}
_+_ : β„• β†’ β„• β†’ β„•
zero + m = m
suc n + m = suc (n + m)
{-# BUILTIN NATURAL β„• #-}
1+1≑2 : 1 + 1 ≑ 2
1+1≑2 = {!!}
{-
Definitional vs propositional
-}
+-idΛ‘ : βˆ€ {n} β†’ 0 + n ≑ n
+-idΛ‘ = {!!}
+-idΚ³ : βˆ€ {n} β†’ n ≑ n + 0
+-idΚ³ = {!!}
{-
Tools: equational reasoning
also available in other flavours (preorders etc)
-}
infix 3 _∎
infixr 2 step-≑
infix 1 begin_
begin_ : βˆ€{x y : A} β†’ x ≑ y β†’ x ≑ y
begin_ x≑y = x≑y
step-≑ : βˆ€ (x {y z} : A) β†’ y ≑ z β†’ x ≑ y β†’ x ≑ z
step-≑ _ y≑z x≑y = trans x≑y y≑z
syntax step-≑ x y≑z x≑y = x β‰‘βŸ¨ x≑y ⟩ y≑z
_∎ : βˆ€ (x : A) β†’ x ≑ x
_∎ _ = refl
+-suc : βˆ€ n m β†’ suc (n + m) ≑ n + suc m
+-suc zero m = refl
+-suc (suc n) m = cong suc (+-suc n m)
+-comm : βˆ€ n m β†’ n + m ≑ m + n
+-comm zero m = {!!}
+-comm (suc n) m = begin
suc (n + m) β‰‘βŸ¨ {!!} ⟩
(m + suc n) ∎
{-
Applications to programming:
- restrict domain of arguments (eg division)
- explicitly maintain invariants
- assert functional properties
-}
data Vec (A : Set) : β„• β†’ Set where
[] : Vec A zero
_∷_ : βˆ€ {n} β†’ A β†’ Vec A n β†’ Vec A (suc n)
replicate : A β†’ (n : β„•) β†’ Vec A n
replicate x n = {!!}
head : βˆ€ {n} β†’ Vec A (suc n) β†’ A
head xs = {!!}
_++_ : βˆ€ {n m} β†’ Vec A n β†’ Vec A m β†’ Vec A (n + m)
xs ++ ys = {!!}
data Fin : β„• β†’ Set where
zero : βˆ€ {n} β†’ Fin (suc n)
suc : βˆ€ {n} β†’ Fin n β†’ Fin (suc n)
_!_ : βˆ€ {n} β†’ Vec A n β†’ Fin n β†’ A
xs ! i = {!!}
{-
Applications to theorem proving
Great educational tool!
Programming Languages theory
- formalisation of programming languages
- verification of metatheoretical properties
Homotopy Type Theory
- univalent foundations
- equality between types (cubical: computes!)
- topological results
Topology, category theory, number theory, group theory, analysis...
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment