使用(* COMMENTS HERE *)进行注释
每个Coq命令都要加上.表示结束.
逻辑命题(Prop),数学集合(Set)和 抽象类型(Type)
Coq < Check True.
True : Prop
Coq < Check ((3=5)/\True).
3 = 5 /\ True : Prop
Coq < Check (fun x:nat => x = 3).
fun x : nat => x = 3 : nat -> Prop
Coq < Check (forall x:nat, x < 3 \/ (exists y:nat, x = y + 3)).
forall x : nat, x < 3 \/ (exists y : nat, x = y + 3) : Prop
Coq < Check (let f := fun x => (x * 3,x) in f 3).
let f := fun x : nat => (x * 3, x) in f 3 : nat * nat
Coq < Locate "_ * _".
Notation
"x * y" := Nat.mul x y : nat_scope (default interpretation)
"x * y" := prod x y : type_scope
Coq < Locate "_ <= _".
Notation
"n <= m" := le n m : nat_scope (default interpretation)
Coq < Locate "_ -> _".
Notation
"A -> B" := forall _ : A, B : type_scope (default interpretation)
Coq < Locate "_ /\ _".
Notation
"A /\ B" := and A B : type_scope (default interpretation)
Coq < Eval compute in let f := fun x => (x * 3, x) in f 3.
= (9, 3)
: nat * nat
三种函数定义方式:
Coq < Definition f (a b c d e:nat) := a*b*c*d*e.
Coq < Definition f (a:nat)(b:nat)(c:nat)(d:nat)(e:nat) := a*b*c*d*e.
Coq < Definition f := fun a:nat => fun b:nat => fun c:nat => fun d:nat => fun e:nat => a*b*c*d*e.
f is defined
Coq < Check f.
f
: nat -> nat -> nat -> nat -> nat -> nat
Coq < Eval compute in (f 1 2 3 4 5).
= 120
: nat
Coq < Definition example1 := fun x : nat => x*x+2*x+1.
example1 is defined
Coq < Definition example1 (x : nat) := x*x+2*x+1.
example1 is defined
Coq < Reset example1.
Coq < Print example1.
example1 = fun x : nat => x * x + 2 * x + 1
: nat -> nat
Argument scope is [nat_scope]
Coq < Require Import Bool.
Coq < Eval compute in if true then 3 else 5.
= 3
: nat
Coq < Search bool.
false: bool
true: bool
......
Coq < SearchAbout bool.
false: bool
true: bool
is_true: bool -> Prop
......
Coq < SearchPattern (_ + _ <= _ + _).
plus_le_compat_r: forall n m p : nat, n <= m -> n + p <= m + p
plus_le_compat_l: forall n m p : nat, n <= m -> p + n <= p + m
.....
Coq < SearchRewrite (_ + (_ - _)).
le_plus_minus: forall n m : nat, n <= m -> m = n + (m - n)
le_plus_minus_r: forall n m : nat, n <= m -> n + (m - n) = m
Nat.add_sub_assoc: forall n m p : nat, p <= m -> n + (m - p) = n + m - p
1 = S 0
2 = S ( S 0 )
3 = s ( S ( S 0 ) )
....
Coq < Definition is_zero (n:nat) :=
match n with
0 => true
| S p => false
end.
注 :match ...with ...end 模式匹配
Coq match n with 0 => 0 | S p => p + sum_n p end. Coq < Check 1::2::3::nil. 1 :: 2 :: 3 :: nil : list nat Coq < Check (nil : list nat). nil : list nat : list nat Coq < Eval compute in map (fun x => x + 3) (1::3::2::nil). = 4 :: 6 :: 5 :: nil : list nat Coq < Eval compute in map S (1::22::3::nil). = 2 :: 23 :: 4 :: nil : list nat Coq < Eval compute in let l := (1::2::3::nil) in l ++ map (fun x => x + 3) l. = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: nil : list nat --计算列表总和 Coq < Fixpoint sum_list l := match l with nil => 0 | n::tl => n + sum_list tl end. sum_list is defined sum_list is recursively defined (decreasing on 1st argument) Coq < Eval compute in sum_list (1::3::5::7::nil). = 16 : nat --把n插入到有序列表 并保持有序状态 Coq < Fixpoint insert n l := match l with nil => n::nil | a::tl => if leb n a then n::l else a::insert n tl end. insert is defined insert is recursively defined (decreasing on 2nd argument) Coq < Eval compute in insert 5 (4::5::6::7::nil). = 4 :: 5 :: 5 :: 6 :: 7 :: nil : list nat --排序列表 Coq < Fixpoint sort l := match l with nil => nil | a::tl => insert a (sort tl) end. sort is defined sort is recursively defined (decreasing on 1st argument) Coq < Eval compute in sort (1::4::3::22::5::16::7::nil). = 1 :: 3 :: 4 :: 5 :: 7 :: 16 :: 22 :: nil : list nat Coq < Inductive bin : Type := L : bin | N : bin -> bin -> bin. bin is defined bin_rect is defined bin_ind is defined bin_rec is defined Coq < Check N L (N L L). N L (N L L) : bin Coq < Check N. N : bin -> bin -> bin Coq < Inductive ttype :Type := N:ttype | M : nat -> ttype -> ttype | L : bool -> ttype -> ttype -> ttype . ttype is defined ttype_rect is defined ttype_ind is defined ttype_rec is defined