Slides Isabelle
Slides Isabelle
with Isabelle/HOL
Tobias Nipkow
Fakultät für Informatik
Technische Universität München
2017-3-8
1
Part I
Isabelle
2
Chapter 2
3
1 Overview of Isabelle/HOL
3 Induction Heuristics
4 Simplification
4
Notation
A =⇒ B =⇒ C means A =⇒ (B =⇒ C)
A1 ... An means A1 =⇒ . . . =⇒ An =⇒ B
B
6
1 Overview of Isabelle/HOL
3 Induction Heuristics
4 Simplification
7
HOL = Higher-Order Logic
HOL = Functional Programming + Logic
HOL has
• datatypes
• recursive functions
• logical operators
HOL is a programming language!
Higher-order = functions are values, too!
HOL Formulas:
• For the moment: only term = term,
e.g. 1 + 2 = 4
• Later: ∧, ∨, −→, ∀, . . .
8
1 Overview of Isabelle/HOL
Types and terms
Interface
By example: types bool, nat and list
Summary
9
Types
Basic syntax:
τ ::= (τ )
| bool | nat | int | . . . base types
0
| a | 0b | . . . type variables
| τ ⇒τ functions
| τ ×τ pairs (ascii: *)
| τ list lists
| τ set sets
| ... user-defined types
Convention: τ 1 ⇒ τ 2 ⇒ τ 3 ≡ τ 1 ⇒ (τ 2 ⇒ τ 3 )
10
Terms
11
Terms
Basic syntax:
t ::= (t)
| a constant or variable (identifier)
| tt function application
| λx. t function abstraction
| ... lots of syntactic sugar
Examples: f (g x) y
h (λx. f (g x))
Convention: f t1 t2 t3 ≡ ((f t1 ) t2 ) t3
(λx. t) u = t[u/x]
Example: (λx. x + 5) 3 = 3 + 5
13
Terms must be well-typed
(the argument of every function call must be of the right type)
Notation:
t :: τ means “t is a well-typed term of type τ ”.
t :: τ 1 ⇒ τ 2 u :: τ 1
t u :: τ 2
14
Type inference
15
Currying
• Curried: f :: τ 1 ⇒ τ 2 ⇒ τ
• Tupled: f 0 :: τ 1 × τ 2 ⇒ τ
Advantage:
16
Predefined syntactic sugar
• Infix: +, −, ∗, #, @, . . .
• Mixfix: if then else , case of, . . .
Syntax: theory M yT h
imports T1 . . . Tn
begin
(definitions, theorems, proofs, ...)∗
end
18
Concrete syntax
In .thy files:
Types, terms and formulas need to be inclosed in "
19
1 Overview of Isabelle/HOL
Types and terms
Interface
By example: types bool, nat and list
Summary
20
isabelle jedit
21
Overview_Demo.thy
22
1 Overview of Isabelle/HOL
Types and terms
Interface
By example: types bool, nat and list
Summary
23
Type bool
Predefined functions:
∧, ∨, −→, . . . :: bool ⇒ bool ⇒ bool
if-and-only-if: =
24
Type nat
datatype nat = 0 | Suc nat
25
Nat_Demo.thy
26
An informal proof
Lemma add m 0 = m
Proof by induction on m.
• Case 0 (the base case):
add 0 0 = 0 holds by definition of add.
• Case Suc m (the induction step):
We assume add m 0 = m,
the induction hypothesis (IH).
We need to show add (Suc m) 0 = Suc m.
The proof is as follows:
add (Suc m) 0 = Suc (add m 0) by def. of add
= Suc m by IH
27
Type 0a list
Lists of elements of type 0a
Syntactic sugar:
• [] = Nil: empty list
• x # xs = Cons x xs:
list with first element x (“head”) and rest xs (“tail”)
• [x1 , . . . , xn ] = x1 # . . . xn # []
28
Structural Induction for lists
29
List_Demo.thy
30
An informal proof
Lemma app (app xs ys) zs = app xs (app ys zs)
Proof by induction on xs.
• Case Nil: app (app Nil ys) zs = app ys zs =
app Nil (app ys zs) holds by definition of app.
• Case Cons x xs: We assume app (app xs ys) zs =
app xs (app ys zs) (IH), and we need to show
app (app (Cons x xs) ys) zs =
app (Cons x xs) (app ys zs).
The proof is as follows:
app (app (Cons x xs) ys) zs
= Cons x (app (app xs ys) zs) by definition of app
= Cons x (app xs (app ys zs)) by IH
= app (Cons x xs) (app ys zs) by definition of app
31
Large library: HOL/List.thy
Included in Main.
map f [x1 , . . . , xn ] = [f x1 , . . . , f xn ]
33
• datatype defines (possibly) recursive data types.
34
Proof methods
35
Proofs
General schema:
36
Top down proofs
Command
sorry
37
The proof state
V
1. x1 . . . xp . A =⇒ B
38
Multiple assumptions
[[ A1 ; . . . ; An ]] =⇒ B
abbreviates
A1 =⇒ . . . =⇒ An =⇒ B
; ≈ “and”
39
1 Overview of Isabelle/HOL
3 Induction Heuristics
4 Simplification
40
2 Type and function definitions
Type definitions
Function definitions
41
Type synonyms
type_synonym name = τ
Introduces a synonym name for type τ
Examples
type_synonym string = char list
type_synonym ( 0a, 0b)foo = 0a list × 0b list
42
datatype — the general case
datatype (α1 , . . . , αn )t = C1 τ1,1 . . . τ1,n1
| ...
| Ck τk,1 . . . τk,nk
• Types: Ci :: τi,1 ⇒ · · · ⇒ τi,ni ⇒ (α1 , . . . , αn )t
• Distinctness: Ci . . . 6= Cj . . . if i 6= j
• Injectivity: (Ci x1 . . . xni = Ci y1 . . . yni ) =
(x1 = y1 ∧ · · · ∧ xni = yni )
43
Case expressions
Datatype values can be taken apart with case:
(case xs of [] ⇒ . . . | y#ys ⇒ ... y ... ys ...)
Wildcards:
(case m of 0 ⇒ Suc 0 | Suc ⇒ 0)
Nested patterns:
(case xs of [0] ⇒ 0 | [Suc n] ⇒ n | ⇒ 2)
45
The option type
If 0a has values a1 , a2 , . . .
then 0a option has values None, Some a1 , Some a2 , . . .
Typical application:
fun lookup :: ( 0a × 0b) list ⇒ 0a ⇒ 0b option where
lookup [] x = None |
lookup ((a,b) # ps) x =
(if a = x then Some b else lookup ps x)
46
2 Type and function definitions
Type definitions
Function definitions
47
Non-recursive definitions
Example
definition sq :: nat ⇒ nat where sq n = n∗n
48
The danger of nontermination
How about f x = f x + 1 ?
49
Key features of fun
50
Example: separation
51
Example: Ackermann
fun ack :: nat ⇒ nat ⇒ nat where
ack 0 n = Suc n |
ack (Suc m) 0 = ack m (Suc 0) |
ack (Suc m) (Suc n) = ack m (ack (Suc m) n)
Terminates because the arguments decrease
lexicographically with each recursive call:
• (Suc m, 0) > (m, Suc 0)
• (Suc m, Suc n) > (Suc m, n)
• (Suc m, Suc n) > (m, )
52
primrec
• A restrictive version of fun
• Means primitive recursive
• Most functions are primitive recursive
• Frequently found in Isabelle theories
53
1 Overview of Isabelle/HOL
3 Induction Heuristics
4 Simplification
54
Basic induction heuristics
55
A tail recursive reverse
Our initial reverse:
fun rev :: 0a list ⇒ 0a list where
rev [] = [] |
rev (x#xs) = rev xs @ [x]
A tail recursive version:
fun itrev :: 0a list ⇒ 0a list ⇒ 0a list where
itrev [] ys = ys |
itrev (x#xs) ys =
56
Induction_Demo.thy
Generalisation
57
Generalisation
58
So far, all proofs were by structural induction
because all functions were primitive recursive.
In each induction step, 1 constructor is added.
In each recursive call, 1 constructor is removed.
Now: induction for complex recursion patterns.
59
Computation Induction
Example
fun div2 :: nat ⇒ nat where
div2 0 = 0 |
div2 (Suc 0) = 0 |
div2 (Suc(Suc n)) = Suc(div2 n)
60
Computation Induction
If f :: τ ⇒ τ 0 is defined by fun, a special induction
schema is provided to prove P (x) for all x :: τ :
for each defining equation
61
How to apply f.induct
If f :: τ1 ⇒ · · · ⇒ τn ⇒ τ 0 :
Heuristic:
• there should be a call f a1 . . . an in your goal
• ideally the ai should be variables.
62
Induction_Demo.thy
Computation Induction
63
1 Overview of Isabelle/HOL
3 Induction Heuristics
4 Simplification
64
Simplification means . . .
65
An example
0+n = n (1)
(Suc m) + n = Suc (m + n) (2)
Equations:
(Suc m ≤ Suc n) = (m ≤ n) (3)
(0 ≤ m) = T rue (4)
(1)
0 + Suc 0 ≤ Suc 0 + x =
(2)
Suc 0 ≤ Suc 0 + x =
Rewriting: (3)
Suc 0 ≤ Suc (0 + x) =
(4)
0 ≤ 0+x =
T rue
66
Conditional rewriting
Simplification rules can be conditional:
[[ P1 ; . . . ; Pk ]] =⇒ l = r
Example
p(0) = T rue
p(x) =⇒ f (x) = g(x)
We can simplify f (0) to g(0) but
we cannot simplify f (1) because p(1) is not provable.
67
Termination
Simplification may not terminate.
Isabelle uses simp-rules (almost) blindly from left to right.
[[ P1 ; . . . ; Pk ]] =⇒ l = r
is suitable as a simp-rule only
if l is “bigger” than r and each Pi
70
Rewriting with definitions
71
Case splitting with simp
Automatic:
By hand:
P(case e of 0 ⇒ a | Suc n ⇒ b)
=
(e = 0 −→ P(a)) ∧ (∀ n. e = Suc n −→ P(b))
73
Chapter 3
74
5 Case Study: IMP Expressions
75
5 Case Study: IMP Expressions
76
This section introduces
77
5 Case Study: IMP Expressions
Arithmetic Expressions
Boolean Expressions
Stack Machine and Compilation
78
Concrete and abstract syntax
Concrete syntax: strings, eg "a+5*b"
Abstract syntax: trees, eg +
@
@
a @
*
A
AA
5 b
79
Concrete syntax is defined by a context-free grammar, eg
a ::= n | x | (a) | a + a | a ∗ a | . . .
80
Datatype aexp
Variable names are strings, values are integers:
type_synonym vname = string
datatype aexp = N int | V vname | Plus aexp aexp
Concrete Abstract
5 N5
x V 00x 00
x+y Plus (V 00x 00) (V 00y 00)
2+(z+3) Plus (N 2) (Plus (V 00z 00) (N 3))
81
Warning
N 0 6= Plus (N 0) (N 0)
82
The (program) state
83
Function update notation
f (a := b)
84
How to write down a state
Some states:
• λx. 0
• (λx. 0)( 00a 00 := 3)
• ((λx. 0)( 00a 00 := 5))( 00x 00 := 3)
Nicer notation:
85
AExp.thy
86
5 Case Study: IMP Expressions
Arithmetic Expressions
Boolean Expressions
Stack Machine and Compilation
87
BExp.thy
88
5 Case Study: IMP Expressions
Arithmetic Expressions
Boolean Expressions
Stack Machine and Compilation
89
ASM.thy
90
This was easy.
Because evaluation of expressions always terminates.
But execution of programs may not terminate.
Hence we cannot define it by a total recursive function.
91
Chapter 4
92
6 Logical Formulas
7 Proof Automation
9 Inductive Definitions
93
6 Logical Formulas
7 Proof Automation
9 Inductive Definitions
94
Syntax (in decreasing precedence):
form ::= (form) | term = term | ¬form
| form ∧ form | form ∨ form | form −→ form
| ∀x. form | ∃x. form
Examples:
¬A∧B∨C ≡ ((¬ A) ∧ B) ∨ C
s=t∧C ≡ (s = t) ∧ C
A∧B=B∧A ≡ A ∧ (B = B) ∧ A
∀ x. P x ∧ Q x ≡ ∀ x. (P x ∧ Q x)
Input syntax: ←→ (same precedence as −→)
95
Variable binding convention:
∀ x y. P x y ≡ ∀ x. ∀ y. P x y
96
Warning
! P ∧ ∀ x. Q x ; P ∧ (∀ x. Q x) !
97
Mathematical symbols
. . . and their ascii representations:
∀ \<forall> ALL
∃ \<exists> EX
λ \<lambda> %
−→ -->
←→ <->
∧ /\ &
∨ \/ |
¬ \<not> ~
6 = \<noteq> ~=
98
Sets over type 0a
0
a set
∈ \<in> :
⊆ \<subseteq> <=
∪ \<union> Un
∩ \<inter> Int
99
Set comprehension
100
6 Logical Formulas
7 Proof Automation
9 Inductive Definitions
101
simp and auto
102
fastforce
103
blast
104
Automating arithmetic
arith:
• proves linear formulas (no “∗”)
• complete for quantifier-free real arithmetic
• complete for first-order theory of nat and int
(Presburger arithmetic)
105
Sledgehammer
106
Architecture:
Isabelle
Goal
& filtered library
↓ ↑ Proof
external
ATPs1
Characteristics:
• Sometimes it works,
• sometimes it doesn’t.
apply(proof-method)
done
108
Auto_Proof_Demo.thy
109
6 Logical Formulas
7 Proof Automation
9 Inductive Definitions
110
Step-by-step proofs can be necessary if automation fails
and you have to explore where and why it failed by
taking the goal apart.
111
What are these ?-variables ?
After you have finished a proof, Isabelle turns all free
variables V in the theorem into ?V.
Example: theorem conjI: [[?P; ?Q]] =⇒ ?P ∧ ?Q
These ?-variables can later be instantiated:
• By hand:
conjI[of "a=b" "False"] ;
[[a = b; False]] =⇒ a = b ∧ False
• By unification:
unifying ?P ∧ ?Q with a=b ∧ False
sets ?P to a=b and ?Q to False.
112
Rule application
Example: rule: [[?P; ?Q]] =⇒ ?P ∧ ?Q
subgoal: 1. . . . =⇒ A ∧ B
Result: 1. . . . =⇒ A
2. . . . =⇒ B
The general case: applying rule [[ A1 ; . . . ; An ]] =⇒ A
to subgoal . . . =⇒ C:
• Unify A and C
• Replace C with n new subgoals A1 . . . An
apply(rule xyz)
“Backchaining”
113
Typical backwards rules
?P ?Q
conjI
?P ∧ ?Q
V
?P =⇒ ?Q x. ?P x
impI allI
?P −→ ?Q ∀ x. ?P x
?P =⇒ ?Q ?Q =⇒ ?P
iffI
?P = ?Q
(blast intro: r)
r[OF s]
116
The general case:
If r is a theorem [[ A1 ; . . . ; An ]] =⇒ A
and r1 , . . . , rm (m≤n) are theorems then
r[OF r1 . . . rm ]
117
From now on: ? mostly suppressed on slides
118
Single_Step_Demo.thy
119
=⇒ versus −→
120
6 Logical Formulas
7 Proof Automation
9 Inductive Definitions
121
Example: even numbers
Informally:
• 0 is even
• If n is even, so is n + 2
• These are the only even numbers
In Isabelle/HOL:
inductive ev :: nat ⇒ bool
where
ev 0 |
ev n =⇒ ev (n + 2)
122
An easy proof: ev 4
ev 0 =⇒ ev 2 =⇒ ev 4
123
Consider
fun evn :: nat ⇒ bool where
evn 0 = True |
evn (Suc 0) = False |
evn (Suc (Suc n)) = evn n
A trickier proof: ev m =⇒ evn m
By induction on the structure of the derivation of ev m
Two cases: ev m is proved by
• rule ev 0
=⇒ m = 0 =⇒ evn m = True
• rule ev n =⇒ ev (n+2)
=⇒ m = n+2 and evn n (IH)
=⇒ evn m = evn (n+2) = evn n = True
124
Rule induction for ev
To prove
ev n =⇒ P n
Rule ev.induct:
V
ev n P 0 n. [[ ev n; P n ]] =⇒ P(n+2)
Pn
125
Format of inductive definitions
126
Rule induction in general
To prove
I x =⇒ P x
by rule induction on I x
we must prove for every rule
[[ I a1 ; . . . ; I an ]] =⇒ I a
that P is preserved:
[[ I a1 ; P a1 ; . . . ; I an ; P an ]] =⇒ P a
127
Rule induction is absolutely central
! to (operational) semantics !
and the rest of this lecture course
128
Inductive_Demo.thy
129
Inductively defined sets
130
Chapter 5
131
10 Isar by example
11 Proof patterns
12 Streamlining Proofs
132
Apply scripts
• unreadable
• hard to maintain
• do not scale
No structure!
133
Apply scripts versus Isar proofs
134
A typical Isar proof
proof
assume formula 0
have formula 1 by simp
..
.
have formula n by blast
show formula n+1 by . . .
qed
proves formula 0 =⇒ formula n+1
135
Isar core syntax
proof = proof [method] step∗ qed
| by method
fact = name | . . .
136
10 Isar by example
11 Proof patterns
12 Streamlining Proofs
137
Example: Cantor’s theorem
lemma ¬ surj(f :: 0a ⇒ 0a set)
proof default proof: assume surj, show False
assume a: surj f
from a have b: ∀ A. ∃ a. A = f a
by(simp add: surj def)
from b have c: ∃ a. {x. x ∈ / f x} = f a
by blast
from c show False
by blast
qed
138
Isar_Demo.thy
139
Abbreviations
140
using and with
with facts
=
from facts this
141
Structured lemma statement
lemma
fixes f :: 0a ⇒ 0a set
assumes s: surj f
shows False
proof − no automatic proof step
have ∃ a. {x. x ∈ / f x} = f a using s
by(auto simp: surj def)
thus False by blast
qed
Proves surj f =⇒ False
but surj f becomes local fact s in proof.
142
The essence of structured proofs
143
Structured lemma statements
fixes x :: τ1 and y :: τ2 . . .
assumes a: P and b: Q . . .
shows R
144
10 Isar by example
11 Proof patterns
12 Streamlining Proofs
145
Case distinction
show R have P ∨ Q . . .
proof cases then show R
assume P proof
..
. assume P
..
show R . . . .
next show R . . .
assume ¬ P next
..
. assume Q
show R . . . ..
.
qed show R . . .
qed
146
Contradiction
show ¬ P show P
proof proof (rule ccontr)
assume P assume ¬P
.. ..
. .
show False . . . show False . . .
qed qed
147
←→
show P ←→ Q
proof
assume P
..
.
show Q . . .
next
assume Q
..
.
show P . . .
qed
148
∀ and ∃ introduction
show ∀ x. P(x)
proof
fix x local fixed variable
show P(x) . . .
qed
show ∃ x. P(x)
proof
..
.
show P(witness) . . .
qed
149
∃ elimination: obtain
have ∃ x. P(x)
then obtain x where p: P(x) by blast
..
. x fixed local variable
150
obtain example
151
Set equality and subset
show A = B show A ⊆ B
proof proof
show A ⊆ B . . . fix x
next assume x ∈ A
..
show B ⊆ A . . . .
qed show x ∈ B . . .
qed
152
Isar_Demo.thy
Exercise
153
10 Isar by example
11 Proof patterns
12 Streamlining Proofs
154
12 Streamlining Proofs
Pattern Matching and Quotations
Top down proof development
moreover
Raw proof blocks
155
Example: pattern matching
show formula 1 ←→ formula 2 (is ?L ←→ ?R)
proof
assume ?L
..
.
show ?R . . .
next
assume ?R
..
.
show ?L . . .
qed
156
?thesis
157
let
158
Quoting facts by value
By name:
have x0: ”x > 0” . . .
..
.
from x0 . . .
By value:
have ”x > 0” . . .
..
.
from ‘x>0‘ . . .
↑ ↑
back quotes
159
Isar_Demo.thy
160
12 Streamlining Proofs
Pattern Matching and Quotations
Top down proof development
moreover
Raw proof blocks
161
Example
lemma
(∃ ys zs. xs = ys @ zs ∧ length ys = length zs) ∨
(∃ ys zs. xs = ys @ zs ∧ length ys = length zs + 1)
proof ???
162
Isar_Demo.thy
163
When automation fails
Split proof up into smaller steps.
Or explore by apply:
have . . . using . . .
apply - to make incoming facts
part of proof state
apply auto or whatever
apply . . .
At the end:
• done
• Better: convert to structured proof
164
12 Streamlining Proofs
Pattern Matching and Quotations
Top down proof development
moreover
Raw proof blocks
165
moreover—ultimately
have P1 . . . have lab1 : P1 . . .
moreover have lab2 : P2 . . .
have P2 . . . ..
.
moreover ≈ have labn : Pn . . .
..
. from lab1 lab2 . . .
moreover have P ...
have Pn . . .
ultimately
have P . . .
With names
166
12 Streamlining Proofs
Pattern Matching and Quotations
Top down proof development
moreover
Raw proof blocks
167
Raw proof blocks
{ fix x1 . . . xn
assume A1 . . . Am
..
.
have B
}
proves [[ A1 ; . . . ; Am ]] =⇒ B
where all xi have been replaced by ?xi .
168
Isar_Demo.thy
moreover and { }
169
Proof state and Isar text
In general: proof method
Applies method and generates subgoal(s):
V
x1 . . . xn [[ A1 ; . . . ; Am ]] =⇒ B
How to prove each subgoal:
fix x1 . . . xn
assume A1 . . . Am
..
.
show B
Separated by next
170
10 Isar by example
11 Proof patterns
12 Streamlining Proofs
171
Isar_Induction_Demo.thy
Proof by cases
172
Datatype case analysis
datatype t = C1 ~τ | . . .
proof (cases "term")
case (C1 x1 . . . xk )
. . . xj . . .
next
..
.
qed
where case (Ci x1 . . . xk ) ≡
fix x1 . . . xk
assume |{z} Ci : term = (Ci x1 . . . xk )
| {z }
label formula
173
Isar_Induction_Demo.thy
174
Structural induction for nat
show P(n)
proof (induction n)
case 0 ≡ let ?case = P (0)
..
.
show ?case
next
case (Suc n) ≡ fix n assume Suc: P (n)
..
.. let ?case = P (Suc n)
..
show ?case
qed
175
Structural induction with =⇒
show A(n) =⇒ P(n)
proof (induction n)
case 0 ≡ assume 0: A(0)
..
. let ?case = P(0)
show ?case
next
case (Suc n) ≡ fix n
..
. assume Suc: A(n) =⇒ P(n)
A(Suc n)
..
. let ?case = P(Suc n)
show ?case
qed
176
Named assumptions
In a proof of
A1 =⇒ . . . =⇒ An =⇒ B
by structural induction:
In the context of
case C
we have
C.IH the induction hypotheses
C.prems the premises Ai
C C.IH + C.prems
177
A remark on style
178
13 Proof by Cases and Induction
Rule Induction
Rule Inversion
179
Isar_Induction_Demo.thy
Rule induction
180
Rule induction
inductive I :: τ ⇒ σ ⇒ bool show I x y =⇒ P x y
where proof (induction rule: I.induct)
rule1 : . . . case rule1
.. ...
.
rulen : . . . show ?case
next
..
.
next
case rulen
...
show ?case
qed
181
Fixing your own variable names
case (rulei x1 . . . xk )
182
Named assumptions
In a proof of
I . . . =⇒ A1 =⇒ . . . =⇒ An =⇒ B
by rule induction on I . . . :
In the context of
case R
we have
R.IH the induction hypotheses
R.hyps the assumptions of rule R
R.prems the premises Ai
R R.IH + R.hyps + R.prems
183
13 Proof by Cases and Induction
Rule Induction
Rule Inversion
184
Rule inversion
inductive ev :: nat ⇒ bool where
ev0: ev 0 |
evSS: ev n =⇒ ev(Suc(Suc n))
ev n =⇒ n = 0 ∨ (∃ k. n = Suc (Suc k) ∧ ev k)
185
Isar_Induction_Demo.thy
Rule inversion
186
Rule inversion template
from ‘ev n‘ have P
proof cases
case ev0 n=0
..
.
show ?thesis . . .
next
case (evSS k) n = Suc (Suc k), ev k
..
.
show ?thesis . . .
qed