DeBruijnBase.agda 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. module Termination.Sized.DeBruijnBase where
  2. open import Data.Maybe
  3. open import Data.Maybe.Categorical
  4. open import Function -- composition, identity
  5. open import Relation.Binary.PropositionalEquality hiding ( subst )
  6. open ≡-Reasoning
  7. open import Category.Functor
  8. fmap : {A B : Set} -> (A -> B) -> Maybe A -> Maybe B
  9. fmap = RawFunctor._<$>_ functor
  10. -- OR:
  11. -- open RawFunctor MaybeFunctor using () renaming (_<$>_ to fmap)
  12. fmapExt : {A B : Set}{f g : A -> B} ->
  13. (forall a -> f a ≡ g a) -> forall m -> fmap f m ≡ fmap g m
  14. fmapExt f≡g nothing = refl
  15. fmapExt f≡g (just a) = cong just (f≡g a)
  16. fmapLaw1 : {A : Set}(a : Maybe A) -> fmap id a ≡ a
  17. fmapLaw1 nothing = refl
  18. fmapLaw1 (just a) = refl
  19. fmapLaw2 : {A B C : Set}(f : B -> C)(g : A -> B) ->
  20. forall m -> fmap f (fmap g m) ≡ fmap (f ∘ g) m
  21. fmapLaw2 f g nothing = refl
  22. fmapLaw2 f g (just a) = refl
  23. -- untyped de Bruijn terms
  24. data Lam (A : Set) : Set where
  25. var : A -> Lam A
  26. app : Lam A -> Lam A -> Lam A
  27. abs : Lam (Maybe A) -> Lam A
  28. -- functoriality of Lam
  29. lam : {A B : Set} -> (A -> B) -> Lam A -> Lam B
  30. lam f (var a) = var (f a)
  31. lam f (app t1 t2) = app (lam f t1) (lam f t2)
  32. lam f (abs r) = abs (lam (fmap f) r)
  33. lamExt : {A B : Set}{f g : A -> B} ->
  34. (forall a -> f a ≡ g a) -> forall t -> lam f t ≡ lam g t
  35. lamExt f≡g (var a) = cong var (f≡g a)
  36. lamExt f≡g (abs r) = cong abs (lamExt (fmapExt f≡g) r)
  37. lamExt f≡g (app r s) = cong₂ app (lamExt f≡g r) (lamExt f≡g s)
  38. lamLaw1 : {A : Set}(t : Lam A) -> lam id t ≡ t
  39. lamLaw1 (var a) = refl
  40. lamLaw1 (app r s) = begin
  41. lam id (app r s)
  42. ≡⟨ refl ⟩
  43. app (lam id r) (lam id s)
  44. ≡⟨ cong (app (lam id r)) (lamLaw1 s) ⟩
  45. app (lam id r) s
  46. ≡⟨ cong (\ x -> app x s) (lamLaw1 r) ⟩
  47. app r s
  48. lamLaw1 (abs t) = begin
  49. lam id (abs t)
  50. ≡⟨ refl ⟩
  51. abs (lam (fmap id) t)
  52. ≡⟨ cong abs (lamExt {g = id} fmapLaw1 t) ⟩
  53. abs (lam id t)
  54. ≡⟨ cong abs (lamLaw1 t) ⟩
  55. abs t
  56. lamLaw2 : {A B C : Set}(f : B -> C)(g : A -> B) ->
  57. forall t -> lam f (lam g t) ≡ lam (f ∘ g) t
  58. lamLaw2 f g (var a) = refl
  59. lamLaw2 f g (app r s) = cong₂ app (lamLaw2 f g r) (lamLaw2 f g s)
  60. lamLaw2 f g (abs t) = begin
  61. lam f (lam g (abs t))
  62. ≡⟨ refl ⟩
  63. lam f (abs (lam (fmap g) t))
  64. ≡⟨ refl ⟩
  65. abs (lam (fmap f) (lam (fmap g) t))
  66. ≡⟨ cong abs (lamLaw2 (fmap f) (fmap g) t) ⟩
  67. abs (lam (fmap f ∘ fmap g) t)
  68. ≡⟨ cong abs (lamExt (fmapLaw2 f g) t) ⟩
  69. abs (lam (fmap (f ∘ g)) t)
  70. ≡⟨ refl ⟩
  71. lam (f ∘ g) (abs t)
  72. -- lifting a substitution A -> Lam B under a binder
  73. lift : {A B : Set} -> (A -> Lam B) -> Maybe A -> Lam (Maybe B)
  74. lift f nothing = var nothing
  75. lift f (just a) = lam just (f a)
  76. -- extensionality of lifting
  77. liftExt : {A B : Set}{f g : A -> Lam B} ->
  78. ((a : A) -> f a ≡ g a) -> (t : Maybe A) -> lift f t ≡ lift g t
  79. liftExt H nothing = refl
  80. liftExt H (just a) = cong (lam just) $ H a
  81. -- simultaneous substitution
  82. subst : {A B : Set} -> (A -> Lam B) -> Lam A -> Lam B
  83. subst f (var a) = f a
  84. subst f (app t1 t2) = app (subst f t1) (subst f t2)
  85. subst f (abs r) = abs (subst (lift f) r)
  86. -- extensionality of subst
  87. substExt : {A B : Set}{f g : A -> Lam B} ->
  88. ((a : A) -> f a ≡ g a) -> (t : Lam A) -> subst f t ≡ subst g t
  89. substExt H (var a) = H a
  90. substExt {f = f}{g = g} H (app t1 t2) = begin
  91. subst f (app t1 t2)
  92. ≡⟨ refl ⟩
  93. app (subst f t1) (subst f t2)
  94. ≡⟨ cong (\ x -> app x (subst f t2)) (substExt H t1) ⟩
  95. app (subst g t1) (subst f t2)
  96. ≡⟨ cong (\ x -> app (subst g t1) x) (substExt H t2) ⟩
  97. app (subst g t1) (subst g t2)
  98. substExt {f = f}{g = g} H (abs r) = begin
  99. subst f (abs r)
  100. ≡⟨ refl ⟩
  101. abs (subst (lift f) r)
  102. ≡⟨ cong abs (substExt (liftExt H) r) ⟩
  103. abs (subst (lift g) r)
  104. ≡⟨ refl ⟩
  105. subst g (abs r)
  106. -- Lemma: lift g ∘ fmap f = lift (g ∘ f)
  107. liftLaw1 : {A B C : Set}(f : A -> B)(g : B -> Lam C)(t : Maybe A) ->
  108. lift g (fmap f t) ≡ lift (g ∘ f) t
  109. liftLaw1 f g nothing = begin
  110. lift g (fmap f nothing)
  111. ≡⟨ refl ⟩
  112. lift g nothing
  113. ≡⟨ refl ⟩
  114. var nothing
  115. ≡⟨ refl ⟩
  116. lift (g ∘ f) nothing
  117. liftLaw1 f g (just a) = begin
  118. lift g (fmap f (just a))
  119. ≡⟨ refl ⟩
  120. lift g (just (f a))
  121. ≡⟨ refl ⟩
  122. lam just (g (f a))
  123. ≡⟨ refl ⟩
  124. lift (g ∘ f) (just a)
  125. -- Lemma: subst g ∘ lam f t = subst (g ∘ f)
  126. substLaw1 : {A B C : Set}(f : A -> B)(g : B -> Lam C)(t : Lam A) ->
  127. subst g (lam f t) ≡ subst (g ∘ f) t
  128. substLaw1 f g (var a) = refl
  129. substLaw1 f g (app t1 t2) = begin
  130. subst g (lam f (app t1 t2))
  131. ≡⟨ refl ⟩
  132. subst g (app (lam f t1) (lam f t2))
  133. ≡⟨ refl ⟩
  134. app (subst g (lam f t1)) (subst g (lam f t2))
  135. ≡⟨ cong (\ x -> app x (subst g (lam f t2))) (substLaw1 f g t1) ⟩
  136. app (subst (g ∘ f) t1) (subst g (lam f t2))
  137. ≡⟨ cong (\ x -> app (subst (g ∘ f) t1) x) (substLaw1 f g t2) ⟩
  138. app (subst (g ∘ f) t1) (subst (g ∘ f) t2)
  139. substLaw1 f g (abs r) =
  140. begin subst g (lam f (abs r))
  141. ≡⟨ refl ⟩
  142. subst g (abs (lam (fmap f) r))
  143. ≡⟨ refl ⟩
  144. abs (subst (lift g) (lam (fmap f) r))
  145. ≡⟨ cong abs (substLaw1 (fmap f) (lift g) r) ⟩
  146. abs (subst (lift g ∘ fmap f) r)
  147. ≡⟨ cong abs (substExt {f = lift g ∘ fmap f} {g = lift (g ∘ f)} (liftLaw1 f g) r) ⟩
  148. abs (subst (lift (g ∘ f)) r)
  149. -- Lemma: lift (lam f ∘ g) = lam f ∘ subst g
  150. liftLaw2 : {A B C : Set}(f : B -> C)(g : A -> Lam B)(t : Maybe A) ->
  151. lift (lam f ∘ g) t ≡ lam (fmap f) (lift g t)
  152. liftLaw2 f g nothing = begin
  153. lift (lam f ∘ g) nothing
  154. ≡⟨ refl ⟩
  155. var nothing
  156. ≡⟨ refl ⟩
  157. var (fmap f nothing)
  158. ≡⟨ refl ⟩
  159. lam (fmap f) (var nothing)
  160. ≡⟨ refl ⟩
  161. lam (fmap f) (lift g nothing)
  162. liftLaw2 f g (just a) = begin
  163. lift (lam f ∘ g) (just a)
  164. ≡⟨ refl ⟩
  165. lam just (lam f (g a))
  166. ≡⟨ lamLaw2 just f (g a) ⟩
  167. lam (just ∘ f) (g a)
  168. ≡⟨ lamExt (\ a -> refl) (g a) ⟩
  169. lam (fmap f ∘ just) (g a)
  170. ≡⟨ sym (lamLaw2 (fmap f) just (g a)) ⟩
  171. lam (fmap f) (lam just (g a))
  172. ≡⟨ refl ⟩
  173. lam (fmap f) (lift g (just a))
  174. -- Lemma: subst (lam f ∘ g) = lam f ∘ subst g
  175. substLaw2 : {A B C : Set}(f : B -> C)(g : A -> Lam B)(t : Lam A) ->
  176. subst (lam f ∘ g) t ≡ lam f (subst g t)
  177. substLaw2 f g (var a) = refl
  178. substLaw2 f g (app r s) = begin
  179. subst (lam f ∘ g) (app r s)
  180. ≡⟨ refl ⟩
  181. app (subst (lam f ∘ g) r) (subst (lam f ∘ g) s)
  182. ≡⟨ cong (app (subst (lam f ∘ g) r)) (substLaw2 f g s) ⟩
  183. app (subst (lam f ∘ g) r) (lam f (subst g s))
  184. ≡⟨ cong (\ x -> app x (lam f (subst g s))) (substLaw2 f g r) ⟩
  185. app (lam f (subst g r)) (lam f (subst g s))
  186. ≡⟨ refl ⟩
  187. lam f (app (subst g r) (subst g s))
  188. ≡⟨ refl ⟩
  189. lam f (subst g (app r s))
  190. substLaw2 f g (abs t) = begin
  191. subst (lam f ∘ g) (abs t)
  192. ≡⟨ refl ⟩
  193. abs (subst (lift (lam f ∘ g)) t)
  194. ≡⟨ cong abs (substExt (liftLaw2 f g) t) ⟩
  195. abs (subst (lam (fmap f) ∘ (lift g)) t)
  196. ≡⟨ cong abs (substLaw2 (fmap f) (lift g) t) ⟩
  197. abs (lam (fmap f) (subst (lift g) t))
  198. ≡⟨ refl ⟩
  199. lam f (abs (subst (lift g) t))
  200. ≡⟨ refl ⟩
  201. lam f (subst g (abs t))