78
Einführung in Agda https://tinyurl.com/bobkonf17-agda Albert-Ludwigs-Universität Freiburg Peter Thiemann University of Freiburg, Germany [email protected] 24 Feb 2017

Einführung in Agda - uni-freiburg.de

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Einführung in Agda - uni-freiburg.de

Einführung in Agdahttps://tinyurl.com/bobkonf17-agda

Albert-Ludwigs-Universität Freiburg

Peter ThiemannUniversity of Freiburg, Germany

[email protected]

24 Feb 2017

Page 2: Einführung in Agda - uni-freiburg.de

Programs that work — the dependent stairway

Choose an expressive type systemExpress your specification as a typeWrite the only possible program of this type

Thiemann Agda 2017-02-24 2 / 38

Page 3: Einführung in Agda - uni-freiburg.de

Programs that work — the dependent stairway

Choose an expressive type systemExpress your specification as a typeWrite the only possible program of this type

Thiemann Agda 2017-02-24 2 / 38

Page 4: Einführung in Agda - uni-freiburg.de

Programs that work — the dependent stairway

Choose an expressive type system

Express your specification as a typeWrite the only possible program of this type

Thiemann Agda 2017-02-24 2 / 38

Page 5: Einführung in Agda - uni-freiburg.de

Programs that work — the dependent stairway

Choose an expressive type systemExpress your specification as a type

Write the only possible program of this type

Thiemann Agda 2017-02-24 2 / 38

Page 6: Einführung in Agda - uni-freiburg.de

Programs that work — the dependent stairway

Choose an expressive type systemExpress your specification as a typeWrite the only possible program of this type

Thiemann Agda 2017-02-24 2 / 38

Page 7: Einführung in Agda - uni-freiburg.de

Thiemann Agda 2017-02-24 3 / 38

Page 8: Einführung in Agda - uni-freiburg.de

Why does it work?

The Curry-Howard Correspondence

Propositions as typesProofs as programs

Central insight

Write program of this type=

Find a proof for this proposition

Thiemann Agda 2017-02-24 4 / 38

Page 9: Einführung in Agda - uni-freiburg.de

Why does it work?

The Curry-Howard Correspondence

Propositions as types

Proofs as programs

Central insight

Write program of this type=

Find a proof for this proposition

Thiemann Agda 2017-02-24 4 / 38

Page 10: Einführung in Agda - uni-freiburg.de

Why does it work?

The Curry-Howard Correspondence

Propositions as typesProofs as programs

Central insight

Write program of this type=

Find a proof for this proposition

Thiemann Agda 2017-02-24 4 / 38

Page 11: Einführung in Agda - uni-freiburg.de

Why does it work?

The Curry-Howard Correspondence

Propositions as typesProofs as programs

Central insight

Write program of this type=

Find a proof for this proposition

Thiemann Agda 2017-02-24 4 / 38

Page 12: Einführung in Agda - uni-freiburg.de

In Agda

Remember Curry-Howard

A type corresponds to a propositionElements of the type are proofs for that proposition

The role of functionsA function f : A→ B . . .

transforms an element of A to an element of Btransforms a proof of A to a proof of Bshows: if we have a proof of A, then we have a proof of Bis a proof of the logical implication A→ B

Thiemann Agda 2017-02-24 5 / 38

Page 13: Einführung in Agda - uni-freiburg.de

In Agda

Remember Curry-Howard

A type corresponds to a propositionElements of the type are proofs for that proposition

The role of functionsA function f : A→ B . . .

transforms an element of A to an element of Btransforms a proof of A to a proof of Bshows: if we have a proof of A, then we have a proof of Bis a proof of the logical implication A→ B

Thiemann Agda 2017-02-24 5 / 38

Page 14: Einführung in Agda - uni-freiburg.de

In Agda

Remember Curry-Howard

A type corresponds to a propositionElements of the type are proofs for that proposition

The role of functionsA function f : A→ B . . .

transforms an element of A to an element of B

transforms a proof of A to a proof of Bshows: if we have a proof of A, then we have a proof of Bis a proof of the logical implication A→ B

