在 Coq 中描述递归类型


Describing a recursive type in Coq

我想用以下规则定义抽象人类个体的类型:

  • 一个人是男性还是女性
  • 一个人有一个与自己不同性别的配偶,其配偶的配偶应该是自己。在数学术语中,forall h : Human, spouse (spouse h) = h

所以预计人类会有 Sex -> Human -> Human 的类型。

1
2
Inductive Sex := male | female.
Definition Human (sex_ : Sex) (spouse_ : Human) : Human := ???.

顺便说一句,我需要在下面定义一组函数:

1
2
3
man : Human -> Prop
woman : Human -> Prop
spouse : Human -> Human

我应该如何在 Coq 中描述它们?此外,我可以通过什么方式定义人类个体的实例或成对定义它们?非常感谢。


你可以从外部证明配偶的属性,这里是 traditional。不过我承认,它并没有我想要的那么好。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Inductive Sex := male | female.

Definition other (s:Sex) :=
  match s with
  | male => female
  | female => male
  end.

Inductive Human := stephen | stephanie | robert | roberta.

Definition sex (h:Human) : Sex :=
  match h with  
  | stephen => male
  | stephanie => female
  | robert => male
  | roberta => female
 end.

Definition spouse' (h:Human) : {h' : Human | sex h' = other (sex h)}.
  refine (match h with  
  | stephen   => exist _ stephanie _
  | robert    => exist _ roberta _
  | stephanie => exist _ stephen _
  | robertra  => exist _ robert _
  end); reflexivity.
Defined.

Definition man h := sex h = male.
Definition woman h := sex h = male.

Definition spouse (h:Human) := let ' exist h' _ := spouse' h in h'.

Theorem traditional (h:Human) : spouse (spouse h) = h.
  compute.
  destruct h; reflexivity.
Qed.


假设您不介意您的人口数量是有限的,(1) 实现一个有限图,(2) 将人类定义为男性或女性并给他们一个 id(例如,a nat), (3) 将这些人连接在一个图中。如果您对连接人们的某些方式不满意,请定义一个谓词 acceptable : graph human -> Prop 并坚持您认为可以接受的人群子集 {g1 : graph human | acceptable g1}。您还需要定义 marry : forall h1 h2 : human, male h1 -> female h2 -> {g1 | acceptable g1} -> {g1 | acceptable g1}.

如果您只想谈论人口,无论他们是什么,就像我们可以谈论群体或领域一样,无论他们是什么,您都可以将人口定义为任何具有性别的一夫一妻制和异性恋人群。

1
2
3
4
5
Inductive sex : Set := male : sex | female : sex.

Definition population : Type := {human : Type & {gender : human -> sex & {spouse : human -> human | forall h1, spouse (spouse h1) = h1 /\\ gender (spouse h1) <> gender h1}}}.

Definition human : population -> Type := @projT1 _ _.

布尔值可以构成一个种群。

1
2
3
4
5
6
7
8
9
10
11
Definition gender (b1 : bool) : sex :=
  match b1 with
  | true => male
  | false => female
  end.

Theorem acceptable : forall b1, negb (negb b1) = b1 /\\ gender (negb b1) <> gender b1.
Proof. destruct b1; repeat (firstorder || simpl || congruence). Qed.

Definition boolean_population : population.
Proof. unfold population. repeat refine (existT _ _ _). apply acceptable. Defined.

假设你已经证明了人口中任何人的某些情况。

1
2
Conjecture P : forall p1, human p1 -> Prop.
Conjecture fact : forall p1 h1, P p1 h1.

你也证明了它的布尔值。

1
2
Theorem boolean_fact : forall b1, P boolean_population b1.
Proof. apply fact. Qed.