相互递归类型的自定义归纳原理

问题描述 投票:0回答:1

我试图为某些特定的相互递归类型定义归纳原理,我有点困惑。使用Schema并没有真正解决我的问题,这就是为什么我想自己定义原理(也许我使用相互递归类型的整个方法是不合适的......这也是一个选项)。

我正在使用亚瑟的extructure库(这是ordType来自的地方,它可能是其他东西)。所以,我的类型是:

From mathcomp Require Import all_ssreflect.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.


From extructures Require Import ord fmap.

Section R.

Variables Name Vals : ordType.

Inductive ResponseObject : Type :=
  | SingleResponse : Result -> ResponseObject
  | MultipleResponses : Result -> ResponseObject -> ResponseObject
  with Result : Type :=
       | Empty : Result
       | Null : Name -> Result
       | SingleResult : Name -> Vals -> Result
       | ListResult : Name -> seq Vals -> Result
       | NestedResult : Name -> ResponseObject -> Result
       | NestedListResult : Name -> seq ResponseObject -> Result.

它基本上是一个非空的Result对象列表,它们本身可以包含非空列表(NestedResultNestedListResult)。

我的问题是NestedListResult的情况,因为使用Schema生成的归纳原理不会检查ResponseObject列表中的每个元素。它陈述如下:

Scheme ResponseObject_ind := Induction for ResponseObject Sort Prop
    with Result_ind := Induction for Result Sort Prop.

Check ResponseObject_ind.
ResponseObject_ind
     : forall (P : ResponseObject -> Prop) (P0 : Result -> Prop),
       (forall r : Result, P0 r -> P (SingleResponse r)) ->
       (forall r : Result, P0 r -> forall r0 : ResponseObject, P r0 -> P (MultipleResponses r r0)) ->
       P0 Empty ->
       (forall s : Name, P0 (Null s)) ->
       (forall (s : Name) (s0 : Vals), P0 (SingleResult s s0)) ->
       (forall (s : Name) (l : seq Vals), P0 (ListResult s l)) ->
       (forall (s : Name) (r : ResponseObject), P r -> P0 (NestedResult s r)) ->
       (forall (s : Name) (l : seq ResponseObject), P0  (NestedListResult s l)) -> forall r : ResponseObject, P r

我尝试从SSReflect GenTree(它基本上折叠列表,并检查每个元素是否满足谓词)的例子,但我在尝试定义它时遇到语法错误(我猜这是一个语法错误?)我不知道如何要解决这个问题。我的猜测是使用fixwith是不正确的,我应该用其他方式写它?

我的尝试是这样的:

Definition ResponseObject_ind P Ps IH_SingleResponse IH_MultipleResponses IH_Empty IH_Null IH_SingleResult IH_ListResult IH_NestedResult IH_NestedListResult :=
    fix loop (r : ResponseObject) : P r : Prop :=
       match r with
       | SingleResponse r' => IH_SingleResponse r' (Result_ind r')
       | MultipleResponses r' rs => IH_MultipleResponses r' (Result_ind r') rs (loop rs)
       end
      with Result_ind (r : Result) : Ps r : Prop :=
       match r with
       | Empty => IH_Empty
       | Null l => IH_Null l
       | SingleResult l v => IH_SingleResult l v
       | ListResult l vals => IH_ListResult l vals
       | NestedResult l r' => IH_NestedResult l r' (Result_ind r')
       | NestedListResult l rs =>
         let fix iter_conj rs : foldr (fun r => and (P r)) True rs :=
             if rs is r :: rs' then conj (loop r) (iter_conj rs') else Logic.I
         in
         IH_NestedListResult l rs (iter_conj rs)
       end.

任何帮助,将不胜感激 :)

PS。实际上,也许其他一些方法比使用相互递归类型更好...之前我使用ResponseObject作为Result类型的另一个构造函数并检查它是否形成“正确”列表(没有嵌套的ResponseObjects形成一个奇怪的树状物)。上面这个选项似乎更优雅,但也许它不太方便。

coq
1个回答
0
投票

您的解决方案不遵循语法和键入约束。修复这些约束的一种方法是导致以下代码:

Definition ResponseObject_ind' (P : ResponseObject -> Prop) 
 (Ps : Result -> Prop) IH_SingleResponse IH_MultipleResponses IH_Empty IH_Null
IH_SingleResult IH_ListResult
(IH_NestedResult : forall (s : Name) (r : ResponseObject),
   P r -> Ps (NestedResult s r))
IH_NestedListResult :=
fix loop (r : ResponseObject) : P r : Prop :=
   match r with
   | SingleResponse r' => IH_SingleResponse r' (Result_ind r')
   | MultipleResponses r' rs => IH_MultipleResponses r' (Result_ind r') rs (loop rs)
   end
  with Result_ind (r : Result) : Ps r : Prop :=
   match r with
   | Empty => IH_Empty
   | Null l => IH_Null l
   | SingleResult l v => IH_SingleResult l v
   | ListResult l vals => IH_ListResult l vals
   | NestedResult l r' => IH_NestedResult l r' (loop r')
   | NestedListResult l rs =>
     let fix iter_conj rs : foldr (fun r => and (P r)) True rs :=
         if rs is r :: rs' then conj (loop r) (iter_conj rs') else Logic.I
     in
     IH_NestedListResult l rs (iter_conj rs)
   end
 for loop.

我不喜欢你的iter_conj解决方案。

在我看来,你实际上是在制作一堆包含三种归纳类型的归纳类型:ResponseObjectResultseq ResponseObject。所以你不应该使用两个谓词PPs的归纳原理,你应该有三个谓词:PResponseObjectPsResultPlseq ResponseObject。以下是我的解决方案:

Definition RO_ind := fun
  P P0 P1 
  (f : forall r : Result, P0 r -> P (SingleResponse r))
  (f0 : forall r : Result,
        P0 r ->
        forall r0 : ResponseObject, P r0 -> P (MultipleResponses r r0))
  (f1 : P0 Empty) (f2 : forall n : Name, P0 (Null n))
  (f3 : forall (n : Name) (v : Vals), P0 (SingleResult n v))
  (f4 : forall (n : Name) (l : seq Vals), P0 (ListResult n l))
  (f5 : forall (n : Name) (r : ResponseObject), P r -> P0 (NestedResult n r))
  (f6 : forall (n : Name) (l : seq ResponseObject),
          P1 l -> P0 (NestedListResult n l))
  (f7 : P1 [::])
  (f8 : forall (ro : ResponseObject),  P ro ->
       forall (l : seq ResponseObject),
       P1 l -> (P1 (ro :: l))) (ro : ResponseObject) =>
fix F (r : ResponseObject) : P r :=
  match r as r0 return (P r0) with
  | SingleResponse r0 => f r0 (F0 r0)
  | MultipleResponses r0 r1 => f0 r0 (F0 r0) r1 (F r1)
  end
with F0 (r : Result) : P0 r :=
  match r as r0 return (P0 r0) with
  | Empty => f1
  | Null n => f2 n
  | SingleResult n v => f3 n v
  | ListResult n l => f4 n l
  | NestedResult n r0 => f5 n r0 (F r0)
  | NestedListResult n l => f6 n l
    ((fix F1 l' : P1 l' :=
       match l' with
       | [::] => f7
       | ro :: l' => f8 ro (F ro) l' (F1 l')
       end) l)
  end
for F0.

这个解决方案是通过调用Scheme命令然后编辑标题(为序列添加P1f7f8)并最终编辑NestedListResult构造的情况而产生的。

© www.soinside.com 2019 - 2024. All rights reserved.