Skip to content

Commit 2dd0e78

Browse files
committed
wip
abstract univ instances (TODO: keep relations between univs and instances)
1 parent ca1b82d commit 2dd0e78

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

builtin-doc/coq-builtin.elpi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ external pred coq.env.current-section-path o:list string.
789789
% - @using! (default: section variables actually used)
790790
% - @univpoly! (default unset)
791791
% - @udecl! (default unset)
792+
% - @udecl-cumul! (default unset)
792793
% - @dropunivs! (default: false, drops all universe constraints from the
793794
% store after the definition)
794795
%

src/rocq_elpi_HOAS.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ val universe_level_variable : Univ.Level.t Conversion.t
246246
val univ : Univ.Universe.t Conversion.t
247247
val isuniv : RawOpaqueData.t -> bool
248248
val univout : RawOpaqueData.t -> Univ.Universe.t
249+
val isuinstance : RawOpaqueData.t -> bool
250+
val uinstanceout : RawOpaqueData.t -> UVars.Instance.t
249251

250252
val is_sort : depth:int -> term -> bool
251253
val is_prod : depth:int -> term -> (term * term) option (* ty, bo @ depth+1 *)

src/rocq_elpi_builtins.ml

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -539,53 +539,62 @@ let err_if_contains_alg_univ ~depth t =
539539
match Univ.Universe.level u with
540540
| None -> true
541541
| Some l -> is_global_level env l in
542-
let rec aux ~depth acc t =
542+
let rec aux ~depth (acc,acci) t =
543543
match E.look ~depth t with
544+
| E.CData c when isuinstance c -> (acc,acci+1)
544545
| E.CData c when isuniv c ->
545546
let u = univout c in
546-
if is_global u then acc
547+
if is_global u then acc, acci
547548
else
548549
begin match Univ.Universe.level u with
549550
| None ->
550551
err Pp.(strbrk "The hypothetical clause contains terms of type univ which are not global, you should abstract them out or replace them by global ones: " ++
551552
Univ.Universe.pr UnivNames.pr_level_with_global_universes u)
552-
| _ -> Univ.Universe.Set.add u acc
553+
| _ -> Univ.Universe.Set.add u acc, acci
553554
end
554-
| x -> Rocq_elpi_utils.fold_elpi_term aux acc ~depth x
555+
| x -> Rocq_elpi_utils.fold_elpi_term aux (acc,acci) ~depth x
555556
in
556-
let univs = aux ~depth Univ.Universe.Set.empty t in
557+
let univs = aux ~depth (Univ.Universe.Set.empty,0) t in
557558
univs
558559

