Skip to content

Instantly share code, notes, and snippets.

@leque
Last active April 3, 2023 09:18
Show Gist options
  • Save leque/611fb0326350de16763a6de8fb7febf0 to your computer and use it in GitHub Desktop.
Save leque/611fb0326350de16763a6de8fb7febf0 to your computer and use it in GitHub Desktop.
Flexiblly Extend Nested Structures with OCaml
(*
inspired by
- Flexiblly Extend Nested Structures - "Trees that Grow" in TypeScript
https://dev.to/igrep/flexiblly-extend-nested-structures-trees-that-grow-in-typescript-4347
- 入れ子構造を自由に拡張する – TypeScript版「Trees that Grow」 (Japanese translation of above article)
https://eng-blog.iij.ad.jp/archives/18900
*)
type 'a literal = [`Literal of int * 'lit]
constraint 'a = <literal: 'lit; ..>
type 'a variable = [`Variable of string * 'var]
constraint 'a = <variable: 'var; ..>
type 'a set_variable = [`SetVariable of string * 'expr * 'sv]
constraint 'a = <expression: 'expr; set_variable: 'sv; ..>
type 'a func = [`Func of string * 'expr * 'func]
constraint 'a = <expression: 'expr; func: 'func; ..>
type 'a call_func = [`CallFunc of 'expr * 'expr * 'call_func]
constraint 'a = <expression: 'expr; call_func: 'call_func; ..>
module Full = struct
type ext = < literal: unit
; variable: unit
; set_variable : unit
; func : unit
; call_func : unit
; expression : t
>
and 'a t_ = ['a literal | 'a variable | 'a set_variable | 'a func | 'a call_func]
and t = ext t_
end
module NoFunc = struct
type ext = < literal: unit
; variable: unit
; set_variable : unit
; expression : t
>
and 'a t_ = ['a literal | 'a variable | 'a set_variable ]
and t = ext t_
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment