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