Skip to content

Commit bafd106

Browse files
committed
API: reduction flags
1 parent 1b8b762 commit bafd106

File tree

8 files changed

+225
-1026
lines changed

8 files changed

+225
-1026
lines changed

Makefile.examples.coq

Lines changed: 0 additions & 951 deletions
This file was deleted.

Makefile.examples.coq.conf

Lines changed: 0 additions & 64 deletions
This file was deleted.

coq-builtin.elpi

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ macro @using! S :- get-option "coq:using" S. % like the #[using=S] attribute
385385
macro @inline-at! N :- get-option "coq:inline" (coq.inline.at N). % like Inline(N)
386386
macro @inline! N :- get-option "coq:inline" coq.inline.default. % like
387387

388+
macro @redflags! F :- get-option "coq:redflags" F. % for whd & co
389+
388390
% both arguments are strings eg "8.12.0" "use foo instead"
389391
macro @deprecated! Since Msg :-
390392
get-option "coq:deprecated" (pr Since Msg).
@@ -1359,15 +1361,57 @@ external pred coq.elaborate-skeleton i:term, o:term, o:term, o:diagnostic.
13591361
external pred coq.elaborate-ty-skeleton i:term, o:sort, o:term,
13601362
o:diagnostic.
13611363

1364+
% -- Coq's reduction flags ------------------------------------
1365+
1366+
% Flags for lazy, cbv, ... reductions
1367+
kind coq.redflag type.
1368+
type coq.redflags.beta coq.redflag.
1369+
type coq.redflags.delta coq.redflag.
1370+
type coq.redflags.match coq.redflag.
1371+
type coq.redflags.fix coq.redflag.
1372+
type coq.redflags.cofix coq.redflag.
1373+
type coq.redflags.zeta coq.redflag.
1374+
type coq.redflags.const constant -> coq.redflag.
1375+
1376+
% Set of flags for lazy, cbv, ... reductions
1377+
typeabbrev coq.redflags (ctype "coq.redflags").
1378+
1379+
type coq.redflags.all coq.redflags.
1380+
type coq.redflags.allnolet coq.redflags.
1381+
type coq.redflags.beta coq.redflags.
1382+
type coq.redflags.betadeltazeta coq.redflags.
1383+
type coq.redflags.betaiota coq.redflags.
1384+
type coq.redflags.betaiotazeta coq.redflags.
1385+
type coq.redflags.betazeta coq.redflags.
1386+
type coq.redflags.delta coq.redflags.
1387+
type coq.redflags.zeta coq.redflags.
1388+
type coq.redflags.nored coq.redflags.
1389+
1390+
% [coq.redflags.add Flags Options NewFlags] Updates reduction Flags by
1391+
% adding Options
1392+
external pred coq.redflags.add i:coq.redflags, i:list coq.redflag,
1393+
o:coq.redflags.
1394+
1395+
% [coq.redflags.sub Flags Options NewFlags] Updates reduction Flags by
1396+
% removing Options
1397+
external pred coq.redflags.sub i:coq.redflags, i:list coq.redflag,
1398+
o:coq.redflags.
1399+
13621400
% -- Coq's reduction machines ------------------------------------
13631401

1364-
% [coq.reduction.lazy.whd_all T Tred] Puts T in weak head normal form
1365-
external pred coq.reduction.lazy.whd_all i:term, o:term.
1402+
% [coq.reduction.lazy.whd T Tred] Puts T in weak head normal form.
1403+
% Supported attributes:
1404+
% - @redflags! (default coq.redflags.all)
1405+
external pred coq.reduction.lazy.whd i:term, o:term.
13661406

1367-
% [coq.reduction.lazy.norm T Tred] Puts T in normal form
1407+
% [coq.reduction.lazy.norm T Tred] Puts T in normal form.
1408+
% Supported attributes:
1409+
% - @redflags! (default coq.redflags.all)
13681410
external pred coq.reduction.lazy.norm i:term, o:term.
13691411

