Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness...
Transcript of Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness...
![Page 1: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/1.jpg)
Dependently Typed Functional Programming withIdris
Lecture 1: A Tour of Idris
Edwin BradyUniversity of St Andrews
@edwinbrady
![Page 2: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/2.jpg)
Lambda Mining
![Page 3: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/3.jpg)
Introduction
Idris is a pure functional programming language with dependenttypes
cabal update; cabal install idris
http://idris-lang.org/documentation/dsl-2013
In these lectures:
1 Today: An introduction to the language
Software correctness - why Idris?Language overview; programming idioms; theorem proving
2 Tomorrow: Embedded Domain Specific Languages
A well-type interpreterDealing with effects and resources
![Page 4: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/4.jpg)
Introduction
Idris is a pure functional programming language with dependenttypes
cabal update; cabal install idris
http://idris-lang.org/documentation/dsl-2013
In these lectures:
1 Today: An introduction to the language
Software correctness - why Idris?Language overview; programming idioms; theorem proving
2 Tomorrow: Embedded Domain Specific Languages
A well-type interpreterDealing with effects and resources
![Page 5: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/5.jpg)
![Page 6: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/6.jpg)
Software Correctness
![Page 7: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/7.jpg)
Software Correctness
![Page 8: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/8.jpg)
Types and Programming Languages
Modern programming languages help the programmer by checkingthat types are used consistently. Several choices are available:
Static vs Dynamic
Does the language check for type errors before running theprogram (Static typing) or while running the program(Dynamic typing)?
Strong vs Weak
Is it an error to violate all type distinctions (Strong typing) orcan we have more flexibility (Weak typing; e.g. treating anInt as a Float)
I am interested in strong, static typing.
![Page 9: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/9.jpg)
Idris Overview
General purpose programming language
Compiled, supports foreign functions, . . .
Influenced by Haskell
Pattern matching, where, . . .Type classes, do-notation, comprehensions, . . .
Has full dependent types
Types may be predicated on valuesCan encode (and check) program propertiesSupports tactic based theorem proving
Support for Embedded Domain Specific Langauges
Syntax overloading, dsl notation
![Page 10: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/10.jpg)
Idris Overview
General purpose programming language
Compiled, supports foreign functions, . . .
Influenced by Haskell
Pattern matching, where, . . .Type classes, do-notation, comprehensions, . . .
Has full dependent types
Types may be predicated on valuesCan encode (and check) program propertiesSupports tactic based theorem proving
Support for Embedded Domain Specific Langauges
Syntax overloading, dsl notation
![Page 11: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/11.jpg)
Idris Overview
General purpose programming language
Compiled, supports foreign functions, . . .
Influenced by Haskell
Pattern matching, where, . . .Type classes, do-notation, comprehensions, . . .
Has full dependent types
Types may be predicated on valuesCan encode (and check) program propertiesSupports tactic based theorem proving
Support for Embedded Domain Specific Langauges
Syntax overloading, dsl notation
![Page 12: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/12.jpg)
Idris Overview
General purpose programming language
Compiled, supports foreign functions, . . .
Influenced by Haskell
Pattern matching, where, . . .Type classes, do-notation, comprehensions, . . .
Has full dependent types
Types may be predicated on valuesCan encode (and check) program propertiesSupports tactic based theorem proving
Support for Embedded Domain Specific Langauges
Syntax overloading, dsl notation
![Page 13: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/13.jpg)
Dependent Types in Idris
Unary natural numbers
data Nat = O | S Nat
Polymorphic lists
data List : Type -> Type where
Nil : List a
(::) : a -> List a -> List a
Vectors — polymorphic lists with length
data Vect : Type -> Nat -> Type where
Nil : Vect a O
(::) : a -> Vect a k -> Vect a (S k)
![Page 14: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/14.jpg)
Dependent Types in Idris
Unary natural numbers
data Nat = O | S Nat
Polymorphic lists
data List : Type -> Type where
Nil : List a
(::) : a -> List a -> List a
Vectors — polymorphic lists with length
data Vect : Type -> Nat -> Type where
Nil : Vect a O
(::) : a -> Vect a k -> Vect a (S k)
![Page 15: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/15.jpg)
Dependent Types in Idris
Unary natural numbers
data Nat = O | S Nat
Polymorphic lists
data List : Type -> Type where
Nil : List a
(::) : a -> List a -> List a
Vectors — polymorphic lists with length
data Vect : Type -> Nat -> Type where
Nil : Vect a O
(::) : a -> Vect a k -> Vect a (S k)
![Page 16: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/16.jpg)
Dependent Types — Examples
Append
(++) : Vect a m -> Vect a n -> Vect a (m + n)
(++) [] ys = ys
(++) (x::xs) ys = x :: xs ++ ys
Pairwise addition
vAdd : Num a => Vect a n -> Vect a n -> Vect a n
vAdd [] [] = []
vAdd (x :: xs) (y :: ys) = x + y :: vAdd xs ys
![Page 17: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/17.jpg)
Dependent Types — Examples
Append
(++) : Vect a m -> Vect a n -> Vect a (m + n)
(++) [] ys = ys
(++) (x::xs) ys = x :: xs ++ ys
Pairwise addition
vAdd : Num a => Vect a n -> Vect a n -> Vect a n
vAdd [] [] = []
vAdd (x :: xs) (y :: ys) = x + y :: vAdd xs ys
![Page 18: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/18.jpg)
Dependent Types — Examples
Append
(++) : Vect a m -> Vect a n -> Vect a (m + n)
(++) [] ys = ys
(++) (x::xs) ys = x :: xs ++ ys
Pairwise addition
total
vAdd : Num a => Vect a n -> Vect a n -> Vect a n
vAdd [] [] = []
vAdd (x :: xs) (y :: ys) = x + y :: vAdd xs ys
![Page 19: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/19.jpg)
Why Dependent Types?
Precise types
sort : List Int -> List Int
![Page 20: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/20.jpg)
Why Dependent Types?
Precise types
sort : Vect Int n -> Vect Int n
![Page 21: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/21.jpg)
Why Dependent Types?
Precise types
sort : (xs : Vect Int n) ->
(ys : Vect Int n ** Permutation xs ys)
![Page 22: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/22.jpg)
Why Dependent Types?
Precise types
sort : (xs : Vect Int n) ->
(ys : Vect Int n ** Permutation xs ys)
We can make types as precise as we require.
However, precise types may require compleximplementations/proofs.
![Page 23: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/23.jpg)
Why Dependent Types?
In these lectures, we will see examples of:
Precise types for supporting machine-checked proofs ofcorrectness
Both functional and extra-functional correctness
Generic programming
First class types, so types can be calculated by programs
Expressivity
Using types to make more expressive libraries(Or: why I will never write a monad transformer again)
![Page 24: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/24.jpg)
Why Dependent Types?
In these lectures, we will see examples of:
Precise types for supporting machine-checked proofs ofcorrectness
Both functional and extra-functional correctness
Generic programming
First class types, so types can be calculated by programs
Expressivity
Using types to make more expressive libraries(Or: why I will never write a monad transformer again)
![Page 25: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/25.jpg)
Why Dependent Types?
In these lectures, we will see examples of:
Precise types for supporting machine-checked proofs ofcorrectness
Both functional and extra-functional correctness
Generic programming
First class types, so types can be calculated by programs
Expressivity
Using types to make more expressive libraries
(Or: why I will never write a monad transformer again)
![Page 26: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/26.jpg)
Why Dependent Types?
In these lectures, we will see examples of:
Precise types for supporting machine-checked proofs ofcorrectness
Both functional and extra-functional correctness
Generic programming
First class types, so types can be calculated by programs
Expressivity
Using types to make more expressive libraries(Or: why I will never write a monad transformer again)
![Page 27: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/27.jpg)
Why Idris?
There are several dependently typed languages available (e.g.Agda, Coq, Epigram, . . . ). Why did I make Idris?
We are still learning about dependent types
Plenty of scope for experimenting, still
Freedom to make language design decisions
e.g. partial functions, high level notation, primitive types,evaluation strategy, . . .
Desire to experiment with practical aspects
Efficient compilation, Operating system interaction, foreignfunction calls, . . .
![Page 28: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/28.jpg)
Why Idris?
There are several dependently typed languages available (e.g.Agda, Coq, Epigram, . . . ). Why did I make Idris?
We are still learning about dependent types
Plenty of scope for experimenting, still
Freedom to make language design decisions
e.g. partial functions, high level notation, primitive types,evaluation strategy, . . .
Desire to experiment with practical aspects
Efficient compilation, Operating system interaction, foreignfunction calls, . . .
![Page 29: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/29.jpg)
Why Idris?
There are several dependently typed languages available (e.g.Agda, Coq, Epigram, . . . ). Why did I make Idris?
We are still learning about dependent types
Plenty of scope for experimenting, still
Freedom to make language design decisions
e.g. partial functions, high level notation, primitive types,evaluation strategy, . . .
Desire to experiment with practical aspects
Efficient compilation, Operating system interaction, foreignfunction calls, . . .
![Page 30: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/30.jpg)
Demonstration: Basic Usage
![Page 31: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/31.jpg)
Aside: Evaluation Strategy
By default, Idris uses a strict evaluation strategy. But whatabout. . .
Control Structures
boolElim : Bool -> a -> a -> a
boolElim True t e = t
boolElim False t e = e
syntax if [test] then [t] else [e]
= boolElim test t e
foo : t
foo = if expr then largeexpr1 else largeexpr2
Both largeexpr1 and largeexpr2 have to be evaluated in full!
![Page 32: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/32.jpg)
Aside: Evaluation Strategy
By default, Idris uses a strict evaluation strategy. But whatabout. . .
Control Structures
boolElim : Bool -> a -> a -> a
boolElim True t e = t
boolElim False t e = e
syntax if [test] then [t] else [e]
= boolElim test t e
foo : t
foo = if expr then largeexpr1 else largeexpr2
Both largeexpr1 and largeexpr2 have to be evaluated in full!
![Page 33: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/33.jpg)
Aside: Evaluation Strategy
By default, Idris uses a strict evaluation strategy. But whatabout. . .
Control Structures with Laziness
boolElim : Bool -> |(t : a) -> |(e : a) -> a
boolElim True t e = t
boolElim False t e = e
syntax if [test] then [t] else [e]
= boolElim test t e
foo : t
foo = if expr then largeexpr1 else largeexpr2
Now only one of largeexpr1 and largeexpr2 will be evaluated,depending which is needed.
![Page 34: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/34.jpg)
Theorem Proving
Thanks to the Curry-Howard Correspondence, we can view a typeas a specification, and a program as a proof of a specification, e.g.
Some proofs
data Or a b = Inl a | Inr b
data And a b = And_intro a b
theorem1 : a -> Or a b
theorem1 x = Inl x
theorem2 : a -> b -> And a b
theorem2 x y = And_intro x y
![Page 35: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/35.jpg)
Theorem Proving
Thanks to the Curry-Howard Correspondence, we can view a typeas a specification, and a program as a proof of a specification, e.g.
Some proofs
data Or a b = Inl a | Inr b
data And a b = And_intro a b
theorem1 : a -> Or a b
theorem1 x = Inl x
theorem2 : a -> b -> And a b
theorem2 x y = And_intro x y
![Page 36: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/36.jpg)
Theorem Proving
Thanks to the Curry-Howard Correspondence, we can view a typeas a specification, and a program as a proof of a specification, e.g.
Some proofs
data Or a b = Inl a | Inr b
data And a b = And_intro a b
theorem1 : a -> Or a b
theorem1 x =
Inl x
theorem2 : a -> b -> And a b
theorem2 x y = And_intro x y
![Page 37: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/37.jpg)
Theorem Proving
Thanks to the Curry-Howard Correspondence, we can view a typeas a specification, and a program as a proof of a specification, e.g.
Some proofs
data Or a b = Inl a | Inr b
data And a b = And_intro a b
theorem1 : a -> Or a b
theorem1 x = Inl x
theorem2 : a -> b -> And a b
theorem2 x y = And_intro x y
![Page 38: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/38.jpg)
Theorem Proving
Thanks to the Curry-Howard Correspondence, we can view a typeas a specification, and a program as a proof of a specification, e.g.
Some proofs
data Or a b = Inl a | Inr b
data And a b = And_intro a b
theorem1 : a -> Or a b
theorem1 x = Inl x
theorem2 : a -> b -> And a b
theorem2 x y = And_intro x y
![Page 39: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/39.jpg)
Theorem Proving
Thanks to the Curry-Howard Correspondence, we can view a typeas a specification, and a program as a proof of a specification, e.g.
Some proofs
data Or a b = Inl a | Inr b
data And a b = And_intro a b
theorem1 : a -> Or a b
theorem1 x = Inl x
theorem2 : a -> b -> And a b
theorem2 x y = And_intro x y
![Page 40: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/40.jpg)
Equality Proofs
Built-in types
data (=) : a -> b -> Type where
refl : x = x
data _|_ where {- empty type -}
Rewriting
replace : {P : a -> Type} -> x = y -> P x -> P y
![Page 41: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/41.jpg)
Equality Proofs
Built-in types
data (=) : a -> b -> Type where
refl : x = x
data _|_ where {- empty type -}
Rewriting
replace : {P : a -> Type} -> x = y -> P x -> P y
![Page 42: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/42.jpg)
Equality Proofs
Example
twoPlusTwo : 2 + 2 = 4
twoPlusTwo = refl
![Page 44: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/44.jpg)
The with rule
Parity
data Parity : Nat -> Type where
even : Parity (n + n)
odd : Parity (S (n + n))
Every number has a parity
parity : (n : Nat) -> Parity n
Demonstration: Implementing parity
![Page 45: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/45.jpg)
The with rule
Parity
data Parity : Nat -> Type where
even : Parity (n + n)
odd : Parity (S (n + n))
Every number has a parity
parity : (n : Nat) -> Parity n
Demonstration: Implementing parity
![Page 46: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/46.jpg)
The with rule
Parity
data Parity : Nat -> Type where
even : Parity (n + n)
odd : Parity (S (n + n))
Every number has a parity
parity : (n : Nat) -> Parity n
Demonstration: Implementing parity
![Page 47: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/47.jpg)
Decidable equality
Type
data Dec : Type -> Type where
Yes : a -> Dec a
No : (a -> _|_) -> Dec a
Checking Equality
class DecEq t where
decEq : (x1 : t) -> (x2 : t) -> Dec (x1 = x2)
A Bool tells us the result of an equality test. Dec tells us why.
![Page 48: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/48.jpg)
Decidable equality
Type
data Dec : Type -> Type where
Yes : a -> Dec a
No : (a -> _|_) -> Dec a
Checking Equality
class DecEq t where
decEq : (x1 : t) -> (x2 : t) -> Dec (x1 = x2)
A Bool tells us the result of an equality test. Dec tells us why.
![Page 49: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/49.jpg)
Membership predicates
List Membership
data Elem : a -> List a -> Type where
Here : {xs : List a} -> Elem x (x :: xs)
There : {xs : List a} ->
Elem x xs -> Elem x (y :: xs)
Example
inList : Elem 2 [1,2,3,4]
inList = There Here
Demonstration: Building membership predicates
![Page 50: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/50.jpg)
Membership predicates
List Membership
data Elem : a -> List a -> Type where
Here : {xs : List a} -> Elem x (x :: xs)
There : {xs : List a} ->
Elem x xs -> Elem x (y :: xs)
Example
inList : Elem 2 [1,2,3,4]
inList = There Here
Demonstration: Building membership predicates
![Page 51: Dependently Typed Functional Programming with IdrisBoth functional and extra-functional correctness Generic programming First class types, so types can be calculated by programs Expressivity](https://reader035.fdocuments.in/reader035/viewer/2022081612/5f6e5f7a13d4e235ac6a5146/html5/thumbnails/51.jpg)
Membership predicates
List Membership
data Elem : a -> List a -> Type where
Here : {xs : List a} -> Elem x (x :: xs)
There : {xs : List a} ->
Elem x xs -> Elem x (y :: xs)
Example
inList : Elem 2 [1,2,3,4]
inList = There Here
Demonstration: Building membership predicates