1 % (c) 2009-2024 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(typing_tools,[
6 is_infinite_type/1, % check if a type is infinite for animation purposes
7 is_provably_finite_type/1, % check if a type is provably finite (not just for animation)
8 is_finite_type_in_context/2, % check if a type is finite, either in animation or proving context
9
10 type_has_at_least_two_elements/1,
11 non_empty_type/1,
12
13 valid_ground_type/1, % check if a type is a valid ground type
14 contains_infinite_type/1,
15 any_value_for_type/2,
16 normalize_type/2,
17 create_maximal_type_set/2, % converts sequences to cartesian product
18 create_type_set/2, % keeps sequence types
19 create_type_set/3, % with flag for keeping sequences types or not
20 lift_type_parameters/3
21 ]).
22 :- use_module(module_information,[module_info/2]).
23 :- module_info(group,typechecker).
24 :- module_info(description,'This module provides utilities for B types.').
25
26 %:- use_module(bsyntaxtree, [get_texpr_types/2, syntaxtraversion/6, find_typed_identifier_uses/3]).
27 :- use_module(library(lists), [maplist/2, maplist/3]).
28
29 % check if a type is infinite for ProB animation; deferred sets are not counted as infinite here!
30 is_infinite_type(X) :- var(X),!, add_internal_error('Variable as type:',is_infinite_type(X)),fail.
31 is_infinite_type(integer).
32 is_infinite_type(real).
33 is_infinite_type(string).
34 is_infinite_type(freetype(_)).
35 is_infinite_type(global(G)) :- infinite_global_set(G).
36 is_infinite_type(set(X)) :- is_infinite_type(X).
37 is_infinite_type(seq(_)).
38 is_infinite_type(couple(X,Y)) :- (is_infinite_type(X) -> true ; is_infinite_type(Y)).
39 is_infinite_type(record(Fields)) :-
40 ? ((member(field(_,Type),Fields), is_infinite_type(Type)) -> true).
41
42 contains_infinite_type([H|T]) :-
43 (is_infinite_type(H) -> true ; contains_infinite_type(T)).
44
45 :- use_module(b_global_sets,[provably_finite_global_set/1, b_empty_global_set/1, infinite_global_set/1]).
46
47 % this a finite type for the prover, not just for animation
48 % e.g., deferred sets in Event-B are not finite; in classical B they are in principle finite
49 is_provably_finite_type(X) :-
50 var(X),!,fail. % e.g., in tests 402, 403 this predicate can be called with variables in the type
51 is_provably_finite_type(boolean).
52 is_provably_finite_type(set(X)) :- is_provably_finite_type(X).
53 is_provably_finite_type(couple(X,Y)) :- is_provably_finite_type(X),is_provably_finite_type(Y).
54 is_provably_finite_type(global(G)) :-
55 provably_finite_global_set(G). % Note: in Classical B in principle all SETS are finite !
56 % note that scope_ DEFINITIONS make the set G finite here: TODO : investigate what we want/need
57 is_provably_finite_type(record(Fields)) :- maplist(finite_field,Fields).
58
59 finite_field(field(_,Type)) :- is_provably_finite_type(Type).
60
61
62
63 is_finite_type_in_context(animation,Type) :-
64 !,\+ is_infinite_type(Type). % all deferred sets counted as finite here; assumes record_construction already ran
65 is_finite_type_in_context(proving,Type) :-
66 is_provably_finite_type(Type). % here deferred sets can be in principle infinite; see Classical B comment above
67
68 % -------------------
69
70 type_has_at_least_two_elements(X) :- var(X),!,fail.
71 type_has_at_least_two_elements(boolean).
72 type_has_at_least_two_elements(string).
73 type_has_at_least_two_elements(integer).
74 type_has_at_least_two_elements(real).
75 type_has_at_least_two_elements(couple(A,B)) :-
76 ( type_has_at_least_two_elements(A) -> non_empty_type(B)
77 ; type_has_at_least_two_elements(B), non_empty_type(A)).
78 type_has_at_least_two_elements(set(A)) :- non_empty_type(A).
79 type_has_at_least_two_elements(seq(A)) :- non_empty_type(A).
80 type_has_at_least_two_elements(record([field(_,Type)|_])) :-
81 type_has_at_least_two_elements(Type). % TODO: look further if this has only one value
82 type_has_at_least_two_elements(freetype(_)).
83 % TODO: freetype
84
85 % normally all types are non-empty!
86 non_empty_type(X) :- var(X),!,fail.
87 non_empty_type(boolean).
88 non_empty_type(string).
89 non_empty_type(integer).
90 non_empty_type(real).
91 non_empty_type(global(G)) :- \+ b_empty_global_set(G).
92 non_empty_type(set(_)). % always empty set
93 non_empty_type(seq(_)).
94 non_empty_type(couple(A,B)) :- non_empty_type(A), non_empty_type(B).
95 non_empty_type(freetype(_)).
96 non_empty_type(record(Fields)) :- maplist(non_empty_field,Fields).
97
98 non_empty_field(field(_,Type)) :- non_empty_type(Type).
99
100 % --------------------
101
102 :- use_module(error_manager).
103
104 valid_ground_type(X) :- var(X),!, add_error(valid_ground_type,'Variable as type: ',X),fail.
105 valid_ground_type(X) :- valid_ground_type_aux(X),!.
106 valid_ground_type(X) :- add_error(valid_ground_type,'Illegal type term: ',X),fail.
107
108 %:- use_module(b_global_sets,[b_global_set/1]).
109
110 valid_ground_type_aux(any) :- format(user_error,'! Type contains *any*~n',[]). % can happen, e.g., for test 949 due to assertion prj1({{}},BOOL)({}|->TRUE) = {}
111 valid_ground_type_aux(integer).
112 valid_ground_type_aux(real).
113 valid_ground_type_aux(string).
114 valid_ground_type_aux(boolean).
115 valid_ground_type_aux(global(G)) :- atom(G).
116 %bmachine:b_get_machine_set(G).
117 %b_global_sets:b_global_set(G).
118 valid_ground_type_aux(freetype(FT)) :- ground(FT).
119 valid_ground_type_aux(set(X)) :- valid_ground_type(X).
120 valid_ground_type_aux(seq(X)) :- valid_ground_type(X).
121 valid_ground_type_aux(couple(X,Y)) :- valid_ground_type(X), valid_ground_type(Y).
122 valid_ground_type_aux(record(Fields)) :- valid_fields(Fields,'$prev_field_name').
123 valid_ground_type_aux(constant([X])) :- ground(X). % appears in Z freetype cases, test 738
124
125 valid_fields([],_).
126 valid_fields([field(Name,_)|_],_) :- \+ atom(Name),!,
127 add_error(valid_ground_type,'Illegal record field name: ',Name),fail.
128 valid_fields([field(Name,_)|_],PrevName) :- Name @=< PrevName,!,
129 add_error(valid_ground_type,'Record field names not sorted: ',PrevName:Name),fail.
130 valid_fields([field(Name,Type)|T],_) :-
131 valid_ground_type(Type),
132 valid_fields(T,Name).
133
134
135 % --------------
136
137 :- use_module(kernel_freetypes,[get_freeval_type/3]).
138 :- use_module(b_global_sets,[b_get_fd_type_bounds/3,get_global_type_value/3]).
139 any_value_for_type(T,V) :- if(inst2(T,V),true, (nl,print('*** instantiate_to_any_value failed: '), print(T:V),nl,nl %,trace,inst2(T,V)
140 )).
141 %inst2(_,V) :- ground(V),!.
142 inst2(boolean,V) :- !, inst3(V,pred_true).
143 inst2(pred,V) :- !, inst3(V,pred_true).
144 inst2(integer,V) :- !, V=int(X),inst_fd(X,0).
145 inst2(set(_),V) :- !, inst3(V,[]). % TO DO: what if V already partially instantiated
146 inst2(seq(_),V) :- !, inst3(V,[]). % TO DO: what if V already partially instantiated
147 inst2(couple(TA,TB),V) :- !, V=(A,B), inst2(TA,A), inst2(TB,B).
148 %inst2(real,V) :- !, V=... % TO DO
149 inst2(string,V) :- !, V=string(X), inst3(X,'').
150 inst2(real,V) :- !, V=term(X), (var(X) -> X=floating(0.0) ; X=floating(Y), inst3(Y,0.0)).
151 inst2(global(T),V) :- !, get_global_type_value(V,T,X),
152 b_get_fd_type_bounds(T,LowBnd,_UpBnd),
153 inst_fd(X,LowBnd).
154 inst2(record(Fields),rec(Vals)) :- !, instantiate_fields(Fields,Vals).
155 inst2(constant([A]),V) :- !, V=term(A).
156 inst2(freetype(FT),V) :- !, V=freeval(FT,CASE,Val),
157 (nonvar(CASE) -> (get_freeval_type(FT,CASE,Type) -> any_value_for_type(Type,Val)) % ensure no backtracking
158 ; nonvar(Val) -> print((nonvar_value(V))),nl, get_freeval_type(FT,CASE,Type), any_value_for_type(Type,Val)
159 ? ; (get_freeval_type(FT,C,Type) -> CASE=C, any_value_for_type(Type,Val))
160 ).
161 inst2(X,V) :- print(cannot_inst2(X,V)),nl.
162
163 inst3(X,V) :- (var(X) -> X=V ; true).
164
165 :- use_module(probsrc(clpfd_interface), [clpfd_some_element_in_domain/2]).
166 inst_fd(X,_) :- nonvar(X),!.
167 inst_fd(X,_Default) :- clpfd_some_element_in_domain(X,El),!, X=El.
168 inst_fd(X,X).
169
170 instantiate_fields([],[]).
171 instantiate_fields([field(Name,Type)|T],[field(Name,Val)|TV]) :-
172 any_value_for_type(Type,Val),
173 instantiate_fields(T,TV).
174
175 % ----------------------
176
177 % replace seq(X) by set(couple(integer,X))
178
179 normalize_type(X,Y) :-
180 (normalize_type_aux(X,Y) -> true ; add_internal_error('Illegal type: ',normalize_type(X,Y)),Y=X).
181
182 normalize_type_aux(V,R) :- var(V),!,R=V.
183 normalize_type_aux(any,any).
184 normalize_type_aux(pred,pred).
185 normalize_type_aux(subst,subst).
186 normalize_type_aux(boolean,boolean).
187 normalize_type_aux(integer,integer).
188 normalize_type_aux(string,string).
189 normalize_type_aux(real,real).
190 normalize_type_aux(constant(C),constant(C)).
191 normalize_type_aux(global(F),global(F)).
192 normalize_type_aux(freetype(F),freetype(F)).
193 normalize_type_aux(set(X),set(N)) :- normalize_type_aux(X,N).
194 normalize_type_aux(seq(X),set(couple(integer,N))) :- normalize_type_aux(X,N).
195 normalize_type_aux(couple(X,Y),couple(NX,NY)) :- normalize_type_aux(X,NX),normalize_type_aux(Y,NY).
196 normalize_type_aux(record(F),record(NF)) :- normalize_type_fields(F,NF).
197 normalize_type_aux(op(A,B),op(NA,NB)) :- maplist(normalize_type_aux,A,NA), maplist(normalize_type_aux,B,NB).
198
199 normalize_type_fields(V,R) :- var(V),!,R=V.
200 normalize_type_fields([],[]).
201 normalize_type_fields([field(N,T)|R],[field(N,NT)|RN]) :- normalize_type_aux(T,NT),normalize_type_fields(R,RN).
202
203 % -------------------------
204
205 % usage: lift_type_parameters(couple(t1,t1),[t1],R) --> R=couple(T,T)
206 lift_type_parameters(GroundType,[],LiftedType) :- !, LiftedType=GroundType.
207 lift_type_parameters(Type,TypeParameters,LiftedType) :-
208 sort(TypeParameters,STP),
209 maplist(gen_var,STP,Env),
210 lift_aux(Type,Env,LT),
211 LT=LiftedType.
212
213 gen_var(ID,ID-_).
214
215 lift_aux([],_,R) :- !, R=[].
216 lift_aux([H|T],Env,[LH|LT]) :- !, lift_aux(H,Env,LH), lift_aux(T,Env,LT).
217 lift_aux(Type,Env,Res) :- member(Type-Var,Env),!, Res=Var.
218 lift_aux(set(X),Env,set(LX)) :- !, lift_aux(X,Env,LX).
219 lift_aux(seq(X),Env,seq(LX)) :- !, lift_aux(X,Env,LX).
220 lift_aux(record(X),Env,record(LX)) :- !, lift_aux(X,Env,LX).
221 lift_aux(field(Name,X),Env,field(Name,LX)) :- lift_aux(X,Env,LX).
222 lift_aux(couple(X,Y),Env,couple(NX,NY)) :- !, lift_aux(X,Env,NX), lift_aux(Y,Env,NY).
223 lift_aux(Type,_,Type).
224
225 % -------------------------
226
227 :- use_module(bsyntaxtree, [create_texpr/4]).
228
229 % translate a ProB type into a B expression so that we can write x:TSet
230 % translate_type:
231 create_type_set(Type,TSet) :- create_type_set(Type,true,TSet).
232 create_type_set(Type,KeepSeq,TSet) :-
233 ( var(Type) -> add_error_and_fail(translate,'type_set Type is variable: ',Type)
234 ;
235 type_set2(Type,KeepSeq,Set,Infos),
236 create_texpr(Set,set(Type),Infos,TSet)).
237 type_set2(set(T),KeepSeq,pow_subset(Set),[]) :-
238 create_type_set(T,KeepSeq,Set).
239 type_set2(seq(T),KeepSeq,Res,Info) :-
240 (KeepSeq = true
241 -> Res=seq(Set), Info=[], create_type_set(T,KeepSeq,Set) % keep sequence types as is
242 ; type_set2(set(couple(integer,T)),KeepSeq,Res,Info)). % write as POW(INTEGER*T)
243 type_set2(couple(TA,TB),KeepSeq,cartesian_product(SetA,SetB),[]) :-
244 create_type_set(TA,KeepSeq,SetA), create_type_set(TB,KeepSeq,SetB).
245 %type_set2(global(G),_,identifier(G),[given_set]).
246 type_set2(global(G),_,value(global_set(G)),[given_set]). % avoids variable capture/clash
247 type_set2(integer,_,integer_set('INTEGER'),[]).
248 type_set2(string,_,string_set,[]).
249 type_set2(real,_,real_set,[]).
250 type_set2(boolean,_,bool_set,[]).
251 type_set2(record(TFields), KeepSeq, struct(Rec),[]) :-
252 create_texpr(rec(SFields),record(TFields),[],Rec),
253 type_set_fields(TFields,KeepSeq,SFields).
254 %type_set2(freetype(Id),identifier(Id),[given_set]).
255 % Missing: no case for constant([C]) term
256 type_set2(freetype(Id),_,freetype_set(Id),[]).
257 type_set2(constant(L),_,value(Terms),[]) :- % these types occur in Event-B for Theory plugin inductive datatypes
258 (var(L) -> add_internal_error('Illegal var constant list: ',type_set2(constant(L),_,value(Terms),[])) ; true),
259 maplist(create_term,L,Terms).
260 create_term(T,term(T)).
261 type_set_fields([],_,[]).
262 type_set_fields([field(Id,Type)|TFrest],KeepSeq,[field(Id,TSet)|EFrest]) :-
263 create_type_set(Type,KeepSeq,TSet),
264 type_set_fields(TFrest,KeepSeq,EFrest).
265
266 % translate a ProB type to a typed expression representing the maximal type
267 % set_type_to_maximal_texpr
268 create_maximal_type_set(ProBType,TExpr) :- create_type_set(ProBType,false,TExpr).
269 %replace_seq(ProBType,MType),create_type_set(MType,TExpr).
270
271 % translate a ProB type into a B value of all values of the type is done in b_type2_set
272
273
274
275