我目前见过的所有coq类型都是Inductive的.简单来说,你需要在构造类型的时候,列举这个类型的若干种构造策略,例如:
Inductive nat : Type :=
| O
| S (n : nat).
于是所有的类型里的东西都会沿着构造策略进行构造. Prop也是同理.当你写一个命题的时候,你实际上写出的是一个类型(Type).而当你写一个证明的时候,你实际上就是在沿着Prop的定义去构造了其中的一个元素. 换言之,一个Prop是真的,当且仅当它作为Type存在一个能被我们构造出来的以它作为Type的元素(或evidence).
既然如此,类似集合论里的正则公理,Inductive自然也有类似的性质:简而言之,如若我们能逐步逆行构造的过程,就总能走到某个”尽头”.或者说:当我们举出某个Type里的某个元素的时候,这个元素总是”有限构造的”.既然如此,induction的正确性则不言自明了:对于每个”有限构造的元素”,我们都能通过归纳法,在有限步内给出一个证明.
Fixpoint build_proof
(P : nat -> Prop)
(evPO : P 0)
(evPS : forall n : nat, P n -> P (S n))
(n : nat) : P n :=
match n with
| 0 => evPO
| S k => evPS k (build_proof P evPO evPS k)
end.
Definition nat_ind_tidy := build_proof.
我之前写过一篇博客讲两步归纳,其实当时的我就并没有理解透彻induction,两步归纳完全可以有更简单的写法:
Definition nat_ind2 :
forall (P : nat -> Prop),
P 0 ->
P 1 ->
(forall n : nat, P n -> P (S(S n))) ->
forall n : nat , P n :=
fun P => fun P0 => fun P1 => fun PSS =>
fix f (n:nat) := match n with
| 0 => P0
| 1 => P1
| S (S n') => PSS n' (f n')
end.
然而,所谓强归纳法,即利用如果的所有都成立能推出成立,在此模板下当即失灵.原因无他:当我们传进一个的条件,并将函数index从变成的时候,coq无法通过这个条件来推测是的一个子结构.因此,coq没办法断言此构造是有限次的.
当然,我们仍然可以试着证明强归纳法,不过此时就必须途径本身的定义:
Lemma strong_ind (P : nat -> Prop) :
P 0 ->
(forall m, (forall k : nat, k < m -> P k) -> P m) ->
forall n, P n.
Proof.
intros P0 H n. enough (H0: forall p, p <= n -> P p).
- apply H0. apply le_n.
- induction n.
+ intros. destruct p.
* apply P0.
* inversion H0.
+ intros.
inversion H0.
* specialize (H (S n)). unfold lt in H. apply H. intros.
apply Sn_le_Sm__n_le_m in H2. apply IHn in H2. apply H2.
* apply IHn. apply H2.
Qed.
上面的策略为:将一个命题P n强化为forall m<=n,P m,而后者的归纳策略是明显的.
然而正当我以为我已经完全理解induction的时候,我看到了这个东西:
Inductive foo (X Y:Type) : Type :=
| bar (x:X)
| baz (y:Y)
| quux (f1:nat -> foo X Y).
Check foo_ind :
forall (X Y : Type) (P : foo X Y -> Prop),
(forall x : X, P (bar X Y x)) ->
(forall y : Y, P (baz X Y y)) ->
(forall f1 : nat -> foo X Y,
(forall n : nat, P (f1 n)) -> P (quux X Y f1)) ->
forall f2 : foo X Y, P f2.
Definition foo_ind2 :
forall (X Y : Type) (P : foo X Y -> Prop),
(forall x : X, P (bar X Y x)) ->
(forall y : Y, P (baz X Y y)) ->
(forall f1 : nat -> foo X Y,
(forall n : nat, P (f1 n)) -> P (quux X Y f1)) ->
forall f2 : foo X Y, P f2 :=
fun X => fun Y => fun P => fun PX => fun PY => fun PQ =>
fix fiq (f:foo X Y) := match f with
| bar _ _ x => (PX x)
| baz _ _ y => (PY y)
| quux _ _ ft => (PQ ft (fun n => (fiq (ft n))))
end.
我一下子就被震撼住了啊!它将一个类型本身嵌入函数中,并且继续嵌套.这种东西竟然也可以做归纳.并且这种函数并不是什么都可以递归的,例如下面的代码会爆出不满足”strictly positive”:
Fail Inductive foo2 (X Y:Type) : Type :=
| bar2 (x:X)
| baz2 (y:Y)
| quux2 (f1: foo2 X Y->nat).
原因是:coq的Inductive类型从定义之初就ban掉了任何可能产生bug的定义.上述代码的确会发生很严重的自指问题.众所周知,停机问题说一个program在运行前无法判定是否终止,而如果我们能定义一个program -> bool的东西,它就不能是一个program.
写到这里的时候,我突然想起来上学期学过的Haskell.当时我们学过了->运算符的Monad性质,如下:
instance Functor ((->) a) where
-- fmap :: (b->c)->(a->b)->(a->c)
fmap fbc fab = fbc.fab
instance Applicative ((->) a) where
-- pure :: c->(a->c)
-- <*> :: (a->(b->c))->(a->b)->(a->c)
pure tb = \ta -> tb
(<*>) fabc fab = \ta -> ((fmap (fabc ta) fab) ta)
instance Monad ((->) r) where
-- (>>=) :: (r->a)->(a->(r->b))->(r->b)
(>>=) fra farb = \tr -> ( farb (fra tr) tr )
换言之,当你做类型构造的时候,A->_应该是自然的,可_->A并非,原因是前者才是Monad而后者不是(你可以自行改一下上面定义就能看出为啥不是).
我想也许这里还有很多我暂时没想清楚的地方,比如monad到底是怎么和自指扯上关系的,甚至包括停机问题那里的具体构造我也没太想清楚(gpt说是Y combinator,但我暂时还不会这个),可能要等到后面的博客再想想了.