mirror of
https://github.com/HigherOrderCO/Kind.git
synced 2024-09-11 16:36:13 +03:00
initially, I wanted Kind2 to be bootstrapped, but I'm facing difficulties, because pure hvm parser is still much slower than the typescript parser; a few seconds vs instant to parse the entire codebase. I think HVM2 will need to have proper immutable strings (i.e., nodes that expand as if they were cons-strings, but are stored as buffers) before it can have fast parsers. as such, I'm making the decision to NOT bootstrap it now. that is a tough decision becase, honestly, if we don't do it as soon as possible in the development process, it is very unlikely we'll ever do it. but the tech isn't ready for it, and we need to move forward, so, that's the only solution. as such, I'll be rewritting the typescript to Rust, and we'll have a very similar design to former Kind2, with Rust doing the parsing, IO and error reporting, and a checker.hvm1 / checker.hvm2 file doing the actual type-checking (and HOAS-based type-level evaluation). also, I believe we'll not even generate .hvm from a .kind bootstrap. while losing types is significant, being able to manually edit the HVM is very helpful to debugging, it simplifies and accelerates the development process and removes many complications that are bought by bootstrapping
290 lines
11 KiB
Plaintext
290 lines
11 KiB
Plaintext
// TypeScript function we need to implement
|
|
//case "All": return `(All "${term.nam}" ${compile(term.inp, used_refs, dep)} λ${name(dep)} ${compile(term.bod(Var(term.nam,dep)), used_refs, dep + 1)})`;
|
|
//case "Lam": return `(Lam "${term.nam}" λ${name(dep)} ${compile(term.bod(Var(term.nam,dep)), used_refs, dep + 1)})`;
|
|
//case "App": return `(App ${compile(term.fun, used_refs, dep)} ${compile(term.arg, used_refs, dep)})`;
|
|
//case "Ann": return `(Ann ${compile(term.val, used_refs, dep)} ${compile(term.typ, used_refs, dep)})`;
|
|
//case "Slf": return `(Slf "${term.nam}" λ${name(dep)} ${compile(term.bod(Var(term.nam,dep)), used_refs, dep + 1)})`;
|
|
//case "Ins": return `(Ins ${compile(term.val, used_refs, dep)})`;
|
|
//case "Set": return `(Set)`;
|
|
//case "U60": return `(U60)`;
|
|
//case "Num": return `(Num ${term.val.toString()})`;
|
|
//case "Op2": return `(Op2 ${compile_oper(term.opr)} ${compile(term.fst, used_refs, dep)} ${compile(term.snd, used_refs, dep)})`;
|
|
//case "Mat": return `(Mat "${term.nam}" ${compile(term.x, used_refs, dep)} ${compile(term.z, used_refs, dep)} λ${name(dep)} ${compile(term.s(Var(term.nam,dep)), used_refs, dep + 1)} λ${name(dep)} ${compile(term.p(Var(term.nam,dep)), used_refs, dep + 1)})`;
|
|
//case "Txt": return `(Txt \`${term.txt}\`)`;
|
|
//case "Hol": return `(Hol "${term.nam}" ${context(dep)})`;
|
|
//case "Var": return name(term.idx);
|
|
//case "Ref": return (used_refs[term.nam] = 1), ("Book." + term.nam);
|
|
//case "Let": return `(Let "${term.nam}" ${compile(term.val, used_refs, dep)} λ${name(dep)} ${compile(term.bod(Var(term.nam,dep)), used_refs, dep + 1)})`;
|
|
|
|
// Example function in Kind's syntax
|
|
//Kind.Term.to_hvm.go
|
|
//: ∀(term: Kind.Term)
|
|
//∀(dep: Nat)
|
|
//∀(inc: Bool)
|
|
//∀(tab: Nat)
|
|
//String.Concatenator
|
|
//= λterm λdep λinc λtab
|
|
//let P = λx String.Concatenator
|
|
//let all = λnam λinp λbod λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "0")
|
|
//nil))
|
|
//let lam = λnam λbod λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "λ")
|
|
////((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
//((Kind.Text.show.go (String.cons '_' nam))
|
|
//((Kind.Text.show.go " "
|
|
//((Kind.Term.to_hvm.go (bod (Kind.var nam dep)) (Nat.succ dep) Bool.true tab)
|
|
//nil))))))
|
|
//let app = λfun λarg λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "(")
|
|
//((Kind.Term.to_hvm.go fun dep Bool.true tab)
|
|
//((Kind.Text.show.go " ")
|
|
//((Kind.Term.to_hvm.go arg dep Bool.true tab)
|
|
//((Kind.Text.show.go ")")
|
|
//nil))))))
|
|
//let ann = λval λtyp λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Term.to_hvm.go val dep Bool.true tab)
|
|
//nil))
|
|
//let slf = λnam λbod λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "0")
|
|
//nil))
|
|
//let ins = λval λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Term.to_hvm.go val dep Bool.true tab)
|
|
//nil))
|
|
//let ref = λnam λval λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "(Book.")
|
|
//((Kind.Text.show.go nam)
|
|
//((Kind.Text.show.go ")")
|
|
//nil))))
|
|
//let def = λnam λval λbod λnil
|
|
//let tab = ((~inc λx∀(x:Nat)Nat Nat.succ λx(x)) tab)
|
|
//((Kind.Text.show.go String.newline)
|
|
//((Kind.Text.show.go (String.indent tab))
|
|
//((Kind.Text.show.go "let ")
|
|
////((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
//((Kind.Text.show.go (String.cons '_' nam))
|
|
//((Kind.Text.show.go " = ")
|
|
//((Kind.Term.to_hvm.go val dep Bool.true tab)
|
|
//((Kind.Text.show.go " ")
|
|
//((Kind.Term.to_hvm.go (bod (Kind.var nam dep)) (Nat.succ dep) Bool.false tab)
|
|
//nil))))))))
|
|
//let set = λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "0")
|
|
//nil))
|
|
//let u60 = λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "0")
|
|
//nil))
|
|
//let num = λval λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((U60.show.go val)
|
|
//nil))
|
|
//let op2 = λopr λfst λsnd λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "(")
|
|
//((Kind.Oper.show.go opr) // TODO: Kind.Oper.to_hvm
|
|
//((Kind.Text.show.go " ")
|
|
//((Kind.Term.to_hvm.go fst dep Bool.true tab)
|
|
//((Kind.Text.show.go " ")
|
|
//((Kind.Term.to_hvm.go snd dep Bool.true tab)
|
|
//((Kind.Text.show.go ")")
|
|
//nil))))))))
|
|
//let mat = λnam λx λz λs λp λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "(U60.match ")
|
|
//((Kind.Term.to_hvm.go x dep Bool.true tab)
|
|
//((Kind.Text.show.go " ")
|
|
//((Kind.Term.to_hvm.go z dep Bool.true tab)
|
|
//((Kind.Text.show.go " ")
|
|
//((Kind.Text.show.go "λ")
|
|
//((Kind.Text.show.go (String.cons '_' (String.concat nam "_1")))
|
|
////((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
//((Kind.Text.show.go "(")
|
|
//((Kind.Term.to_hvm.go (s (Kind.var (String.concat nam "_1") dep)) (Nat.succ dep) Bool.true tab)
|
|
//((Kind.Text.show.go ")")
|
|
//((Kind.Text.show.go ")")
|
|
//nil))))))))))))
|
|
//let txt = λtxt λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "(Str ")
|
|
//((Kind.Text.show.go String.quote)
|
|
//((Kind.Text.show.go txt)
|
|
//((Kind.Text.show.go String.quote)
|
|
//((Kind.Text.show.go ")")
|
|
//nil))))))
|
|
//let hol = λnam λctx λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
//((Kind.Text.show.go "0")
|
|
//nil))
|
|
//let var = λnam λidx λnil
|
|
//((Kind.Term.to_hvm.nl inc tab)
|
|
////((Kind.Text.show.go (U60.name (U60.from_nat idx)))
|
|
//((Kind.Text.show.go (String.cons '_' nam))
|
|
//nil))
|
|
//(~term P all lam app ann slf ins ref def set u60 num op2 mat txt hol var)
|
|
|
|
// Kind function we need to implement
|
|
Kind.Term.to_hvm.quoted.go
|
|
: ∀(term: Kind.Term)
|
|
∀(dep: Nat)
|
|
String.Concatenator
|
|
= λterm λdep
|
|
let P = λx String.Concatenator
|
|
let all = λnam λinp λbod λnil
|
|
((Kind.Text.show.go "(All ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go inp dep)
|
|
((Kind.Text.show.go " λ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go (bod (Kind.var nam dep)) (Nat.succ dep))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))))))
|
|
let lam = λnam λbod λnil
|
|
((Kind.Text.show.go "(Lam ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " λ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go (bod (Kind.var nam dep)) (Nat.succ dep))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))))
|
|
let app = λfun λarg λnil
|
|
((Kind.Text.show.go "(App ")
|
|
((Kind.Term.to_hvm.quoted.go fun dep)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go arg dep)
|
|
((Kind.Text.show.go ")")
|
|
nil)))))
|
|
let ann = λval λtyp λnil
|
|
((Kind.Text.show.go "(Ann ")
|
|
((Kind.Term.to_hvm.quoted.go val dep)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go typ dep)
|
|
((Kind.Text.show.go ")")
|
|
nil)))))
|
|
let slf = λnam λbod λnil
|
|
((Kind.Text.show.go "(Slf ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " λ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go (bod (Kind.var nam dep)) (Nat.succ dep))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))))
|
|
let ins = λval λnil
|
|
((Kind.Text.show.go "(Ins ")
|
|
((Kind.Term.to_hvm.quoted.go val dep)
|
|
((Kind.Text.show.go ")")
|
|
nil)))
|
|
let ref = λnam λval λnil
|
|
((Kind.Text.show.go "(Ref ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go (String.concat "Book." nam))
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go ")")
|
|
nil)))))
|
|
let def = λnam λval λbod λnil
|
|
((Kind.Text.show.go "(Let ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go val dep)
|
|
((Kind.Text.show.go " λ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go (bod (Kind.var nam dep)) (Nat.succ dep))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))))))
|
|
let set = λnil
|
|
((Kind.Text.show.go "(Set)")
|
|
nil)
|
|
let u60 = λnil
|
|
((Kind.Text.show.go "(U60)")
|
|
nil)
|
|
let num = λval λnil
|
|
((Kind.Text.show.go "(Num ")
|
|
((U60.show.go val)
|
|
((Kind.Text.show.go ")")
|
|
nil)))
|
|
let op2 = λopr λfst λsnd λnil
|
|
((Kind.Text.show.go "(Op2 ")
|
|
((Kind.Oper.show.go opr)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go fst dep)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go snd dep)
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))
|
|
let mat = λnam λx λz λs λp λnil
|
|
((Kind.Text.show.go "(Mat ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go x dep)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go z dep)
|
|
((Kind.Text.show.go " λ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go (s (Kind.var nam dep)) (Nat.succ dep))
|
|
((Kind.Text.show.go " λ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat dep)))
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Term.to_hvm.quoted.go (p (Kind.var nam dep)) (Nat.succ dep))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))))))))))))
|
|
let txt = λtxt λnil
|
|
((Kind.Text.show.go "(Txt ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go txt)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go ")")
|
|
nil)))))
|
|
let hol = λnam λctx λnil
|
|
((Kind.Text.show.go "(Hol ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Text.show.go (String.wrap "[" (String.join "," (Kind.Term.to_hvm.quoted.hol.ctx dep #0)) "]"))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))
|
|
let var = λnam λidx λnil
|
|
((Kind.Text.show.go "(Var ")
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go nam)
|
|
((Kind.Text.show.go String.quote)
|
|
((Kind.Text.show.go " ")
|
|
((Kind.Text.show.go (U60.name (U60.from_nat idx)))
|
|
((Kind.Text.show.go ")")
|
|
nil)))))))
|
|
(~term P all lam app ann slf ins ref def set u60 num op2 mat txt hol var)
|
|
|
|
Kind.Term.to_hvm.quoted.hol.ctx
|
|
: ∀(dep: Nat)
|
|
∀(idx: #U60)
|
|
(List String)
|
|
= λdep λidx
|
|
let P = λx (List String)
|
|
let succ = λdep.pred (List.cons String (U60.name idx) (Kind.Term.to_hvm.quoted.hol.ctx dep #(+ idx #1)))
|
|
let zero = (List.nil String)
|
|
(~dep P succ zero)
|