forked from ecranceMERCE/trakt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.elpi
153 lines (122 loc) · 4.99 KB
/
proof.elpi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Trakt %
% _______ _ _ % Copyright (C) 2022 MERCE %
% |__ __| | | | | % (Mitsubishi Electric R&D Centre Europe) %
% | |_ __ __ _| | _| |_ % Enzo Crance <[email protected]> %
% | | '__/ _` | |/ / __| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% | | | | (_| | <| |_ % This file is distributed under the terms of the %
% |_|_| \__,_|_|\_\\__| % GNU Lesser General Public License Version 3 %
% % (see LICENSE file for the text of the license) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% simplify the proof before making a Coq term from it, preventing the final Coq proof term from
% being a useless composition of identities
pred minimise i:proof, o:proof.
minimise (proof.forall T PF CoqPF) (proof.forall T PF' CoqPF) :-
!, pi x\ decl x _ T => minimise (PF x) (PF' x).
minimise (proof.forall2 T U PF CoqPF) (proof.forall2 T U PF' CoqPF) :-
!,
pi x\ decl x _ T =>
pi y\ decl y _ (U x) =>
minimise (PF x y) (PF' x y).
minimise (proof.lift-logic ML Ps) MinProof :-
!, std.map Ps minimise Ps',
if (std.map Ps' (p\ p'\ sigma T\ p = proof.none T, p' = {{ @id lp:T }}) Ps'') (
coq.typecheck (app [ML|Ps'']) {{ lp:A -> lp:A }} ok,
MinProof = proof.none A
) (
MinProof = proof.lift-logic ML Ps'
).
minimise (proof.trans TVar Ps) MinProof :-
!, std.map Ps minimise Ps1,
std.filter Ps1 (p\ not (p = proof.none _)) Ps2,
if (Ps2 = []) (
Ps1 = [proof.none T|_],
MinProof = proof.none T
) (if (Ps2 = [P]) (
MinProof = P
) (
MinProof = proof.trans TVar Ps2
)).
minimise Proof Proof.
% make a Coq term from a structured elpi proof
pred build i:proof, o:term.
build Proof Term :-
minimise Proof MinProof,
build.aux MinProof Term.
pred build.aux i:proof, o:term.
build.aux (proof.none T) {{ @id lp:T }}.
build.aux (proof.of-term Term) Term.
build.aux (proof.forall T PF CoqProofF) CoqProof :-
pi x\ decl x _ T =>
build.aux (PF x) (CoqPF x),
if (CoqPF x = {{ id }}) (
copy (app [{{ id }}, X]) X => copy (CoqProofF CoqPF) CoqProof
) (
CoqProof = CoqProofF CoqPF
).
build.aux (proof.forall2 T U PF CoqProofF) CoqProof :-
pi x\ decl x _ T =>
pi y\ decl y _ (U x) =>
build.aux (PF x y) (CoqPF x y),
if (CoqPF x y = {{ id }}) (
copy (app [{{ id }}, X]) X => copy (CoqProofF CoqPF) CoqProof
) (
CoqProof = CoqProofF CoqPF
).
build.aux (proof.trans TVar Ps) Proof :-
if (TVar = covariant) (
std.rev Ps Ps'
) (
Ps' = Ps
),
std.map Ps' build.aux Impls,
std.filter Impls (i\ not (i = {{ id }})) Impls',
compose-all Impls' Proof.
build.aux (proof.lift-eq TVar CtxF EqProof) Proof :-
coq.typecheck EqProof {{ @eq lp:T lp:X lp:Y }} ok,
Ctx = fun _ T CtxF,
if (TVar = covariant) (
Proof = app [{{ @lift_eq_to_impl }}, T, X, Y, Ctx, EqProof]
) (
Proof = app [{{ @lift_eq_to_impl_contra }}, T, X, Y, Ctx, EqProof]
).
build.aux (proof.lift-logic LProof ArgProofs) (app [LProof|CoqArgProofs]) :-
std.map ArgProofs build.aux CoqArgProofs.
pred clear-custom-terms-in-proof i:proof, o:proof.
clear-custom-terms-in-proof (proof.trans TVar Proofs) (proof.trans TVar Proofs') :-
std.map Proofs clear-custom-terms-in-proof Proofs'.
clear-custom-terms-in-proof (proof.lift-eq TVar CtxF EqProof) (proof.lift-eq TVar CtxF' EqProof') :-
pi x\ clear-custom-terms (CtxF x) (CtxF' x),
clear-custom-terms EqProof EqProof'.
clear-custom-terms-in-proof (proof.of-term T) (proof.of-term T') :-
clear-custom-terms T T'.
clear-custom-terms-in-proof
(proof.lift-logic LProof ArgProofs) (proof.lift-logic LProof' ArgProofs') :-
clear-custom-terms LProof LProof',
std.map ArgProofs clear-custom-terms-in-proof ArgProofs'.
clear-custom-terms-in-proof (proof.forall T PF CoqPF) (proof.forall T' PF' CoqPF') :-
clear-custom-terms T T',
pi x\ clear-custom-terms-in-proof (PF x) (PF' x),
pi p\ clear-custom-terms (CoqPF p) (CoqPF' p).
clear-custom-terms-in-proof (proof.forall2 T U PF CoqPF) (proof.forall2 T' U' PF' CoqPF') :-
clear-custom-terms T T',
pi t\ clear-custom-terms (U t) (U' t),
pi x\ pi y\ clear-custom-terms-in-proof (PF x y) (PF' x y),
pi p\ clear-custom-terms (CoqPF p) (CoqPF' p).
clear-custom-terms-in-proof (proof.none T) (proof.none T') :-
clear-custom-terms T T'.
% util
% compose a list of functions
pred compose-all i:(list term), o:term.
compose-all Functions (fun `H` OriginT F) :-
Functions = [F1|_],
coq.typecheck F1 {{ lp:OriginT -> _ }} ok,
% coq.typecheck F1 TF1 ok,
% coq.unify-eq TF1 {{ lp:OriginT -> _ }} ok,
pi x\ decl x _ OriginT => apply-all Functions x (F x).
compose-all [] {{ id }}.
% apply a list of functions to a term
pred apply-all i:(list term), i:term, o:term.
apply-all [] Term Term.
apply-all [F|Fs] Term Term' :-
apply-all Fs (app [F, Term]) Term'.