Top Banner
Intro to Agda by Larry Diehl (larrytheliquid)
39

Intro To Agda

Jun 16, 2015

Download

Technology

Larry Diehl

BAHUG meeting on February 22, 2010
http://groups.google.com/group/bahaskell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro To Agda

Intro to Agda

by Larry Diehl (larrytheliquid)

Page 2: Intro To Agda

Why?

Page 3: Intro To Agda

induction centric

data Nat : Set where  zero : Nat  suc : Nat -> Nat

one = suc zerotwo = suc (suc zero)

Page 4: Intro To Agda

unicode centric

data ℕ : Set where  zero : ℕ  suc : ℕ → ℕ

suc₂ : ℕ → ℕsuc₂ n = suc (suc n)

Page 5: Intro To Agda

unicode in emacs

\r →\bn ℕ\:: ∷\member ∈ \equiv ≡\and ∧\or ∨\neg ¬\ex ∃\all ∀

Page 6: Intro To Agda

built-ins

data ℕ : Set where  zero : ℕ  suc : ℕ → ℕ

{-# BUILTIN NATURAL ℕ #-}{-# BUILTIN ZERO zero #-}{-# BUILTIN SUC suc #-}

nine = suc₂ 7

Page 7: Intro To Agda

standard library

open import Data.Nat

Page 8: Intro To Agda

infix & pattern matching

infixl 6 _+_infixl 7 _*__+_ : ℕ → ℕ → ℕzero + n = nsuc m + n = suc (m + n)

_*_ : ℕ → ℕ → ℕzero * n = zerosuc m * n = n + m * n

Page 9: Intro To Agda

emacs goal mode

_+_ : ℕ → ℕ → ℕ n + m = ? _+_ : ℕ → ℕ → ℕn + m = {!!}

_+_ : ℕ → ℕ → ℕn + m = { }0-- ?0 : ℕ

Page 10: Intro To Agda

coverage checking

data Day : Set where  Sunday Monday Tuesday : Day  Wednesday Thursday : Day  Friday Saturday : Day isWeekend : Day → BoolisWeekend Sunday = trueisWeekend Saturday = trueisWeekend _ = false

Page 11: Intro To Agda

termination checking (fail)

isWeekend : Day → BoolisWeekend day = isWeekend day

isWeekend : Day → BoolisWeekend Sunday = trueisWeekend Saturday = isWeekend SundayisWeekend _ = false

Page 12: Intro To Agda

termination checking (win)

_+_ : ℕ → ℕ → ℕzero + n = nsuc m + n = suc (m + n)

_*_ : ℕ → ℕ → ℕzero * n = zerosuc m * n = n + m * n

Page 13: Intro To Agda

data type polymorphism

data List (A : Set) : Set where  []  : List A  _∷_ : (x : A) (xs : List A) → List A

-- open import Data.List

Page 14: Intro To Agda

type-restriction

append : List ℕ → List ℕ → List ℕappend [] ys = ysappend (x ∷ xs) ys = x ∷ (append xs ys)

Page 15: Intro To Agda

parametric polymorphism

append : (A : Set) → List A → List A → List Aappend A [] ys = ysappend A (x ∷ xs) ys = x ∷ (append A xs ys)

Page 16: Intro To Agda

implicit arguments

append : {A : Set} → List A → List A → List Aappend [] ys = ysappend (x ∷ xs) ys = x ∷ (append xs ys)

-- append = _++_

Page 17: Intro To Agda

indexed types

data Vec (A : Set) : ℕ → Set where  []  : Vec A zero  _∷_ : {n : ℕ} (x : A) (xs : Vec A n) →            Vec A (suc n)

bool-vec : Vec Bool 2bool-vec = _∷_ {1} true   (_∷_ {0} false [])  bool-vec : Vec Bool 2bool-vec = true ∷ false ∷ []

Page 18: Intro To Agda

indexed types

append : {n m : ℕ} {A : Set} → Vec A n →  Vec A m → Vec A (n + m)append [] ys = ysappend (x ∷ xs) ys = x ∷ (append xs ys)

Page 19: Intro To Agda

coverage checking

head : {A : Set} → Vec A 9 → Ahead (x ∷ xs) = x head : {n : ℕ} {A : Set} → Vec A (suc n) →  Ahead (x ∷ xs) = x head : {n : ℕ} {A : Set} →  Vec A (1 + 1 * n) → Ahead (x ∷ xs) = x 

Page 20: Intro To Agda

records

record Car : Set where  field    make : Make    model : Model    year : ℕ

electricCar : CarelectricCar = record {     make = Tesla  ; model = Roadster  ; year = 2010  }

Page 21: Intro To Agda

records

record Car : Set where  constructor _,_,_  field    make : Make    model : Model    year : ℕ

electricCar : CarelectricCar = Tesla , Roadster , 2010carYear :  ℕcarYear = Car.year electricCar

Page 22: Intro To Agda

truth & absurdity

record ⊤ : Set wheredata ⊥ : Set where

T : Bool → SetT true = ⊤T false = ⊥

Page 23: Intro To Agda

preconditions

even : ℕ → Booleven zero = trueeven (suc zero) = falseeven (suc (suc n)) = even n

evenSquare : (n : ℕ) → {_ : T (even  n)} → ℕevenSquare n = n * n

Page 24: Intro To Agda

preconditions

nonZero : ℕ → BoolnonZero zero = falsenonZero _ = true

postulate _div_ : ℕ → (n : ℕ) →   {_ : T (nonZero n)} → ℕ

Page 25: Intro To Agda

propositional equality

infix 4 _≡_data _≡_ {A : Set} (x : A) : A → Set where  refl : x ≡ x

testMultiplication : 56 ≡ 8 * 7testMultiplication = refl

Page 26: Intro To Agda

preconditions

lookup : {A : Set}(n : ℕ)(xs : List A)  {_ : T (n < length xs)} → Alookup n []{()}lookup zero (x ∷ xs) = xlookup (suc n) (x ∷ xs) {p} = lookup n xs {p}

testLookup : 2 ≡ lookup 1 (1 ∷ 2 ∷ [])testLookup = refl

Page 27: Intro To Agda

finite sets

data Fin : ℕ → Set where  zero : {n : ℕ} → Fin (suc n)  suc  : {n : ℕ} (i : Fin n) → Fin (suc n)

finTest : Fin 3finTest = zero

finTest₂ : Fin 3finTest₂ = suc (suc zero)

Page 28: Intro To Agda

precondition with indexes

lookup : {A : Set}{n : ℕ} → Fin n → Vec A n → Alookup zero (x ∷ xs) = xlookup (suc i) (x ∷ xs) = lookup i xs testLookup : 2 ≡ lookup (suc zero) (1 ∷ 2 ∷ [])testLookup = refl

testLookup₂ : 2 ≡ lookup (fromℕ 1) (1 ∷ 2 ∷ [])testLookup₂ = refl

Page 29: Intro To Agda

with

doubleOrNothing : ℕ → ℕdoubleOrNothing n with even ndoubleOrNothing n | true = n * 2doubleOrNothing n | false = 0 doubleOrNothing : ℕ → ℕdoubleOrNothing n with even n... | true = n * 2... | false = 0

Page 30: Intro To Agda

views

parity : (n : ℕ) → Parity nparity zero = even zeroparity (suc n) with parity nparity (suc .(k * 2)) | even k = odd kparity (suc .(1 + k * 2)) | odd k = even (suc k)

half : ℕ → ℕhalf n with parity nhalf .(k * 2) | even k = khalf .(1 + k * 2) | odd k = k

Page 31: Intro To Agda

inverse

data Image_∋_ {A B : Set}(f : A → B) :  B → Set where  im : {x : A} → Image f ∋ f x

inv : {A B : Set}(f : A → B)(y : B) →  Image f ∋ y → Ainv f .(f x) (im {x}) = x

Page 32: Intro To Agda

inverse

Bool-to-ℕ : Bool → ℕBool-to-ℕ false = 0Bool-to-ℕ true = 1

testImage : Image Bool-to-ℕ ∋ 0testImage = imtestImage₂ : Image Bool-to-ℕ ∋ 1testImage₂ = im

testInv : inv  Bool-to-ℕ 0 im ≡ falsetestInv = refltestInv₂ : inv  Bool-to-ℕ 1 im ≡ truetestInv₂ = refl

Page 33: Intro To Agda

intuitionistic logic

data _∧_ (A B : Set) : Set where  <_,_> : A → B → A ∧ B

fst : {A B : Set} → A ∧ B → Afst < a , _ > = asnd : {A B : Set} → A ∧ B → Bsnd < _ , b > = b

Page 34: Intro To Agda

intuitionistic logic

data _∨_ (A B : Set) : Set where  left : A → A ∨ B  right : B → A ∨ B

case : {A B C : Set} → A ∨ B → (A → C) →  (B → C) → Ccase (left a) x _ = x acase (right b) _ y = y b

Page 35: Intro To Agda

intuitionistic logic

record ⊤ : Set where  constructor tt

data ⊥ : Set where

exFalso : {A : Set} → ⊥ → AexFalso ()

¬ : Set → Set¬ A = A → ⊥

Page 36: Intro To Agda

intuitionistic logic

_=>_ : (A B : Set) → SetA => B = A → B

modusPonens : {A B : Set} → A => B → A → BmodusPonens f a = f a

_<=>_ : Set → Set → SetA <=> B = (A => B) ∧ (B => A)

Page 37: Intro To Agda

intuitionistic logic

∀′ : (A : Set)(B : A → Set) → Set∀′ A B = (a : A) → B a

data ∃ (A : Set)(B : A → Set) : Set where  exists : (a : A) → B a → ∃ A B

witness : {A : Set}{B : A → Set} → ∃ A B → Awitness (exists a _) = a

Page 38: Intro To Agda

intuitionistic logic

data Man : Set where  person : Man

isMan : Man → SetisMan m = ⊤

isMortal : Man → SetisMortal m = ⊤

socratesIsMortal : {Socrates : Man} →   (∀′ Man isMortal ∧ isMan Socrates) => isMortal SocratessocratesIsMortal {soc} < lemma , _ > = lemma soc

Page 39: Intro To Agda

The End

Agda Wikihttp://is.gd/8YgYM

Dependently Typed Programming in Agdahttp://is.gd/8Ygyz

Dependent Types at Work

http://is.gd/8YggR

lemmatheultimate.com