6.5610-project

Cryptography final project

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
-- A type checker for simply typed lambda calculus with a few inductive types

/--
Types in the μLean language

We use Greek letters for variables with type `Typ`

TODO: Equality
-/
inductive Typ
  /-- New named type -/
  | new : Nat  Typ
  /-- Function type -/
  | fn : Typ  Typ  Typ
  -- All types below are inductive
  /-- Product type -/
  | prod : Typ  Typ  Typ
  /-- Sum type -/
  | sum : Typ  Typ  Typ
  /-- Natural number type -/
  | nat : Typ
  /-- False (no terms of this type) -/
  | fls : Typ
deriving BEq, ReflBEq, LawfulBEq

/--
Terms in μLean

We hardcode all the inductive type constructors and eliminators here instead of implementing them separately as axioms (which would significantly simplify the type checker) to prevent adversies from writing fake proofs that use arbitrary axioms
-/
inductive Term
  /-- Variable -/
  | var : Nat  Term
  /-- Lambda -/
  | lam : Term × Typ  Term
  /-- Function application -/
  | app : Term × Typ  Term × Typ  Term
  /-- Construct a product -/
  | and : Term × Typ  Term × Typ  Term
  /-- Get first element of product -/
  | and1 : Term × Typ  Term
  /-- Get second element of product -/
  | and2 : Term × Typ  Term
  /-- Construct a sum -/
  | or : Term × Typ  Term
  /-- Zero as a nat -/
  | zero : Term
  /-- One or greater as a nat -/
  | succ : Term × Typ  Term
  /-- Eliminator (recursor) for nats -/
  | nat_elim : Typ  Term × Typ  Term × Typ  Term × Typ  Term
  /-- Eliminator for false -/
  | fls_elim : Term × Typ  Term

/-- Convert the Lean-based μLean syntax to the Lurk-based s-exp syntax -/
def Typ.toString : Typ  String
  | new α => s!"(list 0n {α}n)"
  | fn α β => s!"(list 1n {α.toString} {β.toString})"
  | prod α β => s!"(list 2n {α.toString} {β.toString})"
  | sum α β => s!"(list 3n {α.toString} {β.toString})"
  | nat => s!"'(4n)"
  | fls => s!"'(5n)"

instance : ToString Typ := Typ.toString

mutual
def toString (t : Term × Typ) := s!"(cons {t.1.toString} {t.2})"

def Term.toString : Term  String
  | .var x => s!"(list 10n {x})"
  | .lam b => s!"(list 11n {toString b})"
  | .app f a => s!"(list 12n {toString f} {toString a})"
  | .and x y => s!"(list 13n {toString x} {toString y})"
  | .and1 x => s!"(list 14n {toString x})"
  | .and2 x => s!"(list 15n {toString x})"
  | .or z => s!"(list 16n {toString z})"
  | .zero => s!"'(17n)"
  | .succ n => s!"(list 18n {toString n})"
  | .nat_elim α n x f => s!"(list 19n {α} {toString n} {toString x} {toString f})"
  | .fls_elim x => s!"(list 20n {toString x})"
end

instance : ToString (Term × Typ) := toString

instance : ToString Term := Term.toString

/--
The μLean type checker!

