Skip to content

Instantly share code, notes, and snippets.

@smoofra
Created December 7, 2023 17:52
Show Gist options
  • Save smoofra/ba8c1d8861757e6be367257e2f7284e1 to your computer and use it in GitHub Desktop.
Save smoofra/ba8c1d8861757e6be367257e2f7284e1 to your computer and use it in GitHub Desktop.
extendable trees
type Grammer = {
"expression": unknown
}
type Expr<α extends Grammer> = α["expression"]
type Plus<α extends Grammer> = {
kind: "plus"
left: Expr<α>
right: Expr<α>
}
type Times<α extends Grammer> = {
kind: "times"
left: Expr<α>
right: Expr<α>
}
type Variable<α> = {
kind: "variable"
name: string
}
type AbelianGroupBaseGrammer<α extends Grammer> = {
"expression": Variable<α> | Plus<α>
}
type AbelianGroupGrammer = {
"expression": Expr<AbelianGroupBaseGrammer<AbelianGroupGrammer>>
}
type RingBaseGrammer<α extends Grammer> = {
"expression": Expr<AbelianGroupBaseGrammer<α>> | Times<α>
}
type RingGrammer = {
"expression": Expr<RingBaseGrammer<RingGrammer>>
}
const x : Expr<AbelianGroupGrammer> = {kind: "variable", name: "x"}
const y : Expr<AbelianGroupGrammer> = {kind: "variable", name: "y"}
const x_plus_y : Expr<AbelianGroupGrammer> = {kind:"plus", left: x, right: y}
const x_times_x_plus_y : Expr<RingGrammer> = {kind:"times", left: x, right: x_plus_y}
function group2ring(a : Expr<AbelianGroupGrammer>) : Expr<RingGrammer> {
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment