Coq学习——《Software-Foundation》Volume1.1-Basic
Basic
Intro
Functional style and the sense of "functional" in "functional programming"
- Pure without side effects, then all we need to undersatand about it is how it maps inputs to outputs(compute a mathematical objects).
- programs <-> mathematical objects
- well behavior <-> correct proofs
Data and Functions
New Types from Old
Apart from enumerate types (their definitions explicitly enumerate a finite set of elements called constructors
).cunstructors
是构造元素(like word),constructor expressions
是构造表示(like sentence),用来描述 constructor 到零个或者多个 constructor 或 constructor expressions 的映射。
Inductive
定义包含了一组 constructor expressions 并赋予名字。The definitions say which constructor expressions belong to the specific sets(like [rgb]).
- Start with an infinite set of constructors. (E.g. [red] [primary])
- Constructor expressions are formed by applying constructors to zero or more other constructors or constructor. (E.g. [red] that is zero [primary red] apply [primary] to another constructor [red] 1 param)
1 |
|
Definition
define functions just as we did for the enumerate types.
1 |
|
Modules
Aid in organizing large developments. Like name space. Definitions are referred to by names like [X.foo] instead of just [foo]. Limit the scope of definitions and reuse names.
1 |
|
Tuples
Multiple parameters canbe used to create a tuple type.
1 |
|
The [bits] constructor ([bits] is a constructor and bits ( have 4 bits) is a constructor is a constructor expression) acts as a wrapper for its contents.
match x with
pattern-matching
Numbers
Neither base 10 or base 2. We wish here to choose a representation that makes proofs simpler (even simpler than binary), so we use base 1, namely unary.
To represent unary numbers with a Coq datatype, we use two constructors. Here is the complete datatype definition.
1 |
|
Unary here is like counting the infinite numbers, but give infinite (only two) constructors expressions or inductive rules to describe it, which makes proofs simpler.
We've done so far is just to define a representation of numbers: a way of writing them down. The names [O] and [S] are arnotrary without any special meaning. Actually we could write essentially the same definition this way:
1 |
|
[nat] compare to [rgb] and [color](理解之前的rgb
拆开的原因,这样控制 primary 生成的链有限)。
Coq provides a tiny bit of built-in magic for parsing and printing nat (unary base 1 <->decimal base 10):
- Ordinary decimal numerals canbe used as an alternative to the "unary" notation.
- Coq prints numbers in decimal form by default.
For convinience.
1 |
|
Pattern matching with recursion. Introoduced with the keyword [Fixpoint] instead of [Definition].
Proof by Simplification
The proofs of examples claimed before were always the same: use [simpl] [reflexivity] to check that both sides contain identical values.
[reflexivity] is more powerful than I have expected. It automatically perfoem some simplification.
1 |
|
- [Theorem] instead of [Example] (for style)
- [forall n:nat] talks all nat.
- [intros] from the quantifier in the goal to a context of current assumptions.
A tactics is a command that is used between [Proof] and [Qed] to guide the process.
Proof by Rewriting
Without universal claim about all nat, talk about a specialized property only holds when [n = m].
1 |
|
After [intros] we get [n] [m] are arbitrary numbers. We can't just use simplification to prove (absolutely imposibble) but need to use the hypothesis.
1 |
|
With [rewrite <- / ->] tactics we can use previously proved theorems or hypothesis to rewrite our goal.
Proof by Case Analysis
Not everything can be proved by simple calculation and rewriting.
Block like this.
1 |
|
Reason: [n] and [n + 1] (Notation of plus n 1) lie on each side of [=?]. [equ] [plus] begins by performing match on first argument. Both unknown number [n]! Match fail.
The reason for this is that the definitions of both [eqb] and [+] begin by performing a [match] on their first argument. But here, the first argument to [+] is the unknown number [n] and the argument to [eqb] is the compound expression [n + 1]; neither can be simplified.
Tells Coq to consider seprately, the cases where [n = O] and where [n = S n'] is called destruct.
Exercise
1 |
|
If there are no constructor arguments that need names, we can just write [[]] to get the case analysis.
1 |
|
Fixpoints and Structural Recursion (Optional)
decreasing and structural recursion
Coq demands that some argument of every [Fixpoint] definition is "decreasing".
Tests
Not to intros. at the very begining.
All passed. 14/14.
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!