Coq的强制类型转换 Coercion
由于布尔谓词输出仍为bool类型,当我们需要Prop类型时,每次都需要在后面加上=true,这样就很麻烦,比如
Lemma leq11 : (1<=1) = true.
我们可以使用强制类型转换将其转换为Prop类型
在plugin.ssrbool里有这么一条命令,可以将bool类型转化为适合的三大基本类型之一
Coercion is_true : bool >-> Sortclass.
上述引理即可简写成
Lemma leq11 : 1<=1.
顺哥说还可以这样写
Coercion is_true (t:bool) := match t with
|true => True
|false => False end.
这两种强制类型转换的写法尝试如下
Parameter contain : bool -> bool -> bool.
Notation "p |> q" := (contain p q)(at level 10).
Coercion is_true : bool >-> Sortclass.
(* Coercion is_true (t:bool) := match t with
|true => True
|false => False end. *)
Axiom L1 : forall p q, (p |> (q |> p)).
Hint Resolve L1.
Lemma TTT p q: (p |> (q |> p)).
Proof. auto. Qed.
这两种写法都以将L1写进auto里面用来自动证明引理TTT。
但是发现还有一些问题:
如果将L1中的=true加上去,在证明引理TTT的时候就不能用auto了,但是第一种写法用apply L1仍然可以。
Parameter contain : bool -> bool -> bool.
Notation "p |> q" := (contain p q)(at level 10).
Coercion is_true : bool >-> Sortclass.
Axiom L1 : forall p q, (p |> (q |> p)) = true.
Hint Resolve L1.
Lemma TTT p q: (p |> (q |> p)).
Proof. (* auto. *)apply L1. Qed.
但如果把强制类型转化写成第二种形式,那么连apply L1都用不了了。
Parameter contain : bool -> bool -> bool.
Notation "p |> q" := (contain p q)(at level 10).
Coercion is_true (t:bool) := match t with
|true => True
|false => False end.
Axiom L1 : forall p q, (p |> (q |> p)) = true.
Hint Resolve L1.
Lemma TTT p q: (p |> (q |> p)).
Proof. (* auto. *)(* apply L1. *)
大概的稍微总结一下,大概Hint Resolve命令,可以将某个经过强制类型转换的引理(公理)加入auto(eauto)的自动推理策略集里面,但是必须是结构完全一样才可以匹配成功使用。
如果公理中有项的类型是未经过Coercion的形式,而需要证明的定理是经过Coercion的形式,此时二者结构看起来就不一样了,Coq的auto策略也不会成功。但是第一种Coercion强制转换时,
仍然可以成功用apply L1这种命令,但第二种Coercion连apply L1都会使用失败。

浙公网安备 33010602011771号