Coq中文手册.docx
《Coq中文手册.docx》由会员分享,可在线阅读,更多相关《Coq中文手册.docx(13页珍藏版)》请在冰豆网上搜索。
Coq中文手册
CoqHandBook
CCNTLab
ZhilingLuo
指令:
RequireImportlibs
Libs:
Arith
自然数数学库(默认)
ZArith
整数数学库
Logic
命题逻辑库(默认)
Bool
布尔库
Ensembles
集合库
Strings
字符串库
List
列表库
Reals
实数库
OpenScopelib_scope
lib_scope:
nat_scope
自然数数学库符(默认)
Z_scope
整数数学库符
String_scope
字符串库符
R_scope
实数库符
SectionsecEndsec
由section和End包围的是一个段,sec为段名。
Example:
SectionLoc.EndLoc.
Printident
打印定义,ident可以是本段定义的任何对象,类型以及本段所包括的库中定义的对象和类型。
Example:
PrintR.
Checkident
检查类型,check可以检查本段定义的对象和类型。
Example:
Variabletab:
Type.
Checktab.
Definitionident:
type:
=define
定义一个对象,可以选择性声明其类型,但是必须要有定义体,即:
=后的内容。
Example:
Definitionsetiod:
=nat.
Variableident:
type
声明一个局部变量,只需给出类型,不需要定义体。
当没有section包围时,顶级的局部变量等价于全局变量。
Example:
Variableck:
nat.
Parameterident:
type
声明一个全局变量,只需给出类型,不需要定义体。
Example:
Parameterck:
nat.
Inductiveident:
type:
=
|constru:
type1‐>typ2‐>type
|…….
定义一个归纳体,可以包含若干个构造子(constru),但是每个构造子的类型的最后必须是归纳体类型。
Example:
Inductiveexpr:
Type:
=
|Evar:
ident‐>expr
|Econst:
Z‐>expr
|Eadd:
expr‐>expr‐>expr
|Esub:
expr‐>expr‐>expr
.
Lemmaem:
type
定义一个猜想,其类型为type。
通常需要给出证明
Example:
Lemmanot_all_not_ex:
forallP:
U‐>Prop,~(foralln:
U,~Pn)‐>existsn:
U,Pn.
Fixpointfun(A:
type)(B:
type):
type:
=tail.
fixpoint可以定义递归函数,其中括号中的是传入参数。
在tail中常使用match结构可以对归纳结构进行拆分。
Example:
Fixpointtail_plusnm:
nat:
=
matchnwith
|0=>m
|Sn=>tail_plusn(Sm)end.
Computeexpr
归纳演算expr的值。
Example:
Compute1+1.
Structureexp:
={
dom1:
typ1;
dom2:
typ2;
dom3:
typ3:
=value
}
定义结构体,可以包含若干个域,域的名字不能重复,通过dom1exp来访问域的值,利用
Build_exp来构造。
Example:
Structureperson:
={
name:
string;
age:
nat
}.
CoercionRela:
typ1>‐>typ2
建立从typ1到typ2的强制子类型约束,Rela是之前定义的转换关系。
Example:
Variabledog:
Type.Variablehusky:
Type.
Variablebelong:
husky‐>dog.
Coercionbelong:
husky>‐>dog.
Searchkeyword
搜索关键字的定义
Example:
Searchnat.
SearchAboutkeyword
搜索所有关键字相关的定义
Example:
SearchAboutnat.
SearchPatternexp
搜索指定形式的定义式。
Example:
SearchAbout(_‐>nat).
特殊库的使用
String:
打开字符串库符之后字符串的表述:
“Ser”
Example:
Check“Ds”.
List
构造子有nil和n:
:
l两个,可用++连接两个列表。
Ensemble
需要先定义元素类型,然后可以声明集合。
Example:
VariableI:
Type.
Variableset1:
EnsembleI.
CheckUnionIset1set2.
CheckIntersectionIset1set2
证明相关
Proof.
开始证明
Qed.
证明结束
HintResolvelem
将lem加入auto库
证明策略:
intros/intro
用于goal里面有forall,~,P‐>
introsPH0.intros.
unfold
展开非递归函数
unfoldis_ture.unfoldis_trueinH.
rewriteH
将H的右边当作左边带入goal或者指定的目标
rewriteH.rewriteHinH0.
rewrite<‐H
将H的左边当作右边带入goal或指定目标
rewrite<‐H.rewrite<‐HinH0.
existsX
向goal或指定的目标指定existx:
T的目标
existsXinH0.existsX.
subst
在指定地方或goal里面删去可去的参数或者指定的参数
subst.substi.substiingoal.
symmetry
换位,将goal的等号两边换位置,用于将a=b变成b=a
symmetry.
reflexivity
自反,用于证明a=a
reflexivity.
simpl
规约goal或者指定的目标
simpl.simplinH.
discriminate
从已知式子中导出矛盾
discriminate.
destruct
拆掉指定假设中的或和与,当H含forall时加上一个对象p。
destructHas[H1H2].destructHas[H1|H2].destruct(Hp).
split
拆分goal中间的与
split.
left
指证goal中间的与的左边
left.
right
指证goal中间的与的右边
right.
applyH
应用指定假设到goal或者指定位置
apply.applyHinH0.
eapplyH
自动选择参数应用指定假设
eapplyH.
omega
需要omega库,自动证明加法和不等式
omega.
generalizeH
将H返回到goal中,产生forall
generalizeH.
inversionH
指出H的构造子
inversionH.
inductionn
在n上使用归纳法
inductionn.
casen
将goal中的n分构造子讨论
casen.
auto
自动证明
auto.
autowitharith.
tauto
自动证明布尔逻辑命题
tauto.
assert
指定一个新的假设
assert(H:
P‐>Q).
高级技法:
证明即程序:
Definitionhalf(n:
nat):
{p:
nat&{n=2*p}+{n=S(2*p)}}.
inductionn.
exists0;left;auto.
destructIHnas[x[Hx|Hx]].
existsx;right;auto.
exists(Sx);left;auto.
RequireImportArith.
rewriteHx;ring.
Defined.
PrintsigT.
Computehalf3.
CheckexistT.
Definitionhalf'(n:
nat):
nat:
=matchhalfnwithexistTp_=>pend.
附录1:
Ensemble定义:
LibraryCoq.Sets.Ensembles
SectionEnsembles.
VariableU:
Type.
DefinitionEnsemble:
=U‐>Prop.
DefinitionIn(A:
Ensemble)(x:
U):
Prop:
=Ax.
DefinitionIncluded(BC:
Ensemble):
Prop:
=forallx:
U,InBx‐>InCx.
InductiveEmpty_set:
Ensemble:
=.
InductiveFull_set:
Ensemble:
=
Full_intro:
forallx:
U,InFull_setx.
NB:
Thefollowingdefinitionbuilds‐inequalityofelementsinUasLeibnizequality.
ThismayhavetobechangedifwereplaceUbyaSetoidonUwithitsownequalityeqs,withIn_singleton:
(y:
U)(eqsxy)‐>(In(Singletonx)y).
InductiveSingleton(x:
U):
Ensemble:
=
In_singleton:
In(Singletonx)x.
InductiveUnion(BC:
Ensemble):
Ensemble:
=
|Union_introl:
forallx:
U,InBx‐>In(UnionBC)x
|Union_intror:
forallx:
U,InCx‐>In(UnionBC)x.
DefinitionAdd(B:
Ensemble)(x:
U):
Ensemble:
=UnionB(Singletonx).
InductiveIntersection(BC:
Ensemble):
Ensemble:
=
Intersection_intro:
forallx:
U,InBx‐>InCx‐>In(IntersectionBC)x.
InductiveCouple(xy:
U):
Ensemble:
=
|Couple_l:
In(Couplexy)x
|Couple_r:
In(Couplexy)y.
InductiveTriple(xyz:
U):
Ensemble:
=
|Triple_l:
In(Triplexyz)x
|Triple_m:
In(Triplexyz)y
|Triple_r:
In(Triplexyz)z.
DefinitionComplement(A:
Ensemble):
Ensemble:
=funx:
U=>~InAx.
DefinitionSetminus(BC:
Ensemble):
Ensemble:
=
funx:
U=>InBx/\~InCx.
DefinitionSubtract(B:
Ensemble)(x:
U):
Ensemble:
=SetminusB(Singletonx).
InductiveDisjoint(BC:
Ensemble):
Prop:
=
Disjoint_intro:
(forallx:
U,~In(IntersectionBC)x)‐>DisjointBC.
InductiveInhabited(B:
Ensemble):
Prop:
=
Inhabited_intro:
forallx:
U,InBx‐>InhabitedB.
DefinitionStrict_Included(BC:
Ensemble):
Prop:
=IncludedBC/\B<>C.
DefinitionSame_set(BC:
Ensemble):
Prop:
=IncludedBC/\IncludedCB.
ExtensionalityAxiom
AxiomExtensionality_Ensembles:
forallAB:
Ensemble,Same_setAB‐>A=B.
EndEnsembles.
HintUnfoldInIncludedSame_setStrict_IncludedAddSetminusSubtract:
sets
v62.
HintResolveUnion_introlUnion_introrIntersection_introIn_singleton
Couple_lCouple_rTriple_lTriple_mTriple_rDisjoint_introExtensionality_Ensembles:
setsv62.