Debian Codegraph @

examples/example_record_to_sigma.v

raw
1 From elpi Require Import elpi.
2
3 (* Define a command to turn records into nested sigma types, suggested
4 by M. Maggesi for the UniMath library *)
5 Elpi Command UM.expand.
6 Elpi Accumulate lp:{{
7
8 % From a record declaration to an iterated sigma type
9 pred wrap-fields-ty i:record-decl, o:term.
10 wrap-fields-ty (field _ _ Ty _\ end-record) Ty.
11 wrap-fields-ty (field _ Proj Ty Fields) {{ sigT lp:F }} :-
12 coq.string->name Proj Name,
13 F = fun Name Ty Bo,
14 pi x\ decl x Name Ty ==> wrap-fields-ty (Fields x) (Bo x).
15
16 % From a record declaration to a function building the iterated pairs
17 % [wrap-fields-bo Rec Acc SigmaTypeDef SigmaTypeName Builder BuilderType]
18 % Builder = fun arg1 .. argn => existT arg1 (existT arg2 (.. argn) ..)
19 % Acc gathers arg1 .. argn while building the fun
20 % SigmaTypeDef is used to fill in the implicit arguments of existT
21 % SigmaTypeName is used for BuilderType = forall arg1 .. argn, SigmaTypeName
22 pred wrap-fields-bo i:record-decl, i:list term, i:term, i:term, o:term, o:term.
23 wrap-fields-bo end-record Acc SigTy Sig T Sig :-
24 std.rev Acc Args,
25 wrap-fields-bo.aux Args SigTy T.
26 wrap-fields-bo (field _ Proj Ty Fields) Acc SigTy Sig (fun Name Ty Bo) (prod Name Ty Tgt) :-
27 coq.string->name Proj Name,
28 pi x\ decl x Name Ty ==> wrap-fields-bo (Fields x) [x|Acc] SigTy Sig (Bo x) (Tgt x).
29
30 pred wrap-fields-bo.aux i:list term, i:term, o:term.
31 wrap-fields-bo.aux [Last] _ Last.
32 wrap-fields-bo.aux [X|XS] {{ sigT lp:F }} {{ existT lp:F lp:X lp:Rest }} :-
33 F = fun _ _ G,
34 wrap-fields-bo.aux XS (G X) Rest.
35
36 % We declare the type and its constructor.
37 % Missing features:
38 % - parameters are not supported
39 % - projections are not synthesized
40 main [indt-decl (record Name _Sort Kname Fields)] :-
41 wrap-fields-ty Fields T,
42 std.assert-ok! (coq.typecheck T Ty) "oops, wrap-fields-ty is bugged",
43 coq.env.add-const Name T Ty _ C,
44 wrap-fields-bo Fields [] T (global (const C)) K KTy,
45 std.assert-ok! (coq.typecheck K KTy) "oops, wrap-fields-bo is bugged",
46 coq.env.add-const Kname K KTy _ _.
47
48 }}.
49
50 Elpi Export UM.expand.
51
52 (* From now on UM.expand is a regular command taking as the only argument
53 a record declaration. *)
54
55 UM.expand Record foo := mk_foo {
56 f1 : Type;
57 f2 : f1 -> Type;
58 f3 : forall t : f1, f2 t -> bool
59 }.
60
61 Print foo.
62 (* foo = {f1 : Type & {f2 : f1 -> Type & forall t : f1, f2 t -> bool}} : Type *)
63
64 Print mk_foo.
65 (* mk_foo = fun (f1 : Type) (f2 : f1 -> Type) (f3 : forall t : f1, f2 t -> bool) =>
66 existT (fun f4 : Type => {f5 : f4 -> Type & forall t : f4, f5 t -> bool}) f1
67 (existT (fun f4 : f1 -> Type => forall t : f1, f4 t -> bool) f2 f3)
68 : forall (f1 : Type) (f2 : f1 -> Type), (forall t : f1, f2 t -> bool) -> foo *)