简体   繁体   English

Coq - 在if ... then ... else中使用Prop(True | False)

[英]Coq - use Prop (True | False) in if … then … else

I'm kind of new to Coq. 我是Coq的新手。

I'm trying to implement a generic version of insertion sort. 我正在尝试实现插入排序的通用版本。 I'm implementing is as a module that takes a Comparator as a parameter. 我正在实现的是将Comparator作为参数的模块。 This Comparator implements comparison operators (such as is_eq, is_le, is_neq, etc.). 此Comparator实现比较运算符(例如is_eq,is_le,is_neq等)。

In insertion sort, in order to insert, I must compare two elements in the input list, and based on the result of the comparison, insert the element into the correct location. 在插入排序中,为了插入,我必须比较输入列表中的两个元素,并根据比较结果将元素插入到正确的位置。

My problem is that the implementations of the comparison operators are type -> type -> prop (i need them to be like this for implementation of other types/proofs). 我的问题是比较运算符的实现是type -> type -> prop (我需要它们像这样实现其他类型/证明)。 I'd rather not create type -> type -> bool versions of the operators if it can be avoided. 如果可以避免,我宁愿不创建type -> type -> bool版本的运算符。

Is there any way to convert a True | False 有没有办法转换True | False True | False prop to a bool for use in a if ... then ... else clause? if ... then ... else子句中使用bool的True | False支柱?

The comparator module type: 比较器模块类型:

Module Type ComparatorSig.

  Parameter X: Set.
  Parameter is_eq : X -> X -> Prop.
  Parameter is_le : X -> X -> Prop.
  Parameter is_neq :  X -> X -> Prop.

  Infix "=" := is_eq (at level 70).
  Infix "<>" := (~ is_eq) (at level 70).
  Infix "<=" := is_le (at level 70).

  Parameter eqDec : forall x y : X, { x = y } + { x <> y }.

  Axiom is_le_trans : forall (x y z:X), is_le x y -> is_le y z -> is_le x z.

End ComparatorSig.

An implementation for natural numbers: 自然数的实现:

Module IntComparator <: Comparator.ComparatorSig.
  Definition X := nat.
  Definition is_le x y := x <= y.
  Definition is_eq x y := eq_nat x y.
  Definition is_neq x y:= ~ is_eq  x y.

  Definition eqDec := eq_nat_dec.

  Definition is_le_trans := le_trans.
End IntComparator.

The insertion part of insertion sort: 插入排序的插入部分:

  Fixpoint insert (x : IntComparator .X) (l : list IntComparator .X) :=
    match l with
      | nil => x :: nil
      | h :: tl => if IntComparator.is_le x h then x :: h :: tl else h :: (insert x tl)
    end.

(obviously, the insert fixpoint doesn't work, since is_le is returns Prop and not bool). (显然,插入修复点不起作用,因为is_le返回Prop而不是bool)。

Any help is appreciated. 任何帮助表示赞赏。

You seem to be a bit confused about Prop. 你似乎对Prop有点困惑。

is_le xy is of type Prop, and is the statement x is less or equal to y . is_le xy是Prop类型,并且是x is less or equal to y的语句。 It is not a proof that this statement is correct. 这并不能证明这种说法是正确的。 A proof that this statement is correct would be p : is_le xy , an inhabitant of that type (ie a witness of that statement's truth). 这个陈述是正确的证据是p : is_le xy ,这种类型的居民(即该陈述的真相的证人)。

This is why it does not make much sense to pattern match on IntComparator.is_le xh . 这就是为什么在IntComparator.is_le xh上模式匹配没有多大意义。

A better interface would be the following: 更好的界面如下:

Module Type ComparatorSig.

  Parameter X: Set.
  Parameter is_le : X -> X -> Prop.
  Parameter is_le_dec : forall x y, { is_le x y } + { ~ is_le x y }.

In particular, the type of is_le_dec is that of a decision procedure for the property is_le , that is, it returns either a proof that x <= y , or a proof that ~ (x <= y) . 特别是, is_le_dec的类型是属性is_le的决策过程的is_le ,也就是说,它返回x <= y的证明或~ (x <= y)的证明。 Since this is a type with two constructors, you can leverage the if sugar: 由于这是一个带有两个构造函数的类型,因此您可以利用if糖:

... (if IntComparator.is_le_dec xh then ... else ...) ...

This is, in some sense, an enhanced bool , which returns a witness for what it is trying to decide. 从某种意义上说,这是一个强化的bool ,它会为它所要决定的东西返回一个见证。 The type in question is called sumbool and you can learn about it here: http://coq.inria.fr/library/Coq.Init.Specif.html#sumbool 有问题的类型叫做sumbool ,你可以在这里了解它: httpsumbool


In general, it does not make sense to talk about True or False in executing code. 通常,在执行代码时谈论TrueFalse是没有意义的。

First, because these live in Prop , which means that they cannot be computationally relevant as they will be erased. 首先,因为它们存在于Prop ,这意味着它们在计算上Prop ,因为它们将被删除。

Second, because they are not the only inhabitants of Prop . 其次,因为他们不是Prop的唯一居民。 While true and false are the only values of type bool , which implies you can pattern-match, the type Prop contains an infinite number of elements (all the statements you can imagine), thus it makes no sense to try and pattern-match on a element of type Prop . 虽然truefalsebool类型的唯一值,这意味着你可以模式匹配,但Prop类型包含无限数量的元素(你可以想象的所有语句),因此尝试和模式匹配是没有意义的。 Prop类型的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM