Skip to content

Commit

Permalink
Fix the inifinite loop in variant expansions.
Browse files Browse the repository at this point in the history
Get rid of all the complicated and useless code and just
use a reinterpret cast, since all polymorphic variants
have the same representation.
  • Loading branch information
skaller committed Mar 4, 2024
1 parent 087fa8a commit b290653
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/compiler/flx_core/flx_beta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,13 @@ print_endline "Type list index returned None";
begin match bbdcl with
| Flx_bbdcl.BBDCL_type_alias (bvs, alias) ->
let alias = Flx_btype_subst.tsubst sr bvs ts alias in
(*
print_endline ("Flx_beta: Alias " ^ bsym.id ^"<"^string_of_int index^">" ^ "["^String.concat "," (List.map Flx_btype.st ts) ^"] after substitution " ^ Flx_btype.st alias);
*)
let alias = beta_reduce' calltag counter bsym_table sr depth ((t,depth)::termlist) alias in
(*
print_endline ("Flx_beta: Alias " ^ bsym.id ^"<"^string_of_int index^">" ^ "["^String.concat "," (List.map Flx_btype.st ts) ^"] after beta-reduction " ^ Flx_btype.st alias);
*)
(* NOTE: Alias viewification performed here .. not sure it would ever be used .. *)
begin match m with
| `P
Expand Down Expand Up @@ -421,13 +427,19 @@ print_endline ("Beta-reducing typeop " ^ op ^ ", type=" ^ sbt bsym_table t);
btyp_variant (List.combine ss (List.map br ls))

| BTYP_polyvariant ts ->
(* NO DEPTH INCREASE FOR ALIAS EXPANSION *)
let br' t = beta_reduce' calltag counter bsym_table sr depth termlist t in
let ctors = List.fold_left (fun acc term -> match term with
| `Ctor (s,t) -> (s,br t)::acc
| `Base t -> match br t with
| `Ctor (s,t) -> (s,br t)::acc (* depth expansion *)
| `Base t -> match br' t with (* No depth expansion *)
| BTYP_variant ts -> ts @ acc
| _ -> print_endline ("Reduction of polyvariant failed"); assert false
) [] ts
in btyp_variant ctors
in
let t = btyp_variant ctors in
(* print_endline ("Reduced polyvariant type " ^ Flx_btype.st t); *)
t

(*
btyp_polyvariant (List.map (fun k -> match k with
| `Ctor (s,t) -> `Ctor (s, br t)
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/flx_core/flx_type_fun.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ print_endline ("Type apply " ^ Flx_btype.st f ^ " to " ^ Flx_btype.st arg);
begin match bbdcl with
| Flx_bbdcl.BBDCL_type_alias (bvs, alias) ->
let salias = Flx_btype_subst.tsubst sr bvs ts alias in
(*
print_endline ("Alias " ^ bsym.id ^ "["^String.concat "," (List.map Flx_btype.st ts) ^"] after substitution " ^ Flx_btype.st salias);
*)
type_apply beta_reduce' calltag counter bsym_table sr depth termlist salias arg
| _ -> assert false
end
Expand Down
12 changes: 8 additions & 4 deletions src/compiler/flx_frontend/flx_xcoerce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ and unique_check ls =
ls

and variant_coercion new_table bsym_table counter parent remap ((srcx,srct) as srce) dstt ls rs sr =
let e = remap parent srce in
bexpr_reinterpret_cast (e,dstt)
(*
let coerce parent e dstt = expand_coercion new_table bsym_table counter parent remap e dstt sr in
if debug then
print_endline ("Variant coercion " ^ Flx_btype.st srct ^ " => " ^ Flx_btype.st dstt);
Expand Down Expand Up @@ -254,7 +257,7 @@ print_endline ("MINIMISED dstt = " ^ Flx_btype.st dstt);
remap parent result
end
end

*)

and record_coercion new_table bsym_table counter parent remap ((srcx,srct) as srce) dstt ls rs sr =
let coerce parent e dstt = expand_coercion new_table bsym_table counter parent remap e dstt sr in
Expand Down Expand Up @@ -557,9 +560,7 @@ and process_expr new_table bsym_table counter parent sr expr =
match e with
(* coercion with argument free of reducible coercions *)
| BEXPR_coerce ((srcx,srct) as srce,dstt),_ ->
(*
print_endline ("Examining coercion " ^ Flx_print.sbe bsym_table e );
*)
(* print_endline ("Examining coercion " ^ Flx_print.sbe bsym_table e ); *)
let e' =
try
expand_coercion new_table bsym_table counter parent remap srce dstt sr
Expand Down Expand Up @@ -615,6 +616,9 @@ print_endline ("Processing function " ^ Flx_bsym.id bsym);
not be used, but cannot be eliminated in case they're used.
*)
let expand_coercions syms bsym_table =
(*
print_endline ("EXPAND COERCIONS: START");
*)
let new_table = Flx_bsym_table.create_fresh () in
Flx_bsym_table.iter
(fun i parent bsym -> process_entry new_table bsym_table syms.Flx_mtypes2.counter parent i bsym)
Expand Down

0 comments on commit b290653

Please sign in to comment.