-
-
Save 5HT/39891e58ab7f6446b4fdf5a204a4b99e to your computer and use it in GitHub Desktop.
(* Type System *) | |
type exp = | |
| EVar of string | ELam of exp * (string * exp) | EApp of exp * exp | |
| EPi of exp * (string * exp) | ESig of exp * (string * exp) | EPair of string * exp * exp | |
| EId of exp | ERef of exp | |
| EInt | EIntConst of Z.t | ERat | ERatConst of exp * exp | ERatLt of exp * exp | |
| EReal | ECut of exp * exp * exp option * exp option * exp option * exp option * exp option * exp option * exp option | |
| ERealLt of exp * exp | ERealEq of exp * exp | ERealOps of real_op * exp * exp | |
| EIm of exp | EInf of exp | EIndIm of exp * exp | |
| EDisc of exp | EHub of exp | EBase of exp | ESpoke of exp * exp | EIndDisc of exp * exp * exp * exp * exp | |
| EOpen | EFinSet of exp | EUnion of exp | EIn of exp * exp | ESingleton of exp | EAnd of exp * exp | |
and real_op = RealPlus | RealMinus | RealMult | RealDiv | |
type context = (string * exp) list | |
(* Constants *) | |
let zero = ECut (ELam (ERat, ("q", ERatLt (EVar "q", ERatConst (EIntConst Z.zero, EIntConst Z.one)))), | |
ELam (ERat, ("q", ERatLt (ERatConst (EIntConst Z.zero, EIntConst Z.one), EVar "q"))), | |
None, None, None, None, None, None, None) | |
let one = ECut (ELam (ERat, ("q", ERatLt (EVar "q", ERatConst (EIntConst Z.one, EIntConst Z.one)))), | |
ELam (ERat, ("q", ERatLt (ERatConst (EIntConst Z.one, EIntConst Z.one), EVar "q"))), | |
None, None, None, None, None, None, None) | |
(* D^2 and D^1 *) | |
let r_pair = ESig (EReal, ("x", EReal)) | |
let d2 = EDisc r_pair | |
let d1 = EPathP (EReal, zero, one) | |
let hub = EHub (EPair ("origin", zero, zero)) | |
let base x y = EBase (EPair ("boundary", x, y)) | |
(* Distance and Sup/Inf *) | |
let sq x = ERealOps (RealMult, x, x) | |
let dist x1 y1 x2 y2 = ERealOps (RealPlus, sq (ERealOps (RealMinus, x1, x2)), sq (ERealOps (RealMinus, y1, y2))) | |
let g f = ELam (d2, ("x", dist (EFst (EVar "x")) (ESnd (EVar "x")) (EFst (EApp (f, EVar "x"))) (ESnd (EApp (f, EVar "x"))))) | |
let supf dom f = | |
let l = ELam (ERat, ("q", ESig (dom, ("x", ERealLt (EVar "q", EApp (f, EVar "x")))))) | |
and u = ELam (ERat, ("q", EPi (dom, ("x", ERealLt (EApp (f, EVar "x"), EVar "q"))))) | |
in ECut (l, u, Some (EPair ("", ERatConst (EIntConst (Z.of_int (-1)), EIntConst Z.one), EBool)), | |
Some (EPair ("", ERatConst (EIntConst Z.one, EIntConst Z.one), EBool)), | |
Some (EPair ("", ERatConst (EIntConst Z.one, EIntConst Z.one), EBool)), | |
Some (EPair ("", ERatConst (EIntConst (Z.of_int (-1)), EIntConst Z.one), EBool)), | |
Some (ELam (ERat, ("q", ELam (EBool, ("_", EPair ("", ERealOps (RealPlus, EVar "q", ERatConst (EIntConst Z.one, EIntConst Z.one)), EBool)))))), | |
Some (ELam (ERat, ("q", ELam (EBool, ("_", EPair ("", ERealOps (RealMinus, EVar "q", ERatConst (EIntConst Z.one, EIntConst Z.one)), EBool)))))), | |
Some (ELam (ERat, ("q", ELam (ERat, ("r", ELam (EBool, ("_", EBool)))))))) | |
let neg x = ERealOps (RealMinus, zero, x) | |
let inf_g f = neg (supf d2 (ELam (d2, ("x", neg (EApp (g f, EVar "x")))))) | |
(* Theorems *) | |
let ivt = | |
ELam (d1, ("f", EPi (d1, ("x", EReal))), | |
ELam (EIm d1, ("cont", | |
ELam (EReal, ("c", | |
ELam (EAnd (ERealLt (EApp (EVar "f", zero), EVar "c"), ERealLt (EVar "c", EApp (EVar "f", one))), ("bounds", | |
let g = ELam (d1, ("x", ERealOps (RealMinus, EApp (EVar "f", EVar "x"), EVar "c"))) in | |
let sup_x = supf d1 (ELam (d1, ("x", ERealLt (EApp (g, EVar "x"), zero)))) in | |
EPair ("x", sup_x, (* TODO: EId proof *)) | |
)))))) | |
let evt = | |
ELam (d2, ("f", EPi (d2, ("x", EReal))), | |
ELam (EIm d2, ("cont", | |
let sup_val = supf d2 (EVar "f") in | |
let inf_val = inf_g (EVar "f") in | |
EPair ("maxmin", | |
EIndDisc (r_pair, ELam (d2, ("x", EId (EReal, EApp (EVar "f", EVar "x"), sup_val))), | |
(* TODO: Cases *), hub), | |
EIndDisc (r_pair, ELam (d2, ("x", EId (EReal, EApp (EVar "f", EVar "x"), inf_val))), | |
(* TODO: Cases *), hub))))) | |
let heine_borel = | |
ELam (d2, ("cover", EPi (d2, ("x", ESig (EOpen, ("U", EIn (EVar "x", EVar "U"))))), | |
EIndDisc (r_pair, ELam (d2, ("x", ESig (EFinSet (EOpen), ("Us", EIn (EVar "x", EUnion (EVar "Us")))))), | |
EPair ("hub_cover", ESingleton (EApp (EVar "cover", hub)), (* TODO *)), | |
ELam (r_pair, ("b", (* TODO *))), | |
ELam (r_pair, ("h", ELam (r_pair, ("b", (* TODO *))))), | |
hub)) | |
(* Type Checker *) | |
exception TypeError of string | |
let rec infer (ctx: context) (e: exp) : exp = | |
match e with | |
| EVar x -> (match List.assoc_opt x ctx with Some ty -> ty | None -> raise (TypeError ("Unbound: " ^ x))) | |
| ELam (a, (x, e')) -> let b = infer ((x, a) :: ctx) e' in EPi (a, (x, b)) | |
| EApp (e1, e2) -> | |
let pi_ty = infer ctx e1 in | |
(match pi_ty with EPi (a, (x, b)) -> if check ctx e2 a then b else raise (TypeError "App mismatch") | _ -> raise (TypeError "Expected Pi")) | |
| EPi (a, (x, b)) -> if check ctx a (EKan 0) && check ((x, a) :: ctx) b (EKan 0) then EKan 0 else raise (TypeError "Pi failed") | |
| ESig (a, (x, b)) -> if check ctx a (EKan 0) && check ((x, a) :: ctx) b (EKan 0) then EKan 0 else raise (TypeError "Sigma failed") | |
| EPair (_, e1, e2) -> let a = infer ctx e1 in let b = ELam (a, (x, infer ((x, a) :: ctx) e2)) in ESig (a, (x, b)) | |
| EId a -> if check ctx a (EKan 0) then EKan 0 else raise (TypeError "Id failed") | |
| ERef e -> let a = infer ctx e in EId a | |
| EPathP (a, e1, e2) -> if check ctx e1 a && check ctx e2 a then EKan 0 else raise (TypeError "PathP failed") | |
| EInt -> EKan 0 | EIntConst _ -> EInt | ERat -> EKan 0 | |
| ERatConst (n, d) -> if check ctx n EInt && check ctx d EInt then ERat else raise (TypeError "Rat failed") | |
| ERatLt (q1, q2) -> if check ctx q1 ERat && check ctx q2 ERat then EBool else raise (TypeError "RatLt failed") | |
| EReal -> EKan 0 | |
| ECut (l, u, il_opt, iu_opt, bl_opt, bu_opt, ol_opt, ou_opt, d_opt) -> | |
let prop_ty = EPi (ERat, ("q", EBool)) in | |
let q = EVar "q" and r = EVar "r" in | |
let check_opt opt ty = match opt with Some e -> check ctx e ty | None -> true in | |
if check ctx l prop_ty && | |
check ctx u prop_ty && | |
check_opt il_opt (ESig (ERat, ("q", EApp (l, q)))) && | |
check_opt iu_opt (ESig (ERat, ("q", EApp (u, q)))) && | |
check_opt bl_opt (ESig (ERat, ("q", EApp (l, q)))) && | |
check_opt bu_opt (ESig (ERat, ("q", EApp (u, q)))) && | |
check_opt ol_opt (EPi (ERat, ("q", EPi (EBool, ("lq", ESig (ERat, ("r", EAnd (ERatLt (q, r), EApp (l, r))))))))) && | |
check_opt ou_opt (EPi (ERat, ("r", EPi (EBool, ("ur", ESig (ERat, ("q", EAnd (ERatLt (q, r), EApp (u, q))))))))) && | |
check_opt d_opt (EPi (ERat, ("q", EPi (ERat, ("r", EPi (ERatLt (q, r), ("_", EOr (EApp (l, q), EApp (u, r))))))))) | |
then EReal else raise (TypeError "Cut failed") | |
| ERealLt (r1, r2) -> if check ctx r1 EReal && check ctx r2 EReal then EBool else raise (TypeError "RealLt failed") | |
| ERealEq (r1, r2) -> if check ctx r1 EReal && check ctx r2 EReal then EBool else raise (TypeError "RealEq failed") | |
| ERealOps (op, r1, r2) -> if check ctx r1 EReal && check ctx r2 EReal then EReal else raise (TypeError "RealOps failed") | |
| EDisc a -> if check ctx a (EKan 0) then EKan 0 else raise (TypeError "Disc failed") | |
| EHub h -> let a = infer ctx h in EDisc a | |
| EBase b -> let a = infer ctx b in EDisc a | |
| ESpoke (h, b) -> | |
let a = infer ctx h in | |
if check ctx b a then EPathP (EDisc a, EHub h, EBase b) | |
else raise (TypeError "Spoke mismatch") | |
| EIndDisc (a, b, h_case, b_case, s_case) -> | |
let d = EDisc a in | |
let h = EVar "h" and b_var = EVar "b" in | |
if check ctx a (EKan 0) && | |
check ctx b (EPi (d, ("x", EKan 0))) && | |
check ctx h_case (EApp (b, EHub h)) && | |
check ctx b_case (EPi (a, ("b", EApp (b, EBase b_var)))) && | |
check ctx s_case (EPi (a, ("h", EPi (a, ("b", EPathP (d, EHub h, EBase b_var)))))) | |
then EApp (b, EHub h) | |
else raise (TypeError "IndDisc failed") | |
| EIm a -> if check ctx a (EKan 0) then EKan 0 else raise (TypeError "Im failed") | |
| EInf e -> let a = infer ctx e in EIm a | |
| EIndIm (a, b, f) -> | |
let x = EVar "x" in | |
if check ctx a (EKan 0) && | |
check ctx b (EPi (EIm a, ("_", EKan 0))) && | |
check ctx f (EPi (a, ("x", EApp (b, EInf x)))) | |
then EApp (b, EInf x) | |
else raise (TypeError "IndIm failed") | |
| EOpen -> EKan 0 | EFinSet a -> EKan 0 | EUnion e -> infer ctx e | EIn (x, u) -> EBool | |
| ESingleton e -> EFinSet (infer ctx e) | |
| EAnd (e1, e2) -> if check ctx e1 EBool && check ctx e2 EBool then EBool else raise (TypeError "And failed") | |
| _ -> raise (TypeError "Not implemented") | |
and check (ctx: context) (e: exp) (ty: exp) : bool = | |
let inferred = infer ctx e in eq_type ctx inferred ty | |
and subst (e: exp) (x: string) (v: exp) : exp = e (* Placeholder *) | |
and eq_type (ctx: context) (t1: exp) (t2: exp) : bool = true (* Placeholder *) | |
(* Test *) | |
let test_ivt = infer [] ivt | |
let test_evt = infer [] evt | |
let test_hb = infer [] heine_borel |
Extreme Value Theorem (EVT)
Classical: If f:[a,b]→R is continuous, then f attains its maximum and minimum on [a,b].
Our Version: f:D2 →R, continuous, attains sup𝑓 and inf𝑓.
let evt =
ELam (d2, ("f", EPi (d2, ("x", EReal))),
ELam (EIm d2, ("cont",
ESig (ESig (d2, ("max", EId (EReal, EApp (EVar "f", EVar "max"), supf d2 (EVar "f")))), (* max = sup f *)
("min", ESig (d2, ("min", EId (EReal, EApp (EVar "f", EVar "min"), inf_g (EVar "f")))))))))
let evt_proof =
ELam (d2, ("f", EPi (d2, ("x", EReal))),
ELam (EIm d2, ("cont",
let sup_val = supf d2 (EVar "f") in
let inf_val = inf_g (EVar "f") in
EPair ("maxmin",
EIndDisc (r_pair, ELam (d2, ("x", EId (EReal, EApp (EVar "f", EVar "x"), sup_val))),
(* Hub, Base, Spoke cases TODO *), hub),
EIndDisc (r_pair, ELam (d2, ("x", EId (EReal, EApp (EVar "f", EVar "x"), inf_val))),
(* TODO *), hub)))))
Heine-Borel Theorem
Classical: Every open cover of a closed, bounded interval [a,b] has a finite subcover.
Our Version: Every open cover of D2 has a finite subcover (simplified to compactness).
let heine_borel =
ELam (d2, ("cover", EPi (d2, ("x", ESig (EOpen, ("U", EIn (EVar "x", EVar "U"))))),
ESig (EFinSet d2, ("finite", EPi (d2, ("x", ESig (EFinSet (EOpen), ("Us", EIn (EVar "x", EUnion (EVar "Us")))))))))
let hb_proof =
ELam (d2, ("cover", EPi (d2, ("x", ESig (EOpen, ("U", EIn (EVar "x", EVar "U"))))),
EIndDisc (r_pair, ELam (d2, ("x", ESig (EFinSet (EOpen), ("Us", EIn (EVar "x", EUnion (EVar "Us")))))),
(* Hub: Cover hub with one open set *)
EPair ("hub_cover", ESingleton (EApp (EVar "cover", hub)), (* TODO: Membership *)),
(* Base and Spoke: Build finite cover *)
ELam (r_pair, ("b", (* TODO *))),
ELam (r_pair, ("h", ELam (r_pair, ("b", (* TODO *))))),
hub)))
let e_open = ESig (EPi (d2, ("x", EBool)), ("P", EPi (d2, ("x",
EPi (EBool, ("px", ETrunc 0 (ESig (EReal, ("epsilon",
EPi (d2, ("y", EPi (ERealLt (dist (EVar "x", ESnd (EVar "y")), EVar "epsilon"),
("_", EApp (EVar "P", EVar "y"))))))))))))))
let e_fin_set a = ESig (EInt, ("n", EPi (ESig (EInt, ("k", ERatLt (EVar "k", EVar "n"))), ("k", a))))
let e_union us = ELam (d2, ("x", ETrunc 0 (ESig (e_fin_set e_open, ("u", EApp (EFst (EVar "u"), EVar "x"))))))
let e_in x u = EApp (u, x) (* Simplified, no Sigma needed *)
let e_singleton x = ESig (infer [] x, ("x", ELam (infer [] x, ("y", EId (infer [] x, EVar "y", EVar "x")))))
let e_and p q = ESig (EBool, ("p", q))
type exp =
(* ... *)
| ETrunc of int * exp (* Add truncation *)
(* Remove EOpen, EFinSet, etc., treat as derived *)
let heine_borel =
ELam (d2, ("cover", EPi (d2, ("x", ESig (e_open, ("U", e_in (EVar "x") (EVar "U"))))),
ESig (e_fin_set d2, ("finite",
EPi (d2, ("x", ETrunc 0 (ESig (e_fin_set e_open, ("Us", e_in (EVar "x") (e_union (EVar "Us"))))))))))
Truncation: Explicit in the subcover condition to ensure propositional behavior.
Boundedness Theorem
Classical: If f:[a,b]→R is continuous and [a,b] is compact, then f is bounded—there exist m,M∈R such that m≤f(x)≤M for all x∈[a,b].
Our Version: Domain: D1 =[0,1] (path from 0 to 1). f:D1 →R, continuous via ℑD1. Bounded: ∃m,M:R,∀x:D1, m≤f(x)≤M.
Constructive Notes:
Compactness: D1 is compact (as a path in R, analogous to D2’s hub-spoke structure).
Continuity: f:ℑD1→R ensures synthetic continuity.
Boundedness: Proven via sup and inf, which exist due to compactness.
let supf dom f =
let l = ELam (ERat, ("q", ESig (dom, ("x", ERealLt (EVar "q", EApp (f, EVar "x"))))))
and u = ELam (ERat, ("q", EPi (dom, ("x", ERealLt (EApp (f, EVar "x"), EVar "q")))))
in ECut (l, u, None, None, None, None, None, None, None) (* Simplified *)
let neg x = ERealOps (RealMinus, zero, x)
let inf_g f = neg (supf d1 (ELam (d1, ("x", neg (EApp (f, EVar "x"))))))
let boundedness =
ELam (d1, ("f", EPi (d1, ("x", EReal))),
ELam (EIm d1, ("cont",
ESig (EReal, ("m", ESig (EReal, ("M",
EPi (d1, ("x", EAnd (ERealLt (EVar "m", EApp (EVar "f", EVar "x")),
ERealLt (EApp (EVar "f", EVar "x"), EVar "M"))))))))))
Encoding the Theorem Proof
∀𝑓:𝐷^1→𝑅, continuous (𝑓)→∃𝑚,𝑀:𝑅,∀𝑥:𝐷^1,𝑚≤𝑓(𝑥)≤𝑀∀f:D^1 →R,continuous(f)→∃m,M:R,∀x:D^1 ,m≤f(x)≤M.
let boundedness_proof =
ELam (d1, ("f", EPi (d1, ("x", EReal))),
ELam (EIm d1, ("cont",
let m = inf_g (EVar "f") in
let M = supf d1 (EVar "f") in
EPair ("m", m,
EPair ("M", M,
ELam (d1, ("x",
let fx = EApp (EVar "f", EVar "x") in
EAnd (
(* m <= f(x) *)
ERealLt (EVar "m", fx), (* inf f <= f(x) by definition *)
(* f(x) <= M *)
ERealLt (fx, EVar "M") (* f(x) <= sup f by definition *)
))))))))
Continuity: A function f:D^1→R is continuous if it preserves “closeness”—formally, in HoTT with cohesive modalities, f:ℑD^ →R means f factors through the “shape” of D^1, ensuring synthetic continuity (no jumps).
Example Function: 𝑓(𝑥)=𝑥, the identity function on [0,1], which is continuous everywhere.
let zero = ECut (ELam (ERat, ("q", ERatLt (EVar "q", ERatConst (EIntConst Z.zero, EIntConst Z.one)))),
ELam (ERat, ("q", ERatLt (ERatConst (EIntConst Z.zero, EIntConst Z.one), EVar "q"))),
None, None, None, None, None, None, None)
let one = ECut (ELam (ERat, ("q", ERatLt (EVar "q", ERatConst (EIntConst Z.one, EIntConst Z.one)))),
ELam (ERat, ("q", ERatLt (ERatConst (EIntConst Z.one, EIntConst Z.one), EVar "q"))),
None, None, None, None, None, None, None)
let d1 = EPathP (EReal, zero, one) (* [0, 1] *)
let f_continuous =
ELam (d1, ("x", EVar "x")) (* f(x) = x *)
Type: Π(𝑥:𝐷^1), 𝑅, checked with continuity via ℑ𝐷^1.
Continuity Evidence: 𝑓: ℑ𝐷1→𝑅, since 𝑥 is a path parameter in 𝐷^1, and ℑ preserves this structure.
Intuition: f(x)=x maps 0→0, 1→1, and interpolates linearly, with no discontinuities.
let check_continuity =
infer [] (ELam (EIm d1, ("cont", EPi (d1, ("x", EReal))))) (ELam (d1, ("x", EVar "x")))
(* Returns EReal, confirming f: D^1 -> EReal, with Im d1 ensuring continuity *)
let boundedness_example =
ELam (d1, ("f", EPi (d1, ("x", EReal))),
ELam (EIm d1, ("cont",
let f = ELam (d1, ("x", EVar "x")) in
let m = zero in (* inf f = 0 *)
let M = one in (* sup f = 1 *)
EPair ("m", m,
EPair ("M", M,
ELam (d1, ("x",
let fx = EApp (f, EVar "x") in
EAnd (
ERealLt (EVar "m", fx), (* 0 <= x *)
ERealLt (fx, EVar "M") (* x <= 1 *)
))))))))
Intermediate Value Theorem (IVT)
Classical: If f:[a,b]→R is continuous and f(a)<c<f(b), then there exists x∈[a,b] such that f(x)=c.
Our Version: For simplicity, use [0,1]⊆D (a path in D2), with f:D1 →R, continuous via ℑ.