135 lines
3.0 KiB
Agda
135 lines
3.0 KiB
Agda
Type = Set
|
||
|
||
data Bool : Type where
|
||
true false : Bool
|
||
|
||
not : Bool → Bool
|
||
not true = false
|
||
not false = true
|
||
|
||
idBool : Bool → Bool
|
||
idBool x = x
|
||
|
||
idBool' : Bool → Bool
|
||
idBool' = λ (x : Bool) → x
|
||
|
||
id' : (X : Type) → X → X
|
||
id' X x = x
|
||
|
||
id : {X : Type} → X → X
|
||
id x = x
|
||
|
||
idBool'' : Bool → Bool
|
||
idBool'' = id' _ -- asdf
|
||
|
||
example : {P Q : Type} → P → (Q → P)
|
||
example {P} {Q} p = f
|
||
where
|
||
f : Q → P
|
||
f _ = p
|
||
|
||
|
||
example2 : {P Q : Type} → P → (Q → P)
|
||
example2 {P} {Q} p = (λ q → p)
|
||
|
||
|
||
-- example : {P Q : Type} → P → (Q → P)
|
||
-- example {P} {Q} p = f
|
||
-- example : {P Q : Type} → P → (Q → P)
|
||
-- example {P} {Q} p = f
|
||
|
||
-- open import binary-products
|
||
--
|
||
-- ex : {P Q : Type} → P x Q → Q x
|
||
|
||
data ℕ : Type where
|
||
zero : ℕ
|
||
succ : ℕ → ℕ
|
||
|
||
three : ℕ
|
||
three = succ (succ (succ zero))
|
||
|
||
{-# BUILTIN NATURAL ℕ #-}
|
||
-- a pragma
|
||
three' : ℕ
|
||
three' = 3 -- same as three
|
||
|
||
-- Dependent types again
|
||
D : Bool → Type
|
||
D true = ℕ
|
||
D false = Bool
|
||
|
||
-- 'mix-fix' operator
|
||
if_then_else : {X : Type} → Bool → X → X → X
|
||
if true then x else y = x
|
||
if false then x else y = y
|
||
|
||
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
|
||
|
||
-- Π type
|
||
unfamiliar : (b : Bool) → D b
|
||
unfamiliar b = if[ D ] b then 3 else false
|
||
|
||
-- A type indexed by a type
|
||
data List (A : Type) : Type where
|
||
[] : List A
|
||
_::_ : A → List A → List A
|
||
|
||
ff : Type → Type
|
||
ff = List
|
||
-- okay, list is indeed Type → Type
|
||
|
||
sample-list₀ : List ℕ
|
||
sample-list₀ = 0 :: (1 :: (2 :: [])) -- brackets not really needed here, it's right associative
|
||
|
||
infix 10 _::_
|
||
|
||
-- brackets not really needed here, it's right associative
|
||
-- sample-list1 : List ℕ
|
||
-- sample-list1 = 0 :: 1 :: 2 :: []
|
||
|
||
length : {X : Type} → List X → ℕ
|
||
length [] = 0
|
||
length (x :: xs) = succ (length xs)
|
||
|
||
-- arbitrary recursive definitions not possible. Just like Coq.
|
||
-- Recursions got to be structurally smaller.
|
||
-- On a sub-term.
|
||
|
||
|
||
ℕ-elim : {A : ℕ → Type}
|
||
→ A 0 -- base case
|
||
→ ((k : ℕ) → A k → A (succ k)) -- induction step
|
||
→ (n : ℕ) → A n
|
||
ℕ-elim {A} a f = h
|
||
where
|
||
h : (n : ℕ) → A n
|
||
h zero = a
|
||
h (succ n) = f n IH
|
||
where
|
||
IH : A n
|
||
IH = h n -- induction hypothesis
|
||
|
||
ℕ-elim' : {A : ℕ → Type}
|
||
→ A 0 -- base case
|
||
→ ((k : ℕ) → A k → A (succ k)) -- induction step
|
||
→ (n : ℕ) → A n
|
||
ℕ-elim' {A} a f zero = a
|
||
ℕ-elim' {A} a f (succ n) = f n (ℕ-elim' a f n)
|
||
|
||
|
||
-- Elimination principal for lists
|
||
List-elim : {X : Type} (A : List X → Type)
|
||
→ A [] -- base case
|
||
→ ((x : X) (xs : List X) → A xs → A (x :: xs)) -- induction step
|
||
→ (xs : List X) → A xs
|
||
List-elim {X} A a f = h
|
||
where
|
||
h : (xs : List X)
|