1 % (c) 2009-2026 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(input_syntax_tree, [remove_raw_position_info/2
6 ,get_raw_position_info/2
7 ,try_get_raw_position_info/2 % same but without checks
8 ,extract_raw_identifiers/2 % also works with list of raw ASTs
9 ,extract_all_raw_identifiers/3
10 ,get_definition_name/2
11 ,raw_replace/4
12 ,raw_conjunct/2
13 ,create_fresh_identifier/3
14 ,raw_create_z_let/8, remove_unnecessary_let_params/4
15 ,map_over_raw_expr/3
16 ,raw_update_file_nrs/3
17 ,raw_operator/1
18 ,raw_symbolic_annotation/2
19 ,raw_operator_term/1, raw_literal_term/1, raw_special_set_term/1
20 ]).
21
22 :- use_module(module_information,[module_info/2]).
23 :- module_info(group,typechecker).
24 :- module_info(description,'Utilities on the raw AST.').
25
26 :- meta_predicate map_over(2,*,*).
27 :- meta_predicate map_over_raw_expr(*,2,*).
28
29 :- use_module(error_manager).
30 :- use_module(probsrc(tools_strings),[ajoin/2]).
31 :- use_module(tools_positions, [is_position/1, update_position_filenumber/4]).
32 :- use_module(tools,[safe_functor/4]).
33 :- use_module(translate,[translate_raw_bexpr_with_limit/3]).
34 :- use_module(preferences,[get_preference/2]).
35 :- use_module(self_check).
36
37 get_raw_position_info(Compound,Info) :-
38 try_get_raw_position_info(Compound,Info),
39 is_position(Info),!.
40 get_raw_position_info(Compound,Info) :-
41 add_internal_error('Could not get position info: ',get_raw_position_info(Compound,Info)),
42 Info = unknown.
43
44 try_get_raw_position_info(Compound,Info) :-
45 compound(Compound),
46 functor(Compound,_,Arity), Arity>0,
47 arg(1,Compound,Info).
48
49 update_raw_position_info(Compound,Pos,NewCompound,NewPos) :-
50 compound(Compound),
51 Compound =.. [Functor,Pos|Rest],
52 NewCompound =.. [Functor,NewPos|Rest].
53
54 :- assert_must_succeed((input_syntax_tree:remove_raw_position_info(rewrite_protected(identifier(p,x)),R),
55 R == rewrite_protected(identifier(x)))).
56
57 remove_raw_position_info([],Output) :- !,Output=[].
58 remove_raw_position_info([I|Irest],[O|Orest]) :-
59 !,remove_raw_position_info(I,O),
60 remove_raw_position_info(Irest,Orest).
61 remove_raw_position_info(rewrite_protected(X),Output) :- !, % has no position info
62 Output=rewrite_protected(Y), remove_raw_position_info(X,Y).
63 remove_raw_position_info(Input,Output) :-
64 compound(Input),functor(Input,Functor,Arity),Arity>0,!,
65 Arity2 is Arity-1,
66 functor(Output,Functor,Arity2),
67 remove_raw_position_info2(1,Arity,Input,Output).
68 remove_raw_position_info(Input,Input).
69
70
71 remove_raw_position_info2(Pos,Arity,Input,Output) :-
72 Pos < Arity,!,Pos2 is Pos+1,
73 arg(Pos2,Input,IA), arg(Pos,Output,OA),
74 remove_raw_position_info(IA,OA),
75 remove_raw_position_info2(Pos2,Arity,Input,Output).
76 remove_raw_position_info2(_Pos,_Arity,_Input,_Output).
77
78 :- use_module(library(ordsets),[ord_union/3, ord_member/2]).
79 extract_raw_identifiers(Ast,Ids) :-
80 extract_raw_identifiers2(Ast,[],Unsorted,[]),
81 sort(Unsorted,Ids).
82 extract_raw_identifiers2([],_) --> !,[].
83 extract_raw_identifiers2([I|Irest],Bound) -->
84 !,extract_raw_identifiers2(I,Bound),
85 extract_raw_identifiers2(Irest,Bound).
86 extract_raw_identifiers2(identifier(_,I),Bound) --> !,add_if_free(I,Bound).
87 extract_raw_identifiers2(definition(_,I,Args),Bound) --> !,
88 add_if_free(I,Bound),
89 extract_raw_identifiers2(Args,Bound). % Note: we do not see the Ids inside the definition body!
90 extract_raw_identifiers2(Input,Bound) -->
91 {raw_quantifier(Input,Ids,Subs)},!,
92 %{append(Ids,Bound,NewBound)},
93 {sort(Ids,SIds),ord_union(SIds,Bound,NewBound)},
94 extract_raw_identifiers2(Subs,NewBound).
95 extract_raw_identifiers2(Input,Bound) -->
96 {compound(Input),functor(Input,_Functor,Arity),Arity>1,!},
97 extract_raw_identifiers3(2,Arity,Input,Bound).
98 extract_raw_identifiers2(_,_) --> [].
99
100 add_if_free(Id,Bound,In,Out) :-
101 %(ordsets:is_ordset(Bound) -> true ; add_internal_error('Not ordset: ',add_if_free(Id,Bound))),
102 (ord_member(Id,Bound) -> In=Out ; In=[Id|Out]).
103
104 extract_raw_identifiers3(Pos,Arity,Input,Bound) -->
105 {Pos=<Arity,!,Pos2 is Pos+1, arg(Pos,Input,Arg)},
106 extract_raw_identifiers2(Arg,Bound),
107 extract_raw_identifiers3(Pos2,Arity,Input,Bound).
108 extract_raw_identifiers3(_Pos,_Arity,_Input,_Bound) --> [].
109
110 :- use_module(library(lists),[maplist/3]).
111 :- assert_must_succeed((input_syntax_tree:extract_raw_identifiers(
112 let_expression(p,[identifier(p,x)],equal(p,identifier(p,x),identifier(p,y)),identifier(p,x)),R),
113 R == [y])).
114 raw_quantifier(Expr,SortedIds,Subs) :-
115 raw_quantifier_aux(Expr,RawIds,Subs),
116 maplist(raw_id,RawIds,Ids),
117 sort(Ids,SortedIds).
118 raw_id(identifier(_,ID),ID).
119
120 raw_quantifier_aux(SymTerm,Ids,Subs) :- raw_symbolic_annotation(SymTerm,Term),!,
121 raw_quantifier_aux(Term,Ids,Subs).
122 raw_quantifier_aux(forall(_,Ids,P),Ids,[P]).
123 raw_quantifier_aux(exists(_,Ids,P),Ids,[P]).
124 raw_quantifier_aux(comprehension_set(_,Ids,P),Ids,[P]).
125 raw_quantifier_aux(event_b_comprehension_set(_,Ids,E,P),Ids,[E,P]).
126 raw_quantifier_aux(lambda(_,Ids,P,E),Ids,[P,E]).
127 raw_quantifier_aux(general_sum(_,Ids,P,E),Ids,[P,E]).
128 raw_quantifier_aux(general_product(_,Ids,P,E),Ids,[P,E]).
129 raw_quantifier_aux(quantified_intersection(_,Ids,P,E),Ids,[P,E]).
130 raw_quantifier_aux(quantified_union(_,Ids,P,E),Ids,[P,E]).
131 raw_quantifier_aux(any(_,Ids,P,S),Ids,[P,S]).
132 raw_quantifier_aux(let(_,Ids,P,S),Ids,[P,S]).
133 raw_quantifier_aux(let_expression(_,Ids,P,S),Ids,[P,S]). % in the raw AST P is an equality predicate
134 raw_quantifier_aux(let_predicate(_,Ids,P,S),Ids,[P,S]). % ditto
135 raw_quantifier_aux(var(_,Ids,S),Ids,[S]).
136 raw_quantifier_aux(recursive_let(_,Id,E),[Id],[E]).
137
138 get_definition_name(expression_definition(_Pos,Name,_Args,_Body),Name).
139 get_definition_name(predicate_definition(_Pos,Name,_Args,_Body),Name).
140 get_definition_name(substitution_definition(_Pos,Name,_Args,_Body),Name).
141 get_definition_name(expression(_Pos,Name,_Args,_Body),Name). % from EXPRESSIONS section
142 get_definition_name(predicate(_Pos,Name,_Args,_Body),Name). % from PREDICATES section
143 get_definition_name(substitution(_Pos,Name,_Args,_Body),Name). % from SUBSTITUTIONS section
144
145 :- use_module(parsercall, [transform_string_template/3]).
146
147 % raw_replace(+InputAST,-Replaces,+OutputAST,DefName):
148 % InputAST: A raw AST as it comes from the parser (without type information)
149 % Replaces: A list of "replace(Id,Expr)" where Id the the identifier which
150 % should be replaced by the expression Expr
151 % OutputAST: A raw AST where all matching identifiers are replaced by their
152 % expression
153 % DefName: name of definition for which we do the rewrite
154 % used for rewriting DEFINITION calls
155 raw_replace(Expr,[],Expr,_) :- !.
156 raw_replace(Old,Replaces,New,DefName) :-
157 (Replaces = [replace(Id,_)|_], atom(Id) -> true
158 ; add_internal_error('Illegal raw_replace list: ',Replaces)),
159 get_introduced_ids(Replaces,IntroducedIds), %print(intro(IntroducedIds)),nl,
160 ? raw_replace2(Old,Replaces,IntroducedIds,New,DefName).
161 raw_replace2([],_,_,[],_) :- !.
162 raw_replace2([E|Rest],Replaces,IntroducedIds,[N|NRest],DefName) :- !,
163 ? raw_replace2(E,Replaces,IntroducedIds,N,DefName),
164 ? raw_replace2(Rest,Replaces,IntroducedIds,NRest,DefName).
165 raw_replace2(rewrite_protected(X),_Replaces,_,rewrite_protected(Y),_) :- !,X=Y.
166 raw_replace2(becomes_such(Pos,Vars,Cond),Replaces,IntroducedIds,becomes_such(Pos,NVars,NCond),DefName) :- !,
167 raw_replace2(Cond,Replaces,IntroducedIds,NCond,DefName),
168 raw_replace_id_list(Vars,Replaces,IntroducedIds,NVars).
169 raw_replace2(becomes_element_of(Pos,Vars,Cond),Replaces,IntroducedIds,becomes_element_of(Pos,NVars,NCond),DefName) :- !,
170 raw_replace2(Cond,Replaces,IntroducedIds,NCond,DefName),
171 raw_replace_id_list(Vars,Replaces,IntroducedIds,NVars).
172 % TODO: support more raw AST nodes with identifier lists
173 raw_replace2(identifier(Pos,Old),Replaces,_,New,_) :- !,
174 (memberchk(replace(Old,New),Replaces) -> true ; New=identifier(Pos,Old)).
175 raw_replace2(record_field(RPos,Record,Field),Replaces,IntroducedIds,record_field(RPos,NRecord,NField),DefName) :- !,
176 raw_replace2(Record,Replaces,IntroducedIds,NRecord,DefName),
177 (Field=identifier(_,FieldName),
178 raw_replace2(Field,Replaces,IntroducedIds,NField,DefName)
179 -> (NField=identifier(_,NewFieldName)
180 -> (NewFieldName=FieldName
181 -> true
182 ; get_preference(use_hygienic_definitions,true) ->
183 ajoin(['Rewriting field name ',FieldName,' to ',NewFieldName,
184 ' not allowed for HYGIENIC_DEFINITIONS'],Msg),
185 % probably due to introduce LET and the let variable cannot be of type "field-name"
186 add_warning(definition_rewrite,Msg,'',RPos)
187 ; ajoin(['Rewriting field name ',FieldName,' during definition expansion to: '],Msg),
188 add_message(definition_rewrite,Msg,NewFieldName,RPos)
189 )
190 ; translate_raw_bexpr_with_limit(NField,50,FS),
191 ajoin(['Rewriting field name ',FieldName,' to an illegal expression: '],Msg),
192 (try_get_raw_position_info(NField,NFPos) -> true ; NFPos=RPos),
193 add_warning(definition_rewrite,Msg,FS,NFPos)
194 )
195 ; NField=Field).
196 raw_replace2(multiline_template(Pos,TS),Replaces,Ids,New,DefName) :- !,
197 % rewrite template, it may contain expressions with ids
198 ? transform_string_template(TS,Pos,RawExpr),
199 raw_replace2(RawExpr,Replaces,Ids,New,DefName).
200 raw_replace2(Expr,_,IntroducedIds,_,DefName) :-
201 quantifier_capture_warning(Expr,IntroducedIds,DefName),
202 fail.
203 raw_replace2(X,_Replaces,_,Res,_) :- simple_expr(X), !,Res=X.
204 raw_replace2(Expr,Replaces,IntroducedIds,New,DefName) :-
205 safe_functor(raw_replace2_expr,Expr,Functor,Arity),
206 safe_functor(raw_replace2_new,New,Functor,Arity),
207 ? raw_replace3(Arity,Expr,Replaces,IntroducedIds,New,DefName).
208 raw_replace3(0,_,_,_,_,_) :- !.
209 raw_replace3(N,Expr,Replaces,IntroducedIds,NExpr,DefName) :-
210 arg(N,Expr,Old),
211 arg(N,NExpr,New),
212 ? raw_replace2(Old,Replaces,IntroducedIds,New,DefName),
213 N2 is N-1,
214 ? raw_replace3(N2,Expr,Replaces,IntroducedIds,NExpr,DefName).
215
216 simple_expr(real(_,_)).
217 simple_expr(string(_,_)).
218 simple_expr(integer(_,_)).
219
220
221 % replace a list of identifiers in the orginal AST which should remain a list of identifiers
222 % we allow
223 raw_replace_id_list([],_,_,[]).
224 raw_replace_id_list([identifier(Pos,Old)|T],Replaces,Intro,Res) :- !,
225 (memberchk(replace(Old,New),Replaces)
226 -> (flatten_couple_id_list(New,Res,Tail) -> NT=Tail
227 ; ajoin(['Rewriting identifier ',Old,' to an illegal value during definition expansion: '],Msg),
228 add_warning(definition_rewrite,Msg,New,Pos),
229 Res = [New|NT]
230 )
231 ; Res=[identifier(Pos,Old)|NT]
232 ), raw_replace_id_list(T,Replaces,Intro,NT).
233 raw_replace_id_list([H|T],Replaces,Intro,[H|NT]) :-
234 arg(1,H,Pos),
235 add_warning(definition_rewrite,'Not an identifier in raw AST: ',H,Pos), % should not happen
236 raw_replace_id_list(T,Replaces,Intro,NT).
237
238 % flatten a couple list of identifiers; can happen when we have vars == x,y and pass vars to a definition as argument
239 flatten_couple_id_list(couple(_Pos,List)) --> !,
240 % one could check that in Pos we have a pos_context with definition_call
241 l_flatten_couple_id_list(List).
242 flatten_couple_id_list(OtherVal) --> {OtherVal=identifier(_,_)},[OtherVal].
243
244 l_flatten_couple_id_list([]) --> [].
245 l_flatten_couple_id_list([H|T]) --> !, flatten_couple_id_list(H), l_flatten_couple_id_list(T).
246
247 :- use_module(library(ordsets),[ord_union/2, ord_intersection/3]).
248 % this can happen e.g. for egt(x) == (#y.(y:NAT1 & y<100 & x<y)) when calling egt(y+1), i.e., replacing x by y+1 in body of DEFINITION
249 quantifier_capture_warning(Expr,IntroducedIds,DefName) :-
250 raw_quantifier(Expr,SortedIds,_Body),
251 ord_intersection(IntroducedIds,SortedIds,Clash),
252 Clash \= [],
253 % TO DO?: check if we really use a corresponding LHS identifier in Body: extract_raw_identifiers(Body,UsedIds), maplist(get_lhs_id,Replaces,LHSIds),
254 %format('Quantifier ids: ~w~nIntroduced Ids: ~w~n',[SortedIds,IntroducedIds]),
255 arg(1,Expr,Pos),
256 functor(Expr,QKind,_),
257 ajoin(['Quantifier (',QKind,') inside DEFINITION ',DefName,
258 ' may capture identifiers from arguments of definition call: '],Msg),
259 add_warning(definition_variable_capture,Msg,Clash,Pos).
260
261 % compute which ids are introduced by performing the replacements
262 get_introduced_ids(Replaces,Ids) :- maplist(get_rhs_ids,Replaces,ListOfList),
263 ord_union(ListOfList,Ids).
264 get_rhs_ids(replace(_,New),Ids) :- extract_raw_identifiers(New,Ids).
265 %get_lhs_id(replace(Id,_),Id).
266
267 raw_conjunct([],truth(none)).
268 raw_conjunct([H|T],R) :-
269 ( T=[] -> H=R
270 ;
271 R=conjunct(none,H,RT),
272 raw_conjunct(T,RT)
273 ).
274
275 %raw_occurs(ID,Term) :- raw_id(Term,ID).
276 %raw_occurs(ID,Term) :- compound(Term), Term =.. [_Func,_Pos|Args],
277 % member(A,Args), raw_occurs(ID,A).
278
279
280 % create_fresh_identifier(+PreferredName,+Ast,-Name):
281 % Generates an identifier that does not occur in Ast
282 % PreferredName: An atom with the preferred name of the identifier
283 % Ast: An untyped AST
284 % Name: An identifier (an atom) that does not occur free in AST,
285 % If PreferredName does not occur free in AST, it's 'PreferredName',
286 % otherwise it's 'PreferredName_N' where N is a natural number
287 create_fresh_identifier(PreferredName,Ast,Name) :-
288 extract_raw_identifiers(Ast,UsedIds),
289 ( memberchk(PreferredName,UsedIds) ->
290 % Allready in use, find a fresh one by adding a number
291 atom_codes(PreferredName,PreferredCodes),
292 create_fresh_identifier_aux(PreferredCodes,UsedIds,1,Name)
293 ;
294 Name = PreferredName).
295 create_fresh_identifier_aux(PreferredCodes,UsedIds,I,Name) :-
296 number_codes(I,ICodes),append(PreferredCodes,[95|ICodes],Codes),
297 atom_codes(NewName,Codes),
298 ( memberchk(NewName,UsedIds) ->
299 % Allready in use, try again with a higher number
300 I2 is I+1,
301 create_fresh_identifier_aux(PreferredCodes,UsedIds,I2,Name)
302 ;
303 Name = NewName).
304
305 % ----------------------------
306
307 % the raw equivalent of create_z_let(DefType,Params,Args,Body,_NewInfos,NewBody) for hygenic macros
308 % and to avoid performance issues with macro duplication
309 % VarsToAvoid: sorted list of all identifiers (free or bound) occurring in Body and Args, and in the bodies
310 % of the DEFINITIONS they call (transitively); these are expanded later inside the scope of the LET
311 % (see extract_all_raw_identifiers/3 and its use in btypechecker)
312 % a parameter is renamed if it occurs in VarsToAvoid; in particular if it occurs in an argument, as
313 % the LET equalities Param=Arg are inside the scope of the LET (e.g., x in pos(1,x) for pos(x,y) == (y>0))
314 raw_create_z_let(DefType,Name,CallPos,Params,Args,Body,VarsToAvoid,NewBody) :-
315 maplist(raw_id,Params,ParamIds), sort(ParamIds,SParamIds),
316 ord_union(VarsToAvoid,SParamIds,UsedIds), % fresh ids must not clash with any of these
317 alpha_raw_id_rename(Params,VarsToAvoid,UsedIds,NewParams,RenameList),
318 raw_replace(Body,RenameList,RenamedBody,Name),
319 create_raw_let_expr_or_pred(DefType,CallPos,NewParams,Args,RenamedBody,NewBody).
320
321 % extract_all_raw_identifiers(+RawAST,-Ids,-DefNames)
322 % all identifiers occurring in RawAST, whether free or bound, and the names of the DEFINITIONS called
323 % (definition calls without arguments are just identifiers and are also returned in Ids)
324 extract_all_raw_identifiers(Ast,Ids,DefNames) :-
325 all_raw_ids(Ast,Ids0,[],Defs0,[]),
326 sort(Ids0,Ids), sort(Defs0,DefNames).
327 all_raw_ids(V,I,I,D,D) :- var(V),!.
328 all_raw_ids(identifier(_,ID),[ID|I],I,D,D) :- atom(ID),!.
329 all_raw_ids(definition(_,Name,Args),I,IR,[Name|D],DR) :- !, all_raw_ids(Args,I,IR,D,DR).
330 all_raw_ids(T,I,IR,D,DR) :- compound(T),!, functor(T,_,N), all_raw_ids_args(1,N,T,I,IR,D,DR).
331 all_raw_ids(_,I,I,D,D).
332 all_raw_ids_args(K,N,T,I,IR,D,DR) :-
333 (K>N -> I=IR, D=DR
334 ; arg(K,T,A), all_raw_ids(A,I,I1,D,D1), K1 is K+1, all_raw_ids_args(K1,N,T,I1,IR,D1,DR)).
335
336 :- use_module(probsrc(gensym),[gensym/2]).
337 % create a fresh id if necessary along with renaming substitution
338 alpha_raw_id_rename([],_,_,[],[]).
339 alpha_raw_id_rename([identifier(Pos,OldID)|TI],VarsToAvoid,UsedIds,[identifier(Pos,FreshID)|TRes],Replaces) :-
340 (ord_member(OldID,VarsToAvoid)
341 -> fresh_gensym(OldID,UsedIds,FreshID),
342 Replaces=[replace(OldID,identifier(Pos,FreshID))|TRep]
343 ; FreshID=OldID, Replaces=TRep),
344 alpha_raw_id_rename(TI,VarsToAvoid,UsedIds,TRes,TRep).
345
346 % generate ids of the form _x__1; they can only be written with backquotes in B (`_x__1`),
347 % but gensym does not know about the ids in the formula: hence we also check UsedIds
348 fresh_gensym(OldID,UsedIds,FreshID) :- atom_concat('_',OldID,Prefix), gensym(Prefix,ID),
349 (ord_member(ID,UsedIds) -> fresh_gensym(OldID,UsedIds,FreshID) ; FreshID=ID).
350
351 % remove unnecessary assignments from parameters to itself
352 % happens quite often with multiple defs calling each other using same names for parameters
353 remove_unnecessary_let_params([],[],[],[]).
354 remove_unnecessary_let_params([ID|TI],[Val|TV],RI,RV) :-
355 (is_unnecessary_let(ID,Val) -> remove_unnecessary_let_params(TI,TV,RI,RV)
356 ; RI=[ID|TRI], RV=[Val|TRV], remove_unnecessary_let_params(TI,TV,TRI,TRV)).
357 is_unnecessary_let(identifier(_,ID),identifier(_,ID)).
358
359 % create a let of right type (expression, pred, subst) and converting value list into equality predicate
360 create_raw_let_expr_or_pred(_,_,[],[],Body,Res) :- !, Res=Body. % no let required
361 create_raw_let_expr_or_pred(DefKind,Pos,Ids,Values,Body,RawLet) :-
362 create_let_equalities(Ids,Values,Pos,Equalities),
363 create_raw_let_aux(DefKind,Pos,Ids,Equalities,Body,RawLet).
364
365 create_let_equalities([],[],Pos,truth(Pos)).
366 create_let_equalities([ID|TI],[V|TV],Pos,Res) :- Equal = equal(Pos,ID,V),
367 (TI=[] -> Res = Equal
368 ; Res = conjunct(Pos,Equal,Rest), create_let_equalities(TI,TV,Pos,Rest)).
369
370 create_raw_let_aux(expression,Pos,Ids,Equalities,Body, let_expression(Pos,Ids,Equalities,Body)).
371 create_raw_let_aux(predicate,Pos,Ids,Equalities,Body, let_predicate(Pos,Ids,Equalities,Body)).
372 create_raw_let_aux(substitution,Pos,Ids,Equalities,Body, let(Pos,Ids,Equalities,Body)).
373
374 % -----------------------------
375
376 % map a predicate over the raw AST to transform it
377 % we suppose that if the predicate P succeeds the sub-arguments do not need to be transformed
378 % note: P is also applied to the bound identifiers of quantifiers, comprehension sets, lambdas, ...
379
380 map_over(P,RawExpr,Res) :- map_over_raw_expr(RawExpr,P,Res).
381
382 map_over_raw_expr(RawExpr,P,Res) :- call(P,RawExpr,NewRawExpr),!,
383 Res=NewRawExpr.
384 map_over_raw_expr(RawExpr,P,Res) :- recur_over_raw_expr(RawExpr,P,Res).
385
386 recur_over_raw_expr([A|T],P,[MA|MT]) :- !,
387 map_over_raw_expr(A,P,MA),
388 map_over_raw_expr(T,P,MT).
389 recur_over_raw_expr(conjunct(Pos,A,B),P,conjunct(Pos,MA,MB)) :- !,
390 map_over_raw_expr(A,P,MA),
391 map_over_raw_expr(B,P,MB).
392 recur_over_raw_expr(disjunct(Pos,A,B),P,disjunct(Pos,MA,MB)) :- !,
393 map_over_raw_expr(A,P,MA),
394 map_over_raw_expr(B,P,MB).
395 recur_over_raw_expr(equal(Pos,A,B),P,equal(Pos,MA,MB)) :- !,
396 map_over_raw_expr(A,P,MA),
397 map_over_raw_expr(B,P,MB).
398 recur_over_raw_expr(A,_,R) :- atomic(A),!,R=A.
399 recur_over_raw_expr(A,P,R) :- raw_syntax_traversion(A,Fixed,Args),!,
400 functor(A,F,N), functor(R,F,N),
401 raw_syntax_traversion(R,Fixed,NewArgs),
402 maplist(map_over(P),Args,NewArgs).
403 recur_over_raw_expr(F,P,MF) :- F =.. [Functor,Pos|Args], % default: arg1 is position, rest is sub-arguments
404 maplist(map_over(P),Args,NewArgs),
405 MF =.. [Functor,Pos|NewArgs].
406
407 raw_syntax_traversion(definitions(Pos,List),defs(Pos),List). % definitions section
408 raw_syntax_traversion(expression_definition(Pos,Name,Paras,Body),expression_definition(Pos,Name),[Body|Paras]).
409
410 raw_syntax_traversion(conjunct(Pos,List),con(Pos),List). % n-ary conjunct
411 raw_syntax_traversion(disjunct(Pos,List),disj(Pos),List). % n-ary disjunct
412 raw_syntax_traversion(comprehension_set(Pos,Ids,Body),cs(Pos),[Ids,Body]).
413 raw_syntax_traversion(event_b_comprehension_set(Pos,Ids,Pred,Body),cs(Pos),[Ids,Pred,Body]).
414 raw_syntax_traversion(lambda(Pos,Ids,Pred,Expr),lm(Pos),[Ids,Pred,Expr]).
415 raw_syntax_traversion(symbolic_comprehension_set(Pos,Ids,Body),sc(Pos),[Ids,Body]).
416 raw_syntax_traversion(symbolic_event_b_comprehension_set(Pos,Ids,Pred,Body),sc(Pos),[Ids,Pred,Body]).
417 raw_syntax_traversion(symbolic_lambda(Pos,Ids,Pred,Expr),symlam(Pos),[Ids,Pred,Expr]).
418 raw_syntax_traversion(function(Pos,Fun,List),fun(Pos),[Fun|List]). % function call
419 raw_syntax_traversion(sequence_extension(Pos,List),sqext(Pos),List).
420 raw_syntax_traversion(set_extension(Pos,List),sext(Pos),List).
421 raw_syntax_traversion(definition(Pos,Name,ParaList),defcall(Pos,Name),ParaList).
422 raw_syntax_traversion(rec(Pos,List),rec(Pos),List). % record
423 % all other terms (forall, exists, let_predicate, ...) are handled by the default clause of recur_over_raw_expr
424
425
426
427 % update file numbers to new numbers (e.g., useful when adding separately parsed .prob files to an existing machine)
428 % the NewNrs list gives the new numbers of files 1,2,3,... etc respectively
429
430 :- assert_must_succeed((input_syntax_tree:raw_update_file_nrs(
431 comprehension_set(p5(1,1,1,1,9),[identifier(p5(1,1,2,1,3),x)],identifier(p5(1,1,5,1,6),x)),[3],R),
432 R == comprehension_set(p5(3,1,1,1,9),[identifier(p5(3,1,2,1,3),x)],identifier(p5(3,1,5,1,6),x)))).
433 raw_update_file_nrs(RawExpr,NewNrs,NewRawExpr) :-
434 map_over_raw_expr(RawExpr,raw_update_file_nrs3(NewNrs),NewRawExpr).
435
436 :- use_module(library(lists),[nth1/3]).
437 raw_update_file_nrs3(NewNrs,OldRaw,NewRaw) :-
438 OldRaw \= [_|_], % a list has no position info
439 update_raw_position_info(OldRaw,Pos,UpdatedRaw,NewPos),
440 update_position_filenumber(Pos,OldNr,NewPos,NewNr),
441 (nth1(OldNr,NewNrs,NewNr) -> true
442 ; add_internal_error('Unknown file number in raw AST: ',raw_update_file_nrs(OldNr,NewNrs)),fail),
443 !,
444 recur_over_raw_expr(UpdatedRaw,raw_update_file_nrs3(NewNrs),NewRaw).
445 raw_update_file_nrs3(NewNrs,OldRaw,NewRaw) :-
446 recur_over_raw_expr(OldRaw,raw_update_file_nrs3(NewNrs),NewRaw).
447
448 % ---------------------------------
449
450 % raw set_extension or comprehension_set or similar
451 raw_special_set_term(SymTerm) :- raw_symbolic_annotation(SymTerm,Term),!, raw_special_set_term(Term).
452 raw_special_set_term(comprehension_set(_,_,_)).
453 raw_special_set_term(event_b_comprehension_set(_,_,_,_)).
454 raw_special_set_term(lambda(_,_,_,_)).
455 raw_special_set_term(quantified_intersection(_,_,_,_)).
456 raw_special_set_term(quantified_union(_,_,_,_)).
457 raw_special_set_term(sequence_extension(_,_)).
458 raw_special_set_term(set_extension(_,_)).
459
460 % check if we have a symbolic annotation and transform into regular raw term without annotation:
461 raw_symbolic_annotation(symbolic_composition(A,B,C),composition(A,B,C)).
462 raw_symbolic_annotation(symbolic_comprehension_set(Pos,Ids,Body),
463 comprehension_set(Pos,Ids,Body)).
464 raw_symbolic_annotation(symbolic_event_b_comprehension_set(Pos,Ids,Pred,Body),
465 event_b_comprehension_set(Pos,Ids,Pred,Body)).
466 raw_symbolic_annotation(symbolic_lambda(Pos,Ids,Pred,Expr),
467 lambda(Pos,Ids,Pred,Expr)).
468 raw_symbolic_annotation(symbolic_quantified_union(A,B,C,D),quantified_union(A,B,C,D)).
469
470 % simple raw operators whose first argument is a position and all other are raw formulas
471 raw_operator(add).
472 raw_operator(add_real).
473 raw_operator(block).
474 raw_operator(card).
475 raw_operator(cartesian_product).
476 raw_operator(composition).
477 raw_operator(concat).
478 raw_operator(conjunct).
479 raw_operator(convert_bool).
480 raw_operator(convert_real).
481 raw_operator(convert_int_floor).
482 raw_operator(convert_int_ceiling).
483 %raw_operator(couple).
484 %raw_operator(definition).
485 raw_operator(description).
486 raw_operator(disjunct).
487 raw_operator(div).
488 raw_operator(div_real).
489 raw_operator(domain).
490 raw_operator(domain_restriction).
491 raw_operator(domain_subtraction).
492 raw_operator(equal).
493 raw_operator(equivalence).
494 raw_operator(first).
495 raw_operator(first_projection).
496 raw_operator(front).
497 raw_operator(general_concat).
498 raw_operator(general_union).
499 raw_operator(greater).
500 raw_operator(greater_equal).
501 raw_operator(identifier).
502 raw_operator(identity).
503 raw_operator(if_then_else).
504 raw_operator(image).
505 raw_operator(implication).
506 raw_operator(interval).
507 raw_operator(intersection).
508 raw_operator(last).
509 raw_operator(less).
510 raw_operator(less_equal).
511 raw_operator(less_real).
512 raw_operator(less_equal_real).
513 raw_operator(max).
514 raw_operator(member).
515 raw_operator(minus_or_set_subtract).
516 raw_operator(minus).
517 raw_operator(minus_real).
518 raw_operator(min).
519 raw_operator(multiplication).
520 raw_operator(multiplication_real).
521 raw_operator(mult_or_cart).
522 raw_operator(negation).
523 raw_operator(not_equal).
524 raw_operator(not_member).
525 raw_operator(not_subset_strict).
526 raw_operator(not_subset).
527 raw_operator(overwrite).
528 raw_operator(partial_function).
529 raw_operator(perm).
530 raw_operator(pow_subset).
531 raw_operator(power_of).
532 raw_operator(power_of_real).
533 raw_operator(range).
534 raw_operator(range_restriction).
535 raw_operator(range_subtraction).
536 raw_operator(rec_entry).
537 raw_operator(record_field).
538 raw_operator(reverse).
539 raw_operator(seq).
540 raw_operator(size).
541 raw_operator(struct).
542 raw_operator(subset_strict).
543 raw_operator(subset).
544 raw_operator(tail).
545 raw_operator(total_injection).
546 raw_operator(total_function).
547 raw_operator(trans_function).
548 raw_operator(unary_minus).
549 raw_operator(unary_minus_real).
550 raw_operator(union).
551 % external_function_call, ... : treated in list_operator
552 %raw_operator(Op) :- print(uncovered_op(Op)),nl,fail.
553 % TO DO: add more operators
554
555 raw_operator_term(Term) :- functor(Term,Op,_), raw_operator(Op). % TODO: match arity
556
557 % raw value literal term:
558 raw_literal_term(boolean_true(_)).
559 raw_literal_term(boolean_false(_)).
560 raw_literal_term(integer(_,_)).
561 raw_literal_term(multiline_template(_,_)).
562 raw_literal_term(real(_,_)).
563 raw_literal_term(string(_,_)).
564 % sets:
565 raw_literal_term(bool_set(_)).
566 raw_literal_term(integer_set(_)).
567 raw_literal_term(real_set(_)).
568 raw_literal_term(string_set(_)).
569 raw_literal_term(empty_set(_)).
570 raw_literal_term(empty_sequence(_)).
571