Thiemann Agda 2017-02-24 5 / 38

Page 15: Einführung in Agda - uni-freiburg.de

In Agda

Remember Curry-Howard

A type corresponds to a propositionElements of the type are proofs for that proposition

The role of functionsA function f : A→ B . . .

transforms an element of A to an element of Btransforms a proof of A to a proof of B

shows: if we have a proof of A, then we have a proof of Bis a proof of the logical implication A→ B

Thiemann Agda 2017-02-24 5 / 38

Page 16: Einführung in Agda - uni-freiburg.de

In Agda

Remember Curry-Howard

A type corresponds to a propositionElements of the type are proofs for that proposition

The role of functionsA function f : A→ B . . .

transforms an element of A to an element of Btransforms a proof of A to a proof of Bshows: if we have a proof of A, then we have a proof of B

is a proof of the logical implication A→ B

Thiemann Agda 2017-02-24 5 / 38

Page 17: Einführung in Agda - uni-freiburg.de

In Agda

Remember Curry-Howard

A type corresponds to a propositionElements of the type are proofs for that proposition

The role of functionsA function f : A→ B . . .

transforms an element of A to an element of Btransforms a proof of A to a proof of Bshows: if we have a proof of A, then we have a proof of Bis a proof of the logical implication A→ B

Thiemann Agda 2017-02-24 5 / 38

Page 18: Einführung in Agda - uni-freiburg.de

Plan

1 Prelude

2 Logic

3 Numbers

4 Vectors

5 Going further

Thiemann Agda 2017-02-24 6 / 38

Page 19: Einführung in Agda - uni-freiburg.de

Logic in AgdaDefining types: the true proposition

– Truthdata > : Set where

tt : >

Explanation (cf. data in Haskell)

– Truth a commentdata defines a new datatype> is the name of the typeSet is its kindtt is the single element of >

Thiemann Agda 2017-02-24 7 / 38

Page 20: Einführung in Agda - uni-freiburg.de

Logic in AgdaDefining types: the true proposition

– Truthdata > : Set where

tt : >

Explanation (cf. data in Haskell)

– Truth a commentdata defines a new datatype> is the name of the typeSet is its kindtt is the single element of >

Thiemann Agda 2017-02-24 7 / 38

Page 21: Einführung in Agda - uni-freiburg.de

Logic in AgdaConjunction is really just a pair

– Conjunctiondata _∧_ (P Q : Set) : Set where〈_,_〉 : P → Q → (P ∧ Q)

Explanation

_∧_ the name of an infix type constructorthe underlines indicate the positions of the arguments(P Q : Set) parameters of the type〈_,_〉 data constructor with two parameters

Thiemann Agda 2017-02-24 8 / 38

Page 22: Einführung in Agda - uni-freiburg.de

Logic in AgdaConjunction is really just a pair

– Conjunctiondata _∧_ (P Q : Set) : Set where〈_,_〉 : P → Q → (P ∧ Q)

Explanation

_∧_ the name of an infix type constructorthe underlines indicate the positions of the arguments(P Q : Set) parameters of the type〈_,_〉 data constructor with two parameters

Thiemann Agda 2017-02-24 8 / 38

Page 23: Einführung in Agda - uni-freiburg.de

Logic in AgdaDisjunction is really just Either

– Disjunctiondata _∨_ (P Q : Set) : Set where

inl : P → (P ∨ Q)inr : Q → (P ∨ Q)

Explanation

two data constructorseverything covered

Thiemann Agda 2017-02-24 9 / 38

Page 24: Einführung in Agda - uni-freiburg.de

Logic in AgdaDisjunction is really just Either

– Disjunctiondata _∨_ (P Q : Set) : Set where

inl : P → (P ∨ Q)inr : Q → (P ∨ Q)

Explanation

two data constructorseverything covered

Thiemann Agda 2017-02-24 9 / 38

Page 25: Einführung in Agda - uni-freiburg.de

A first program in Agda

Specification

