Skip to content

Commit 7b62527

Browse files
committed
reduction loop in HO unification heuristic + stop refolding beta-redexes + fix check_conv_record
1 parent ea4b95b commit 7b62527

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

pretyping/evarconv.ml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ let check_conv_record env sigma ((proji, u), (params1, c1, extra_args1)) (t2,sk2
356356
end
357357
with | Not_found -> (* If we find no solution, we ask the hook if it has any. *)
358358
match (apply_hooks env sigma ((proji, u), params1, c1) (t2, args2)) with
359-
| Some r -> r, args2' @ args2
359+
| Some r -> r, args2
360360
| None -> raise Not_found
361361
in
362362
let t2 = Stack.zip sigma (h2, (Stack.append_app_list args2 Stack.empty)) in
@@ -717,14 +717,12 @@ and evar_eqappr_x ?(rhs_is_already_stuck = false) flags env evd pbty
717717
UnifFailure (i, NotSameHead)
718718
in
719719
(* Checks whether two terms with the same head unify (without unfolding) *)
720-
let eq_head appr1 appr2 evd =
720+
let eq_head (term1, sk1) (term2, sk2) evd =
721721
(* Gather the universe constraints that would make term1 and term2 equal.
722722
If these only involve unifications of flexible universes to other universes,
723723
allow this identification (first-order unification of universes). Otherwise
724724
fallback to unfolding.
725725
*)
726-
let (term1, sk1) = appr1 in
727-
let (term2, sk2) = appr2 in
728726
let check_univs univs i =
729727
match univs with
730728
| None -> UnifFailure (i,NotSameHead)
@@ -848,7 +846,7 @@ and evar_eqappr_x ?(rhs_is_already_stuck = false) flags env evd pbty
848846
let tM = Stack.zip evd apprM in
849847
miller_pfenning l2r
850848
(fun () -> if Stack.not_purely_applicative skM then (* Postpone the use of an heuristic *)
851-
if not postpone then let () = debug_unification (fun () -> Pp.(str "prevent miller from postponing")) in quick_fail i else
849+
if not postpone then quick_fail i else
852850
switch (fun x y -> Success (Evarutil.add_unification_pb (pbty,env,x,y) i)) (Stack.zip evd apprF) (Stack.zip evd (if l2r then snd hds else fst hds))
853851
else quick_fail i)
854852
ev lF tM i
@@ -1119,12 +1117,13 @@ and evar_eqappr_x ?(rhs_is_already_stuck = false) flags env evd pbty
11191117
ise_try evd [f1; f2]
11201118

11211119
| _, Lambda _ when sk2 <> [] && not (EConstr.isLambda evd term1) ->
1122-
evar_eqappr_x flags env evd pbty hds None appr1
1123-
(whd_betaiota_deltazeta_for_iota_state flags.open_ts env evd appr2)
1120+
(* beta-redexes are the enemy, they should be destroyed as soon as possible. *)
1121+
let appr2 = whd_betaiota_deltazeta_for_iota_state flags.open_ts env evd appr2 in
1122+
evar_eqappr_x flags env evd pbty (fst hds, appr2) None appr1 appr2
11241123
| Lambda _, _ when sk1 <> [] && not (EConstr.isLambda evd term2) ->
1125-
evar_eqappr_x flags env evd pbty hds None
1126-
(whd_betaiota_deltazeta_for_iota_state flags.open_ts env evd appr1)
1127-
appr2
1124+
(* beta-redexes are the enemy, they should be destroyed as soon as possible. *)
1125+
let appr1 = whd_betaiota_deltazeta_for_iota_state flags.open_ts env evd appr1 in
1126+
evar_eqappr_x flags env evd pbty (appr1, snd hds) None appr1 appr2
11281127

11291128
| _, _ ->
11301129
let p1 = match get_proj_case appr1 with Proj.CS _ when lastUnfolded = Some true -> Proj.None | p -> p in
@@ -1949,7 +1948,7 @@ let is_constant_instance sigma (evk, args) alias =
19491948
List.for_all (fun a -> EConstr.eq_constr sigma a alias || isEvar sigma a)
19501949
(remove_instance_local_defs sigma evk args)
19511950

1952-
let apply_conversion_problem_heuristic flags env evd with_ho pbty t1 t2 =
1951+
let rec apply_conversion_problem_heuristic flags env evd with_ho pbty t1 t2 =
19531952
let t1 = apprec_nohdbeta flags env evd (whd_head_evar evd t1) in
19541953
let t2 = apprec_nohdbeta flags env evd (whd_head_evar evd t2) in
19551954
let (term1,l1 as appr1) = try destApp evd t1 with DestKO -> (t1, [||]) in
@@ -1999,26 +1998,34 @@ let apply_conversion_problem_heuristic flags env evd with_ho pbty t1 t2 =
19991998
(position_problem true pbty) ev1 ev2)
20001999
with IllTypedInstance (env,evd,t,u) ->
20012000
UnifFailure (evd,InstanceNotSameType (evk1,env,t,u)))
2002-
| Evar ev1,_ when is_evar_allowed flags (fst ev1) && Array.length l1 <= Array.length l2 ->
2001+
| Evar ev1,_ when is_evar_allowed flags (fst ev1) ->
20032002
(* On "?n t1 .. tn = u u1 .. u(n+p)", try first-order unification *)
20042003
(* and otherwise second-order matching *)
20052004
ise_try evd
2006-
[(fun evd -> first_order_unification flags env evd (ev1,l1) appr2);
2005+
[(fun evd -> if Array.length l1 <= Array.length l2 then first_order_unification flags env evd (ev1,l1) appr2 else UnifFailure (evd, NotSameHead));
2006+
(fun evd ->
2007+
second_order_matching_with_args flags env evd with_ho pbty ev1 l1 t2);
20072008
(fun evd ->
2008-
second_order_matching_with_args flags env evd with_ho pbty ev1 l1 t2)]
2009-
| _,Evar ev2 when is_evar_allowed flags (fst ev2) && Array.length l2 <= Array.length l1 ->
2009+
let term2, sk2 = Reductionops.whd_nored_state env evd (t2, []) in
2010+
match flex_kind_of_term flags env evd term2 sk2 with
2011+
| MaybeFlexible vsk2' ->
2012+
let t2 = Stack.zip evd (whd_betaiota_deltazeta_for_iota_state flags.open_ts env evd vsk2') in
2013+
apply_conversion_problem_heuristic flags env evd with_ho pbty t1 t2
2014+
| _ -> UnifFailure (evd, NotSameHead))]
2015+
| _,Evar ev2 when is_evar_allowed flags (fst ev2) ->
20102016
(* On "u u1 .. u(n+p) = ?n t1 .. tn", try first-order unification *)
20112017
(* and otherwise second-order matching *)
20122018
ise_try evd
2013-
[(fun evd -> first_order_unification flags env evd (ev2,l2) appr1);
2019+
[(fun evd -> if Array.length l2 <= Array.length l1 then first_order_unification flags env evd (ev2,l2) appr1 else UnifFailure (evd, NotSameHead));
20142020
(fun evd ->
2015-
second_order_matching_with_args flags env evd with_ho pbty ev2 l2 t1)]
2016-
| Evar ev1,_ when is_evar_allowed flags (fst ev1) ->
2017-
(* Try second-order pattern-matching *)
2018-
second_order_matching_with_args flags env evd with_ho pbty ev1 l1 t2
2019-
| _,Evar ev2 when is_evar_allowed flags (fst ev2) ->
2020-
(* Try second-order pattern-matching *)
2021-
second_order_matching_with_args flags env evd with_ho pbty ev2 l2 t1
2021+
second_order_matching_with_args flags env evd with_ho pbty ev2 l2 t1);
2022+
(fun evd ->
2023+
let term1, sk1 = Reductionops.whd_nored_state env evd (t1, []) in
2024+
match flex_kind_of_term flags env evd term1 sk1 with
2025+
| MaybeFlexible vsk1' ->
2026+
let t1 = Stack.zip evd (whd_betaiota_deltazeta_for_iota_state flags.open_ts env evd vsk1') in
2027+
apply_conversion_problem_heuristic flags env evd with_ho pbty t1 t2
2028+
| _ -> UnifFailure (evd, NotSameHead))]
20222029
| _ ->
20232030
(* Some head evar have been instantiated, or unknown kind of problem *)
20242031
evar_conv_x flags env evd pbty t1 t2

0 commit comments

Comments
 (0)