basic inctive logic relation list inp会有
文章目录
一、basic
1.1 数据与函数
枚举类型
Inductive 定义一个数据集合
Inductive day : Type :=
| monday
| tuesday
| wednesday
| thursday
| friday
| saturday
| sunday.
Definition 可以写一些操作函数
Definition next_weekday (d:day) : day :=
match d with
| monday ⇒ tuesday
| tuesday ⇒ wednesday
| wednesday ⇒ thursday
| thursday ⇒ friday
| friday ⇒ monday
| saturday ⇒ monday
| sunday ⇒ monday
end.
在 Coq 中检验的方式一共有三种:
第一,用 Compute 指令来计算包含 next_weekday 的复合表达式
Compute (next_weekday friday).
Compute (next_weekday (next_weekday saturday)).
第二,将期望的结果写成 Coq 的示例
Example test_next_weekday:
(next_weekday (next_weekday saturday)) = tuesday.
Proof. simpl. reflexivity. Qed.
这段代码基本上可以读作“若等式两边的求值结果相同,该断言即可得证”
第三,我们可以让 Coq 从 Definition 中提取出用其它更加常规的编程语言编写的程序
布尔值
Inductive bool : Type :=
| true
| false.
布尔值的函数可按照同样的方式来定义
Definition negb (b:bool) : bool :=
match b with
| true ⇒ false
| false ⇒ true
end.
Definition andb (b1:bool) (b2:bool) : bool :=
match b1 with
| true ⇒ b2
| false ⇒ false
end.
Definition orb (b1:bool) (b2:bool) : bool :=
match b1 with
| true ⇒ true
| false ⇒ b2
end.
以下四个单元测试演示了多参数应用的语法
Example test_orb1: (orb true false) = true.
Proof. simpl. reflexivity. Qed.
Example test_orb2: (orb false false) = false.
Proof. simpl. reflexivity. Qed.
Example test_orb3: (orb false true) = true.
Proof. simpl. reflexivity. Qed.
Example test_orb4: (orb true true) = true.
Proof. simpl. reflexivity. Qed
Notation 能为既有的定义赋予新的中缀记法
Notation "x && y" := (andb x y).
Notation "x || y" := (orb x y).
Example test_orb5: false || false || true = true.
Proof. simpl. reflexivity. Qed.
类型
Check 指令会让 Coq 显示一个表达式的类型
Check true.
(* ===> true : bool *)
像 negb 这样的函数本身也有类型,被称为函数类型,用带箭头的类型表示
Check negb
: bool → bool.
Check andb
: bool → bool → bool.
由旧类型构造新类型
我们之前定义的枚举类型,每个元素都只是无参数构造子
下面的类型定义,会让其中一个构造子接受一个参数
Inductive rgb : Type :=
| red
| green
| blue.
Inductive color : Type :=
| black
| white
| primary (p : rgb).
最后一行表示若 p 是属于 rgb 的构造子表达式,则 primary p(构造子 primary 应用于参数 p)是属于集合 color 的构造子表达式
定义一个关于color的函数
Definition monochrome (c : color) : bool :=
match c with
| black ⇒ true
| white ⇒ true
| primary p ⇒ false
end.
Definition isred (c : color) : bool :=
match c with
| black ⇒ false
| white ⇒ false
| primary red ⇒ true
| primary _ ⇒ false
end.
primary _ 是构造子 primary 应用到除 red 之外的任何 rgb 构造子上
元组
定义一个由四位字节组成的元组
Inductive bit : Type :=
| B0
| B1.
Inductive nybble : Type :=
| bits (b0 b1 b2 b3 : bit).
Check (bits B1 B0 B1 B0)
: nybble.
Definition all_zero (nb : nybble) : bool :=
match nb with
| (bits B0 B0 B0 B0) ⇒ true
| (bits _ _ _ _) ⇒ false
end.
数值
我们把这个部分放在一个模块中,这样我们自己对自然数的定义就不会干扰标准库中的自然数
Module NatPlayground.
大写字母O表示零,当S构造函数应用于自然数n的表示时,结果是n+1,其中S代表后继(successor),可被放在一个自然数之前产生另一个自然数
Inductive nat : Type :=
| O
| S (n : nat).
0 → O,1 → S O,2 → S(S O),3 → S(S(S O)),以此类推
定义自然数的前趋函数
Definition pred (n : nat) : nat :=
match n with
| O ⇒ O
| S n' ⇒ n'
end.
定义自然数减法函数
Definition minustwo (n : nat) : nat :=
match n with
| O ⇒ O
| S O ⇒ O
| S (S n') ⇒ n'
end.
Compute (minustwo 4).
(* ===> 2 : nat *)
End NatPlayground.
Coq 会默认将自然数打印为十进制形式
Check (S (S (S (S O)))).
(* ===> 4 : nat *)
递归
关键字 Fixpoint 可用于定义递归函数
比如判断自然数是否为偶数
Fixpoint evenb (n:nat) : bool :=
match n with
| O ⇒ true
| S O ⇒ false
| S (S n') ⇒ evenb n'
end.
判断自然数是否为奇数
Definition oddb (n:nat) : bool :=
negb (evenb n).
Example test_oddb1: oddb 1 = true.
Proof. simpl. reflexivity. Qed.
Example test_oddb2: oddb 4 = false.
Proof. simpl. reflexivity. Qed.
递归多参数函数
Module NatPlayground2.
Fixpoint plus (n : nat) (m : nat) : nat :=
match n with
| O ⇒ m
| S n' ⇒ S (plus n' m)
end
两个自然数的乘法
Fixpoint mult (n m : nat) : nat :=
match n with
| O ⇒ O
| S n' ⇒ plus m (mult n' m)
end.
Example test_mult1: (mult 3 3) = 9.
Proof. simpl. reflexivity. Qed.
两个自然数相减
Fixpoint minus (n m:nat) : nat :=
match n, m with
| O , _ ⇒ O
| S _ , O ⇒ n
| S n', S m' ⇒ minus n' m'
end.
End NatPlayground2.
自然数的幂
Fixpoint exp (base power : nat) : nat :=
match power with
| O ⇒ S O
| S p ⇒ mult base (exp base p)
end.
比较两个自然数是否相等
Fixpoint eqb (n m : nat) : bool :=
match n with
| O ⇒ match m with
| O ⇒ true
| S m' ⇒ false
end
| S n' ⇒ match m with
| O ⇒