– Conjunction is commutativecommConj1 : (P : Set) → (Q : Set) → (P ∧ Q) → (Q ∧ P)

Thiemann Agda 2017-02-24 10 / 38

Page 26: Einführung in Agda - uni-freiburg.de

A first program in Agda

Specification

– Conjunction is commutativecommConj1 : (P : Set) → (Q : Set) → (P ∧ Q) → (Q ∧ P)

Explanation

(P : Set) an argument of type Set with name P to be usedlater in the type(P : Set) and (Q : Set) declare that P and Q are types(propositions)(P ∧ Q) → (Q ∧ P) is the proposition we want to prove =the type of the program we want to write

Thiemann Agda 2017-02-24 10 / 38

Page 27: Einführung in Agda - uni-freiburg.de

A first program in Agda

Specification

– Conjunction is commutativecommConj1 : (P : Set) → (Q : Set) → (P ∧ Q) → (Q ∧ P)

Let’s write it interactively

Thiemann Agda 2017-02-24 10 / 38

Page 28: Einführung in Agda - uni-freiburg.de

Should start with a screen like this

Thiemann Agda 2017-02-24 11 / 38

Page 29: Einführung in Agda - uni-freiburg.de

Variations on the specification

Fully explicit

– Conjunction is commutativecommConj1 : (P : Set) → (Q : Set) → (P ∧ Q) → (Q ∧ P)commConj1 P Q 〈 p , q 〉 = 〈 q , p 〉

arguments P and Q are not used and Agda can infer them

With inferred parameters

– Conjunction is commutativecommConj2 : (P Q : Set) → (P ∧ Q) → (Q ∧ P)commConj2 _ _ 〈 p , q 〉 = 〈 q , p 〉

just put _ for inferred arguments

Thiemann Agda 2017-02-24 12 / 38

Page 30: Einführung in Agda - uni-freiburg.de

Variations on the specification

Fully explicit

– Conjunction is commutativecommConj1 : (P : Set) → (Q : Set) → (P ∧ Q) → (Q ∧ P)commConj1 P Q 〈 p , q 〉 = 〈 q , p 〉

arguments P and Q are not used and Agda can infer them

With inferred parameters

– Conjunction is commutativecommConj2 : (P Q : Set) → (P ∧ Q) → (Q ∧ P)commConj2 _ _ 〈 p , q 〉 = 〈 q , p 〉

just put _ for inferred arguments

Thiemann Agda 2017-02-24 12 / 38

Page 31: Einführung in Agda - uni-freiburg.de

Variations on the specification

Implicit parameters

– Conjunction is commutativecommConj : ∀ {P Q} → (P ∧ Q) → (Q ∧ P)commConj 〈 p , q 〉 = 〈 q , p 〉

Explanation

∀ {P Q} is short for {P Q : Set}{P Q : Set} indicates that P and Q are implicit parameters:they need not be provided and Agda tries to infer themSuccessful here, but we get an obscure error message if Agdacannot infer implicit parameters

Thiemann Agda 2017-02-24 13 / 38

Page 32: Einführung in Agda - uni-freiburg.de

Variations on the specification

Implicit parameters

– Conjunction is commutativecommConj : ∀ {P Q} → (P ∧ Q) → (Q ∧ P)commConj 〈 p , q 〉 = 〈 q , p 〉

Explanation

∀ {P Q} is short for {P Q : Set}{P Q : Set} indicates that P and Q are implicit parameters:they need not be provided and Agda tries to infer themSuccessful here, but we get an obscure error message if Agdacannot infer implicit parameters

Thiemann Agda 2017-02-24 13 / 38

Page 33: Einführung in Agda - uni-freiburg.de

A second program in Agda

Specification

– Disjunction is commutativecommDisj : ∀ {P Q} → (P ∨ Q) → (Q ∨ P)

Thiemann Agda 2017-02-24 14 / 38

Page 34: Einführung in Agda - uni-freiburg.de

A second program in Agda

Specification

– Disjunction is commutativecommDisj : ∀ {P Q} → (P ∨ Q) → (Q ∨ P)

Let’s write it interactively

