Advertisement
Nolrai

ord.lean 1

Mar 13th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. inductive ord : Type :=
  2. | Zero : ord
  3. | Stroke : ord -> ord -> ord
  4.  
  5. open ord
  6.  
  7. infix `∥`:50 := Stroke
  8.  
  9. definition ord.has_zero [instance] : has_zero ord :=
  10. {| has_zero, zero := Zero |}
  11.  
  12. attribute Stroke [coercion] --this seems to act flaky?
  13.  
  14. definition ord.has_one [instance] : has_one ord :=
  15. has_one.mk (0 ∥ 0)
  16.  
  17. inductive ord.le : ord -> ord -> Prop :=
  18. | refl {} : ∀ r : ord, ord.le r r
  19. | app {} : ∀ r q f : ord, ord.le q r -> ord.le q (f r)
  20. | mono {} : ∀ r q g f : ord, ord.le q r -> ord.le g f -> ord.le (g q) (f r)
  21. | limit {} : ∀ r q g f : ord, q ≠ f -> ord.le q (f r) -> ord.le g f -> ord.le (g q) (f r)
  22.  
  23. local infix ≤ := ord.le
  24.  
  25. definition ord.has_le [instance] : has_le ord :=
  26. has_le.mk ord.le
  27.  
  28. open ord.le
  29.  
  30. lemma zero_le : ∀ r : ord, 0 ≤ r :=
  31. begin
  32. intro r,
  33. induction r,
  34. {exact (ord.le.refl _)},
  35. {
  36. apply app,
  37. { assumption },
  38. }
  39. end
  40.  
  41. lemma le_zero : ∀ r : ord, r ≤ 0 -> r = 0 :=
  42. begin
  43. intros r H,
  44. cases H,
  45. esimp,
  46. end
  47.  
  48. open nat
  49.  
  50. definition nat.max : nat -> nat -> nat
  51. | 0 m := m
  52. | n 0 := n
  53. | (succ n) (succ m) := succ (nat.max n m)
  54.  
  55. definition hight : ∀ o : ord, nat
  56. | 0 := 0
  57. | (f ∥ r) := 1 + nat.max (hight f) (hight r)
  58.  
  59.  
  60. record has_swap [class] (A : Type) : Type := (swap : A -> A)
  61.  
  62. record has_involution [class] (A : Type) extends (has_swap A) :=
  63. (is_involution : ∀ x, swap (swap x) = x)
  64.  
  65. abbreviation σ {{A}} [H : has_swap A] x := @has_swap.swap A H x
  66.  
  67. inductive ineq : Type :=
  68. | LessThan
  69. | GreaterThan
  70.  
  71. inductive comp : Type :=
  72. | Ineq : ineq -> comp
  73. | EqualTo
  74.  
  75. namespace ineq
  76.  
  77. abbreviation LT := LessThan
  78. abbreviation GT := GreaterThan
  79.  
  80. definition inversion : ineq -> ineq
  81. | LT := GT
  82. | GT := LT
  83.  
  84. definition ineq_swap [instance] : has_swap ineq
  85. := {| has_swap ineq, swap := inversion |}
  86.  
  87. lemma inversion_is_involution :
  88. ∀ (x : ineq), σ(σ x) = x :=
  89. ineq.rec rfl rfl
  90.  
  91. definition ineq_involution [instance] :
  92. has_involution ineq :=
  93. {| has_involution ineq, ineq_swap, is_involution := inversion_is_involution |}
  94.  
  95. end ineq
  96.  
  97. namespace comp
  98.  
  99. export ineq
  100.  
  101. abbreviation EQ := EqualTo
  102. attribute comp.Ineq [coercion]
  103.  
  104. definition nudge : ineq -> comp -> comp
  105. | tie_breaker EQ := Ineq tie_breaker
  106. | _ non_tie := non_tie
  107.  
  108. end comp
  109.  
  110. section comp_spec
  111. open comp
  112.  
  113. parameter {A : Type}
  114. parameter f : A -> A -> comp
  115.  
  116. local infix `≤` := le
  117.  
  118. variables x y : A
  119.  
  120. definition comp_spec' [H : has_le A] : Prop :=
  121. match f x y with
  122. | LT := x ≤ y
  123. | EQ := x = y
  124. | GT := y ≤ x
  125. end
  126.  
  127. definition comp_spec [H : has_le A] : Prop :=
  128. ∀ x y, comp_spec' x y
  129.  
  130. end comp_spec
  131.  
  132. namespace ord
  133.  
  134. open comp
  135. open ineq
  136.  
  137. definition compOrd' : ((ord×ord)->comp)->(ord×ord)->comp
  138. | f (p, q) :=
  139. match (p,q) with
  140. | (0,0) := EQ
  141. | (0,_) := Ineq LT
  142. | (_,0) := Ineq GT
  143. | ((fp∥rp),(fq∥rq)) :=
  144. match f (fp,fq) with
  145. | EQ := f (rp,rq)
  146. | LT := nudge GT (f (rp,q))
  147. | GT := nudge LT (f (p,rq))
  148. end
  149. end
  150.  
  151. inductive is_child : ord -> ord -> Prop :=
  152. | through_f : forall {f r}, is_child f (f∥r)
  153. | through_r : forall {f r}, is_child f (f∥r)
  154.  
  155. inductive trans_completion {A : Type} (R : A -> A -> Prop) : A -> A -> Prop :=
  156. | single : forall {a b},
  157. R a b -> trans_completion R a b
  158. | next : forall {a b} c,
  159. R a b -> trans_completion R b c -> trans_completion R a c
  160.  
  161. abbreviation TR {A} R := @trans_completion A R
  162.  
  163. infix `~>`:50 := is_child
  164.  
  165. open eq.ops well_founded decidable prod
  166.  
  167. namespace trans_completion
  168. section One
  169. parameter A : Type
  170. parameter R : A -> A -> Prop
  171.  
  172. lemma trans_valid : forall {a b},
  173. TR R a b -> ∃ c, R c b :=
  174. begin
  175. intros a b H,
  176. induction H,
  177. {existsi a, assumption},
  178. {assumption},
  179. end
  180.  
  181. end One
  182. end trans_completion
  183.  
  184. open trans_completion
  185.  
  186. lemma zero_has_no_children : ∀ x, ¬ (x ~> 0) :=
  187. begin
  188. intros x H,
  189. cases H,
  190. now
  191. end
  192.  
  193. lemma Zero_is_zero : Zero = 0 := rfl
  194. reveal Zero_is_zero
  195. open ord
  196.  
  197. theorem all_reached : well_founded (TR is_child) :=
  198. well_founded.intro
  199. proof
  200. begin
  201. intro,
  202. constructor,
  203. intros,
  204. induction a,
  205. cases a_1,
  206. {
  207. exfalso,
  208. assert H : ∃ z, z ~> 0,
  209. {existsi y, rewrite Zero_is_zero at a_2, assumption},
  210. cases H, apply zero_has_no_children, assumption
  211. },
  212. {
  213. exfalso,
  214. assert H : ∃ z, z ~> 0,
  215. {apply trans_valid, apply a_3},
  216. cases H,
  217. apply zero_has_no_children,
  218. apply a_1
  219. },
  220. now
  221. end
  222. qed
  223.  
  224. end ord
  225.  
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement