Skip to content

Commit b9a688c

Browse files
mtzguidoActions bot
authored andcommitted
WIP using core, for phase2
1 parent 18109fa commit b9a688c

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

src/typechecker/FStarC.TypeChecker.Tc.fst

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,15 @@ let tc_sig_let env r se lbs lids : list sigelt & list sigelt & Env.env =
457457
(* 2. Turn the top-level lb into a Tm_let with a unit body *)
458458
let e = mk (Tm_let {lbs=(fst lbs, lbs'); body=mk (Tm_constant (Const_unit)) r}) r in
459459

460+
(* For the second phase (if any), we will call the core typechecker
461+
for terms marked with a @@"core" attribute, or if the extension
462+
"core_phase2" is enabled. *)
463+
let use_core =
464+
do_two_phases env && (
465+
Options.Ext.enabled "core_phase2"
466+
|| List.existsb (U.term_eq (U.exp_string "core")) se.sigattrs)
467+
in
468+
460469
(* 3. Type-check the Tm_let and convert it back to Sig_let *)
461470
let env' = { env with top_level = true; generalize = should_generalize } in
462471
let e =
@@ -493,14 +502,25 @@ let tc_sig_let env r se lbs lids : list sigelt & list sigelt & Env.env =
493502
if Debug.medium () || !dbg_TwoPhases then
494503
Format.print1 "Let binding after phase 1, before removing uvars: %s\n" (show e);
495504

496-
let e = N.remove_uvar_solutions env' e |> drop_lbtyp in
505+
let e = N.remove_uvar_solutions env' e in
506+
507+
(* Wart: the normal typechecker drops the lbtyp to avoid double
508+
ascriptions. But we need it to call the core checker. *)
509+
let e =
510+
if use_core
511+
then e
512+
else drop_lbtyp e
513+
in
497514

498515
if Debug.medium () || !dbg_TwoPhases then
499516
Format.print1 "Let binding after phase 1, uvars removed: %s\n" (show e);
500517
e)
501518
else e
502519
in
503520

521+
(* Phony, just so that we can print the result from Core below. *)
522+
let _ : Class.Show.showable Core.guard_commit_token_cb = { show = (fun _ -> "_") } in
523+
504524
let env' =
505525
match (SS.compress e).n with
506526
| Tm_let {lbs} ->
@@ -511,12 +531,53 @@ let tc_sig_let env r se lbs lids : list sigelt & list sigelt & Env.env =
511531
in
512532
Errors.stop_if_err ();
513533
let r =
514-
//We already generalized phase1; don't need to generalize again
515-
let should_generalize = not (do_two_phases env') in
516-
Profiling.profile (fun () -> tc_maybe_toplevel_term { env' with generalize = should_generalize } e)
517-
(Some (Ident.string_of_lid (Env.current_module env)))
518-
"FStarC.TypeChecker.Tc.tc_sig_let-tc-phase2"
534+
let fallback () =
535+
// We already generalized phase1; don't need to generalize again
536+
let should_generalize = not (do_two_phases env') in
537+
Profiling.profile (fun () -> tc_maybe_toplevel_term { env' with generalize = should_generalize } e)
538+
(Some (Ident.string_of_lid (Env.current_module env)))
539+
"FStarC.TypeChecker.Tc.tc_sig_let-tc-phase2"
540+
in
541+
if not use_core then
542+
fallback ()
543+
else (
544+
if Debug.any () then
545+
Format.print1 "Using core as phase2 for let binding: %s\n" (show e);
546+
match (SS.compress e).n with
547+
| Tm_let {lbs=(_, [lb])} -> (
548+
// Format.print1 "GGG lbdef = %s\n" (show lb.lbdef);
549+
// Format.print1 "GGG lbtyp = %s\n" (show lb.lbtyp);
550+
let env'= push_univ_vars env' lb.lbunivs in
551+
let _, def = SS.open_univ_vars lb.lbunivs lb.lbdef in
552+
let _, typ = SS.open_univ_vars lb.lbunivs lb.lbtyp in
553+
let core_res = Core.check_term env' def typ true in
554+
// Format.print1 "Core computed type: %s\n" (show core_res);
555+
match core_res with
556+
| Inl gopt ->
557+
begin match gopt with
558+
| Some (g, _) -> ignore (Rel.discharge_guard env' (Env.guard_of_guard_formula (NonTrivial g)))
559+
| None -> ()
560+
end;
561+
// Format.print1 "Core type: %s\n" (show typ);
562+
let lcomp =
563+
// match tg with
564+
// | Core.E_Total ->
565+
lcomp_of_comp (S.mk_Total typ)
566+
// | Core.E_Ghost -> lcomp_of_comp (S.mk_GTotal typ)
567+
in
568+
e, lcomp, Env.trivial_guard
569+
570+
| Inr err ->
571+
let open FStarC.Pprint in
572+
Errors.diag e [
573+
text "Core (for phase 2) failed:" ^/^ doc_of_string (show err);
574+
];
575+
fallback ()
576+
)
577+
| _ -> failwith "unexpected: not a let"
578+
)
519579
in
580+
520581
let se, lbs = match r with
521582
| {n=Tm_let {lbs; body=e}}, _, g when Env.is_trivial g ->
522583
U.check_mutual_universes (snd lbs);

src/typechecker/FStarC.TypeChecker.TcTerm.fst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,12 +3977,13 @@ and check_inner_let env e =
39773977
let _ =
39783978
if (is_inline_let || is_inline_let_vc) //inline let is allowed only if it is pure or ghost
39793979
&& not (pure_or_ghost || Env.is_erasable_effect env c1.eff_name) //inline let is allowed on erasable effects
3980-
then raise_error e1
3981-
Errors.Fatal_ExpectedPureExpression
3982-
(Format.fmt2 "Definitions marked @inline_let are expected to be pure or ghost; \
3983-
got an expression \"%s\" with effect \"%s\""
3984-
(show e1)
3985-
(show c1.eff_name))
3980+
then
3981+
let open FStarC.Pprint in
3982+
raise_error e1 Errors.Fatal_ExpectedPureExpression [
3983+
text "Definitions marked @inline_let are expected to be pure or ghost.";
3984+
prefix 2 1 (text "Got an expression") (pp e1) ^/^
3985+
prefix 2 1 (text "with effect") (pp c1.eff_name) ^^ dot;
3986+
]
39863987
in
39873988
let x = {Inl?.v lb.lbname with sort=c1.res_typ} in
39883989
let xb, e2 = SS.open_term [S.mk_binder x] e2 in
@@ -4380,8 +4381,10 @@ and check_let_bound_def top_level env lb
43804381
(* 1. extract the annotation of the let-bound term, e1, if any *)
43814382
let topt, wf_annot, univ_vars, univ_opening, env1 = check_lbtyp top_level env lb in
43824383

4383-
if not top_level && univ_vars <> []
4384-
then raise_error e1 Errors.Fatal_UniversePolymorphicInnerLetBound "Inner let-bound definitions cannot be universe polymorphic";
4384+
if not top_level && univ_vars <> [] then
4385+
raise_error e1 Errors.Fatal_UniversePolymorphicInnerLetBound [
4386+
text "Inner let-bound definitions cannot be universe polymorphic."
4387+
];
43854388

43864389
(* 2. type-check e1 *)
43874390
(* Only toplevel terms should have universe openings *)

0 commit comments

Comments
 (0)