Debian Codegraph @

apps/derive/elpi/induction.elpi

raw
1 /* induction principles */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 shorten std.{do!, assert!, last, appendR, rev, map}.
6
7 namespace derive.induction {
8
9 % local db associating to each constructor the hypothesis to be used
10 func induction-hyp-db term -> term.
11
12 func informative. % loaded in the context if we can elim to Type
13
14 % create (paramX.prove (is_T params) P IH)
15 :index (1)
16 func mk-paramX.prove-clause list term, term, term, term -> prop.
17 mk-paramX.prove-clause [_] T P IH (param1-functor-db T P IH).
18
19 mk-paramX.prove-clause [_,_|Args] T P IH (pi x y\ C x y) :-
20 pi x y\ mk-paramX.prove-clause Args {coq.mk-app T [x,y]} {coq.mk-app P [x,y]} {coq.mk-app IH [x,y]} (C x y).
21
22 % branch for constructor k is (hyp-k ...) where ... are the terms
23 % generated by prove-args
24 func branch term, term, list term, list term -> term.
25 branch K _ V VT R :-
26 induction-hyp-db K IH,
27 ((pi H G P\ paramX.prove H G P :- param1-functor-db H G P) =!=>
28 (pi X\ paramX.cross X) =!=>
29 paramX.prove-args V VT Args),
30 coq.mk-app IH Args R.
31
32 func oty term, list term, list term -> term.
33 oty _ _ VT P :-
34 last VT XT,
35 copy XT P.
36
37 func branches term, term, list term, term, int -> int, term, term.
38 branches (prod Name S T) Ity Args IH N M (prod Name S F1) (fun Name S R1) :- !,
39 @pi-decl Name S x\
40 branches (T x) Ity [x|Args] IH {calc (N + 1)} M (F1 x) (R1 x).
41
42 branches (sort _) Ity Args IH Rno Rno Fty (fun `x` ItyArgs Bo) :- do! [
43 coq.mk-app Ity {rev Args} ItyArgs,
44 copy ItyArgs PArgs,
45 Fty = prod `x` ItyArgs (_\ PArgs),
46 copy Ity P,
47 mk-paramX.prove-clause Args Ity P IH C,
48 @pi-decl `x` ItyArgs x\
49 C =>
50 coq.build-match x ItyArgs oty branch (Bo x)
51 ].
52
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 % Synthesize the type of each hypothesis starting from the type
55 % of the constructor of is_T
56
57 func hyp term -> term.
58 hyp (prod N S T) (prod N Q R) :- !,
59 copy S Q,
60 @pi-decl N Q x\ hyp (T x) (R x).
61 hyp S Q :- copy S Q.
62
63 func hyps list term, list term, term, term -> term.
64 hyps [K|KS] [KT|KTS] Ity Arity (fun Name Ind Bo) :-
65 coq.term->gref K GRK,
66 coq.name-suffix `H` {coq.gref->id GRK} Name,
67 hyp KT Ind, !, % we cut since copy generates many solutions
68 @pi-decl `Name` Ind x\
69 induction-hyp-db K x =!=> % This is the hyp to be used for branch K
70 hyps KS KTS Ity Arity (Bo x).
71
72 hyps [] [] Ity Arity (fix `IH` Recno Fty Bo) :-
73 @pi-decl `IH` Fty f\ sigma C\
74 branches Arity Ity [] f 0 Recno Fty (Bo f).
75
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % Parameters and the P of the induction predicate (truncated wrt the type
78 % of the unary parametricity translation, eg P takes only the indexes)
79
80 func truncated-predicate-ty term -> term.
81 truncated-predicate-ty (sort _) T :- informative, !,
82 T = sort (typ U), coq.univ.new U.
83 truncated-predicate-ty (sort _) (sort prop).
84
85 truncated-predicate-ty (prod N S T) (prod N S R) :-
86 @pi-decl N S x\ truncated-predicate-ty (T x) (R x).
87
88 % loads the context with the substitution "is_T params -> P"
89 func mk-subst-clause term, term -> list prop.
90 mk-subst-clause Ity P C :-
91 coq.safe-dest-app Ity IT ITArgs,
92 C = [(copy IT P :- !),
93 (pi Args Rest O\
94 copy (app[IT|Args]) O :- !, appendR ITArgs Rest Args, coq.mk-app P Rest O, !)].
95
96 func params int, term, list term, list term, term -> term.
97 params N Ity K KT (prod Nx Sx x\ prod NP (SP x) (T x)) (fun Nx Sx x\ fun NP (SP x) (R1 x)) :- N > 0, !,
98 M is N - 2,
99 @pi-decl Nx Sx x\
100 @pi-decl NP (SP x) px\
101 % useless, the identity map is already there
102 % paramX.prove px px (fun `x` x a\ fun `pa` (app[px,a]) pa\ pa) =>
103 params M
104 {coq.mk-app Ity [x,px]}
105 {map K (k\ coq.mk-app k [x,px])}
106 {map KT (coq.subst-prod [x,px])}
107 (T x px) (R1 x px).
108
109 params 0 Ity K KT Arity (fun `P` Pty p\ Bo p) :-
110 % P only takes the indexes of Arity
111 truncated-predicate-ty Arity Pty,
112 @pi-decl `P` Pty p\ sigma Subst\
113 (mk-subst-clause Ity p Subst,
114 % replace (is_T params) with P
115 Subst =>
116 hyps K KT Ity Arity (Bo p)).
117
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119
120 func main inductive, string -> list prop.
121 main GR Prefix [Clause] :- do! [
122 T = global (indt GR),
123
124 if (coq.env.informative? GR) (Informative = [informative]) (Informative = []),
125
126 assert! (reali T TR) {calc (
127 "derive.induction: no unary parametricity translation for" ^
128 {coq.term->string T} ^ ", use derive.param1 first")},
129 TR = global (indt GRR),
130 coq.env.indt GRR Ind Lno _ Arity K KT,
131 assert! (Ind = tt) "derive.induction: Coinductive types are not supported",
132
133 % we build the induction principle
134 Informative =!=> params Lno TR {std.map K (k\r\ r = global (indc k))} KT Arity R,
135 % coq.say {coq.term->string R},
136 std.assert-ok! (coq.typecheck R RT) "derive.induction generates illtyped term",
137
138 Name is Prefix ^ "induction",
139 coq.ensure-fresh-global-id Name FName,
140 coq.env.add-const FName R RT _ I,
141
142 % we register it as a clause
143 Clause = (induction-db GR (global (const I)) :- !),
144 coq.elpi.accumulate _ "derive.induction.db" (clause _ (before "induction-db:fail") Clause)
145 ].
146
147 }