In Coq, “if then else” allows non-boolean first argument?

◇◆丶佛笑我妖孽 提交于 2019-12-11 03:42:39

问题


I read in a few tutorials that if a then b else c stands for match a with true => b | false => c end. However the former very strangely does not check the type of a, while the latter of course makes sure that a is a boolean. For instance,

Coq < Check if nil then 1 else 2.
if nil then 1 else 2
     : nat
where
?A : [ |- Type] 


Coq < Check match nil with true => 1 | false => 2 end.
Toplevel input, characters 33-38:
> Check match nil with true => 1 | false => 2 end.
>                                  ^^^^^
Error: Found a constructor of inductive type bool while
a constructor of list is expected.

Why is if ... then ... else ... allowing its first argument to be anything else than a non-boolean? Is there some overloading going on? (Locate "if". gives no result.)


回答1:


Let me quote the Coq Reference manual:

For inductive types with exactly two constructors and for pattern-matchings expressions which do not depend on the arguments of the constructors, it is possible to use a if ... then ... else ... notation. More generally, for an inductive type with constructors C1 and C2, we have the following equivalence:

if term [dep_ret_type] then term1 else term2 

is equivalent to

match term [dep_ret_type] with
| C1 _ ... _ => term1              (* we cannot bind the arguments *)
| C2 _ ... _ => term2
end

As you can see, the first constructor is treated as true value. Here is an example:

Definition is_empty {A : Type} (xs : list A) : bool :=
  if xs then true else false.


来源:https://stackoverflow.com/questions/47965062/in-coq-if-then-else-allows-non-boolean-first-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!