在Fixpoint Coq定义中使用lambda

时间:2015-09-04 00:21:17

标签: recursion coq dependent-type

我试图在递归定义中使用List.map,使用当前定义的递归函数作为参数在列表上进行映射。有可能吗?我可以定义自己的递归修复点定义,而不是使用map,但我有兴趣在这里使用map

Require Import Coq.Lists.List.
Import ListNotations.

Inductive D: nat -> Type := | D0 (x:nat): D x.

Inductive T: nat -> nat -> Type :=
| T0 {i o} (foo:nat): T i o
| T1 {i o} (foo bar:nat) : T i o -> T i o.

Fixpoint E {i o: nat} (t:T i o) (x:nat) (d:D i): option (D o) 
  :=
    (match t in @T i o
           return D i -> option (D o)
     with
     | T0 _ _ foo => fun d0 => None
     | T1 _ _ foo bar t' =>
       fun d0 =>
         let l := List.map (fun n => E t' x d0)  [ 1 ; 2 ; 3 ] in
         let default := Some (D0 o) in
         List.hd default l
     end) d.

上面的例子是人为的,但是证明了这个问题。错误消息:

The term "l" has type "list (option (D n0))"
 while it is expected to have type "list (option (D o))".

1 个答案:

答案 0 :(得分:2)

您只需要绑定T1模式上的名称:

Require Import Coq.Lists.List.
Import ListNotations.

Inductive D: nat -> Type := | D0 (x:nat): D x.

Inductive T: nat -> nat -> Type :=
| T0 {i o} (foo:nat): T i o
| T1 {i o} (foo bar:nat) : T i o -> T i o.

Fixpoint E {i o: nat} (t:T i o) (x:nat) (d:D i): option (D o)
  :=
    (match t in @T i o
           return D i -> option (D o)
     with
     | T0 _ _ foo => fun d0 => None
     (*   \/ change here *)
     | T1 i o foo bar t' =>
       fun d0 =>
         let l := List.map (fun n => E t' x d0)  [ 1 ; 2 ; 3 ] in
         let default := Some (D0 o) in
         List.hd default l
     end) d.

问题在于省略绑定器意味着o分支上使用的T1引用了同名的“外部”变量,而您希望它引用由T1