559560
let preprocess_clause ~depth clause =
560-
let levels_to_abstract = err_if_contains_alg_univ ~depth clause in
561+
let levels_to_abstract, instances_to_abstract = err_if_contains_alg_univ ~depth clause in
561562
let levels_to_abstract_no = Univ.Universe.Set.cardinal levels_to_abstract in
562-
let rec subst ~depth m t =
563+
let rec subst ~depth m mi t =
563564
match E.look ~depth t with
564565
| E.CData c when isuniv c ->
565566
begin try E.mkBound (Univ.Universe.Map.find (univout c) m)
566567
with Not_found -> t end
568+
| E.CData c when isuinstance c ->
569+
decr mi; E.mkBound !mi
567570
| E.App(c,x,xs) ->
568-
E.mkApp c (subst ~depth m x) (List.map (subst ~depth m) xs)
571+
E.mkApp c (subst ~depth m mi x) (List.map (subst ~depth m mi) xs)
569572
| E.Cons(x,xs) ->
570-
E.mkCons (subst ~depth m x) (subst ~depth m xs)
573+
E.mkCons (subst ~depth m mi x) (subst ~depth m mi xs)
571574
| E.Lam x ->
572-
E.mkLam (subst ~depth:(depth+1) m x)
575+
E.mkLam (subst ~depth:(depth+1) m mi x)
573576
| E.Builtin(c,xs) ->
574-
E.mkBuiltin c (List.map (subst ~depth m) xs)
577+
E.mkBuiltin c (List.map (subst ~depth m mi) xs)
575578
| E.UnifVar _ -> CErrors.user_err Pp.(str"The clause begin accumulated contains unification variables, this is forbidden. You must quantify them out using 'pi'.")
576579
| E.Const _ | E.Nil | E.CData _ -> t
577580
in
578581
let clause =
579-
let rec bind d map = function
582+
let rec bind d (map : int Univ.Universe.Map.t) (mapi : int ref) = function
580583
| [] ->
581-
subst ~depth:d map
582-
(API.Utils.move ~from:depth ~to_:(depth + levels_to_abstract_no) clause)
584+
subst ~depth:d map mapi
585+
(API.Utils.move ~from:depth ~to_:(depth + levels_to_abstract_no + !mapi) clause)
583586
| l :: ls ->
584587
E.mkApp E.Constants.pic (E.mkLam (* pi x\ *)
585-
(bind (d+1) (Univ.Universe.Map.add l d map) ls)) []
588+
(bind (d+1) (Univ.Universe.Map.add l d map) mapi ls)) []
589+
and bindi d (map : int Univ.Universe.Map.t) (mapi : int ref) ua n =
590+
if n = 0 then
591+
bind d map mapi ua
592+
else
593+
E.mkApp E.Constants.pic (E.mkLam (* pi x\ *)
594+
(bindi (d+1) map mapi ua (n-1))) []
586595
in
587-
bind depth Univ.Universe.Map.empty
588-
(Univ.Universe.Set.elements levels_to_abstract)
596+
bindi depth Univ.Universe.Map.empty (ref (depth+instances_to_abstract))
597+
(Univ.Universe.Set.elements levels_to_abstract) instances_to_abstract
589598
in
590599
let vars = collect_term_variables ~depth clause in
591600
vars, clause
@@ -2165,7 +2174,7 @@ Supported attributes:
21652174
| B.Given body ->
21662175
let sigma = get_sigma state in
21672176
if not (is_ground sigma body) then
2168-
err Pp.(str"coq.env.add-const: the body must be ground. Did you forge to call coq.typecheck-indt-decl?");
2177+
err Pp.(str"coq.env.add-const: the body must be ground. Did you forge to call coq.typecheck?");
21692178
let opaque = opaque = B.Given true in
21702179
let types =
21712180
match types, opaque with
@@ -2177,7 +2186,7 @@ Supported attributes:
21772186
| e when CErrors.is_anomaly e -> err Pp.(str"coq.env.add-const: illtyped opaque") end
21782187
| B.Given ty, _ ->
21792188
if not (is_ground sigma ty) then
2180-
err Pp.(str"coq.env.add-const: the type must be ground. Did you forge to call coq.typecheck-indt-decl?");
2189+
err Pp.(str"coq.env.add-const: the type must be ground. Did you forge to call coq.typecheck?");
21812190
Some ty in
21822191
let state, poly, cumul, udecl, _ = poly_cumul_udecl_variance_of_options state options in
21832192
let kind = Decls.(IsDefinition Definition) in
@@ -2200,7 +2209,12 @@ Supported attributes:
22002209
let used = Univ.Level.Set.union used (universes_of_udecl state udecl) in
22012210
let sigma = restricted_sigma_of used state in
22022211

2203-
let gr, uctx = declare_definition uctx options.using ~cinfo ~info ~opaque ~body sigma in
2212+
let gr, uctx =
2213+
try declare_definition uctx options.using ~cinfo ~info ~opaque ~body sigma
2214+
with Loop_checking.Undeclared l as e ->
2215+
Printf.eprintf "Loop_checking.Undeclared %s\n%!" (Univ.Level.to_string l);
2216+
raise e
2217+
in
22042218
let () =
22052219
let lid = CAst.make ~loc:(to_coq_loc @@ State.get Rocq_elpi_builtins_synterp.invocation_site_loc state) (Id.of_string id) in
22062220
match scope with

tests/test_HOAS.v

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,31 @@ Elpi Query lp:{{
6666
coq.env.end-section
6767
}}.
6868

69-
Elpi Db univs.db lp:{{ pred u o:univ. }}.
69+
Polymorphic Definition ut@{u} : Type@{u} := Type.
70+
71+
Elpi Db univs.db lp:{{
72+
pred u o:univ.
73+
pred ut o:term, o:univ.
74+
}}.
7075
Elpi Command test_u.
7176
Elpi Accumulate Db univs.db.
7277
Elpi Query lp:{{
7378
coq.univ.new U,
74-
coq.elpi.accumulate current "univs.db" (clause _ _ (u U))
79+
coq.elpi.accumulate current "univs.db" (clause _ _ (u U)),
80+
coq.univ.variable U L,
81+
coq.univ-instance I [L],
82+
coq.elpi.accumulate current "univs.db" (clause _ _ (ut (pglobal {{:gref ut}} I) U))
7583
}}.
7684

7785
Universe foo.
78-
86+
Universe foo1.
87+
Elpi Print test_u "elpi.tests/test_u".
7988
Elpi Query lp:{{
8089
{{ Type@{foo} }} = sort (typ U),
81-
coq.elpi.accumulate current "univs.db" (clause _ _ (u U))
90+
u U, ut {{ ut@{foo} }} U
8291
}}.
8392

93+
stop
8494

8595
Axiom B : bool -> Type.
8696
Axiom N : nat -> Type.

0 commit comments

Comments
 (0)