-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp_terms.mod
178 lines (132 loc) · 4.37 KB
/
tp_terms.mod
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module tp_terms.
accumulate poly_typing.
tp_ast_get_term (tp_ast U _TP) U.
tp_ast_get_type (tp_ast _U TP) TP.
make_tp_ast u (tp_ast tp_u unit).
make_tp_ast (pair T1 T2) (tp_ast (tp_pair U1 U2) (TP1 ** TP2)) :-
make_tp_ast T1 U1,
tp_ast_get_type U1 TP1,
make_tp_ast T2 U2,
tp_ast_get_type U2 TP2.
make_tp_ast (fst T) (tp_ast (tp_fst U) TP1) :-
make_tp_ast T U,
tp_ast_get_type U (TP1 ** _TP2).
make_tp_ast (snd T) (tp_ast (tp_snd U) TP2) :-
make_tp_ast T U,
tp_ast_get_type U (_TP1 ** TP2).
make_tp_ast (inl T) (tp_ast (tp_inl U) (TP1 ++ _TP2)) :-
make_tp_ast T U,
tp_ast_get_type U TP1.
make_tp_ast (inr T) (tp_ast (tp_inr U) (_TP1 ++ TP2)) :-
make_tp_ast T U,
tp_ast_get_type U TP2.
make_tp_ast (case CT LF RF) (tp_ast (tp_case UC ULF URF) TP3) :-
make_tp_ast CT UC,
tp_ast_get_type UC (TP1 ++ TP2),
conv_fun LF ULF (TP1 --> TP3),
conv_fun RF URF (TP2 --> TP3).
make_tp_ast (lam F) (tp_ast (tp_lam UF) TP) :- conv_fun F UF TP.
make_tp_ast (rec F) (tp_ast (tp_rec UF) TP) :- conv_rec F UF TP.
make_tp_ast (app T1 T2) (tp_ast (tp_app U1 U2) TP2) :-
make_tp_ast T1 U1,
tp_ast_get_type U1 (TP1 --> TP2),
make_tp_ast T2 U2,
tp_ast_get_type U2 TP1.
make_tp_ast (abs_rtp T) (tp_ast (tp_abs_rtp U) TP) :-
make_tp_ast T U,
TP = mu TF,
tp_ast_get_type U (TF TP).
make_tp_ast (rep_rtp T) (tp_ast (tp_rep_rtp U) (TF TP)) :-
make_tp_ast T U,
TP = mu TF,
tp_ast_get_type U TP.
make_tp_ast (tlam TP1 F) (tp_ast (tp_tlam TP1 UF) TP) :-
TP = (TP1 --> TP2),
conv_fun F UF TP.
make_tp_ast (trec TP1 F) (tp_ast (tp_trec TP1 UF) TP) :-
TP = (TP1 --> _TP2),
conv_rec F UF TP.
make_tp_ast (tlet S T F) (tp_ast (tp_tlet S U UF) TP) :-
generalize S T,
make_tp_ast T U,
pi x\ pi tp_x\
make_tp_ast x tp_x =>
(pi tp\ instantiate S tp => tp_ast_get_type tp_x tp) =>
sigma Ux\
make_tp_ast (F x) Ux,
tp_ast_get_type Ux TP,
UF tp_x = Ux.
make_tp_ast (tabs F) (tp_ast (tp_tabs UF) TP) :-
pi x\ pi tp_x\
make_tp_ast x tp_x =>
tp_ast_get_type tp_x TP1 =>
make_tp_ast (F TP1) U,
tp_ast_get_type U TP,
UF TP1 = U.
% CONV_FUN
type conv_fun (tm -> tm) -> (tp_astK -> tp_astK) -> tp -> o.
conv_fun F UF TP :-
TP = (TP1 --> TP2),
pi x\ pi tp_x\
make_tp_ast x tp_x =>
tp_ast_get_type tp_x TP1 =>
sigma U\
make_tp_ast (F x) U,
tp_ast_get_type U TP2,
UF tp_x = U.
% CONV_REC
type conv_rec (tm -> tm -> tm) -> (tp_astK -> tp_astK -> tp_astK) -> tp -> o.
conv_rec F UF TP :-
pi f\ pi tp_f\
make_tp_ast f tp_f =>
tp_ast_get_type tp_f TP =>
conv_fun (F f) (UF tp_f) TP.
% TP_TM_FUN_LINEAR
tp_tm_fun_linear F :-
pi v\
tp_tm_linear_aux v true fail =>
tp_tm_linear_aux (F v) true _.
tp_tm_linear_aux (tp_ast tp_u _) X X.
tp_tm_linear_aux (tp_ast (tp_pair T1 T2) _) A C :-
tp_tm_linear_aux T1 A B,
tp_tm_linear_aux T2 B C.
tp_tm_linear_aux (tp_ast (tp_fst T) _) A B :- tp_tm_linear_aux T A B.
tp_tm_linear_aux (tp_ast (tp_snd T) _) A B :- tp_tm_linear_aux T A B.
tp_tm_linear_aux (tp_ast (tp_inl T) _) A B :- tp_tm_linear_aux T A B.
tp_tm_linear_aux (tp_ast (tp_inr T) _) A B :- tp_tm_linear_aux T A B.
tp_tm_linear_aux (tp_ast (tp_case CT LF RF) _) A C :-
tp_tm_linear_aux CT A B,
tp_tm_fun_linear_aux LF B C1,
tp_tm_fun_linear_aux RF B C2,
(
C1,
C = C2
;
C = fail
).
tp_tm_linear_aux (tp_ast (tp_lam F) _) A B :- tp_tm_fun_linear_aux F A B.
tp_tm_linear_aux (tp_ast (tp_rec F) _) A B :-
pi f\
(pi X\ tp_tm_linear_aux f X X) =>
tp_tm_fun_linear_aux (F f) A B.
tp_tm_linear_aux (tp_ast (tp_app T1 T2) _) A C :-
tp_tm_linear_aux T1 A B,
tp_tm_linear_aux T2 B C.
tp_tm_linear_aux (tp_ast (tp_abs_rtp T) _) A B :- tp_tm_linear_aux T A B.
tp_tm_linear_aux (tp_ast (tp_rep_rtp T) _) A B :- tp_tm_linear_aux T A B.
tp_tm_linear_aux (tp_ast (tp_tlam _ F) _) A B :- tp_tm_fun_linear_aux F A B.
tp_tm_linear_aux (tp_ast (tp_trec _ F) _) A B :-
pi f\
(pi X\ tp_tm_linear_aux f X X) =>
tp_tm_fun_linear_aux (F f) A B.
tp_tm_linear_aux (tp_ast (tp_tlet _ T F) _) A C :-
tp_tm_linear_aux T A B,
tp_tm_fun_linear_aux F B C.
tp_tm_linear_aux (tp_ast (tp_tabs F) _) A B :-
pi tp\ tp_tm_linear_aux (F tp) A B.
tp_tm_fun_linear_aux F A B :-
pi x\
(pi X\ tp_tm_linear_aux x X X) =>
tp_tm_linear_aux (F x) A B.
output_tp_ast Ch T :- printterm Ch T.
print_tp_ast T :- output_tp_ast std_out T.