IIRD.agda 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. module IIRD where
  2. open import LF
  3. -- A code for an IIRD (captures both general and restricted IIRDs)
  4. -- I - index set
  5. -- D - return type of the recursive component
  6. -- E - generalised return type of the recursive component (including the index)
  7. -- for restricted IIRD E = D i for an external i
  8. -- and for general IIRD E = (i : I) × D i
  9. -- The intuition is that restricted IIRD are parameterised over the index,
  10. -- whereas general IIRD compute their indices.
  11. data OP (I : Set)(D : I -> Set1)(E : Set1) : Set1 where
  12. -- Nullary constructor, contains the result of the recursive component
  13. ι : E -> OP I D E
  14. -- Non-recursive argument A, following arguments may depend on this argument.
  15. -- To code multi-constructor datatypes, the first argument is the name of the
  16. -- constructor and γ does the appropriate case-split on the name.
  17. σ : (A : Set)(γ : A -> OP I D E) -> OP I D E
  18. -- Recursive argument
  19. -- A - assumptions, for instance lim : (Nat -> Ord) -> Ord, where A = Nat
  20. -- i - the index of the inductive occurrence
  21. -- γ - the rest of the arguments, may depend on the result of calling the
  22. -- recursive function on the inductive argument
  23. δ : (A : Set)(i : A -> I)(γ : ((a : A) -> D (i a)) -> OP I D E) -> OP I D E
  24. -- Helper function. The definition is simple, but the type is not.
  25. _«_×_» : {A B : Set}{C : B -> Set}{D : B -> Set1}
  26. (f : (b : B) -> C b -> D b)
  27. (g : A -> B)(h : (a : A) -> C (g a)) ->
  28. (a : A) -> D (g a)
  29. f « g × h » = \a -> f (g a) (h a)
  30. -- The type of constructor arguments. Parameterised over
  31. -- U - the inductive type
  32. -- T - the recursive function
  33. -- This is the F of the simple polynomial type μF
  34. Ku : {I : Set}{D : I -> Set1}{E : Set1} -> OP I D E ->
  35. (U : I -> Set)(T : (i : I) -> U i -> D i) -> Set
  36. Ku (ι e) U T = One
  37. Ku (σ A γ) U T = A × \a -> Ku (γ a) U T
  38. Ku (δ A i γ) U T = ((a : A) -> U (i a)) × \g -> Ku (γ (T « i × g »)) U T
  39. -- The recursive function. As with Ku this is only the top-level structure.
  40. -- To get the real function there is a recursive knot to be tied.
  41. Kt : {I : Set}{D : I -> Set1}{E : Set1}
  42. (γ : OP I D E)(U : I -> Set)(T : (i : I) -> U i -> D i) ->
  43. Ku γ U T -> E
  44. Kt (ι e) U T ★ = e
  45. Kt (σ A γ) U T < a | b > = Kt (γ a) U T b
  46. Kt (δ A i γ) U T < g | b > = Kt (γ (T « i × g »)) U T b
  47. -- The assumptions of a particular inductive occurrence in a value.
  48. KIArg : {I : Set}{D : I -> Set1}{E : Set1}
  49. (γ : OP I D E)(U : I -> Set)(T : (i : I) -> U i -> D i) ->
  50. Ku γ U T -> Set
  51. KIArg (ι e) U T ★ = Zero
  52. KIArg (σ A γ) U T < a | b > = KIArg (γ a) U T b
  53. KIArg (δ A i γ) U T < g | b > = A + KIArg (γ (T « i × g »)) U T b
  54. -- Given the assumptions of an inductive occurence in a value we can compute
  55. -- its index.
  56. KIArg→I : {I : Set}{D : I -> Set1}{E : Set1}
  57. (γ : OP I D E)(U : I -> Set)(T : (i : I) -> U i -> D i) ->
  58. (a : Ku γ U T) -> KIArg γ U T a -> I
  59. KIArg→I (ι e) U T ★ ()
  60. KIArg→I (σ A γ) U T < a | b > c = KIArg→I (γ a) U T b c
  61. KIArg→I (δ A i γ) U T < g | b > (inl a) = i a
  62. KIArg→I (δ A i γ) U T < g | b > (inr a) = KIArg→I (γ (T « i × g »)) U T b a
  63. -- Given the assumptions of an inductive occurrence in a value we can compute
  64. -- its value.
  65. KIArg→U : {I : Set}{D : I -> Set1}{E : Set1}
  66. (γ : OP I D E)(U : I -> Set)(T : (i : I) -> U i -> D i) ->
  67. (a : Ku γ U T)(v : KIArg γ U T a) -> U (KIArg→I γ U T a v)
  68. KIArg→U (ι e) U T ★ ()
  69. KIArg→U (σ A γ) U T < a | b > c = KIArg→U (γ a) U T b c
  70. KIArg→U (δ A i γ) U T < g | b > (inl a) = g a
  71. KIArg→U (δ A i γ) U T < g | b > (inr a) = KIArg→U (γ (T « i × g »)) U T b a
  72. -- The type of induction hypotheses. Basically
  73. -- forall assumptions, the predicate holds for an inductive occurrence with
  74. -- those assumptions
  75. KIH : {I : Set}{D : I -> Set1}{E : Set1}
  76. (γ : OP I D E)(U : I -> Set)(T : (i : I) -> U i -> D i) ->
  77. (F : (i : I) -> U i -> Set1)(a : Ku γ U T) -> Set1
  78. KIH γ U T F a = (v : KIArg γ U T a) -> F (KIArg→I γ U T a v) (KIArg→U γ U T a v)
  79. -- If we can prove a predicate F for any values, we can construct the inductive
  80. -- hypotheses for a given value.
  81. -- Termination note: g will only be applied to values smaller than a
  82. Kmap : {I : Set}{D : I -> Set1}{E : Set1}
  83. (γ : OP I D E)(U : I -> Set)(T : (i : I) -> U i -> D i) ->
  84. (F : (i : I) -> U i -> Set1)
  85. (g : (i : I)(u : U i) -> F i u)
  86. (a : Ku γ U T) -> KIH γ U T F a
  87. Kmap γ U T F g a = \v -> g (KIArg→I γ U T a v) (KIArg→U γ U T a v)
  88. -- Things needed for general IIRD
  89. OPg : (I : Set)(D : I -> Set1) -> Set1
  90. OPg I D = OP I D (I ×' D)
  91. Gu : {I : Set}{D : I -> Set1}(γ : OPg I D)(U : I -> Set)(T : (i : I) -> U i -> D i) -> Set
  92. Gu γ U T = Ku γ U T
  93. Gi : {I : Set}{D : I -> Set1}(γ : OPg I D)(U : I -> Set)(T : (i : I) -> U i -> D i)
  94. (a : Gu γ U T) -> I
  95. Gi γ U T a = π₀' (Kt γ U T a)
  96. Gt : {I : Set}{D : I -> Set1}(γ : OPg I D)(U : I -> Set)(T : (i : I) -> U i -> D i)
  97. (a : Gu γ U T) -> D (Gi γ U T a)
  98. Gt γ U T a = π₁' (Kt γ U T a)
  99. -- Things needed for restricted IIRD
  100. OPr : (I : Set)(D : I -> Set1) -> Set1
  101. OPr I D = (i : I) -> OP I D (D i)
  102. Hu : {I : Set}{D : I -> Set1}
  103. (γ : OPr I D)(U : I -> Set)(T : (i : I) -> U i -> D i)
  104. (i : I) -> Set
  105. Hu γ U T i = Ku (γ i) U T
  106. Ht : {I : Set}{D : I -> Set1}
  107. (γ : OPr I D)(U : I -> Set)(T : (i : I) -> U i -> D i)
  108. (i : I)(a : Hu γ U T i) -> D i
  109. Ht γ U T i a = Kt (γ i) U T a