Thiemann Agda 2017-02-24 14 / 38

Page 35: Einführung in Agda - uni-freiburg.de

Logic in AgdaNegation at last

– Falsitydata ⊥ : Set where

– Negation¬ : Set → Set¬ P = P → ⊥

Explanation

The type ⊥ has no elements, hence no constructorsNegation is defined by reductio ad absurdum: P → ⊥i.e., having a proof for P would lead to a contradiction

Thiemann Agda 2017-02-24 15 / 38

Page 36: Einführung in Agda - uni-freiburg.de

Logic in AgdaNegation at last

– Falsitydata ⊥ : Set where

– Negation¬ : Set → Set¬ P = P → ⊥

Explanation

The type ⊥ has no elements, hence no constructorsNegation is defined by reductio ad absurdum: P → ⊥i.e., having a proof for P would lead to a contradiction

Thiemann Agda 2017-02-24 15 / 38

Page 37: Einführung in Agda - uni-freiburg.de

De Morgan’s laws

Specification

– DeMorgan’s lawsdemND1 : ∀ {P Q} → ¬ (P ∨ Q) → (¬ P ∧ ¬ Q)demND2 : ∀ {P Q} → (¬ P ∧ ¬ Q) → ¬ (P ∨ Q)

Thiemann Agda 2017-02-24 16 / 38

Page 38: Einführung in Agda - uni-freiburg.de

De Morgan’s laws

Specification

– DeMorgan’s lawsdemND1 : ∀ {P Q} → ¬ (P ∨ Q) → (¬ P ∧ ¬ Q)demND2 : ∀ {P Q} → (¬ P ∧ ¬ Q) → ¬ (P ∨ Q)

Interaction time

Thiemann Agda 2017-02-24 16 / 38

Page 39: Einführung in Agda - uni-freiburg.de

Plan

1 Prelude

2 Logic

3 Numbers

4 Vectors

5 Going further

Thiemann Agda 2017-02-24 17 / 38

Page 40: Einführung in Agda - uni-freiburg.de

Numbers in Agda

Surprise

Numbers are not predefined in AgdaWe have to define them ourselves(But there is a library)

Let’s try

Thiemann Agda 2017-02-24 18 / 38

Page 41: Einführung in Agda - uni-freiburg.de

Numbers in Agda

Surprise

Numbers are not predefined in AgdaWe have to define them ourselves(But there is a library)

Let’s try

Thiemann Agda 2017-02-24 18 / 38

Page 42: Einführung in Agda - uni-freiburg.de

Peano’s axioms1

Giuseppe Peano says . . .

1 zero is a natural number2 If n is a natural number, then

suc n is also a natural number3 All natural numbers can be (and

must be) constructed from 1. and 2.

An inductive definition

1Image Attribution: By Unknown - School of Mathematics and Statistics, University of St

Andrews, Scotland [1], Public Domain, https://commons.wikimedia.org/w/index.php?curid=2633677

Thiemann Agda 2017-02-24 19 / 38

Page 43: Einführung in Agda - uni-freiburg.de

Peano’s axioms1

Giuseppe Peano says . . .

1 zero is a natural number2 If n is a natural number, then

suc n is also a natural number3 All natural numbers can be (and

must be) constructed from 1. and 2.

An inductive definition

1Image Attribution: By Unknown - School of Mathematics and Statistics, University of St

Andrews, Scotland [1], Public Domain, https://commons.wikimedia.org/w/index.php?curid=2633677

Thiemann Agda 2017-02-24 19 / 38

Page 44: Einführung in Agda - uni-freiburg.de

Inductive definition in Agda

Natural numbersdata N : Set where

zero : Nsuc : N → N

Explanation

Defines zero and suc justlike demanded by PeanoDefine functions on N byinduction and patternmatching on theconstructors

Thiemann Agda 2017-02-24 20 / 38

Page 45: Einführung in Agda - uni-freiburg.de

Inductive definition in Agda

Natural numbersdata N : Set where

zero : Nsuc : N → N

Explanation

