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

Create a new input syntax for function. #1254

Draft
wants to merge 22 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7280a28
For draft PR
DavidFangWJ Mar 25, 2024
1077ddc
Could not compile for now
DavidFangWJ Apr 10, 2024
948cbc7
Confused on how to write it.
DavidFangWJ Apr 14, 2024
b741422
Can compile now. Not sure about the part to determine IDs.
DavidFangWJ Apr 14, 2024
8fac38a
Reconstructed the code to avoid most of the repetitions.
DavidFangWJ Apr 21, 2024
3ba68df
Removes the check for arguent's syntax.
DavidFangWJ Apr 22, 2024
e851b7f
Merge branch 'dev' into haz3l-new-func-syntax
DavidFangWJ Apr 22, 2024
50b95f4
Change the determination of input type into a recursive function.
DavidFangWJ Apr 22, 2024
e7eaab5
Merge branch 'haz3l-new-func-syntax' of https://github.com/hazelgrove…
DavidFangWJ Apr 22, 2024
e434fa8
Merge branch 'dev' into haz3l-new-func-syntax
DavidFangWJ Apr 24, 2024
81d039f
Merge branch 'dev' into haz3l-new-func-syntax
DavidFangWJ May 13, 2024
206531c
Move the code to check annotated functions.
DavidFangWJ May 13, 2024
a7de72c
Added checks for annotated functions into the process.
DavidFangWJ May 14, 2024
7dec214
Fix p_syn in Statics.re.
DavidFangWJ Jul 21, 2024
67a0c84
Makes the program work. Probably needs more changes.
DavidFangWJ Aug 11, 2024
a38b124
Merge branch 'dev' into haz3l-new-func-syntax
DavidFangWJ Sep 2, 2024
54e0338
Tried my best to change the code. Could not fix the last bugs on Line…
DavidFangWJ Sep 15, 2024
8be885d
Preliminary change. Fails.
DavidFangWJ Sep 30, 2024
0e350d2
Aligns with `dev` except for two files I changed.
DavidFangWJ Sep 30, 2024
9b060d8
Merge branch 'dev' into haz3l-new-func-syntax
DavidFangWJ Oct 7, 2024
2a7c517
Add debugging statement.
DavidFangWJ Nov 2, 2024
8bab67e
Minimal change.
DavidFangWJ Nov 22, 2024
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
6 changes: 6 additions & 0 deletions src/haz3lcore/dynamics/Elaborator.re
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ let rec elaborate_pattern =
DHPat.Tuple(ps') |> rewrap |> cast_from(Typ.Prod(tys) |> Typ.temp);
| Ap(p1, p2) =>
let (p1', ty1) = elaborate_pattern(m, p1);
// print_endline("p1' = " ++ DHPat.show(p1'));
// print_endline("ty1 = " ++ Typ.show(ty1));
let (p2', ty2) = elaborate_pattern(m, p2);
let (ty1l, ty1r) = Typ.matched_arrow(ctx, ty1);
let p1'' = fresh_pat_cast(p1', ty1, Arrow(ty1l, ty1r) |> Typ.temp);
Expand Down Expand Up @@ -295,14 +297,18 @@ let rec elaborate = (m: Statics.Map.t, uexp: UExp.t): (DHExp.t, Typ.t) => {
};
}
);
print_endline("ELA p(1) = " ++ UPat.show(p));
print_endline("ELA m = " ++ Statics.Map.show(m));
let (p, ty1) = elaborate_pattern(m, p);
let is_recursive =
Statics.is_recursive(ctx, p, def, ty1)
&& Pat.get_bindings(p)
|> Option.get
|> List.exists(f => VarMap.lookup(co_ctx, f) != None);
if (!is_recursive) {
print_endline("ELA p(2) = " ++ DHPat.show(p));
let def = add_name(Pat.get_var(p), def);
print_endline("ELA def = " ++ DHExp.show(def));
let (def, ty2) = elaborate(m, def);
let (body, ty) = elaborate(m, body);
Exp.Let(p, fresh_cast(def, ty2, ty1), body)
Expand Down
76 changes: 76 additions & 0 deletions src/haz3lcore/statics/Statics.re
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,47 @@ let typ_exp_unop: Operators.op_un => (Typ.t, Typ.t) =
| Bool(Not) => (Bool |> Typ.temp, Bool |> Typ.temp)
| Int(Minus) => (Int |> Typ.temp, Int |> Typ.temp);

let rec caf_input_type = (args: UPat.t): UTyp.t => {
let (arg_term, arg_rule) = IdTagged.unwrap(args);
switch (arg_term) {
| Tuple(ps) => UTyp.Prod(List.map(caf_input_type, ps)) |> arg_rule
| Cast(_, _, t) => t
| _ => Unknown(Hole(EmptyHole)) |> arg_rule
};
};

let check_annotated_function_helper =
(pat: UPat.t, ret_type: UTyp.t): option((Var.t, UPat.t, UTyp.t)) => {
let (pat_term, pat_rule) = IdTagged.unwrap(pat);
switch (pat_term) {
| Ap(func_name_group, args) =>
switch (IdTagged.term_of(func_name_group)) {
| Var(func_name) =>
let func_type = UTyp.Arrow(caf_input_type(args), ret_type) |> pat_rule;
Some((func_name, args, func_type));
| _ => None
}
| _ => None
};
};

/*
* Check whether a particular let binding is an annotated function under the new syntax.
* It is parsed as a constructor syntax.
* I am not sure what is the top-level pattern; the only thing similar seems to be UPat.Ap.
*/
let check_annotated_function =
(pat: UPat.t): option((Var.t, UPat.t, UTyp.t)) => {
let (pat_term, pat_rule) = IdTagged.unwrap(pat);
switch (pat_term) {
| Cast(inner_pat, _, ret_type) =>
check_annotated_function_helper(inner_pat, ret_type)
| _ =>
let ret_type = UTyp.Unknown(Hole(EmptyHole)) |> pat_rule;
check_annotated_function_helper(pat, ret_type);
};
};

let rec any_to_info_map =
(~ctx: Ctx.t, ~ancestors, any: Any.t, m: Map.t): (CoCtx.t, Map.t) =>
switch (any) {
Expand Down Expand Up @@ -399,8 +440,41 @@ and uexp_to_info_map =
go_pat(~is_synswitch=true, ~co_ctx=CoCtx.empty, ~mode=Syn, p, m);
let (def, p_ana_ctx, m, ty_p_ana) =
if (!is_recursive(ctx, p, def, p_syn.ty)) {
let (def, p, p_syn) =
switch (check_annotated_function(p)) {
| Some((f_name, f_args, f_type)) =>
let def: UExp.t = {
ids,
term: UExp.Fun(f_args, def, None, None),
copied: false,
};
let p: UPat.t = {
ids,
term:
UPat.Cast(
{ids, term: UPat.Var(f_name), copied: false},
f_type,
Typ.temp(Unknown(Internal)),
),
copied: false,
};
let (p_syn, _) =
go_pat(
~is_synswitch=true,
~co_ctx=CoCtx.empty,
~mode=Syn,
p,
m,
);
(def, p, p_syn);
| None => (def, p, p_syn) // Use the original code
};
// print_endline("STA def = " ++ UExp.show(def));
// print_endline("STA p = " ++ UPat.show(p));
// print_endline("STA p_syn = " ++ Info.show_pat(p_syn));
let (def, m) = go(~mode=Ana(p_syn.ty), def, m);
let ty_p_ana = def.ty;

let (p_ana', _) =
go_pat(
~is_synswitch=false,
Expand All @@ -409,12 +483,14 @@ and uexp_to_info_map =
p,
m,
);
// print_endline("STA p_ana'.ctx = " ++ Ctx.show(p_ana'.ctx));
(def, p_ana'.ctx, m, ty_p_ana);
} else {
let (def_base, _) =
go'(~ctx=p_syn.ctx, ~mode=Ana(p_syn.ty), def, m);
let ty_p_ana = def_base.ty;
/* Analyze pattern to incorporate def type into ctx */

let (p_ana', _) =
go_pat(
~is_synswitch=false,
Expand Down
Loading