Debian Codegraph @

apps/derive/README.md

raw symbols SCIP
1 # Derive
2
3 The `derive` command automatically synthesizes a bunch of useful lemmas
4 given an inductive type declaration.
5
6 ## In a nutshell
7
8 ```coq
9 From elpi.apps Require Import derive.std.
10
11 #[module] derive Inductive peano := Zero | Succ (p : peano).
12
13 Print peano.peano.
14 (* Inductive peano : Set := Zero : peano | Succ : peano -> peano. *)
15
16 Eval compute in peano.eqb Zero (Succ Zero).
17 (* = false : bool *)
18
19 About peano.eqb_OK.
20 (*
21 peano.eqb_OK : forall x1 x2 : peano, reflect (x1 = x2) (peano.eqb x1 x2)
22
23 peano.eqb_OK is not universe polymorphic
24 Arguments peano.eqb_OK x1 x2
25 peano.eqb_OK is opaque
26 Expands to: Constant elpi.apps.derive.examples.readme.peano.eqb_OK
27 *)
28 ```
29
30 See also [examples/usage.v](examples/usage.v) and [tests/test_readme.v](tests/test_readme.v).
31
32 :warning: The line `From elpi.apps Require Import derive.std.` sets globally
33 `Uniform Inductive Parameters`.
34 See the [documentation of that option in the Coq reference manual](https://coq.inria.fr/refman/language/core/inductive.html#coq:flag.Uniform-Inductive-Parameters).
35
36 ## Usage and attributes
37
38 Using `derive Inductive ty := ...` produces the inductive `ty`, together with
39 derivations, all in the current scope. The `#[module=<string>]` attribute can
40 be used to specify that the inductive and the derivations should be wrapped
41 in a module of the given name (the name of the inductive is used if no name
42 is specified).
43
44 When a wrapper module is generated, an alias (i.e., a notation) is generated
45 for the inductive to be accessible with its name, outside of the module scope.
46 This behaviour can be disabled by using the `#[no_alias]` boolean attribute.
47
48 The `#[prefix=<string>]` attribute can be used to specify a prefix for all the
49 derived definitions/lemmas.
50
51 ## Documentation
52
53 Elpi's `derive` app is a little framework to register derivations.
54 Currently there are 3 groups:
55 - `derive.std` contains well tested derivations including:
56 + `eqb` and `eqbOK` generate sound boolean equality test in linear time/space, see
57 [Practical and sound equality tests, automatically](https://hal.inria.fr/hal-03800154)
58 + `eqbOK` generates its soundness proof in linear time/space
59 + `induction` generates deep induction principles, see
60 [Stronger Induction Principles for Containers](http://drops.dagstuhl.de/opus/volltexte/2019/11084/)
61 + `param1` and `param2` generate the unary and binary parametricity translations
62 + `map` map over a container
63 + `param1_functor` functoriality lemmas (map over the param1 translation)
64 + `lens` and `lens_laws` generate lenses focusing on record fields and some
65 equations governing setter/setters (aka record update syntax)
66 - `derive.legacy` contains derivations superseded by `std`:
67 + `eq` and `eqOK` generate sound equality tests in quadratic time/space, see
68 [Deriving proved equality tests in Coq-elpi](http://drops.dagstuhl.de/opus/volltexte/2019/11084/)
69 - `derive.experimental` contains derivations not suitable for mainstream use:
70 + `idx2inv` generates an inductive type where indexes are replaced by
71 non uniform parameters and equations
72
73
74 The `elpi/` directory contains the elpi files implementing various automatic
75 derivation of terms. The corresponding .v files, defining the Coq commands,
76 are in `theories/derive/`.
77
78 Single steps of the derivation are available as separate commands.
79 Only the main entry point `derive` comes with an handy syntax; the other
80 commands have to be invoked mentioning `Elpi` and only accept an already
81 declared inductive as input.
82
83 ## Derivations
84
85 <details><summary>std (click to expand)</summary><p>
86
87 ### `map`
88
89 Map a container over its parameters.
90
91 ```coq
92 Elpi derive.map list.
93 Check list_map : forall A B, (A -> B) -> list A -> list B.
94 ```
95
96 ### `lens`
97 See also [theories/derive/lens.v](theories/derive/lens.v) for the `Lens` definition and the support constants `view`, `set` and `over`.
98 ```coq
99 Record pa_record A := { f3 : peano; f4 : A; }.
100 Elpi derive.lens pa_record.
101 Check _f3 : forall A, Lens (pa_record A) (pa_record A) peano peano.
102 ```
103
104 ### `lens_laws`
105 See also [theories/derive/lens_laws.v](theories/derive/lens_laws.v) for the statements of the 4 laws (set_set, view_set, set_view, exchange).
106 ```coq
107 Elpi derive.lens_laws pa_record.
108 Check _f3_view_set : forall A (r : pa_record A) x, view _f3 (set _f3 x r) = x.
109 ```
110
111 ### `param1`
112
113 Unary parametricity translation.
114
115 ```coq
116 Elpi derive.param1 nat.
117 Print is_nat. (*
118 Inductive is_nat : nat -> Type :=
119 | is_O : is_nat 0
120 | is_S : forall n : nat, is_nat n -> is_nat (S n) *)
121 ```
122
123 ### `param2`
124
125 Binary parametricity translation.
126
127 Main command is `derive.param2`
128 ```coq
129 Elpi derive.param2 nat.
130 Print nat_R. (*
131 Inductive nat_R : nat -> nat -> Set :=
132 | O_R : nat_R 0 0
133 | S_R : forall H H0 : nat, nat_R H H0 -> nat_R (S H) (S H0).
134 ```
135
136 The command `derive.param2.register` can be used to register
137 handcrafted parametricity rules, so that they can be used by further
138 `derive.param2` commands.
139 ```coq
140 Definition fa := 0.
141 Definition fb := fa.
142
143 Fail Elpi derive.param2 fb.
144 (* derive.param2: No binary parametricity translation for fa *)
145
146 Definition fa_R := O_R.
147 Elpi derive.param2.register fa fa_R.
148 Elpi derive.param2 fb.
149 ```
150
151 ### `param1_functor`
152
153 ```coq
154 Elpi derive.param1.functor is_list.
155 Check is_list_functor : forall A PA QA,
156 (forall x, PA x -> QA x) -> forall l, is_list A PA l -> list A QA l.
157 ```
158
159 ### `param1_trivial`
160
161 ```coq
162 Elpi derive.param1.trivial is_nat.
163 Check is_nat_trivial : forall x : nat, { p : is_nat x & forall q, p = q }.
164 Check is_nat_inhab : forall x : nat, is_nat x.
165 ```
166
167 ### `induction`
168
169 Induction principle for `T` based on `is_T`
170
171 ```coq
172 Elpi derive.induction list.
173 Check list_induction :
174 forall (A : Type) (PA : A -> Type) P,
175 P (nil A) ->
176 (forall x : A, PA x -> forall xs, P xs -> P (cons A x xs)) ->
177 forall l, is_list A PA l -> P l.
178 ```
179
180 ### `tag`
181
182 The "name" of the constructor
183
184 ```coq
185 Elpi derive.tag peano.
186 Check peano_tag : peano -> positive.
187
188 ```
189
190 ### `fields`
191
192 The types of the fields and the fields of each constructor
193
194 ```coq
195 Elpi derive.fields peano.
196 Check peano_fields_t : positive -> Type.
197 Check peano_fields : forall (n:peano), peano_fields_t (peano_tag n).
198 Check peano_construct : forall (p: positive), peano_fields_t p -> Datatypes.option peano.
199 Check peano_constructP : forall (n:peano), peano_construct (peano_tag n) (peano_fields n) = Datatypes.Some n.
200 ```
201
202 ### `eqb`
203
204 Equality test
205
206 ```coq
207 Elpi derive.eqb peano.
208 Check peano_eqb : peano -> peano -> bool.
209
210 ```
211
212 ### `eqbcorrect`
213
214 Two directions of the soundness proof
215
216 ```coq
217 Elpi derive.eqbcorrect peano.
218 Check peano_eqb_correct : forall n m, peano_eqb n m = true -> n = m.
219 Check peano_eqb_refl : forall n, peano_eqb n n = true.
220 ```
221
222 ### `eqbOK`
223
224 The soundness proof
225
226 ```coq
227 Elpi derive.eqbOK peano.
228 Check peano_eqb_OK : forall n m, reflect (n = m) (peano_eqb n m).
229 ```
230
231 ### `param1_congr`
232
233 Used by `param1_trivial`, not interesting.
234
235 ```coq
236 Elpi derive.param1.congr is_nat.
237 Check is_Succ congr : forall x (px qx : is_nat x),
238 px = qx ->
239 is_Succ x px = is_Succ x qx.
240 ```
241
242 </p></details>
243
244 <details><summary>legacy (click to expand)</summary><p>
245
246 See [Deriving proved equality tests in Coq-elpi: Stronger Induction Principles for
247 Containers](http://drops.dagstuhl.de/opus/volltexte/2019/11084/) for a
248 description of most of these components.
249
250
251 <img align="right" src="https://github.com/LPCIC/coq-elpi/blob/master/apps/derive/derive.svg" width="40%" />
252
253 ### `isK`
254
255 Given an inductive type it generates for each constructor a function that
256 tests if a term is a specific constructor.
257
258 Example:
259 ```coq
260 Elpi derive.isK list.
261 Print list_is_nil. (*
262 list_is_nil =
263 fun (A : Type) (i : list A) =>
264 match i with
265 | nil => true
266 | _ => false
267 end
268 *)
269 ```
270
271 ### `projK`
272
273 Given an inductive type it generates for each constructor `K` and argument
274 `i` of this constructor a function extracting that argument (provided enough
275 default values).
276
277 ```coq
278 Elpi derive.projK Vector.t.
279 Check projcons1. (*
280 projcons1
281 : forall (A : Type) (H : nat),
282 A -> forall n : nat, Vector.t A n ->
283 Vector.t A H -> A
284 ```
285 The intended use is to perform injection, i.e. one already has a term of the
286 shape `K args` and can just use these args to provide the default values.
287
288 If the projected argument's type depends on the value of other arguments, then it
289 is boxed using `existT`.
290 ```coq
291 Check projcons3. (*
292 projcons3
293 : forall (A : Type) (H : nat),
294 A -> forall n : nat, Vector.t A n ->
295 Vector.t A H -> {i1 : nat & Vector.t A i1}
296 *)
297 ```
298
299 ### injection
300
301 `injection H EqAB PL` given an equation `H` of type `EqAB` returns a list
302 of equations `PL`. `EqAB` is expected to be of the form `K .. = K ..` for
303 a constructor `K`.
304
305 coverage: does not do the smart thing when the obtained equations are like `{ i : nat & Vector.t A i } = ...` in which case, given that `nat` is `eqType` one could obtain systematically the two equalities.
306
307 Note: this is not a real derivation, since it generates no constant, but it a piece of
308 code used by derivations.
309
310 ### discriminate
311
312 `discriminate H EqAB G PG` given an equation `H` of type `EqAB` and
313 a goal `G` it provides a proof `PG`. It asserts that `EqAB` is of
314 the form `K1 .. = K2 ..` when `K1` is a constructor different from `K2`.
315
316 Note: this is not a real derivation, since it generates no constant, but it a piece of
317 code used by derivations.
318
319 ### `bcongr`
320
321 We call a boolean congruence lemma an instance of the `reflect` predicate
322 on a proposition `K x1..xn = K y1..yn` and a boolean expression `b1 && .. bn`.
323
324 ```coq
325 Elpi derive.bcongr list.
326 Check nil_congr : forall A, reflect (@nil A = @nil A) true.
327 Check cons_congr :
328 forall A,
329 forall (x y : A) b1, reflect (x = y) b1 ->
330 forall (xs ys : list A) b2, reflect (xs = ys) b2 ->
331 reflect (cons x xs = cons y ys) (b1 && b2).
332 ```
333
334 ### `eq`
335
336 Generates a boolean comparison function.
337
338 ```coq
339 Elpi derive.eq list.
340 Check list_eq. (*
341 list_eq
342 : forall A : Type,
343 (A -> A -> bool) -> list A -> list A -> bool
344 *)
345 ```
346
347 ### `eqK`
348
349 Generates, for each constructor, the correctness lemma for the comparison
350 function.
351
352 ```coq
353 Elpi derive.eqK list.
354
355 Check eq_axiom_nil : forall A fa, axiom (list A) (list_eq A fa) (@nil A).
356
357 Check eq_axiom_cons : forall A fa,
358 forall x, axiom A fa x ->
359 forall xs, axiom (list A) (list_eq A fa) xs ->
360 axiom (list A) (list_eq A fa) (cons x xs).
361 ```
362
363 ### `eqcorrect`
364
365 Correctness of equality test using reified type information.
366
367 ```coq
368 Elpi derive.eqcorrect list.
369 Check list_eq_correct :
370 forall A f l, is_list A (eq_axiom A f) l -> eq_axiom (list A) (list_eq A f) l.
371 ```
372
373 ### `eqOK`
374
375 Correctness of equality test.
376
377 ```coq
378 Elpi derive.eqOK list.
379 Check list_eq_OK :
380 forall A f, (forall a, axiom A f a) -> (forall l, eq_axiom (list A) (list_eq A f) l).
381 ```
382
383 ## Coverage
384
385 This is the list of inductive types we use for testing, and the table with the result of each derivation (:sunny: = OK, :bug: = does not work but might, :cloud: = looks like this can't possible work)
386
387
388 ```coq
389 Inductive empty := .
390 Inductive unit := tt.
391 Inductive peano := Zero | Succ (n : peano).
392 Inductive option A := None | Some (_ : A).
393 Inductive pair A B := Comma (a : A) (b : B).
394 Inductive seq A := Nil | Cons (x : A) (xs : seq A).
395 Inductive rose (A : Type) := Leaf | Node (sib : seq (rose A)).
396 Inductive nest A := NilN | ConsN (x : A) (xs : nest (pair A A)).
397 Fail Inductive bush A := BNil | BCons (x : A) (xs : bush (bush A)).
398 Inductive w A := via (f : A -> w A).
399 Inductive vect A : peano -> Type := VNil : vect A Zero | VCons (x : A) n (xs : vect A n) : vect A (Succ n).
400 Inductive dyn := box (T : Type) (t : T).
401 Inductive zeta Sender (Receiver := Sender) := Envelope (a : Sender) (ReplyTo := a) (c : Receiver).
402 Inductive beta (A : (fun x : Type => x) Type) := Redex (a : (fun x : Type => x) A).
403 Inductive iota := Why n (a : match n in peano return Type with Zero => peano | Succ _ => unit end).
404 Inductive large := K1 (_ : unit) | K2 (_ : unit) (_ : unit) | ...
405 Inductive prim_int := PI (i : Int63.int).
406 Inductive prim_float := PF (f : PrimFloat.float).
407 Record fo_record := { f1 : peano; f2 : unit; }.
408 Record pa_record A := { f3 : peano; f4 : A; }.
409 Record pr_record A := { pf3 : peano; pf4 : A; }. (* with primitive projections *)
410 Record dep_record := { f5 : peano; f6 : vect unit f5; }.
411 Variant enum := E1 | E2 | E3.
412 ```
413
414 test | eq | param1 | map | induction | isK | projK | bcongr | eqK | eqcorrect | eqOK | lens_laws
415 -----------|---------|---------|---------|-----------|---------|---------|---------|---------|-----------|---------|----------
416 empty | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
417 unit | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
418 peano | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
419 option | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
420 pair | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
421 seq | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
422 rose | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
423 nest | :cloud: | :sunny: | :cloud: | :sunny: | :sunny: | :sunny: | :sunny: | :bug: | :bug: | :bug: | :cloud:
424 w | :cloud: | :sunny: | :bug: | :sunny: | :sunny: | :sunny: | :sunny: | :bug: | :bug: | :bug: | :cloud:
425 vect | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :bug: | :bug: | :bug: | :bug: | :cloud:
426 dyn | :cloud: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :bug: | :bug: | :bug: | :bug: | :cloud:
427 zeta | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
428 beta | :sunny: | :sunny: | :bug: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :bug: | :sunny: | :cloud:
429 iota | :cloud: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud: | :bug: | :cloud: | :cloud: | :cloud:
430 large | :sunny: | :sunny: | :bug: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
431 prim_int | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
432 prim_float | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud: | :cloud: | :cloud:
433 fo_record | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny:
434 pa_record | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny:
435 pr_record | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny:
436 dep_record | :bug: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :bug: | :bug: | :bug: | :bug: | :cloud:
437 enum | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :sunny: | :cloud:
438
439
440 test | functor | inhab | congr | trivial |
441 ----------|---------|---------|-----------|---------|
442 is_empty | :sunny: | :sunny: | :sunny: | :sunny: |
443 is_unit | :sunny: | :sunny: | :sunny: | :sunny: |
444 is_peano | :sunny: | :sunny: | :sunny: | :sunny: |
445 is_option | :sunny: | :sunny: | :sunny: | :sunny: |
446 is_pair | :sunny: | :sunny: | :sunny: | :sunny: |
447 is_seq | :sunny: | :sunny: | :sunny: | :sunny: |
448 is_rose | :sunny: | :sunny: | :sunny: | :sunny: |
449 is_nest | :bug: | :bug: | :cloud: | :cloud: |
450 is_w | :bug: | :sunny: | :sunny: | :bug: |
451 is_vect | :sunny: | :bug: | :cloud: | :bug: |
452 is_dyn | :sunny: | :cloud: | :cloud: | :bug: |
453 is_zeta | :sunny: | :sunny: | :sunny: | :sunny: |
454 is_beta | :sunny: | :sunny: | :sunny: | :sunny: |
455 is_iota | :sunny: | :bug: | :cloud: | :bug: |
456 is_large | :sunny: | :sunny: | :bug: | :bug: |
457 is_prim_int | :sunny: | :sunny: | :sunny: | :sunny: |
458 is_is_prim_float| :sunny: | :sunny: | :sunny: | :sunny: |
459 is_fo_record | :sunny: | :sunny: | :sunny: | :sunny: |
460 is_pa_record | :sunny: | :sunny: | :sunny: | :sunny: |
461 is_pr_record | :sunny: | :sunny: | :sunny: | :sunny: |
462 is_dep_record| :sunny: | :bug: | :sunny: | :bug: |
463 is_enum | :sunny: | :sunny: | :sunny: | :sunny: |
464
465 </p></details>
466
467 <details><summary>experimental (click to expand)</summary><p>
468
469
470 ### `invert`
471
472 ```coq
473 Inductive is_list A PA : list A -> Type :=
474 | nilR : is_list (@nil A)
475 | consR : forall a : A, PA a ->
476 forall xs : list A, is_list xs -> is_list (cons a xs).
477
478 Elpi derive.invert is_list.
479 Print is_list_inv. (*
480 Inductive is_list_inv (A : Type) (PA : A -> Type) (idx0 : list A) : Type :=
481 | nilR_inv : idx0 = nil -> is_list_inv A PA idx0
482 | consR_inv : forall a : A, PA a ->
483 forall xs : list A, is_list_inv A PA xs ->
484 idx0 = (cons a xs) ->
485 is_list_inv A PA idx0.
486 *)
487 ```
488
489 ## `idx2inv`
490
491 ```coq
492 Elpi derive.idx2inv is_list.
493 Check is_list_to_is_list_inv :
494 forall A PA l, is_list A PA l -> is_list_inv A PA l.
495 ```
496
497 </p></details>
498
499 ## Writing a new derivation
500
501 A derivation is made of:
502 - a file implementing the derivation
503 - a data base to carry some state
504 - a stand alone command
505 - a hook in the main derive procedure
506
507 At the light of that, here a typical derivation file `myder.v`.
508 The first section
509 loads the standard derive code and declares the dependency the external file
510 `myder.elpi`. The file `derive_hook.elpi` contains a few data types needed
511 in order to register the derivation in the main derive loop.
512
513 ```coq
514 From elpi.apps.derive Extra Dependency "derive_hook.elpi" as derive_hook.
515 From mypkg Extra Dependency "myder.elpi" as myder.
516
517 From elpi Require Import elpi.
518 From elpi.apps Require Import derive.
519 ```
520
521 The database is typically a predicate `myder` linking a type name to some
522 concept previously derived. We also need to know if we did already derive a
523 type, hence we declare a second predicate `myder-done` (we could reuse the
524 former, but sometimes this is not easy, so here we are pedantic).
525 We like to prefix these data bases name with `derive.`.
526
527 ```coq
528 Elpi Db derive.mydb.db lp:{{
529 % [myder T D] links a type T to a derived concept D
530 pred myder o:gref, o:gref.
531
532 % [myder-done T] mean T was already derived
533 pred myder-done o:gref.
534 }}.
535 ```
536
537 Then we build a standalone derivation accessible via the name `derive.myder`
538 which accumulates the external files declared before, the data base and
539 an entry point
540
541 ```coq
542 Elpi Command derive.myder.
543 Elpi Accumulate File derive_hook.
544 Elpi Accumulate File myder.
545 Elpi Accumulate Db derive.mydb.db.
546 Elpi Accumulate lp:{{
547 main [str I] :- !, coq.locate I GR,
548 coq.gref->id GR Tname,
549 Prefix is Tname ^ "_",
550 derive.myder.main GR Prefix _.
551 main _ :- usage.
552
553 pred usage.
554 usage :- coq.error "Usage: derive.myder <object name>".
555 }}.
556
557 ```
558
559 This is enough to run the derivation via something like
560 `Elpi derive.myder nat.`. In order to have `derive` run it one has to
561 accumulate some code on top of `derive` itself.
562
563 ```coq
564 Elpi Accumulate derive Db derive.myder.db.
565 Elpi Accumulate derive File myder.
566 Elpi Accumulate derive lp:{{
567
568 dep1 "myder" "somedep".
569 dep1 "myder" "someotherdep".
570 derivation
571 (indt T) Prefix % inputs
572 (derive "myder" % name (for dep1)
573 (derive.myder.main (indt T) Prefix) % code to run
574 (myder-done (indt T)) % idempotency test
575 ).
576
577 }}.
578 ```
579
580 First, one declares via `dep1`
581 the derivations that should run before, here `somedep`
582 and `someotherdep`. `derive` will compute a topological order and ensure
583 dependencies are run first.
584 Then one declares a derivation for a gref and a prefix. One can restrict
585 which grefs can be derived, here for example we make `myder` only available
586 on `indt` (inductive types, and not definitions or constructors).
587 `Prefix` is a string, typically passed to the main code.
588 The the `(derive ...)` tuple carrier the name of the derivation, already used
589 in `dep1` and two predicates, one to run the derivation and one to
590 test if the derivation was already run.
591 The types for `dep1`, `derivation` and `derive` are declared in
592 `derive_hook.elpi`.
593
594 Finally, one is expected to `Import` the `myder.v` file in a derivation
595 group, for example `better_std.v` would look like so:
596
597 ```coq
598 From elpi.apps Require Export derive.
599 From elpi.apps Require Export
600 derive.map
601 derive.lens
602 derive.lens_laws
603 ...
604 myder (* new derivation *)
605 .
606 ```
607
608 So when the user `Import`s `better_std` he gets a fully loaded `derive`.
609
610 The code of the derivation must be put in a namespace. So `myder.elpi` should
611 look like so
612
613 ```elpi
614 namespace derive.myder {
615
616 pred main i:gref, i:string, o:list prop.
617 main GR Prefix Clauses :- std.do! [
618 ... % synthesize Body and Type
619 Name is Prefix ^ "myconcept",
620 coq.ensure-fresh-global-id Name FName,
621 coq.env.add-const FName Body Type _ C,
622 Clauses = [myder-done GR, myder GR (const C)],
623 std.forall Clauses (x\
624 coq.elpi.accumulate _ "derive.myder.db" (clause _ _ x)
625 ),
626 ].
627
628 }
629 ```
630
631 It is important that all clauses added to the database are also returned
632 (see the last argument of `main`). Derive runs all derivations at once
633 and databases are updated only when the program ends. So derive will
634 assume, with `=>`, the clauses generated by one derivation before running the
635 nest one.