Defines zero and suc justlike demanded by PeanoDefine functions on N byinduction and patternmatching on theconstructors

Thiemann Agda 2017-02-24 20 / 38

Page 46: Einführung in Agda - uni-freiburg.de

Functional programming

Additionadd : N → N → Nadd zero n = nadd (suc m) n = suc (add m n)

Subtractionsub : N → N → Nsub m zero = msub zero (suc n) = zerosub (suc m) (suc n) = sub m n

Thiemann Agda 2017-02-24 21 / 38

Page 47: Einführung in Agda - uni-freiburg.de

Functional programming

Additionadd : N → N → Nadd zero n = nadd (suc m) n = suc (add m n)

Subtractionsub : N → N → Nsub m zero = msub zero (suc n) = zerosub (suc m) (suc n) = sub m n

Thiemann Agda 2017-02-24 21 / 38

Page 48: Einführung in Agda - uni-freiburg.de

Why specify properties?

Deficiency of Testing

Testing shows thepresence, not theabsence of bugs.

E.W. Dijkstra

Thiemann Agda 2017-02-24 22 / 38

Page 49: Einführung in Agda - uni-freiburg.de

What can we specify?

Properties of addition all require equality on numbers

Next surprise

Equality is not predefined in AgdaWe have to define it ourselves(But there is a library)

Let’s try

Thiemann Agda 2017-02-24 23 / 38

Page 50: Einführung in Agda - uni-freiburg.de

What can we specify?

Properties of addition all require equality on numbers

Next surprise

Equality is not predefined in AgdaWe have to define it ourselves(But there is a library)

Let’s try

Thiemann Agda 2017-02-24 23 / 38

Page 51: Einführung in Agda - uni-freiburg.de

What can we specify?

Properties of addition all require equality on numbers

Next surprise

Equality is not predefined in AgdaWe have to define it ourselves(But there is a library)

Let’s try

Thiemann Agda 2017-02-24 23 / 38

Page 52: Einführung in Agda - uni-freiburg.de

Inductive definition of equality

Equality on natural numbers

data _≡_ : N → N → Set wherez≡z : zero ≡ zeros≡s : {m n : N} → m ≡ n → suc m ≡ suc n

Explanation

Unusual: datatype parameterized by two numbersThe constructor s≡s takes a proof that m ≡ n and thusbecomes a proof that suc m ≡ suc n

Thiemann Agda 2017-02-24 24 / 38

Page 53: Einführung in Agda - uni-freiburg.de

Properties of equality

Equality is . . .

– reflexiverefl-≡ : (n : N) → n ≡ n– transitivetrans-≡ : {m n o : N} → m ≡ n → n ≡ o → m ≡ o– symmetricsymm-≡ : {m n : N} → m ≡ n → n ≡ m

Thiemann Agda 2017-02-24 25 / 38

Page 54: Einführung in Agda - uni-freiburg.de

Properties of equality

Reflexivity

Need to define a function that given some n returns a proofof (element of) n ≡ nStraightforward programming exerciseUse pattern matching / inductionAgda can do it automatically

Thiemann Agda 2017-02-24 26 / 38

Page 55: Einführung in Agda - uni-freiburg.de

Properties of equality

Reflexivity

Need to define a function that given some n returns a proofof (element of) n ≡ nStraightforward programming exerciseUse pattern matching / inductionAgda can do it automatically

Interaction time

Thiemann Agda 2017-02-24 26 / 38

Page 56: Einführung in Agda - uni-freiburg.de

Properties of equality

Symmetry

m ≡ n → n ≡ mSymmetry can be proved by induction on m and nIntroduces a new concept: absurd patternsLess cumbersome alternative:pattern matching on equality proof

Thiemann Agda 2017-02-24 27 / 38

Page 57: Einführung in Agda - uni-freiburg.de

Properties of equality

Symmetry

m ≡ n → n ≡ mSymmetry can be proved by induction on m and nIntroduces a new concept: absurd patternsLess cumbersome alternative:pattern matching on equality proof

Interaction time

Thiemann Agda 2017-02-24 27 / 38

