Coq严格的积极性令人费解

时间:2018-01-09 12:16:03

标签: coq

我正在使用带有术语,变量和nat文字的语言进行开发,其中语言的arity构造在预定义中:

Inductive sort : Set := TERM | VAR | NAT.

Inductive termArity : list sort -> Set :=
  | Var    : termArity [VAR]
  | Let    : termArity [VAR ; TERM ; TERM]
  | Lam    : termArity [VAR ; TERM]
  | Ap     : termArity [TERM ; TERM]
  | NumLit : termArity [NAT]
  | Plus   : termArity [TERM ; TERM]
  .

我想要使用的术语的定义包含一个在其arity规范中匹配每个类型的子项(hlist是来自CPDT的异构列表):

Inductive t : Type :=
| Node : forall (sorts : list sort) (code : termArity sorts),
    hlist (fun s : sort => match s with
                           | TERM => t
                           | VAR => String.string
                           | NAT => nat
                           end) sorts
          -> t.

但是coq以'非严格正面出现't“'拒绝它。

正如预期的那样,用t替换定义中的nat会让coq认为它没问题:

Inductive t : Type :=
| Node : forall (sorts : list sort) (code : termArity sorts),
    hlist (fun s : sort => match s with
                           | TERM => nat
                           | VAR => String.string
                           | NAT => nat
                           end
          ) sorts
    -> t.

同样令人惊讶的是,总是返回t

Inductive t : Type :=
| Node : forall (sorts : list sort) (code : termArity sorts),
    hlist (fun s : sort => t) sorts
    -> t.

为什么我的尝试定义失败,而这会通过积极性检查器?我该如何修正我的定义?

编辑:另一种放弃代码但由于宇宙不一致而失败的尝试:

Definition var : Set := String.string.

Inductive termArity : list Type -> Type :=
  | Var    : termArity [var]
  | Let    : termArity [t; t]
  | Lam    : termArity [var; t]
  | Ap     : termArity [t; t]
  | NumLit : termArity [nat]
  | Plus   : termArity [t; t]

with t : Type :=
| Node : forall (sorts : list Type) (code : termArity sorts),
    hlist (fun s : Type => s) sorts -> t.

整个文件:

Require Coq.Bool.Bool. Open Scope bool.
Require Coq.Strings.String. Open Scope string_scope.
Require Coq.Arith.EqNat.
Require Coq.Arith.PeanoNat.
Require Coq.Arith.Peano_dec. Open Scope nat_scope.
Require Coq.Lists.List. Open Scope list_scope.
Require Coq.Vectors.Vector. Open Scope vector_scope.
Require Fin.

Module Export LocalListNotations.
Notation " [ ] " := nil (format "[ ]") : list_scope.
Notation " [ x ; .. ; y ] " := (cons x .. (cons y nil) ..) : list_scope.
Notation " [ x ; y ; .. ; z ] " := (cons x (cons y .. (cons z nil) ..)) : list_scope.
End LocalListNotations.

Module Core.
    Set Implicit Arguments.

    (* cpdt heterogeneous lists *)
    Section hlist.
      Variable A : Type.
      Variable B : A -> Type.

      Inductive hlist : list A -> Type :=
      | HNil : hlist nil
      | HCons : forall (x : A) (ls : list A), B x -> hlist ls -> hlist (x :: ls).

      Variable elm : A.

      Inductive member : list A -> Type :=
      | HFirst : forall ls, member (elm :: ls)
      | HNext : forall x ls, member ls -> member (x :: ls).

      Fixpoint hget ls (mls : hlist ls) : member ls -> B elm :=
        match mls with
          | HNil => fun mem =>
            match mem in member ls' return (match ls' with
                                              | nil => B elm
                                              | _ :: _ => unit
                                            end) with
              | HFirst _ => tt
              | HNext _ _ => tt
            end
          | HCons x mls' => fun mem =>
            match mem in member ls' return (match ls' with
                                              | nil => Empty_set
                                              | x' :: ls'' =>
                                                B x' -> (member ls'' -> B elm)
                                                -> B elm
                                            end) with
              | HFirst _ => fun x _ => x
              | HNext _ mem' => fun _ get_mls' => get_mls' mem'
            end x (hget mls')
        end.
    End hlist.

    Arguments HNil [A B].
    Arguments HCons [A B x ls].

    Arguments HFirst [A elm ls].
    Arguments HNext [A elm x ls].

    Module Exp.
      Inductive sort : Set := TERM | VAR | NAT.

      Inductive termArity : list sort -> Set :=
        | Var    : termArity [VAR]
        | Let    : termArity [VAR ; TERM ; TERM]
        | Lam    : termArity [VAR ; TERM]
        | Ap     : termArity [TERM ; TERM]
        | NumLit : termArity [NAT]
        | Plus   : termArity [TERM ; TERM]
        .

      (* coq complains this is not strictly positive. *)
      Inductive t : Type :=
      | Node3 : forall (sorts : list sort) (code : termArity sorts),
          hlist (fun s : sort => match s with
                                 | TERM => t
                                 | VAR => String.string
                                 | NAT => nat
                                 end) sorts
                -> t.

      (* exactly the same definition, but replacing t with nat, is
         not flagged as non-strictly-positive. this makes sense to me. *)
      Inductive t_not_quite_1 : Type :=
      | Node1 : forall (sorts : list sort) (code : termArity sorts),
          hlist (fun s : sort => match s with
                                 | TERM => nat
                                 | VAR => String.string
                                 | NAT => nat
                                 end
                ) sorts
          -> t_not_quite_1.

      (* this also (like the failed definition) returns t in the
         hlist type, but surprisingly typechecks! I don't understand
         how this is different *)
      Inductive t_not_quite_2 : Type :=
      | Node2 : forall (sorts : list sort) (code : termArity sorts),
          hlist (fun s : sort => t_not_quite_2) sorts
          -> t_not_quite_2.
    End Exp.
End Core.

1 个答案:

答案 0 :(得分:5)

Coq在积极性检查器中看不到匹配语句;这是一项长期的功能请求,请参阅https://github.com/coq/coq/issues/4701https://github.com/coq/coq/issues/1433

我相信如果你把它变成一种归纳法会有效:

Inductive kind_of_sort t : sort -> Type :=
| term_kind : t -> kind_of_sort t TERM
| var_kind : String.string -> kind_of_sort VAR
| nat_kind : nat -> kind_of_sort NAT.

然后使用它代替match语句。