Skip to content

Commit f69b427

Browse files
committed
open_term: order variables by left-to-right traversal
1 parent ca9f064 commit f69b427

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/coq_elpi_arg_HOAS.ml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,18 +904,33 @@ let in_elpi_string_arg ~depth state x =
904904
let in_elpi_int_arg ~depth state x =
905905
state, E.mkApp intc (CD.of_int x) [], []
906906

907+
module NIdSet = struct
908+
include Id.Map
909+
let counter = ref 0
910+
let add x s =
911+
if not (mem x s) then begin
912+
incr counter;
913+
add x !counter s
914+
end else
915+
s
916+
917+
let fold f s acc =
918+
Id.Map.bindings s |> List.sort (fun (_,n) (_,m) -> m - n) |>
919+
List.fold_left (fun acc (x,_) -> f x acc) acc
920+
921+
end
907922
let free_glob_vars known_vars =
908923
let open Glob_term in
909924
let rec vars bound vs c = match DAst.get c with
910-
| GVar id' -> if Id.Set.mem id' bound then vs else Id.Set.add id' vs
925+
| GVar id' -> if Id.Set.mem id' bound then vs else NIdSet.add id' vs
911926
| _ -> Glob_ops.fold_glob_constr_with_binders Id.Set.add vars bound vs c in
912927
fun rt ->
913-
let vs = vars known_vars Id.Set.empty rt in
928+
let vs = vars known_vars NIdSet.empty rt in
914929
vs
915930
let close_glob coq_ctx term =
916931
let open Glob_term in
917932
let fv_set = free_glob_vars coq_ctx.names term in
918-
(Id.Set.cardinal fv_set ,Id.Set.fold (fun id t ->
933+
(NIdSet.cardinal fv_set ,NIdSet.fold (fun id t ->
919934
DAst.(make (GLambda(Name.Name(id),None,Explicit,mkGHole,t)))) fv_set term)
920935

921936
let in_elpi_term_arg ~loc ~base ~depth state coq_ctx hyps sigma ist glob_or_expr =

0 commit comments

Comments
 (0)