Page 58: Einführung in Agda - uni-freiburg.de

Properties of addition

Zero is neutral element of additionneutralAdd0l : (m : N) → add zero m ≡ mneutralAdd0r : (m : N) → add m zero ≡ m

Addition is associativeassocAdd : (m n o : N)→ add m (add n o) ≡ add (add m n) o

Addition is commutativecommAdd : (m n : N) → add m n ≡ add n m

Thiemann Agda 2017-02-24 28 / 38

Page 59: Einführung in Agda - uni-freiburg.de

Properties of addition

Zero is neutral element of additionneutralAdd0l : (m : N) → add zero m ≡ mneutralAdd0r : (m : N) → add m zero ≡ m

Addition is associativeassocAdd : (m n o : N)→ add m (add n o) ≡ add (add m n) o

Addition is commutativecommAdd : (m n : N) → add m n ≡ add n m

Thiemann Agda 2017-02-24 28 / 38

Page 60: Einführung in Agda - uni-freiburg.de

Properties of addition

Zero is neutral element of additionneutralAdd0l : (m : N) → add zero m ≡ mneutralAdd0r : (m : N) → add m zero ≡ m

Addition is associativeassocAdd : (m n o : N)→ add m (add n o) ≡ add (add m n) o

Addition is commutativecommAdd : (m n : N) → add m n ≡ add n m

Thiemann Agda 2017-02-24 28 / 38

Page 61: Einführung in Agda - uni-freiburg.de

Properties of addition

Proving . . .

Neutral element and associativity are straightforwardCommutativity is slightly more involvedRequires an auxiliary function

Thiemann Agda 2017-02-24 29 / 38

Page 62: Einführung in Agda - uni-freiburg.de

Properties of addition

Proving . . .

Neutral element and associativity are straightforwardCommutativity is slightly more involvedRequires an auxiliary function

Interaction time

Thiemann Agda 2017-02-24 29 / 38

Page 63: Einführung in Agda - uni-freiburg.de

Plan

1 Prelude

2 Logic

3 Numbers

4 Vectors

5 Going further

Thiemann Agda 2017-02-24 30 / 38

Page 64: Einführung in Agda - uni-freiburg.de

Vectors in Agda

Vectors with static bounds checks

Flagship application of dependent typingAll vector operations proved safe at compile timeKey: define vector type indexed by its length

Thiemann Agda 2017-02-24 31 / 38

Page 65: Einführung in Agda - uni-freiburg.de

The vector type

data Vec (A : Set) : (n : N) → Set whereNil : Vec A zeroCons : {n : N} → (a : A) → Vec A n → Vec A (suc n)

concat : ∀ {A m n}→ Vec A m → Vec A n → Vec A (add m n)

concat Nil ys = ysconcat (Cons a xs) ys = Cons a (concat xs ys)

Thiemann Agda 2017-02-24 32 / 38

Page 66: Einführung in Agda - uni-freiburg.de

The vector type

data Vec (A : Set) : (n : N) → Set whereNil : Vec A zeroCons : {n : N} → (a : A) → Vec A n → Vec A (suc n)

concat : ∀ {A m n}→ Vec A m → Vec A n → Vec A (add m n)

concat Nil ys = ysconcat (Cons a xs) ys = Cons a (concat xs ys)

Thiemann Agda 2017-02-24 32 / 38

Page 67: Einführung in Agda - uni-freiburg.de

Safe vector access“avoid out of bound indexes”

Trick #1

Type of get depends on length of vector n and index m. . . and a proof that m < n

get : ∀ {A n} → Vec A n → (m : N) → suc m ≤ n → A

Trick #2

. . . type restricts the index to m < n

get1 : ∀ {A n} → Vec A n → Fin n → A

Thiemann Agda 2017-02-24 33 / 38

Page 68: Einführung in Agda - uni-freiburg.de

Safe vector access“avoid out of bound indexes”

Trick #1

Type of get depends on length of vector n and index m. . . and a proof that m < n

get : ∀ {A n} → Vec A n → (m : N) → suc m ≤ n → A