1370-
% [coq.reduction.cbv.norm T Tred] Puts T in weak head normal form
1412+
% [coq.reduction.cbv.norm T Tred] Puts T in weak head normal form.
1413+
% Supported attributes:
1414+
% - @redflags! (default coq.redflags.all)
13711415
external pred coq.reduction.cbv.norm i:term, o:term.
13721416

13731417
% [coq.reduction.vm.norm T Ty Tred] Puts T in normal form. Its type Ty can
@@ -1397,6 +1441,12 @@ coq.reduction.vm.whd_all T TY R :-
13971441
coq.reduction.vm.norm T TY R.
13981442

13991443

1444+
1445+
pred coq.reduction.lazy.whd_all i:term, o:term.
1446+
coq.reduction.lazy.whd_all X Y :-
1447+
@redflags! coq.redflags.all => coq.reduction.lazy.whd X Y.
1448+
1449+
14001450
% -- Coq's conversion strategy tweaks --------------------------
14011451

14021452
% Strategy for conversion test

elpi/coq-HOAS.elpi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ macro @using! S :- get-option "coq:using" S. % like the #[using=S] attribute
370370
macro @inline-at! N :- get-option "coq:inline" (coq.inline.at N). % like Inline(N)
371371
macro @inline! N :- get-option "coq:inline" coq.inline.default. % like
372372

373+
macro @redflags! F :- get-option "coq:redflags" F. % for whd & co
374+
373375
% both arguments are strings eg "8.12.0" "use foo instead"
374376
macro @deprecated! Since Msg :-
375377
get-option "coq:deprecated" (pr Since Msg).

src/coq_elpi_HOAS.ml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ type options = {
342342
nonuniform : bool option;
343343
reversible : bool option;
344344
keepunivs : bool option;
345+
redflags : CClosure.RedFlags.reds option;
345346
}
346347

347348
let default_options = {
@@ -360,6 +361,7 @@ let default_options = {
360361
nonuniform = None;
361362
reversible = None;
362363
keepunivs = None;
364+
redflags = None;
363365
}
364366

365367
type 'a coq_context = {
@@ -983,6 +985,27 @@ let module_inline_default = { module_inline_unspec with
983985
| state, Elpi.Builtin.Unspec, gls -> state,Declaremods.DefaultInline,gls)
984986
}
985987

988+
let reduction_flags = let open API.OpaqueData in let open CClosure in declare {
989+
name = "coq.redflags";
990+
doc = "Set of flags for lazy, cbv, ... reductions";
991+
pp = (fun fmt (x : RedFlags.reds) -> Format.fprintf fmt "TODO");
992+
compare = Stdlib.compare;
993+
hash = Hashtbl.hash;
994+
hconsed = false;
995+
constants = [
996+
"coq.redflags.all", all ;
997+
"coq.redflags.allnolet", allnolet ;
998+
"coq.redflags.beta", beta ;
999+
"coq.redflags.betadeltazeta", betadeltazeta;
1000+
"coq.redflags.betaiota", betaiota ;
1001+
"coq.redflags.betaiotazeta", betaiotazeta ;
1002+
"coq.redflags.betazeta", betazeta ;
1003+
"coq.redflags.delta", delta ;
1004+
"coq.redflags.zeta", zeta ;
1005+
"coq.redflags.nored", nored ;
1006+
];
1007+
}
1008+
9861009
let get_optionc = E.Constants.declare_global_symbol "get-option"
9871010

9881011
let get_options ~depth hyps state =
@@ -1074,6 +1097,13 @@ let get_options ~depth hyps state =
10741097
assert (gl = []);
10751098
NonCumulative ud
10761099
in
1100+
let get_redflags_option () =
1101+
match API.Data.StrMap.find_opt "coq:redflags" map with
1102+
| None -> None
1103+
| Some (t,depth) ->
1104+
let _, rd, gl = reduction_flags.Elpi.API.Conversion.readback ~depth state t in
1105+
assert (gl = []);
1106+
Some rd in
10771107
{
10781108
hoas_holes =
10791109
begin match get_bool_option "HOAS:holes" with
@@ -1094,6 +1124,7 @@ let get_options ~depth hyps state =
10941124
nonuniform = get_bool_option "coq:nonuniform";
10951125
reversible = get_bool_option "coq:reversible";
10961126
keepunivs = get_bool_option "coq:keepunivs";
1127+
redflags = get_redflags_option ();
10971128
}
10981129

