Last active
March 16, 2025 06:28
-
-
Save 5HT/39891e58ab7f6446b4fdf5a204a4b99e to your computer and use it in GitHub Desktop.
Julius-Brower
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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 |
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 *)
))))))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.