152 lines
3.3 KiB
Agda
152 lines
3.3 KiB
Agda
{-# OPTIONS --without-K --safe #-}
|
||
-- safe disables experimental agda features
|
||
-- without-K: related to HoTT. equality, identity, etc or something like that.
|
||
-- by default agda is inconsistant with HoTT. To get around that.
|
||
|
||
open import general-notation
|
||
|
||
data Bool : Type where
|
||
true : Bool
|
||
false : Bool
|
||
|
||
not : Bool → Bool
|
||
not true = false
|
||
not false = true
|
||
|
||
not' : Bool → Bool
|
||
-- not b = ?
|
||
-- not' b = { }0
|
||
--
|
||
-- C-c C-, (couldn't make it work)
|
||
--
|
||
-- C-c C-c
|
||
-- not' true = { }0
|
||
-- not' false = { }1
|
||
--
|
||
-- Fill in false and C-c C-SPC
|
||
not' true = false
|
||
not' false = true
|
||
|
||
|
||
idBool : Bool → Bool
|
||
idBool b = b
|
||
-- or
|
||
|
||
idBool' : Bool → Bool
|
||
idBool' = λ x → x
|
||
|
||
idBool'' : Bool → Bool
|
||
-- idBool'' = λ (x : ?) → x
|
||
-- and do C-c C-s
|
||
idBool'' = λ (x : Bool) → x
|
||
|
||
-- A Pi type
|
||
id : {X : Type} → X → X
|
||
id x = x
|
||
|
||
|
||
idBool3 : Bool → Bool
|
||
-- idBool'' = λ (x : ?) → x
|
||
-- and do C-c C-s
|
||
idBool3 = id {Bool} -- Explicit parameter
|
||
--
|
||
-- or
|
||
-- idBool3 = id -- Implicit parameter. Possible when there is only a unique value possible and agda can figure it out
|
||
--
|
||
-- or
|
||
-- idBool3 = id _ -- Asking agda to figure it out.
|
||
|
||
|
||
-- | Propositions as types
|
||
example : {P Q : Type} → P → (Q → P)
|
||
example {P} {Q} p = f
|
||
where
|
||
f : Q → P
|
||
f q = p
|
||
-- or
|
||
-- f _ = p -- here, the underscore means something like a 'don't care'
|
||
|
||
example' : {P Q : Type} → P → (Q → P)
|
||
-- example' {P} {Q} p = {λ q → ? }0
|
||
example' {P} {Q} p = λ (q : Q) → p
|
||
|
||
|
||
|
||
-- | Natural numbers
|
||
data ℕ : Type where
|
||
zero : ℕ
|
||
suc : ℕ → ℕ
|
||
|
||
three : ℕ
|
||
three = suc (suc (suc zero))
|
||
|
||
{-# BUILTIN NATURAL ℕ #-}
|
||
-- Can use normal numbers to associate it with
|
||
-- the ℕ that we just defined
|
||
|
||
three' : ℕ
|
||
three' = 3
|
||
|
||
D : Bool → Type
|
||
D true = ℕ
|
||
D false = Bool
|
||
|
||
-- | mix-fix operator
|
||
-- A use of '_'
|
||
if_then_else_ : {X : Type} → Bool → X → X → X
|
||
if true then x else y = x
|
||
if false then x else y = y
|
||
|
||
-- 'if' via a dependent type
|
||
if[_]_then_else_ : (X : Bool → Type)
|
||
→ (b : Bool)
|
||
→ X true
|
||
→ X false
|
||
→ X b
|
||
if[ X ] true then x else y = x
|
||
if[ X ] false then x else y = y
|
||
|
||
|
||
-- Doubt: Are pi-types same as dependent types?
|
||
unfamiliar : (b : Bool) → D b
|
||
unfamiliar b = if[ D ] b then 3 else false
|
||
-- the branches are of different type!
|
||
-- Thanks to D being dependently typed.
|
||
|
||
data List (A : Type) : Type where
|
||
[] : List A
|
||
_::_ : A → List A → List A
|
||
|
||
-- right associative with precedence 10
|
||
-- Can make it left associative by using just 'infix' instead of the 'infixr'
|
||
infixr 10 _::_
|
||
|
||
|
||
-- 'List' has type 'Type → Type'
|
||
ff : Type → Type
|
||
ff = List
|
||
|
||
sample-list₀ : List ℕ
|
||
sample-list₀ = 0 :: 1 :: 2 :: []
|
||
-- Same as: sample-list₀ = 0 :: (1 :: (2 :: []))
|
||
|
||
length : {X : Type} → List X → ℕ
|
||
length [] = zero
|
||
length (x :: xs) = suc (length xs)
|
||
|
||
|
||
-- Induction principle for ℕ
|
||
ℕ-elim : {A : ℕ → Type}
|
||
→ A 0 -- Base case: We know that A holds for 0
|
||
→ ((k : ℕ) → A k → A (suc k)) -- Induction step: If A k holds for any natural number k, then A (k+1) holds as well
|
||
→ (n : ℕ) → A n -- Conclusion: 'A n' hold for any natural number 'n'
|
||
ℕ-elim {A} a₀ f = h
|
||
where
|
||
h : (n : ℕ) → A n
|
||
h zero = a₀
|
||
h (suc n) = f n IH
|
||
where
|
||
IH : A n -- Induction hypothesis. A recursive call with a smaller n
|
||
IH = h n
|
||
|