10991130
let mk_coq_context ~options state =

src/coq_elpi_HOAS.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type options = {
4646
nonuniform : bool option;
4747
reversible : bool option;
4848
keepunivs : bool option;
49+
redflags : CClosure.RedFlags.reds option;
4950
}
5051

5152
type 'a coq_context = {
@@ -228,6 +229,7 @@ val in_coq_modpath : depth:int -> term -> Names.ModPath.t
228229
val modpath : Names.ModPath.t Conversion.t
229230
val modtypath : Names.ModPath.t Conversion.t
230231
val module_inline_default : Declaremods.inline Conversion.t
232+
val reduction_flags : CClosure.RedFlags.reds Conversion.t
231233

232234
val in_elpi_module : depth:int -> State.t -> Declarations.module_body -> GlobRef.t list
233235
val in_elpi_module_type : Declarations.module_type_body -> string list

src/coq_elpi_builtins.ml

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,35 @@ let conversion_strategy = let open API.AlgebraicData in let open Conv_oracle in
587587
]
588588
} |> CConv.(!<)
589589

590+
let reduction_kind = let open API.AlgebraicData in let open CClosure.RedFlags in declare {
591+
ty = Conv.TyName "coq.redflag";
592+
doc = "Flags for lazy, cbv, ... reductions";
593+
pp = (fun fmt (x : red_kind) -> Format.fprintf fmt "TODO");
594+
constructors = [
595+
K ("coq.redflags.beta", "",N,
596+
B fBETA,
597+
M (fun ~ok ~ko x -> if x = fBETA then ok else ko ()));
598+
K ("coq.redflags.delta", "",N,
599+
B fDELTA,
600+
M (fun ~ok ~ko x -> if x = fDELTA then ok else ko ()));
601+
K ("coq.redflags.match", "",N,
602+
B fMATCH,
603+
M (fun ~ok ~ko x -> if x = fMATCH then ok else ko ()));
604+
K ("coq.redflags.fix", "",N,
605+
B fFIX,
606+
M (fun ~ok ~ko x -> if x = fFIX then ok else ko ()));
607+
K ("coq.redflags.cofix", "",N,
608+
B fCOFIX,
609+
M (fun ~ok ~ko x -> if x = fCOFIX then ok else ko ()));
610+
K ("coq.redflags.zeta", "",N,
611+
B fZETA,
612+
M (fun ~ok ~ko x -> if x = fZETA then ok else ko ()));
613+
K("coq.redflags.const","",A(constant,N),
614+
B (function Constant x -> fCONST x | Variable x -> fVAR x),
615+
M (fun ~ok ~ko x -> nYI "readback for coq.redflags.const"));
616+
]
617+
} |> CConv.(!<)
618+
590619
let attribute a = let open API.AlgebraicData in declare {
591620
ty = Conv.TyName "attribute";
592621
doc = "Generic attribute";
@@ -3076,35 +3105,71 @@ Supported attributes:
30763105
state, ?: None +? None +! B.mkERROR error, [])),
30773106
DocAbove);
30783107

