Created
February 5, 2025 21:45
-
-
Save 7h3kk1d/2e59a44bb367fe75fba7478e67d91283 to your computer and use it in GitHub Desktop.
module demonstration of grammar
This file contains 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
open Util | |
module type Grammar = sig | |
type typ [@@deriving sexp, show] | |
type exp [@@deriving sexp, show] | |
end | |
module Typ (G : Grammar) = struct | |
type t = | |
| Int | |
| Float | |
| Bool | |
| String | |
| Var of string | |
| List of G.typ | |
| Prod of G.typ list | |
[@@deriving sexp, show] | |
end | |
module Exp (G : Grammar) = struct | |
type t = Cast of G.exp * G.typ | Add of G.exp * G.exp | Int of int | |
[@@deriving sexp, show] | |
end | |
module rec Grammar : | |
(Grammar with type typ = Typ(Grammar).t and type exp = Exp(Grammar).t) = | |
struct | |
module T = Typ (Grammar) | |
module E = Exp (Grammar) | |
type typ = T.t [@@deriving sexp, show] | |
type exp = E.t [@@deriving sexp, show] | |
end | |
module rec IdGrammar : | |
(Grammar | |
with type typ = int * Typ(IdGrammar).t | |
and type exp = int * Exp(IdGrammar).t) = struct | |
module T = Typ (IdGrammar) | |
module E = Exp (IdGrammar) | |
type typ = int * T.t [@@deriving sexp, show] | |
type exp = int * E.t [@@deriving sexp, show] | |
end | |
module T = Typ (Grammar) | |
module E = Exp (Grammar) | |
let plain_expression = | |
(let open E in | |
Cast | |
( Int 1, | |
let open T in | |
Prod [ Int; Bool; String ] ) | |
: Grammar.exp) | |
let serialized = Grammar.show_exp plain_expression | |
module IT = Typ (IdGrammar) | |
module IE = Exp (IdGrammar) | |
let plain_expression = | |
(let open IE in | |
( 3, | |
Cast | |
( (1, Int 1), | |
( 7, | |
let open IT in | |
Prod [ (4, Int); (5, Bool); (6, String) ] ) ) ) | |
: IdGrammar.exp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment