Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax factory #1530

Merged
merged 26 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7815d90
Add DefaultAnnotation module and factory functions for term annotations
7h3kk1d Feb 20, 2025
168ee58
Added optional annotation params to factory methods and rewrite some …
7h3kk1d Feb 20, 2025
96b7190
Nest functions under modules
7h3kk1d Feb 22, 2025
b797431
Migrate more tests
7h3kk1d Feb 22, 2025
fbf7226
More tests
7h3kk1d Feb 22, 2025
fc2299f
All references to the old error/no_error functions removed
7h3kk1d Feb 22, 2025
e1e1597
Use Grammar types for menhir
7h3kk1d Feb 22, 2025
9496947
Merge branch 'syntax_playground' into syntax_factory
7h3kk1d Feb 22, 2025
47c8e45
Merge branch 'syntax_playground' into syntax_factory
7h3kk1d Feb 24, 2025
b7c215a
Merge branch 'syntax_playground' into syntax_factory
7h3kk1d Feb 25, 2025
c5ebe7d
Stop using global mutable array in menhir conversion
7h3kk1d Feb 25, 2025
fd15107
Merge branch 'dev' into syntax_factory
7h3kk1d Feb 25, 2025
3a85821
Remove unused UnitAnnotation module
7h3kk1d Feb 25, 2025
b5c2ecc
Move id indicated conversion to Conversion.re
7h3kk1d Feb 26, 2025
59d90f5
Add FreshGrammar module to provide default IdTag values
7h3kk1d Feb 26, 2025
3ce71cb
Use FreshGrammar in Test_Menhir
7h3kk1d Feb 26, 2025
d77e2f8
Use FreshGrammar in Test_MakeTerm
7h3kk1d Feb 26, 2025
3affe2c
Remove unnecessary curly braces
7h3kk1d Feb 26, 2025
d679c16
Refactor built-in functions to use FreshGrammar
7h3kk1d Feb 26, 2025
e035475
Use FreshGrammar in Transition
7h3kk1d Feb 26, 2025
d2cf90f
Remove unused cls from Exp
7h3kk1d Feb 26, 2025
a564842
Add enumerate deriving to deriving for various types
7h3kk1d Feb 26, 2025
b6b6629
Add UnitGrammar module to Factory for unit type
7h3kk1d Feb 26, 2025
933346d
Add sample_expression function to construct expressions from expressi…
7h3kk1d Feb 26, 2025
b760a03
Add tests for typ and pattern clsses
7h3kk1d Feb 26, 2025
9d3f368
Added tpat classes test
7h3kk1d Feb 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
391 changes: 391 additions & 0 deletions src/haz3lcore/lang/term/Grammar.re
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,394 @@
| MultiHole(l) => MultiHole(List.map(x => map_any_annotation(f, x), l))
};
};

module type DefaultAnnotation = {
type t;
let default_value: unit => t;
};
Comment on lines +421 to +424
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have 2 types here t for the type of the annotation and another type s that could be used in the factory methods below and require a default_value : option(s) -> t as part of the builder. I haven't bothered but for optional annotations it allows the people calling the builder to use a different type like not provide Some constructors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave as a comment in the file