3108+
LPDoc "-- Coq's reduction flags ------------------------------------";
3109+
3110+
MLData reduction_kind;
3111+
MLData reduction_flags;
3112+
3113+
MLCode(Pred("coq.redflags.add",
3114+
In(reduction_flags,"Flags",
3115+
In(B.list reduction_kind,"Options",
3116+
Out(reduction_flags,"NewFlags",
3117+
Easy "Updates reduction Flags by adding Options"))),
3118+
(fun f l _ ~depth ->
3119+
let open CClosure in
3120+
let f = List.fold_left RedFlags.red_add f l in
3121+
!: f)),
3122+
DocAbove);
3123+
3124+
MLCode(Pred("coq.redflags.sub",
3125+
In(reduction_flags,"Flags",
3126+
In(B.list reduction_kind,"Options",
3127+
Out(reduction_flags,"NewFlags",
3128+
Easy "Updates reduction Flags by removing Options"))),
3129+
(fun f l _ ~depth ->
3130+
let open CClosure in
3131+
let f = List.fold_left RedFlags.red_sub f l in
3132+
!: f)),
3133+
DocAbove);
3134+
30793135
LPDoc "-- Coq's reduction machines ------------------------------------";
30803136

3081-
MLCode(Pred("coq.reduction.lazy.whd_all",
3137+
MLCode(Pred("coq.reduction.lazy.whd",
30823138
CIn(term,"T",
30833139
COut(term,"Tred",
3084-
Read(proof_context, "Puts T in weak head normal form"))),
3140+
Read(proof_context, {|Puts T in weak head normal form.
3141+
Supported attributes:
3142+
- @redflags! (default coq.redflags.all)|}))),
30853143
(fun t _ ~depth proof_context constraints state ->
30863144
let sigma = get_sigma state in
3087-
let t = EConstr.of_constr @@ Reduction.whd_all proof_context.env (EConstr.to_constr ~abort_on_undefined_evars:false sigma t) in
3145+
let flags = Option.default CClosure.all proof_context.options.redflags in
3146+
let t = Reductionops.clos_whd_flags flags proof_context.env sigma t in
30883147
!: t)),
30893148
DocAbove);
30903149

30913150
MLCode(Pred("coq.reduction.lazy.norm",
30923151
CIn(term,"T",
30933152
COut(term,"Tred",
3094-
Read(proof_context, "Puts T in normal form"))),
3153+
Read(proof_context, {|Puts T in normal form.
3154+
Supported attributes:
3155+
- @redflags! (default coq.redflags.all)|}))),
30953156
(fun t _ ~depth proof_context constraints state ->
30963157
let sigma = get_sigma state in
3097-
let t = Reductionops.nf_all proof_context.env sigma t in
3158+
let flags = Option.default CClosure.all proof_context.options.redflags in
3159+
let t = Reductionops.clos_norm_flags flags proof_context.env sigma t in
30983160
!: t)),
30993161
DocAbove);
31003162

31013163
MLCode(Pred("coq.reduction.cbv.norm",
31023164
CIn(term,"T",
31033165
COut(term,"Tred",
3104-
Read(proof_context, "Puts T in weak head normal form"))),
3166+
Read(proof_context, {|Puts T in weak head normal form.
3167+
Supported attributes:
3168+
- @redflags! (default coq.redflags.all)|}))),
31053169
(fun t _ ~depth proof_context constraints state ->
31063170
let sigma = get_sigma state in
3107-
let t = Tacred.compute proof_context.env sigma t in
3171+
let flags = Option.default CClosure.all proof_context.options.redflags in
3172+
let t = Tacred.cbv_norm_flags flags proof_context.env sigma t in
31083173
!: t)),
31093174
DocAbove);
31103175

@@ -3161,6 +3226,12 @@ coq.reduction.vm.whd_all T TY R :-
31613226
coq.reduction.vm.norm T TY R.
31623227
|};
31633228

3229+
LPCode {|
3230+
pred coq.reduction.lazy.whd_all i:term, o:term.
3231+
coq.reduction.lazy.whd_all X Y :-
3232+
@redflags! coq.redflags.all => coq.reduction.lazy.whd X Y.
3233+
|};
3234+
31643235
LPDoc "-- Coq's conversion strategy tweaks --------------------------";
31653236

31663237
MLData conversion_strategy;

0 commit comments

Comments
 (0)