Coq学习笔记

  1. 注释

使用(* COMMENTS HERE *)进行注释

  1. 分隔符

每个Coq命令都要加上.表示结束.

 

逻辑命题(Prop),数学集合(Set)和 抽象类型(Type)

 

  • Check 检测表达式是否合理/以及表达式的类型

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

 

  • Locate 找到隐藏在符号[ *,>,<,-> .... ]后面的函数

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)

 

  • Eval 计算表达式

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

 

 

  • Definition 定义新的变量或者对象

 

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

 

  • Reset 删除已定义的变量或者对象

Coq < Reset example1.

 

  • Print 打印已定义的变量或者对象

Coq < Print example1.

example1 = fun x : nat => x * x + 2 * x + 1

: nat -> nat

Argument scope is [nat_scope]

 

  • Require Import [模块名]. 模块导入

Coq < Require Import Bool.

 

  • if ...then ...else ... 条件语句

Coq < Eval compute in if true then 3 else 5.

= 3

: nat

 

  • Search SearchAbout SearchPattern SearchRewrite

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

 

  • 0 与 S 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 模式匹配

 

  • Fixpoint 用于定义递归函数

Coq

match n with

0 => 0

| S p => p + sum_n p

end.

 

  • List 列表计算

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

 

  • Inductive 定义新的类型

 

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

你可能感兴趣的:(形式化方法,Coq)