We have seen…
- propositions: factual claims
- equality propositions (
e1 = e2
) - implications (
P → Q
) - quantified propositions (
∀ x, P
)
- equality propositions (
- proofs: ways of presenting evidence for the truth of a proposition
Prop
type
1 | Check 3 = 3. (* ===> Prop. A provable prop *) |
Prop
is first-class entity we can
- name it
- parametrized!
1 | Definition is_three (n : nat) : Prop := |
Properties
In Coq, functions that return propositions are said to define properties of their arguments.
1 | Definition injective {A B} (f : A → B) := |
The equality operator =
is also a function that returns a Prop
. (property: equality)
1 | Check @eq. (* ===> forall A : Type, A -> A -> Prop *) |
Theroems are types, and proofs are existentials.
Slide Q&A - 1.
Prop
Prop
Prop
- Not typeable
nat -> nat
nat -> Prop
- (3)
think that Big Lambda (the type abstraction) works at type level, accepts type var, substitute and reture a type.forall
in Coq is same (the concrete syntax) and only typecheck with Type
or its subtype Set
& Prop
.
1 | Check (∀n:nat, S (pred n)). (* not typeable *) |
Logical Connectives
noticed that connectives symbols are “unicodize” in book and spacemacs.
Conjuction (logical and)
and
is just binary Prop -> Prop -> Prop
and associative.
1 | Print "/\". |
and introduction
1 | Lemma and_intro : forall A B : Prop, A -> B -> A /\ B. |
To prove a conjunction,
- use the
split
tactic. It will generate two subgoals,- or use
apply and_intro.
, which match the conclusion and give its two premises as your subgoals.
and elimination
if we already have a proof of and
, destruct
can give us both.
1 | Lemma and_example2' : |
Instead of packing into conjunction ∀n m : nat, n = 0 ∧ m = 0 → n + m = 0.
why not two separate premises? ∀n m : nat, n = 0 -> m = 0 → n + m = 0.
Both are fine in this case but conjunction are useful as intermediate step etc.
Coq Intensive Q: why
destruct
can work onand
? isand
inductively defined?
A: Yes.
Disjunction (locial or)
or elimination
We need do case analysis (either P
or Q
should be able to prove the theroem separately!)
1 | Lemma or_example : |
or introduction
When trying to establish (intro into conclusion) an or
, using left
or right
to pick one side to prove is sufficient.
1 | Lemma or_intro : forall A B : Prop, A -> A \/ B. |
Falsehood and negation
False?
Recall the princple of explosion: it asserts that, if we assume a contradiction, then any other proposition can be derived.
we could define ¬ P
(“not P”) as ∀ Q, P → Q.
.
Coq actually makes a slightly different (but equivalent) choice, defining
¬ P as P → False
, whereFalse
is a specific contradictory proposition defined in the standard library.
1 | Definition not (P:Prop) := P → False. |
Prove the princple of explosion:
1 | Theorem ex_falso_quodlibet : forall (P:Prop), |
Inequality
1 | Notation "x <> y" := (~(x = y)). |
Same as SML and OCaml (for structural equality, OCaml uses !=
for physical equality.)
Proving of negation (or how to prove ¬P
)
thinking about as unfold not
, i.e. P -> False
.
so you have an assumptions P
that could be intros HP.
and the residual goal would be simply False
.
which is usually proved by some kind of contradiction in hypotheses with tactics discriminate.
or contradiction.
1 | Theorem contradiction_implies_anything : forall P Q : Prop, |
Tactic exfalso.
If you are trying to prove a goal that is nonsensical (e.g., the goal state is
false = true
), applyex_falso_quodlibet
to change the goal toFalse
. This makes it easier to use assumptions of the form¬P
that may be available in the context — in particular, assumptions of the formx≠y
.
Since reasoning with
ex_falso_quodlibet
is quite common, Coq provides a built-in tactic,exfalso
, for applying it.
Slide Q&A - 2
?
unfold
is implicit
- only
destruct
(if we considerintros
destruct is alsodestruct
.), ?unfold
- none (?
unfold
) left.
destruct
,unfold
,left
andright
discrinminate
(orinversion
)
Truth
1 | Lemma True_is_true : True. |
I : True
is a predefined Prop…
Logical Equivalence
if and only if is just the conjunction of two implications. (so we need split
to get 2 subgoals)
1 | Definition iff (P Q : Prop) := (P → Q) ∧ (Q → P). |
rewrite
andreflexivity
can be used with iff statements, not just equalities.
Existential Quantification
To prove a statement of the form ∃x, P
, we must show that P
holds for some specific choice of value for x
,
known as the witness of the existential.
So we explicitly tell Coq which witness t
we have in mind by invoking exists t
.
then all occurences of that “type variable” would be replaced.
Intro
1 | Lemma four_is_even : exists n : nat, 4 = n + n. |
Elim
Below is an interesting question…by intros and destruct we can have equation n = 4 + m
in hypotheses.
1 | Theorem exists_example_2 : forall n, |
Programming with Propositions
Considering writing a common recursive is_in
for polymorphic lists.
(Though we dont have a polymorphic =?
(eqb
) defined yet)
1 | Fixpoint is_in {A : Type} (x : A) (l : list A) : bool := |
Similarly, we can write this function but with disjunction and return a Prop
!
so we can write function to generate/create statements/propositions! (thx for the idea Prop is first-class)
1 | Fixpoint In {A : Type} (x : A) (l : list A) : Prop := |
The whole thing I understood as a type operator (function in type level) and it’s recursive!
Coq dare to do that becuz its total, which is guarded by its termination checker.
un-total PL, if having this, would make its type system Turing Complete (thus having Halting Problem).
(Recursive Type like ADT/GADT in ML/Haskell is a limited form of recursion allowing no arbitray recursion.)
In_map
I took this one since it’s like a formal version of Property-based Tests!.
1 | Lemma In_map : |
Q & A:
eq
is just another inductively defined and doesn’t have any computational content. (satisfication)- Why use
Prop
instead ofbool
? See reflection below.
Drawbacks
In particular, it is subject to Coq’s usual restrictions regarding the definition of recursive functions,
e.g., the requirement that they be “obviously terminating.”
In the next chapter, we will see how to define propositions inductively,
a different technique with its own set of strengths and limitations.
Applying Theorems to Arguments.
Check some_theorem
print the statement!
1 | Check plus_comm. |
Coq prints the statement of the
plus_comm
theorem in the same way that it prints the type of any term that we ask it to Check. Why?
Hmm…I just noticed that!!
But I should notice because Propositions are Types! (and terms are proof)
Proof Object
proofs as first-class objects.
After Qed.
, Coq defined they as Proof Object and the type of this obj is the statement of the theorem.
Provable: some type is inhabited by some thing (having terms).
So I guess when we apply theorems, Coq implicitly use the type of the Proof Object. (it’s already type abstraction)
…we will get to there later at ProofObject chapter.
Apply theorem as function
rewrite
select variables greedily by default
1 | Lemma plus_comm3_take3 : |
x y z
were some type var and instantiated to values by intros
, e.g. x, y, z:nat
but we can explicilty pass in to plus_comm
, which is a forall type abstraction! (Δ n m. (eq (n + m) (m + n))
)
there must be something there in Proof Object so we can apply values to a type-level function
Coq vs. Set Theory
Coq’s logical core, the Calculus of Inductive Constructions, is a metalanguage for math, but differs from other foundations of math e.g. ZFC Set Theory
Functional Extensionality
1 | (∀x, f x = g x) → f = g |
In common math practice, two functions
f
andg
are considered equal if they produce the same outputs.
This is known as the principle of functional extensionality.
Informally speaking, an “extensional property” is one that pertains to an object’s observable behavior.
https://en.wikipedia.org/wiki/Extensionality
https://en.wikipedia.org/wiki/Extensional_and_intensional_definitions?
This is not built-in Coq, but we can add them as Axioms.
Why not add everything?
- One or multiple axioms combined might render inconsistency.
- Code extraction might be problematic
Fortunately, it is known that adding functional extensionality, in particular, is consistent.
consistency:
a consistent theory is one that does not contain a contradiction.
Adding Axioms
1 | Axiom functional_extensionality : forall {X Y: Type} |
It’s like Admitted.
but alerts we’re not going to fill in later.
Exercise - Proving Reverse with app
and with cons
are fn-exensionally equivalent.
1 | Fixpoint rev_append {X} (l1 l2 : list X) : list X := |
BTW, this version is tail recursive
becuz the recursive call is the last operation needs to performed.
(In rev
i.e. rev t ++ [h]
, recursive call is a argument of function ++
and we are CBV.)
1 | Lemma tr_rev_correct : forall X, @tr_rev X = @rev X. |
Propositions and Booleans
We’ve seen two different ways of expressing logical claims in Coq:
- with booleans (of type
bool
), ; computational way- with propositions (of type
Prop
). ; logical way
There’re two ways to define 42 is even:
1 | Example even_42_bool : evenb 42 = true. |
We wanna show there are interchangable.
1 | Theorem even_bool_prop : ∀n, |
In view of this theorem, we say that the
boolean computationevenb n
reflects the truth of the proposition∃ k, n = double k
.
We can futhur general this to any equations representing as bool
or Prop
:
1 | Theorem eqb_eq : ∀n1 n2 : nat, |
Notes on Computability.
However, even they are equivalent from a purely logical perspective,
they may not be equivalentoperationally
.
1 | Fail Definition is_even_prime n := |
=
, or eq
, as any function in Coq, need to be computable and total. And we have no way to tell whether any given proposition is true or false. (…We can only naturally deduce things are inductively defined)
As a consequence, Prop in Coq does not have a universal case analysis operation telling whether any given proposition is true or false, since such an operation would allow us to write non-computable functions.
Although general non-computable properties cannot be phrased as boolean computations, it is worth noting that even many computable properties are easier to express using Prop than bool, since recursive function definitions are subject to significant restrictions in Coq.
E.g. Verifying Regular Expr in next chapter.
Doing the same with
bool
would amount to writing a full regular expression matcher (so we can execute the regex).
Proof by Reflection!
1 | (* Logically *) |
As an extreme example, the Coq proof of the famous 4-color theorem uses reflection to reduce the analysis of hundreds of different cases to a boolean computation.
Classical vs. Constructive Logic
…
Future Schedule
Proof got messier!
Lean on your past PLT experience
As discussion leader
- having many materials now
- selected troublesome and interesting ones
- 本文标题:「SF-LC」6 Logic
- 创建时间:2021-03-28 00:00:00
- 本文链接:posts/ba6f.html
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!