module Factory = (DefaultAnnotation: DefaultAnnotation) => {
type exp = exp_t(DefaultAnnotation.t);
type pat = pat_t(DefaultAnnotation.t);
type typ = typ_t(DefaultAnnotation.t);
type tpat = tpat_t(DefaultAnnotation.t);
type typ_provenance = type_provenance(DefaultAnnotation.t);

let default_annotation = ann =>
Option.value(~default=DefaultAnnotation.default_value(), ann);
module Exp = {
let invalid = (~ann=?, s): exp_t(DefaultAnnotation.t) => {
term: Invalid(s),
annotation: default_annotation(ann),
};
let empty_hole = (~ann=?, ()): exp_t(DefaultAnnotation.t) => {
term: EmptyHole,
annotation: default_annotation(ann),
};
let multi_hole = (~ann=?, l): exp_t(DefaultAnnotation.t) => {
term: MultiHole(l),
annotation: default_annotation(ann),
};
let dynamic_error_hole = (~ann=?, e, err): exp_t(DefaultAnnotation.t) => {
term: DynamicErrorHole(e, err),
annotation: default_annotation(ann),
};
let failed_cast = (~ann=?, e, t1, t2): exp_t(DefaultAnnotation.t) => {
term: FailedCast(e, t1, t2),
annotation: default_annotation(ann),
};
let deferral = (~ann=?, pos): exp_t(DefaultAnnotation.t) => {
term: Deferral(pos),
annotation: default_annotation(ann),
};
let undefined = (~ann=?, ()): exp_t(DefaultAnnotation.t) => {
term: Undefined,
annotation: default_annotation(ann),
};
let bool = (~ann=?, b): exp_t(DefaultAnnotation.t) => {
term: Bool(b),
annotation: default_annotation(ann),
};
let int = (~ann=?, i): exp_t(DefaultAnnotation.t) => {
term: Int(i),
annotation: default_annotation(ann),
};
let float = (~ann=?, f): exp_t(DefaultAnnotation.t) => {
term: Float(f),
annotation: default_annotation(ann),
};
let string = (~ann=?, s): exp_t(DefaultAnnotation.t) => {
term: String(s),
annotation: default_annotation(ann),
};
let list_lit = (~ann=?, l): exp_t(DefaultAnnotation.t) => {
term: ListLit(l),
annotation: default_annotation(ann),
};
let constructor = (~ann=?, s, t): exp_t(DefaultAnnotation.t) => {
term: Constructor(s, t),
annotation: default_annotation(ann),
};
let fn = (~ann=?, p, e, t, v): exp_t(DefaultAnnotation.t) => {
term: Fun(p, e, t, v),
annotation: default_annotation(ann),
};
let typ_fun = (~ann=?, p, e, v): exp_t(DefaultAnnotation.t) => {
term: TypFun(p, e, v),
annotation: default_annotation(ann),
};
let tuple = (~ann=?, l): exp_t(DefaultAnnotation.t) => {
term: Tuple(l),
annotation: default_annotation(ann),
};
let label = (~ann=?, l): exp_t(DefaultAnnotation.t) => {
term: Label(l),
annotation: default_annotation(ann),
};
let tup_label = (~ann=?, l, e): exp_t(DefaultAnnotation.t) => {
term: TupLabel(l, e),
annotation: default_annotation(ann),
};
let dot = (~ann=?, e1, e2): exp_t(DefaultAnnotation.t) => {
term: Dot(e1, e2),
annotation: default_annotation(ann),
};
let var = (~ann=?, v): exp_t(DefaultAnnotation.t) => {
term: Var(v),
annotation: default_annotation(ann),
};
let let_ = (~ann=?, p, e1, e2): exp_t(DefaultAnnotation.t) => {
term: Let(p, e1, e2),
annotation: default_annotation(ann),
};
let fix_f = (~ann=?, p, e, env): exp_t(DefaultAnnotation.t) => {
term: FixF(p, e, env),
annotation: default_annotation(ann),
};
let ty_alias = (~ann=?, p, t, e): exp_t(DefaultAnnotation.t) => {
term: TyAlias(p, t, e),
annotation: default_annotation(ann),
};
let ap = (~ann=?, d, e1, e2): exp_t(DefaultAnnotation.t) => {
term: Ap(d, e1, e2),
annotation: default_annotation(ann),
};
let typ_ap = (~ann=?, e, t): exp_t(DefaultAnnotation.t) => {
term: TypAp(e, t),
annotation: default_annotation(ann),
};
let deferred_ap = (~ann=?, e, l): exp_t(DefaultAnnotation.t) => {
term: DeferredAp(e, l),
annotation: default_annotation(ann),
};
let if_ = (~ann=?, e1, e2, e3): exp_t(DefaultAnnotation.t) => {
term: If(e1, e2, e3),
annotation: default_annotation(ann),
};
let seq = (~ann=?, e1, e2): exp_t(DefaultAnnotation.t) => {
term: Seq(e1, e2),
annotation: default_annotation(ann),
};
let test = (~ann=?, e): exp_t(DefaultAnnotation.t) => {
term: Test(e),
annotation: default_annotation(ann),
};
let filter = (~ann=?, k, e): exp_t(DefaultAnnotation.t) => {
term: Filter(k, e),
annotation: default_annotation(ann),
};
let closure = (~ann=?, env, e): exp_t(DefaultAnnotation.t) => {

Check warning on line 556 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L556

Added line #L556 was not covered by tests
term: Closure(env, e),
annotation: default_annotation(ann),

Check warning on line 558 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L558

Added line #L558 was not covered by tests
};
let parens = (~ann=?, e): exp_t(DefaultAnnotation.t) => {
term: Parens(e),
annotation: default_annotation(ann),
};
let cons = (~ann=?, e1, e2): exp_t(DefaultAnnotation.t) => {
term: Cons(e1, e2),
annotation: default_annotation(ann),
};
let list_concat = (~ann=?, e1, e2): exp_t(DefaultAnnotation.t) => {
term: ListConcat(e1, e2),
annotation: default_annotation(ann),
};
let un_op = (~ann=?, op, e): exp_t(DefaultAnnotation.t) => {
term: UnOp(op, e),
annotation: default_annotation(ann),
};
let bin_op = (~ann=?, op, e1, e2): exp_t(DefaultAnnotation.t) => {
term: BinOp(op, e1, e2),
annotation: default_annotation(ann),
};
let builtin_fun = (~ann=?, s): exp_t(DefaultAnnotation.t) => {
term: BuiltinFun(s),
annotation: default_annotation(ann),
};
let match = (~ann=?, e, l): exp_t(DefaultAnnotation.t) => {
term: Match(e, l),
annotation: default_annotation(ann),
};
let cast = (~ann=?, e, t1, t2): exp_t(DefaultAnnotation.t) => {
term: Cast(e, t1, t2),
annotation: default_annotation(ann),
};
};
module Pat = {
let invalid = (~ann=?, s): pat_t(DefaultAnnotation.t) => {

Check warning on line 594 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L594

Added line #L594 was not covered by tests
term: Invalid(s),
annotation: default_annotation(ann),

Check warning on line 596 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L596

Added line #L596 was not covered by tests
};
let empty_hole = (~ann=?, ()): pat_t(DefaultAnnotation.t) => {
term: EmptyHole,
annotation: default_annotation(ann),
};
let multi_hole = (~ann=?, l): pat_t(DefaultAnnotation.t) => {

Check warning on line 602 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L602

Added line #L602 was not covered by tests
term: MultiHole(l),
annotation: default_annotation(ann),

Check warning on line 604 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L604

Added line #L604 was not covered by tests
};
let wild = (~ann=?, ()): pat_t(DefaultAnnotation.t) => {
term: Wild,
annotation: default_annotation(ann),
};
let int = (~ann=?, i): pat_t(DefaultAnnotation.t) => {
term: Int(i),
annotation: default_annotation(ann),
};
let float = (~ann=?, f): pat_t(DefaultAnnotation.t) => {
term: Float(f),
annotation: default_annotation(ann),
};
let bool = (~ann=?, b): pat_t(DefaultAnnotation.t) => {
term: Bool(b),
annotation: default_annotation(ann),
};
let string = (~ann=?, s): pat_t(DefaultAnnotation.t) => {
term: String(s),
annotation: default_annotation(ann),
};
let list_lit = (~ann=?, l): pat_t(DefaultAnnotation.t) => {
term: ListLit(l),
annotation: default_annotation(ann),
};
let constructor = (~ann=?, s, t): pat_t(DefaultAnnotation.t) => {
term: Constructor(s, t),
annotation: default_annotation(ann),
};
let cons = (~ann=?, p1, p2): pat_t(DefaultAnnotation.t) => {
term: Cons(p1, p2),
annotation: default_annotation(ann),
};
let var = (~ann=?, v): pat_t(DefaultAnnotation.t) => {
term: Var(v),
annotation: default_annotation(ann),
};
let tuple = (~ann=?, l): pat_t(DefaultAnnotation.t) => {
term: Tuple(l),
annotation: default_annotation(ann),
};
let label = (~ann=?, l): pat_t(DefaultAnnotation.t) => {
term: Label(l),
annotation: default_annotation(ann),
};
let tup_label = (~ann=?, p1, p2): pat_t(DefaultAnnotation.t) => {
term: TupLabel(p1, p2),
annotation: default_annotation(ann),
};
let parens = (~ann=?, p): pat_t(DefaultAnnotation.t) => {
term: Parens(p),
annotation: default_annotation(ann),
};
let ap = (~ann=?, p1, p2): pat_t(DefaultAnnotation.t) => {
term: Ap(p1, p2),
annotation: default_annotation(ann),
};
let cast = (~ann=?, p, t1, t2): pat_t(DefaultAnnotation.t) => {
term: Cast(p, t1, t2),
annotation: default_annotation(ann),
};
};

module Typ = {
let unknown = (~ann=?, p): typ_t(DefaultAnnotation.t) => {
term: Unknown(p),
annotation: default_annotation(ann),
};
let int = (~ann=?, ()): typ_t(DefaultAnnotation.t) => {
term: Int,
annotation: default_annotation(ann),
};
let float = (~ann=?, ()): typ_t(DefaultAnnotation.t) => {
term: Float,
annotation: default_annotation(ann),
};
let bool = (~ann=?, ()): typ_t(DefaultAnnotation.t) => {
term: Bool,
annotation: default_annotation(ann),
};
let string = (~ann=?, ()): typ_t(DefaultAnnotation.t) => {
term: String,
annotation: default_annotation(ann),
};
let var = (~ann=?, s): typ_t(DefaultAnnotation.t) => {
term: Var(s),
annotation: default_annotation(ann),
};
let list = (~ann=?, t): typ_t(DefaultAnnotation.t) => {
term: List(t),
annotation: default_annotation(ann),
};
let arrow = (~ann=?, t1, t2): typ_t(DefaultAnnotation.t) => {
term: Arrow(t1, t2),
annotation: default_annotation(ann),
};
let sum = (~ann=?, m): typ_t(DefaultAnnotation.t) => {
term: Sum(m),
annotation: default_annotation(ann),
};
let prod = (~ann=?, l): typ_t(DefaultAnnotation.t) => {
term: Prod(l),
annotation: default_annotation(ann),
};
let label = (~ann=?, l): typ_t(DefaultAnnotation.t) => {
term: Label(l),
annotation: default_annotation(ann),
};
let tup_label = (~ann=?, t1, t2): typ_t(DefaultAnnotation.t) => {
term: TupLabel(t1, t2),
annotation: default_annotation(ann),
};
let parens = (~ann=?, t): typ_t(DefaultAnnotation.t) => {
term: Parens(t),
annotation: default_annotation(ann),
};
let ap = (~ann=?, t1, t2): typ_t(DefaultAnnotation.t) => {

Check warning on line 721 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L721

Added line #L721 was not covered by tests
term: Ap(t1, t2),
annotation: default_annotation(ann),

Check warning on line 723 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L723

Added line #L723 was not covered by tests
};
let rec_ = (~ann=?, tp, t): typ_t(DefaultAnnotation.t) => {
term: Rec(tp, t),
annotation: default_annotation(ann),
};
let forall = (~ann=?, tp, t): typ_t(DefaultAnnotation.t) => {
term: Forall(tp, t),
annotation: default_annotation(ann),
};
};

module TPat = {
let invalid = (~ann=?, s): tpat_t(DefaultAnnotation.t) => {

Check warning on line 736 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L736

Added line #L736 was not covered by tests
term: Invalid(s),
annotation: default_annotation(ann),

Check warning on line 738 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L738

Added line #L738 was not covered by tests
};
let empty_hole = (~ann=?, ()): tpat_t(DefaultAnnotation.t) => {
term: EmptyHole,
annotation: default_annotation(ann),
};
let multi_hole = (~ann=?, l): tpat_t(DefaultAnnotation.t) => {

Check warning on line 744 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L744

Added line #L744 was not covered by tests
term: MultiHole(l),
annotation: default_annotation(ann),

Check warning on line 746 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L746

Added line #L746 was not covered by tests
};
let var = (~ann=?, s): tpat_t(DefaultAnnotation.t) => {
term: Var(s),
annotation: default_annotation(ann),
};
};

module Rul = {
let rul_invalid = (~ann=?, s): rul_t(DefaultAnnotation.t) => {

Check warning on line 755 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L755

Added line #L755 was not covered by tests
term: Invalid(s),
annotation: default_annotation(ann),

Check warning on line 757 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L757

Added line #L757 was not covered by tests
};
let rul_hole = (~ann=?, l): rul_t(DefaultAnnotation.t) => {

Check warning on line 759 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L759

Added line #L759 was not covered by tests
term: Hole(l),
annotation: default_annotation(ann),

Check warning on line 761 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L761

Added line #L761 was not covered by tests
};
let rul_rules = (~ann=?, e, l): rul_t(DefaultAnnotation.t) => {

Check warning on line 763 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L763

Added line #L763 was not covered by tests
term: Rules(e, l),
annotation: default_annotation(ann),

Check warning on line 765 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L765

Added line #L765 was not covered by tests
};
};

let environment = (env): environment_t(DefaultAnnotation.t) => {
VarBstMap.Ordered.mapo(((_, y)) => map_exp_annotation(x => x, y), env);

Check warning on line 770 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L769-L770

Added lines #L769 - L770 were not covered by tests
};

let closure_environment =
(id, env): closure_environment_t(DefaultAnnotation.t) => {
(id, environment(env));

Check warning on line 775 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L774-L775

Added lines #L774 - L775 were not covered by tests
};

module StepperFilter = {
let filter = (f): stepper_filter_kind_t(DefaultAnnotation.t) => {
Filter({pat: map_exp_annotation(x => x, f.pat), act: f.act});

Check warning on line 780 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L779-L780

Added lines #L779 - L780 were not covered by tests
};
let residue = (i, act): stepper_filter_kind_t(DefaultAnnotation.t) => {

Check warning on line 782 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L782

Added line #L782 was not covered by tests
Residue(i, act);
};
};

module TypeHole = {
let invalid = (s): type_hole(DefaultAnnotation.t) => {

Check warning on line 788 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L788

Added line #L788 was not covered by tests
Invalid(s);
};
let empty_hole = (): type_hole(DefaultAnnotation.t) => {

Check warning on line 791 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L791

Added line #L791 was not covered by tests
EmptyHole;
};
let multi_hole = (l): type_hole(DefaultAnnotation.t) => {

Check warning on line 794 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L794

Added line #L794 was not covered by tests
MultiHole(l);
};
};

module TypeProvenance = {
let syn_switch = (): type_provenance(DefaultAnnotation.t) => {

Check warning on line 800 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L800

Added line #L800 was not covered by tests
SynSwitch;
};
let hole = (h): type_provenance(DefaultAnnotation.t) => {

Check warning on line 803 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L803

Added line #L803 was not covered by tests
Hole(h);
};
let internal = (): type_provenance(DefaultAnnotation.t) => {

Check warning on line 806 in src/haz3lcore/lang/term/Grammar.re

View check run for this annotation

Codecov / codecov/patch

src/haz3lcore/lang/term/Grammar.re#L806

Added line #L806 was not covered by tests
Internal;
};
};
};
Loading