You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The More Things Change, The More They Stay The Same
The Law of Tick Marks
A tick mark directly followed by one or more letters and hyphens is an Atom.
'ratatouille is an Atom
A form of judgement is an observation with blank spaces in it, such as ___ is a ___
The Commandment of Tick Marks
Two expressions are the same Atom if their values are tick marks followed by identical letters and hyphens.
'citron is the same Atom as 'citron
The second form of judgement is that ___ is the same ___ as ___
Expressions that describe other expressions, such as Atom, are called types
The third from of judgement is ___ is a type
The Law of Atom
Atom is a type
The fourth form of judgement is ___ and ___ are the same type
Atom and Atom are the same type
The Four Forms of Judgement
___ is a ___
___ is the same ___ as ___
___ is a type
___ and ___ are the same type
Normal Forms
Given a type, every expression described by that type has a normal form, which is the most direct way of writing it. If two expressions are the same , then they have identical normal forms, and if they have identical normal forms, the they are the same.
Normal Forms and Types
Sameness is always according to a type, so normal forms are also determined by as type.
The First Commandment of cons
Two cons-expression are the same (Pair A D) if their cars are the same · and their cdrs are the same D. Here, A and D stand for any type.
Normal Forms of Types
Every expression that is a type has a normal form, which is the most direct way of writing that type. If two expressions are the same type, then they have identical normal forms, and if two types have identical normal forms, they are the same type.
Claims before Definitions
Using define to associate a name with an expression requires that the expression's type has previously been associated with the name using claim.
(claim one
Nat)
(define one
(add1 zero))
Values
An expression with a constructor at the top is called a value
Values and Normal Forms
Not every value is in normal form. This is because the arguments to a constructor need not be normal. Each expression has only one normal form, but it is sometimes possible to write it as a value in more than one way.
Finding a value that is the same as some starting expression is called evaluation
Everything Is a Expression
values are also expressions, Evaluation finds an expression, not some other kind of thing.
The Commandment of zero
zero is the same Nat as zero
The Commandment of add1
If n is teh same Nat as k, then (add1 n) is the same Nat as (add1 k)
Definitions Are Forever
Once a name has been claimed, it cannot be reclaimed,
and once a name has been defined, it cannot be redefined.
(cons 'zero 'onion) is a (Pair Atom Atom)
zero and add1 are constructors that create data, Pair is a type constructor because it constructs a type
Doin' What Comes Naturally
Constructors and Eliminators
Constructors build values, and eliminators take apart values built by constructors
car is an eliminator
λ builds functions
Eliminating Functions
Applying a function to arguments is the eliminator for functions
The Initial Law of Application
If f is an (-> Y X) and arg is a Y, then (f arg) is an X
The Initial Commandments of λ
Two λ-expressions that expect the same number of arguments are the same if their bodies are the same after consistently renaming their variables
If f is an (-> Y X), then f is the same (-> Y X) as (λ (y) (f y)) as long as y does not occur in f.
The Law of Renaming Variables
Consistently renaming variables can't change the meaning of anything
Expressions that are not values and cannot yet be evaluated due to variable are called neutral
If two expressions have identical eliminators at the top and all arguments to the eliminators are the same, then the expressions are the same.
The Commandment of Neutral Expressions
Neutral expressions that are written identically are the same, no matter their type.
The Law and Commandment of define
Following (claim name X) and (define name expr)
if expr is an X, than name is X
and name is the same X as expr.
The Second Commandment of cons
If p is a (Pair A D), then it is the same (Pair A D) as (cons (car p) (cdr p))
Names in Definitions
Only names that are not already used, whether for constructors, eliminators, or previous definitions, can be used with claim or define.
Dim Names
Unused names are written dimly, but they do need to be there.
The Law of which-Nat
If target is a Nat, base is an X, and step is an (-> Nat X),
then (which-Nat target base step) is an X.
The Commandments of which-Nat
If (which-nat zero base step) is an X, then it is the same X as base
If (which-Nat (add1 n) base step) is an X, then it is the same X as (step n)
Explicit recursion is not an option because it possible write a no value expression (dead loop)
An expression that is described by a type is a value when it has a constructor at its top. Similarly, an expression that is a type is a value when it has a type constructor at its top.
U describes all the types except for itself.
Every U Is a Type
Every expression described by U is a type, but not every type is described by U.
(claim Pear U)
(define Pear (Pair Nat Nat))
Definitions Are Unnecessary
Everything can be done without definitions, but they do improve understanding.
If X is a type and e is an X, then (the X e) is an X.
The Commandment of the
If X is a type and e is an X, then (the X e) is the same X as e.
Eliminate All Natural Numbers!
Sameness
If a "same as" chart could show tha two expressions are the same, then this fact can be used anywhere without further justification.
"Same As" charts are only to help build understanding.
Total Function
A function that always assigns a value to every possible argument is called a total function.
All functions are total.
The Law of iter-Nat
If target is a Nat, base is an X, and step is an (-> X X),
then (iter-Nat target base step) is an X
The Commandments of iter-Nat
If (iter-Nat zero base step) is an X,
then it is the same X as base
If (iter-Nat (add1 n) base step) is an X,
then it is the same X as (step (iter-Nat n base step))
If f is a (Π ((Y U)) X) and Z is a U, then (f Z) is an X where every Y has been consistently replaced by Z
(claim twin
(Π ((Y U))
(-> Y
(Pair Y Y))))
(define twin
(λ (Y)
λ (x)
(cons x x)))
(define twin-Atom
(twin Atom))
List, List and More List
The Law of List
If E is a type, then (List E) is type.
The Law of nil
nil is a (List E), no matter what type E is.
The Law of ::
If e is an E and es is a (List E), then (::e es) is a (List E).
The Law of rec-List
If target is a (List E), base is an X, and step is an (-> E (List E) X X),
then (rec-List target base step) is an X.
The Commandments of rec-List
If (rec-List nil base step) is an X, then it is the same X as base.
If (rec-List (:: e es) base step) is an X,
then it is the same X as (step e es (rec-List es base step))
(claim step-length
(-> Atom (ListAtom) Nat Nat))
(define step-length
(λ (e es length-es)
(add1 length-es)))
(claim length
(-> (ListAtom)
Nat))
(define length
(λ (es)
(rec-List es)
0
step-length))
; use U
(claim step-length
(Π ((E U))
(-> (List E)
Nat)))
(define step-length
(λ (E)
(λ (e es length-es)
(add1 length-es))))
(claim length
(Π ((E U))
(-> (List E)
Nat)))
(define length
(λ (E)
(λ (es)
(rec-List es
0
(step-length E)))))
List Entry Types
All the entries in a list must have the same type.
; ((append Atom) (:: 'A nil) (:: 'B nil)); ->; (:: 'A (:: 'B nil))
(claim step-append
(Π ((E U))
(-> E (List E) (List E)
(List E))))
(define step-append
(λ (E)
(λ (e es append-es)
(:: e append-es))))
(claim append
(Π ((E U))
(-> (List E) (List E)
(List E))))
(define append
(λ (E)
(λ (start end)
(rec-List start
end
(step-append E)))))
; ((snoc Atom) (:: 'A nil) 'B); ->; (:: 'A (:: 'B nil))
(claim snoc
(Π ((E U))
(-> (List) E
(List E))))
(define snoc
(λ (E)
λ (es e)
(rec-List es
(:: e nil) ; Atom -> (List Atom)
(step-append E))))
(claim step-reverse
(Π ((E U))
(-> E (List E) (List E)
(List E))))
(define step-reverse
(λ (E)
(λ e es reverse-es
((snoc E) reverse-es e))))
(claim reverse
(Π ((E U))
(-> (List E)
(List E))))
(define reverse
(λ (E)
(λ es
(rec-List es
nil
(step-reverse E)))))
Precisely How Many?
The Law of Vec
If E is a type and K is a Nat, then (Vec E k) is a type.
The Law of vecnil
vecnil is a (Vec E zero).
The Law of vec::
If e is an E and es is a (Vec E k),
then (vec:: e es) is a (Vec E (add1 k)).
The Law of Π
The expression (Π ((y Y)) X) is a type when Y is a type, and X is a type if y is a Y.
Use a More Specific Type
Make a function total by using a more specific type to rule out unwanted arguments.
-> and Π
The type (-> Y X) is a shorter way of writing (Π ((y Y)) X) when y is not used in X.
(claim first
(Π ((E U)
(l Nat)
(es (Vec E (add1 l)))) ; es is dim
E))
(define first
(λ (E l es)
(head es)))
The Final Law of λ
If x is an X when y is a Y, then (λ (y) x) is a (Π ((y Y)) X).
The Final Law of Application
If f is a (Π ((y Y)) X) and z is a Y, then (f z) is an X
where every y has been consistently replaced by z.
The Final Commandments of λ
If two λ-expression can be made the same (Π ((y Y)) X) by consistently renaming their variables, then they are the same.
If f is a (Π ((y Y)) X) and y does not occur in f, then f is the same as (λ (y) (f y)).
It All Depends On the Motive
Dependent Types
A type that is determined by something that is not a type is called a dependent type.
(claim peas
(Π ((how-many-peas Nat))
(Vec Atom how-many-peas)))
(define peas
(λ (how-many-peas)
(rec-Nat how-many-peas
vecnil
(λ (l-1 peas-l-1) ; it against the type, (-> Nat X X),
(vec:: 'pea peas-l-1))))) ; (vec:: 'pea peas-l-1) and peas-l-1 are not the same type
Motive can be any (-> Nat U)
Use ind-Nat for Dependent Types
Use ind-Nat instead of rec-Nat when the rec-Nat- or ind-Nat-expression's type depends on the targe Nat, The ind-Nat-expression's type is the motive applied to the target
If target is a Nat, mot is an (-> Nat U),
base is a (mot zero),
and step is a
(Π ((n-1 Nat)
(-> (mot n-1)
(mot (add1 n-1)))))
then (ind-Nat target mot base step) is a (mot target)
The Commandments of ind-Nat
The ind-Nat-expression (ind-Nat zero mot base step) is the same (mot zero) as base
The ind-Nat-expression (indNat (add1 n) mot base step) and (step n (ind-Nat n mot base step)) are the same (mot (add1 n)).
(claim step
(Π ((n Nat)); almost Nat
(-> (mot n); almost answer
(mot (add1 n)))))
Induction on Natural Numbers
Building a value for any natural number by giving a value for zero and a way to transform a value for n into a value n + 1 is called induction on natural numbers.
(claim also-rec-Nat
(Π ((X U))
(-> Nat
X
(-> Nat X
X)
X)))
(define alse-rec-Nat
(λ (X)
(λ (target base step)
(ind-Nat target
(λ (k) X) ; k is dim
base
step))))
Define last
(claim last
(Π ((E U)
(l Nat))
(-> (Vec E (add1 l))
E)))
; consider using ind-Nat to define the last
(claim base-last
(Π ((E U))
(-> (Vec E (add1 zero) ; last is a function, so the base is a function too
E))))
(define base-last
(λ (E)
λ (es)
(head es))) ; when l is zero, vector es only contains one entry
(claim mot-last
(-> U Nat
U))
(define mot-last
(λ (E k)
(-> (Vec E (add1 k))
E)))
(claim step-last
(Π ((E U) (l-1 Nat))
(-> (mot-last E l-1) ; almost answer
(mot-last E (add1 l-1)))))
(define step-last
(λ (E l-1)
(λ (last-l-1); last-l-1 is a function too
(λ (es)
(last-l-1 (tail es))))))
(define last
(λ (E l)
(ind-Nat l
(mot-last E)
(base-last E)
(step-last E))))
Readable Expressions
Getting the right answer is worthless if we do not know that it is correct. Understanding the answer is at least as important as having the correct answer.
Pick a Number, Any Number
With a new type constructor, type can express a new idea called equality
The Law of =
An expression (= X from to) is a type if X is a type, from is an X, and to is a3n X.
Reading FROM and TO as Nouns
Because from and to are convenient names, the corresponding parts of an =-expression are referred to as the FROM and TO
Types can be read as statements
(= Atom 'apple 'apple) can be read: "The expressions 'apple and 'apple are equal Atoms"
(Π ((n Nat)) (= Nat (+ 1 n) (add1 n))) can be read: "For every Nat n, (+ 1 n) equals (add1 n)"
The same is the only constructor for =. here, "values" means "proof".
The Law of same
The expression (same e) is an (= X e e) if e is an X.
No matter which Nats j and k are, (+ (add1 j) k) is the same Nat as (add1 (+ j k))
Solve Easy Problems First
If two functions produce equal results, then use the easier one when defining a dependent function, and then use replace to give it the desired type.
The Law of symm
If e is an (= X from to), then (symm e) is an (= X to from).
The Commandment of symm
If x is an X, then (symm (same x)) is the same (= X x x) as (same x).
(define twice-Vec
(λ (E l)
(λ (es)
(replace (symm (twice=double l))
(λ (k) (Vec E k))
(double-Vec E l es)
))))
It Also Depends on the List
The Law of Σ
The expression (Σ ((x A)) D) is a type when A is a type, and D is a type if x is an A.
The Commandment of cons
If p is a (Σ ((x A)) D), then p is the same as (cons (car p) (cdr p)).
If (cons a d) is a (Σ ((x A)) D) then a's type is 4A and d's type is found by consistently replacing every x in D with a.
(cons Nat 4) is a (Σ ((A U)) A).
(Pair A D) is a short way of writing (Σ ((x A)) D) where x is not used in D.
(Pair A D) can be read "A and D"
A Σ-expression can be read as "there exists"
(Σ ((es (List Atom))) (= (List Atom) es (reverse Atom es))) can be read as "There exists a list of atoms that is equal to itself reversed"
Use a Specific Type for Correctness
Specific types can rule out foolish definitions.
; not specific enough
(claim list->vec
(Π ((E U))
(-> (List E)
(Σ ((l Nat))
(Vec E l)))))
; foolish but permitted
(define list->vec
(λ (E es)
(cons0 vecnil)))
; specific type
(claim list->vec
(Π ((E U)
(es (List E)))
(Vec E (length E es))))
The Law of ind-List
If target is a (List E), mot is an (-> (List E) U), base is a (mot nil), and step is a
then (ind-List target mot base step) is a (mot target).
The Commandments of ind-List
The ind-List-expression (ind-List nil mot base step) is the same (mot nil) as base.
The ind-List-expression (ind-List (:: e es) mot base step) is the same
(mot (:: e es)) as (step e es (ind-List es mot base step))
(define list->vec
(λ (E es)
(ind-List es
(mot-list->vec E) ; (λ (E es) (Vec E (length E es)))
vecnil
(step-list->vec E))))
(define step-list->vec
(λ (E e es)
(λ (vec-almost)
(vec:: e vec-almost))))
All Lists Are Created Equal
The Law if ind-Vec
If n is a Nat, target is a (Vec E n), mot is an
(Π ((k Nat))
(-> (Vec E k)
U))
base is a (mot zero vecnil), and step is a
(Π ((k Nat)
(e E)
(es (Vec E k)))
(-> (mot k es)
(mot (add1 k) (vec:: e es))))
then (ind-Vec n target mot base step) is a (mot n target).
The Commandments of ind-Vec
The ind-Vec-expression (ind-Vec zero vecnil mot base step)
is the same (mot zero vecnil) as base.
The ind-Vec-expression (ind-Vec (add1 n) (vec:: e es) mot base step)
is the same (mot (add1 n) (vec:: e es)) as (step n e es (ind-Vec n es mot base step)).
When in Doubt,Evaluate
Gain insight by finding the values of expressions in types and working out examples in "same-as" charts.
Even Numbers Can Be Odd
What is an even number?
A number that can be split into two equal halves.
Two equal halves -- There is some number that, added to itself, yield the original number.
(claim Even
(-> Nat U))
(define Even
(λ (n)
(Σ ((half Nat))
(= Nat n (double half)))))
(Even 10) ; value: (cons 5 (same 10))
Carefully Choose Definitions
Carefully-chosen definitions can greatly simplify later proofs.
(define Even
(λ (n)
(Σ ((half Nat))
(= Nat n (+ half half)))))
Even Haf a Baker's Dozen
Is Every natural number either even or odd?
"Every natural number is either even or odd."
The Law of Either
(Either L R) is a type if L is a type and R is a type.
(left lt) is an (Either L R) if lt is an L.
(right rt) is an (Either L R) if rt is an R.
The Law of ind-Either
If target is an (Either L R), mot is an (-> (Either L R) U), base-left is a (Π ((x L)) (mot (left x))),
and base-right is a (Π ((y R)) (mot (right y)))
then (ind-Either target mot base-left base-right) is a (mot target)
The Commandments of ind-Either
(ind-Either (left x) mot base-left base-right)
is the same (mot (left x)) as (base-left x)
(ind-Either (right y) mot base-left base-right)
is the same (mot (right y)) as (base-right y)
(claim even-or-odd
(Π ((n Nat))
(Either (Even n) (Odd n))))
(define even-or-odd
(λ (n)
(ind-Nat n
mot-even-or-odd ; (λ (k) (Either (Even k) (Odd k)))
(left zero-is-even) ; (cons 0 (same 0))
step-even-or-odd)))
(define step-even-or-odd
(λ (n-1)
(λ (e-or-o-n-1)
(ind-Either e-or-o-n-1
(λ (e-or-o-n-1)
(mot-even-or-odd (add1 n-1)))
(λ (e-n-1) ; e-n-1 is an (Even n-1)
(right ; add1 and become an odd
(add1-even->odd n-1 e-n-1)))
(λ (o-n-1) ; o-n-1 is a (Odd n-1)
(left ; add1 and become an even
(add1-odd->even n-1 o-n-1)))))))
(even-or-odd 2)
; equals
(left (add1-odd->even
1 (add1-even->odd
0 zero-is-even)))
; equals
(left (cons1 (same 2)))
There's Safety in Numbers
To represent the case when there is no entry, we need a new type, called Trivial
The Law of Trivial
Trivial is a type.
sole is a Trivial
If e is a Trivial, then e is the same as sole.
(claim Maybe (-> U U))
; either an X or a Trivial
(define Maybe
(λ (X)
(Either X Trivial)))
(claim nothing
(Π ((E U))
(Maybe E)))
(define nothing
(λ (E)
(right sole)))
(claim just
(Π ((E U))
(-> E (Maybe E))))
(define just
(λ (E e)
(left e)))
(claim maybe-head
(Π ((E U))
(-> (List E))
(Maybe E)))
(define maybe-head
(λ (E es)
(rec-List es
(nothing E)
(λ (hd tl head-ti)
(just E hd)))))
The Law of Absurd
Absurd is a type
There are no Absurd values
Every expression of type Absurd is neutral, and all of them are the same.
The Law of ind-Absurd
The expression (ind-Absurd target mot) is a mot if target is an Absurd and mot is a U.
For each Natn, (Fin n) should be a type with n values.
Using types, it is possible to assume things that may or may not be true, and then see what can be concluded from these assumptions.
Sameness versus Equality
Either two expressions are the same, or they are not. It is impossible to prove that they are the same because sameness is a judgement, not a type, and a proof is an expression with a specific type
If It's All the Same to You
My Thoughts
Dependent type is a type that determined by a value.
Types can be read as statements and values become evidence, as a result, finding values for a given type equals to proving a given statement.