Trick #2

. . . type restricts the index to m < n

get1 : ∀ {A n} → Vec A n → Fin n → A

Thiemann Agda 2017-02-24 33 / 38

Page 69: Einführung in Agda - uni-freiburg.de

Safe vector access“avoid out of bound indexes”

Trick #1

Type of get depends on length of vector n and index m. . . and a proof that m < n

get : ∀ {A n} → Vec A n → (m : N) → suc m ≤ n → A

Trick #2

. . . type restricts the index to m < n

get1 : ∀ {A n} → Vec A n → Fin n → A

Thiemann Agda 2017-02-24 33 / 38

Page 70: Einführung in Agda - uni-freiburg.de

Safe vector access“avoid out of bound indexes”

Trick #1

Type of get depends on length of vector n and index m. . . and a proof that m < n

get : ∀ {A n} → Vec A n → (m : N) → suc m ≤ n → A

Trick #2

. . . type restricts the index to m < n

get1 : ∀ {A n} → Vec A n → Fin n → A

Thiemann Agda 2017-02-24 33 / 38

Page 71: Einführung in Agda - uni-freiburg.de

Finite set type

data Fin : N → Set wherezero : {n : N} → Fin (suc n)suc : {n : N} → Fin n → Fin (suc n)

Explanation

Overloading of constructors okFin zero = ∅ (empty set)Fin (suc zero) = {0}Fin (suc (suc zero)) = {0, 1}etc

Interaction time

Thiemann Agda 2017-02-24 34 / 38

Page 72: Einführung in Agda - uni-freiburg.de

Finite set type

data Fin : N → Set wherezero : {n : N} → Fin (suc n)suc : {n : N} → Fin n → Fin (suc n)

Explanation

Overloading of constructors okFin zero = ∅ (empty set)Fin (suc zero) = {0}Fin (suc (suc zero)) = {0, 1}etc

Interaction time

Thiemann Agda 2017-02-24 34 / 38

Page 73: Einführung in Agda - uni-freiburg.de

Finite set type

data Fin : N → Set wherezero : {n : N} → Fin (suc n)suc : {n : N} → Fin n → Fin (suc n)

Explanation

Overloading of constructors okFin zero = ∅ (empty set)Fin (suc zero) = {0}Fin (suc (suc zero)) = {0, 1}etc

Interaction timeThiemann Agda 2017-02-24 34 / 38

Page 74: Einführung in Agda - uni-freiburg.de

Splitting a vector

We know this type already . . .

– Pairdata _×_ (A B : Set) : Set where

_,_ : (a : A) → (b : B) → (A × B)

– split a vector in two partssplit : ∀ {A n} → Vec A n → (m : N) → m ≤ n→ Vec A m × Vec A (sub n m)

Solution introduces a new feature: with matchingThis operation can also be defined with Fin . . .

Interaction time

Thiemann Agda 2017-02-24 35 / 38

Page 75: Einführung in Agda - uni-freiburg.de

Splitting a vector

We know this type already . . .

– Pairdata _×_ (A B : Set) : Set where

_,_ : (a : A) → (b : B) → (A × B)

– split a vector in two partssplit : ∀ {A n} → Vec A n → (m : N) → m ≤ n→ Vec A m × Vec A (sub n m)

Solution introduces a new feature: with matchingThis operation can also be defined with Fin . . .

Interaction timeThiemann Agda 2017-02-24 35 / 38

Page 76: Einführung in Agda - uni-freiburg.de

Plan

1 Prelude

2 Logic

3 Numbers

4 Vectors

5 Going further

Thiemann Agda 2017-02-24 36 / 38

Page 77: Einführung in Agda - uni-freiburg.de

Going further

http://learnyouanagda.liamoc.net/ nicely pacedtutorial, some more backgroundhttp://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.HomePage definitive resourcehttp://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.Othertutorials with a load of links to tutorials

Thiemann Agda 2017-02-24 37 / 38

Page 78: Einführung in Agda - uni-freiburg.de

Questions?

Thiemann Agda 2017-02-24 38 / 38