The variable names are chosen intentionally so that i.e. `a : Term` corresponds to `α : Typ`.
-/
def check (env : List Typ) : Term  Typ  Bool
  | .var x, α =>
    if _ : x < env.length then env[x] == α else false
  | .lam (b, β), .fn α β' =>
    β' == β && check (.cons α env) b β
  | .app (f, .fn α β) (a, α'), β' =>
    α' == α && β' == β && check env f (.fn α β) && check env a α
  | .and (a, α) (b, β), .prod α' β' =>
    α' == α && β' == β && check env a α && check env b β
  | .and1 (x, .prod α β), α' =>
    α' == α && check env x (.prod α β)
  | .and2 (x, .prod α β), β' =>
    β' == β && check env x (.prod α β)
  | .or (c, γ), .sum α β =>
    (γ == α || γ == β) && check env c γ
  | .zero, .nat =>
    true
  | .succ (n, .nat), .nat =>
    check env n .nat
  | .nat_elim α (n, .nat) (b, β) (f, .fn .nat (.fn γ δ)), .fn .nat τ =>
    τ == α && τ == β && τ == γ && τ == δ && check env n .nat && check env b β && check env f (.fn .nat (.fn γ δ))
  | .fls_elim (x, .fls), _ =>
    check env x .fls
  | _, _ =>
    false

theorem false_empty : check [] t .fls == false := by
  sorry

-- TODO: Implement eval so we can state 2 + 2 = 4

def a_imp_a := (Term.lam (.var 0, .new 0), Typ.fn (.new 0) (.new 0))

#guard check [] a_imp_a.1 a_imp_a.2

#eval IO.println a_imp_a

/-- A → B → B ∧ A -/
def a_imp_b_imp_ba := (Term.lam (.lam (.and (.var 0, .new 1) (.var 1, .new 0), .prod (.new 1) (.new 0)), .fn (.new 1) (.prod (.new 1) (.new 0))), Typ.fn (.new 0) (.fn (.new 1) (.prod (.new 1) (.new 0))))

#guard check [] a_imp_b_imp_ba.1 a_imp_b_imp_ba.2

#eval IO.println a_imp_b_imp_ba

/-- A ∧ B → B ∧ A -/
def ab_imp_ba := (Term.lam (.and (.and2 (.var 0, .prod (.new 0) (.new 1)), .new 1) (.and1 (.var 0, .prod (.new 0) (.new 1)), .new 0), .prod (.new 1) (.new 0)), Typ.fn (.prod (.new 0) (.new 1)) (.prod (.new 1) (.new 0)))

#guard check [] ab_imp_ba.1 ab_imp_ba.2

#eval IO.println ab_imp_ba

/-- ¬(A ∨ B) → ¬A -/
def not_ab_imp_not_a := (Term.lam (.lam (.app (.var 1, .fn (.sum (.new 0) (.new 1)) .fls) (.or (.var 0, .new 0), .sum (.new 0) (.new 1)), .fls), .fn (.new 0) .fls), Typ.fn (.fn (.sum (.new 0) (.new 1)) .fls) (.fn (.new 0) .fls))

#guard check [] not_ab_imp_not_a.1 not_ab_imp_not_a.2

#eval IO.println not_ab_imp_not_a

/-- A → ¬¬A -/
def a_imp_not_not_a := (Term.lam (.lam (.app (.var 0, .fn (.new 0) .fls) (.var 1, .new 0), .fls), .fn (.fn (.new 0) .fls) .fls), Typ.fn (.new 0) (.fn (.fn (.new 0) .fls) .fls))

#guard check [] a_imp_not_not_a.1 a_imp_not_not_a.2

#eval IO.println a_imp_not_not_a

/-- ¬¬¬A → ¬A -/
def not_not_not_a_imp_not_a := (Term.lam (.lam (.app (.var 1, .fn (.fn (.fn (.new 0) .fls) .fls) .fls) (.app a_imp_not_not_a (.var 0, .new 0), .fn (.fn (.new 0) .fls) .fls), .fls), .fn (.new 0) .fls), Typ.fn (.fn (.fn (.fn (.new 0) .fls) .fls) .fls) (.fn (.new 0) .fls))

#guard check [] not_not_not_a_imp_not_a.1 not_not_not_a_imp_not_a.2

#eval IO.println not_not_not_a_imp_not_a

/-- 2 exists (yeah I know this is not super exciting) -/
def two := (Term.succ ((.succ (.zero, .nat)), .nat), Typ.nat)

#guard check [] two.1 two.2

/-- 4 exists -/
def four := (Term.succ (.succ two, .nat), Typ.nat)

#guard check [] four.1 four.2

/-- Addition -/
def add := (Term.lam (.nat_elim .nat (.zero, .nat) (.var 0, .nat) (.lam (.lam (.succ (.var 2, .nat), .nat), .fn .nat .nat), .fn .nat (.fn .nat .nat)), .fn .nat .nat), Typ.fn .nat (.fn .nat .nat))

#guard check [] add.1 add.2

def two_plus_two := (Term.app (.app add two, .fn .nat .nat) two, Typ.nat)

#guard check [] two_plus_two.1 two_plus_two.2