| 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(record_detection,[replace_sets_by_records/2]). | |
| 6 | ||
| 7 | ||
| 8 | :- use_module(tools). | |
| 9 | :- use_module(self_check). | |
| 10 | ||
| 11 | :- use_module(module_information,[module_info/2]). | |
| 12 | :- module_info(group,typechecker). | |
| 13 | :- module_info(description,'This module detects bijections between deferred sets and cartesian products, and compiles them away.'). | |
| 14 | ||
| 15 | ||
| 16 | :- use_module(library(lists)). | |
| 17 | :- use_module(library(ordsets)). | |
| 18 | :- use_module(library(between),[between/3]). | |
| 19 | ||
| 20 | :- use_module(bmachine_structure). | |
| 21 | :- use_module(bsyntaxtree). | |
| 22 | :- use_module(preferences,[get_preference/2]). | |
| 23 | :- use_module(b_ast_cleanup,[clean_up/3]). | |
| 24 | :- use_module(kernel_freetypes,[get_freetype_type_parameters/3]). | |
| 25 | :- use_module(b_global_sets,[register_replaced_global_set/2]). | |
| 26 | ||
| 27 | replace_sets_by_records(Machine,ResultMachine) :- | |
| 28 | % replace until a fixpoint is reached | |
| 29 | ? | replace_sets_by_records2(Machine,RMachine),!, |
| 30 | replace_sets_by_records(RMachine,ResultMachine). | |
| 31 | /* we could do something like this for the values clause for deferred sets mapped to integer intervals: | |
| 32 | it would have to be done for all deferred sets + the problem is that the type checker runs before this code ! | |
| 33 | replace_sets_by_records(Machine,ResultMachine) :- | |
| 34 | bmachine:get_section(values,Machine,Values), | |
| 35 | member(b(values_entry(TID,TVal),_,Info),Values), | |
| 36 | get_texpr_id(TID,Set), | |
| 37 | bmachine:get_section(deferred_sets,Machine,Sets), | |
| 38 | get_texpr_id(TExpr,Set),member(TExpr,Sets), | |
| 39 | get_texpr_type(TVal,Type), | |
| 40 | !, | |
| 41 | Domain = TVal, | |
| 42 | replace_type_in_machine(GlobalSet,Type,Domain,Machine,ResultMachine). | |
| 43 | */ | |
| 44 | replace_sets_by_records(Machine,Machine). | |
| 45 | ||
| 46 | replace_sets_by_records2(Machine,ResultMachine) :- | |
| 47 | get_preference(use_record_construction,true), | |
| 48 | has_deferred_sets(Machine), | |
| 49 | get_section(deferred_sets,Machine,DeferredSets), | |
| 50 | select_section(properties,Properties,NewProperties,Machine,ConsMachine), | |
| 51 | ? | select_constructor_axiom(Properties,DeferredSets,Set,Constructor,Cons,Domain,RestList1), |
| 52 | (debug:debug_mode(on) -> format('Record detected for deferred set ~w~n',[Set]) ; true), | |
| 53 | check_record_construction_ok(Set,Domain), | |
| 54 | replace_sets_by_records3(NewProperties,ConsMachine,Set,Constructor,Cons,Domain,RestList1,ResultMachine). | |
| 55 | ||
| 56 | ||
| 57 | :- use_module(typing_tools,[is_infinite_type/1]). | |
| 58 | :- use_module(specfile,[classical_b_mode/0]). | |
| 59 | :- use_module(error_manager,[add_message/4]). | |
| 60 | check_record_construction_ok(Set,Domain) :- | |
| 61 | ? | classical_b_mode, % classical B |
| 62 | get_texpr_type(Domain,Type), | |
| 63 | is_infinite_type(Type), | |
| 64 | % now we know that we have detected a record | |
| 65 | % Section 5.2.6 of \cite{Abrial:BBook} (page 281) and Section 7.13 of \atelierb{} handbook says that given sets are finite | |
| 66 | !, | |
| 67 | (domain_is_provably_finite(Domain) | |
| 68 | -> true % e.g., {x|x:1..3 & x>5}: the type is infinite but the domain is not | |
| 69 | ; get_preference(disprover_mode,false), % fail in disprover mode for classical B; do not apply construction | |
| 70 | ajoin(['Translating deferred set ',Set,' to possibly infinite set (set USE_RECORD_CONSTRUCTION to FALSE to prevent this) : '],Msg), | |
| 71 | add_message(record_detection,Msg,Domain,Domain) | |
| 72 | ). | |
| 73 | check_record_construction_ok(_,_). | |
| 74 | ||
| 75 | ||
| 76 | :- use_module(probsrc(pathes_extensions_db), [compile_time_unavailable_extension/2]). | |
| 77 | :- if(compile_time_unavailable_extension(wd_extension, _)). | |
| 78 | domain_is_provably_finite(_) :- fail. | |
| 79 | :- else. | |
| 80 | :- use_module(wdsrc(well_def_analyser),[prove_sequent/3]). | |
| 81 | :- use_module(tools_meta,[safe_time_out/3]). | |
| 82 | % try to prove finite(Domain) with the WD prover (without hypotheses, i.e., constants in Domain are not known) | |
| 83 | domain_is_provably_finite(Domain) :- | |
| 84 | create_texpr(finite(Domain),pred,[],Goal), | |
| 85 | catch(safe_time_out(prove_sequent(animation,Goal,[]),100,Res),_,fail), | |
| 86 | Res \== time_out. | |
| 87 | :- endif. | |
| 88 | ||
| 89 | :- use_module(error_manager,[add_internal_error/2, add_error/4]). | |
| 90 | :- use_module(translate,[print_bexpr/1]). | |
| 91 | replace_sets_by_records3(NewProperties,ConsMachine,Set,Constructor1,Cons,Domain,RestList1,ResultMachine) :- | |
| 92 | Cons = constructor(ConstructorDomain,RecDomainType,_), | |
| 93 | add_texpr_infos(Constructor1,[record_detection(constructor)],Constructor), | |
| 94 | create_constructor_definition(Constructor,ConstructorDomain,ConsDef), | |
| 95 | (debug:debug_mode(on) -> format(' Constructor for ~w: ',[Set]),print_bexpr(ConsDef),nl ; true), %% | |
| 96 | create_recordset_definition(Set,Domain,SetDef,NonEmptyDef), | |
| 97 | (debug:debug_mode(on) -> format(' New definition for ~w: ',[Set]),print_bexpr(SetDef),nl ; true), %% | |
| 98 | create_optional_field_access(Set,Domain,Constructor,RecordFunIds,RestList1,RestList), | |
| 99 | conjunct_predicates([ConsDef,SetDef,NonEmptyDef|RestList],NewProperties), | |
| 100 | move_deferred_set(Set,ConsMachine,SetMachine), | |
| 101 | % replace the constants (constructor,accessors,update functions) by versions that | |
| 102 | % have an info field that indicate their function (needed later to exclude them when | |
| 103 | % adding additional constraints): | |
| 104 | foldl(replace_constant,[Constructor|RecordFunIds],SetMachine,M2), | |
| 105 | %% print(replacing(Set,RecDomainType)),nl, %% | |
| 106 | replace_type_in_machine(Set,RecDomainType,Domain,M2,ResultMachine), | |
| 107 | !, | |
| 108 | register_replaced_global_set(Set,Domain). | |
| 109 | replace_sets_by_records3(NewProperties,ConsMachine,Set,Constructor,Cons,Domain,RestList1,ResultMachine) :- | |
| 110 | add_internal_error('Replacing record failed ',replace_sets_by_records3(NewProperties,ConsMachine,Set,Constructor,Cons,Domain,RestList1,ResultMachine)),fail. | |
| 111 | ||
| 112 | has_deferred_sets(Machine) :- | |
| 113 | get_section(deferred_sets,Machine,DefSets), | |
| 114 | DefSets = [_|_]. | |
| 115 | ||
| 116 | % replace_constant(+NewConstant,+In,-Out): | |
| 117 | % replace a constant by a new one of the same name | |
| 118 | replace_constant(NewConstant,In,Out) :- | |
| 119 | % try the replacement in abstract and concrete constants: | |
| 120 | ( replace_id_in_section(NewConstant,abstract_constants,In,Out) -> true | |
| 121 | ; replace_id_in_section(NewConstant,concrete_constants,In,Out) -> true | |
| 122 | ; add_error(record_detection,'Could not replace constant: ',NewConstant,NewConstant), | |
| 123 | fail | |
| 124 | ). | |
| 125 | replace_id_in_section(NewId,Section,In,Out) :- | |
| 126 | get_texpr_id(NewId,Id), get_texpr_id(OldId,Id), | |
| 127 | select_section(Section,OldIds,NewIds,In,Out), | |
| 128 | selectchk(OldId,OldIds,NewId,NewIds). | |
| 129 | ||
| 130 | % select_constructor_axiom(+Properties,+Sets,-Set,-Constructor,-Cons,-Domain,-RestList) | |
| 131 | % Chooses a constructor axiom if any exists | |
| 132 | % Properties: The properties (a typed predicate) of the machine | |
| 133 | % Sets: The list of deferred sets (a list of typed identifiers) | |
| 134 | % Set,Constructor,Cons,Domain: see is_constructor_axiom/6 below | |
| 135 | % RestList: A list of remaining predicates after removing the constructor axiom | |
| 136 | select_constructor_axiom(Properties,Sets,Set,Constructor,Cons,Domain,RestList) :- | |
| 137 | conjunction_to_list(Properties,PList), | |
| 138 | ? | select(Prop,PList,RestList), |
| 139 | ? | is_constructor_axiom(Prop,Sets,Set,Constructor,Cons,Domain). |
| 140 | ||
| 141 | create_constructor_definition(Constructor,Domain,ConsDef) :- | |
| 142 | create_texpr(equal(Constructor,Identity),pred,[],ConsDef), | |
| 143 | get_texpr_type(Constructor,Type), | |
| 144 | create_texpr(identity(Domain),Type,[],Identity). | |
| 145 | ||
| 146 | % SetDef: SetName = Domain | |
| 147 | % NonEmptyDef: SetName /= {}, as deferred sets (and Event-B carrier sets) are non-empty; | |
| 148 | % otherwise an empty Domain would be accepted, e.g., for c : {x|x:1..3 & x>5} >->> SetName | |
| 149 | create_recordset_definition(SetName,Domain,SetDef,NonEmptyDef) :- | |
| 150 | create_texpr(equal(Set,Domain),pred,[],SetDef), | |
| 151 | get_texpr_type(Domain,Type), | |
| 152 | create_texpr(identifier(SetName),Type,[],Set), | |
| 153 | create_texpr(empty_set,Type,[],Empty), | |
| 154 | create_texpr(not_equal(Set,Empty),pred,[],NonEmptyDef). | |
| 155 | ||
| 156 | % move_deferred_set(Set+,OldMachine+,NewMachine-) :- | |
| 157 | % Moves a deferred set to the concrete_constants section | |
| 158 | % Set: The (untyped) id of the set | |
| 159 | % OldMachine: The complete B machine where the set is a deferred set | |
| 160 | % NewMachine: The same B machine, but the deferred set is now a constant | |
| 161 | move_deferred_set(Set,OldMachine,NewMachine) :- | |
| 162 | select_section(deferred_sets,Sets,NewSets,OldMachine,Machine1), | |
| 163 | get_texpr_id(TSet,Set), | |
| 164 | selectchk(TSet,Sets,NewSets), | |
| 165 | select_section(concrete_constants,Constants,[TSet|Constants],Machine1,NewMachine). | |
| 166 | ||
| 167 | :- use_module(bsyntaxtree,[is_set_type/2]). | |
| 168 | ||
| 169 | % is_constructor_axiom(+TAxiom,+Sets,-GType,-TConstructor,-Cons,-TDom) | |
| 170 | % TAxiom: A typed predicate | |
| 171 | % Sets: A list of typed identifiers, the deferred sets | |
| 172 | % GType: The (untyped) ID of the set that represents the record | |
| 173 | % TConstructor: The constructor function (a bijection from a domain to the record type) | |
| 174 | % Cons: a term of the form constructor(Domain,DomainType,Kind) where Kind is either constructor or destructor | |
| 175 | % TDom: The domain of the constructor | |
| 176 | % The constructor can actually be a destructor, i.e. a bijection from the record to the domain. | |
| 177 | % The third argument in the Cons term indicates which case has been encountered | |
| 178 | is_constructor_axiom(TAxiom,Sets,GType,TConstructor,Cons,TDom) :- | |
| 179 | % The axiom has the form c : Dom >->> Set, where | |
| 180 | % c and Set are identifiers | |
| 181 | get_texpr_expr(TAxiom,member(TConstructor,TBijection)), | |
| 182 | get_texpr_type(TConstructor,SetType), | |
| 183 | is_set_type(SetType,couple(FromType,ToType)), | |
| 184 | get_texpr_id(TConstructor,_), % just make sure that TConstructor is an identifer | |
| 185 | FromType \= ToType, % otherwise bijection maps to itself | |
| 186 | (get_texpr_expr(TBijection,total_bijection(TDom,TSet)), | |
| 187 | Cons=constructor(TDom,FromType,constructor) | |
| 188 | ; get_texpr_expr(TBijection,total_bijection(TSet,TDom)), | |
| 189 | % we have the bijection the other way; does not really matter for a bijection anyway | |
| 190 | Cons = constructor(TSet,ToType,destructor)), | |
| 191 | get_texpr_type(TSet,set(global(GType))), | |
| 192 | get_texpr_id(TSet,GType), | |
| 193 | % check if the set is a deferred set, not an enumerated set | |
| 194 | get_texpr_id(SetTest,GType), | |
| 195 | memberchk(SetTest,Sets), | |
| 196 | % in Dom, there should be no reference to Set | |
| 197 | no_reference(TDom,GType), | |
| 198 | % nor in its type, e.g., for D = S*{TRUE} & c : D >->> S; replacing S by D would be circular | |
| 199 | Cons = constructor(_,DomElemType,_), | |
| 200 | \+ type_contains_global_set(DomElemType,GType). | |
| 201 | ||
| 202 | % check if a type contains global(GType); types are ground except for record field names | |
| 203 | type_contains_global_set(global(G),GType) :- !, G==GType. | |
| 204 | type_contains_global_set(Type,GType) :- compound(Type), | |
| 205 | functor(Type,_,N), between(1,N,I), arg(I,Type,Arg), % SICStus arg/3 requires the argument number | |
| 206 | type_contains_global_set(Arg,GType),!. | |
| 207 | ||
| 208 | ||
| 209 | no_reference(TExpr,Type) :- | |
| 210 | syntaxtraversion(TExpr,Expr,_,_,Subs,_), | |
| 211 | no_reference2(Expr,Type), | |
| 212 | no_reference_l(Subs,Type). | |
| 213 | no_reference2(identifier(Type),Type) :- !,fail. | |
| 214 | no_reference2(_,_). | |
| 215 | no_reference_l([],_). | |
| 216 | no_reference_l([Expr|Rest],Type) :- | |
| 217 | no_reference(Expr,Type), | |
| 218 | no_reference_l(Rest,Type). | |
| 219 | ||
| 220 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 221 | create_optional_field_access(SetName,Domain,Constructor,UpAc,Properties,RestList) :- | |
| 222 | get_texpr_id(Set,SetName), | |
| 223 | find_and_remove_field_declaration(Set,Domain,Constructor,Fields,Properties,P4), | |
| 224 | !, | |
| 225 | replace_update_definitions(Fields,Set,Constructor,Updates,P4,P5), | |
| 226 | maplist(create_field_definition(Domain),Fields,Accessors,FieldDefs), | |
| 227 | append(FieldDefs,P5,RestList), | |
| 228 | % A list of identifiers that are used at update and accessor functions: | |
| 229 | append(Updates,Accessors,UpAc). | |
| 230 | create_optional_field_access(_Set,_Domain,_Constructor,[],Properties,Properties). | |
| 231 | ||
| 232 | find_and_remove_field_declaration(Set,Domain,Constructor,Fields,Pin,Pout) :- | |
| 233 | % F1 >< ... >< Fn : Set -->> Domain | |
| 234 | bsyntax_pattern(member(FieldProduct,total_surjection(-Set,-Domain)), SurjMemb), | |
| 235 | selectchk(SurjMemb,Pin,P1), | |
| 236 | % the injection is only defined for closed records | |
| 237 | % (F1 >< ...) >< Fn : Set >-> Domain | |
| 238 | bsyntax_pattern(member(-FieldProduct,total_injection(-Set,-Domain)), InjMemb), | |
| 239 | selectchk(InjMemb,P1,P2), | |
| 240 | access_locations(FieldProduct,Domain,Fields), | |
| 241 | % for each field accessor fx, there should be a declaration | |
| 242 | % fx : Record --> Domain, remove them | |
| 243 | remove_field_declarations(Fields,Set,P2,P3), | |
| 244 | % for each field accessor fx, there should be a predicate | |
| 245 | % !(v1,...,...), remove it | |
| 246 | remove_field_quantifiers(Fields,Constructor,P3,Pout). | |
| 247 | ||
| 248 | % find the field accessor functions | |
| 249 | access_locations(TExpr,TDomain,Fields) :- | |
| 250 | access_locations1(TExpr,TDomain,[],Fields). | |
| 251 | access_locations1(TExpr,TDomain,Path,Fields) :- | |
| 252 | get_texpr_expr(TExpr,Expr), | |
| 253 | access_locations2(Expr,TExpr,TDomain,Path,Fields). | |
| 254 | access_locations2(identifier(_Id),TExpr,TDomain,Path,[fieldloc(TExpr,TDomain,RPath)]) :- reverse(Path,RPath). | |
| 255 | access_locations2(direct_product(F1,F2),_TExpr,TCart,Path,Fields) :- | |
| 256 | get_texpr_expr(TCart,cartesian_product(D1,D2)), | |
| 257 | access_locations1(F1,D1,[left |Path],Fields1), | |
| 258 | access_locations1(F2,D2,[right|Path],Fields2), | |
| 259 | append(Fields1,Fields2,Fields). | |
| 260 | ||
| 261 | create_field_definition(RecordDomain,fieldloc(AccessorId1,Domain,Path),AccessorId,Equal) :- | |
| 262 | add_texpr_infos(AccessorId1,[record_detection(accessor)],AccessorId), | |
| 263 | create_texpr(equal(AccessorId,AccessorDef),pred,[],Equal), | |
| 264 | % the accessor function maps a record to the domain | |
| 265 | get_texpr_type(RecordDomain,RType), | |
| 266 | is_set_type(RType,RecordTupleType), | |
| 267 | create_accessor_function(Domain,Path,RecordTupleType,AccessorDef). | |
| 268 | ||
| 269 | create_accessor_function(Domain,Path,RecordTupleType,AccessDefinition) :- | |
| 270 | % fieldAccess = {r,v | v = prj...(r)} | |
| 271 | create_texpr(identifier(r),RecordTupleType,[],RecordVar), | |
| 272 | get_texpr_type(Domain,FType), | |
| 273 | is_set_type(FType,FieldType), | |
| 274 | create_texpr(identifier(v),FieldType,[],ValueVar), | |
| 275 | create_texpr(equal(ValueVar,Projection),pred,[],Equals), | |
| 276 | create_projection(Path,RecordVar,Projection), | |
| 277 | %translate:print_bexpr(Projection),nl, | |
| 278 | create_symbolic_comprehension_set([RecordVar,ValueVar],Equals, | |
| 279 | [record_detection(accessor)],AccessDefinition). | |
| 280 | ||
| 281 | create_symbolic_comprehension_set(Ids,Pred,Info,Result) :- | |
| 282 | create_comprehension_set(Ids,Pred,Info,CompSet), | |
| 283 | mark_bexpr_as_symbolic(CompSet,Result). | |
| 284 | ||
| 285 | create_projection(Path,RecordVar,ProjectionResult) :- | |
| 286 | get_texpr_type(RecordVar,InnerType), | |
| 287 | create_projection_aux(Path,InnerType,RecordVar,ProjectionResult). | |
| 288 | ||
| 289 | create_projection_aux([],_InnerType,SubExpression,SubExpression). % return record as is | |
| 290 | create_projection_aux([Loc|Path],couple(A,B),SubExpression,ProjectionResult) :- | |
| 291 | ( Loc==left -> Expr=first_of_pair(SubExpression), InnerType=A | |
| 292 | ; Loc==right -> Expr=second_of_pair(SubExpression),InnerType=B), | |
| 293 | create_texpr(Expr,InnerType,[],NewSubExpression), | |
| 294 | create_projection_aux(Path,InnerType,NewSubExpression,ProjectionResult). | |
| 295 | ||
| 296 | ||
| 297 | % remove fieldx : RecSet --> Domx | |
| 298 | remove_field_declarations([],_RecSet,Predicates,Predicates). | |
| 299 | remove_field_declarations([fieldloc(AccessorId,Domain,_Path)|Frest],RecSet,Pin,Pout) :- | |
| 300 | remove_field_declaration(AccessorId,Domain,RecSet,Pin,Pinter), | |
| 301 | remove_field_declarations(Frest,RecSet,Pinter,Pout). | |
| 302 | remove_field_declaration(AccessorId,Domain,RecSet,Pin,Pout) :- | |
| 303 | bsyntax_pattern(member(-AccessorId,total_function(-RecSet,-Domain)),Pattern), | |
| 304 | selectchk(Pattern,Pin,Pout). | |
| 305 | ||
| 306 | % remove !(f1,...,fn). ( f1:T1 & ... & fn:Tn => fieldx(make_rec(f1|->f2|->f3)) = fx) | |
| 307 | remove_field_quantifiers(Fields,Constructor,Pin,Pout) :- | |
| 308 | remove_field_quantifiers2(Fields,0,Constructor,Pin,Pout). | |
| 309 | ||
| 310 | remove_field_quantifiers2(Fields,N,Constructor,Pin,Pout) :- | |
| 311 | ( nth0(N,Fields,fieldloc(Field,_Dom,_Path)) -> | |
| 312 | remove_field_quantifier(Field,N,Fields,Constructor,Pin,Pinter), | |
| 313 | N2 is N+1, | |
| 314 | remove_field_quantifiers2(Fields,N2,Constructor,Pinter,Pout) | |
| 315 | ; | |
| 316 | Pin=Pout). | |
| 317 | remove_field_quantifier(Field,N,Fields,Constructor,Pin,Pout) :- | |
| 318 | make_field_identifiers(Fields,FSkels), | |
| 319 | get_texpr_ids(FSkels,Ids), | |
| 320 | nth0(N,FSkels,Ref), | |
| 321 | bsyntax_pattern(forall(FSkels,_,equal(function(-Field,function(-Constructor,ConstArg)),-Ref)),ForAll), | |
| 322 | select(ForAll,Pin,Pout), | |
| 323 | are_mappings(ConstArg,Ids),!. | |
| 324 | are_mappings(TExpr,Ids) :- | |
| 325 | are_mappings2(TExpr,Ids,[]). | |
| 326 | are_mappings2(TExpr) --> | |
| 327 | {get_texpr_expr(TExpr,Expr)}, | |
| 328 | are_mappings3(Expr). | |
| 329 | are_mappings3(identifier(Id)) --> [Id]. | |
| 330 | are_mappings3(couple(A,B)) --> | |
| 331 | are_mappings2(A), | |
| 332 | are_mappings2(B). | |
| 333 | ||
| 334 | make_field_identifiers([],[]). | |
| 335 | make_field_identifiers([FieldAcc|FRest],[Id|Irest]) :- | |
| 336 | make_field_identifier(FieldAcc,Id), | |
| 337 | make_field_identifiers(FRest,Irest). | |
| 338 | make_field_identifier(fieldloc(FieldAcc,_,_),Id) :- | |
| 339 | get_texpr_type(FieldAcc,SType), | |
| 340 | is_set_type(SType,couple(_,FType)), | |
| 341 | create_texpr(identifier(_),FType,_,Id). | |
| 342 | ||
| 343 | replace_update_definitions(Fields,RecSet,Constructor,Updates,Pin,Pout) :- | |
| 344 | replace_update_definitions2(Fields,Fields,RecSet,Constructor,Updates,Pin,Pout). | |
| 345 | replace_update_definitions2([],_Fields,_RecSet,_Constructor,[],Predicates,Predicates). | |
| 346 | replace_update_definitions2([fieldloc(AccessorId,FDomain,Path)|Frest],Fields,RecSet, | |
| 347 | Constructor,Updates,Pin,Pout) :- | |
| 348 | % update functions are optional: if there is no (recognisable) update function for a field, we keep | |
| 349 | % the predicates as they are; the update function (if any) is then treated like any other constant | |
| 350 | (replace_update_definition(AccessorId,FDomain,Path,Fields,RecSet,Constructor,UpdateId,Pin,Pinter) | |
| 351 | -> Updates = [UpdateId|URest] | |
| 352 | ; Pinter = Pin, Updates = URest), | |
| 353 | replace_update_definitions2(Frest,Fields,RecSet,Constructor,URest,Pinter,Pout). | |
| 354 | ||
| 355 | % Identify two predicates of the form | |
| 356 | % updatex : Record ** Domain --> Record | |
| 357 | % !(Rec,New).(Rec:Record & New:Domain => updatex(Rec,New) = Constructor(f1(Rec)|->...|->New|->...|->fn(Rec))) | |
| 358 | % where New is at the position of the field x and fi are the other field accessors | |
| 359 | % and replace them by | |
| 360 | % updatex = {i,o | #(n,f1,...,fn).(i=(f1,...,fn)|->n & o=(f1,...,n,..,fn))} | |
| 361 | replace_update_definition(FieldAccessorId,FieldDomain,Path,Fields, | |
| 362 | RecSet,Constructor,Update,Pin,[Equal|Pout]) :- | |
| 363 | % Updatex : Record ** Domain --> Record | |
| 364 | bsyntax_pattern(member(Update1,total_function(cartesian_product(-RecSet,-FieldDomain),-RecSet)),Memb), | |
| 365 | select(Memb,Pin,P1), | |
| 366 | get_texpr_id(Update1,_), | |
| 367 | % !(Rec,New).(LHS => Updatex(RecU,NewU) = Constructor(Args)) | |
| 368 | % Rec and RecU (New and NewU) have different position infos: we cannot use the same pattern variable | |
| 369 | bsyntax_pattern(forall([Rec,New],LHS,equal(function(-Update1,couple(RecU,NewU)), | |
| 370 | function(-Constructor,Args))), ForAll), | |
| 371 | select(ForAll,P1,Pout), | |
| 372 | get_texpr_id(Rec,RecId), get_texpr_id(RecU,RecId), | |
| 373 | get_texpr_id(New,NewId), get_texpr_id(NewU,NewId), RecId \== NewId, | |
| 374 | % the axiom must define Updatex for all records and values, not just some: | |
| 375 | is_typing_predicate(LHS,[RecId-RecSet,NewId-FieldDomain]), | |
| 376 | % the axiom must really update the field of FieldAccessorId and keep the other fields: | |
| 377 | is_update_constructor_argument(Fields,FieldAccessorId,RecId,NewId,Args), | |
| 378 | !, | |
| 379 | add_texpr_infos(Update1,[record_detection(update)],Update), | |
| 380 | % Updatex = {i,o | #(i,r,f1,...,fn).(i=r|->n & ) } | |
| 381 | create_texpr(equal(Update,Function),pred,[],Equal), | |
| 382 | create_update_function(FieldAccessorId,FieldDomain,Path,Fields,Function). | |
| 383 | ||
| 384 | % check that LHS only contains typing predicates Id:Set for Id-Set in Typing | |
| 385 | is_typing_predicate(LHS,Typing) :- | |
| 386 | conjunction_to_list(LHS,Conjuncts), | |
| 387 | maplist(is_typing_conjunct(Typing),Conjuncts). | |
| 388 | is_typing_conjunct(_,Conjunct) :- is_truth(Conjunct),!. | |
| 389 | is_typing_conjunct(Typing,Conjunct) :- | |
| 390 | get_texpr_expr(Conjunct,member(TId,TSet)), | |
| 391 | get_texpr_id(TId,Id), | |
| 392 | memberchk(Id-Set,Typing), | |
| 393 | same_texpr(TSet,Set). | |
| 394 | ||
| 395 | % check that Args is the constructor argument for updating the field FieldAccessorId of record RecId | |
| 396 | % with value NewId: at the position of FieldAccessorId we must have NewId, at the position of every | |
| 397 | % other field f we must have f(RecId) | |
| 398 | is_update_constructor_argument(Fields,FieldAccessorId,RecId,NewId,Args) :- | |
| 399 | get_texpr_id(FieldAccessorId,UpdatedField), | |
| 400 | \+ ( member(fieldloc(Accessor,_,Path),Fields), | |
| 401 | \+ is_update_constructor_leaf(Path,Args,Accessor,UpdatedField,RecId,NewId) ). | |
| 402 | is_update_constructor_leaf(Path,Args,Accessor,UpdatedField,RecId,NewId) :- | |
| 403 | arg_at_path(Path,Args,Leaf), | |
| 404 | get_texpr_id(Accessor,Field), | |
| 405 | (Field == UpdatedField | |
| 406 | -> get_texpr_id(Leaf,NewId) | |
| 407 | ; get_texpr_expr(Leaf,function(TAcc,TRec)), | |
| 408 | get_texpr_id(TAcc,Field), get_texpr_id(TRec,RecId) | |
| 409 | ). | |
| 410 | arg_at_path([],Leaf,Leaf). | |
| 411 | arg_at_path([Loc|Path],TExpr,Leaf) :- | |
| 412 | get_texpr_expr(TExpr,couple(A,B)), | |
| 413 | (Loc==left -> arg_at_path(Path,A,Leaf) ; arg_at_path(Path,B,Leaf)). | |
| 414 | ||
| 415 | create_update_function(FieldAccessorId,FieldDomain,Path,Fields,Function) :- | |
| 416 | % {i,o | #n,f1,...,fn. ( i=( (f1,...,fn) |-> n ) ) & o=(f1,...,n,...,fn) } | |
| 417 | get_texpr_type(FieldAccessorId,SType), | |
| 418 | is_set_type(SType,couple(RecType,FieldType)), | |
| 419 | InType = couple(RecType,FieldType), | |
| 420 | create_texpr(identifier(update__in),InType,[],In), | |
| 421 | create_texpr(identifier(update__out),RecType,[],Out), | |
| 422 | create_symbolic_comprehension_set([In,Out],Pred,[record_detection(update)],CompSet), | |
| 423 | ||
| 424 | % #(n,f1,...,fn).(...&...) | |
| 425 | %create_exists([NewValue|FieldVars],ExistsPred,Pred), % moved below; see comment | |
| 426 | create_var_for_field(FieldAccessorId,FieldDomain,'f$n__',NewValue), | |
| 427 | maplist(create_field_var,Fields,FieldVars), | |
| 428 | create_texpr(conjunct(InEqual,OutEqual),pred,[],ExistsPred), | |
| 429 | ||
| 430 | % i = ( (f1,...,fn) |-> n ) | |
| 431 | create_texpr(equal(In,InPair),pred,[],InEqual), | |
| 432 | create_texpr(couple(OldFields,NewValue),InType,[],InPair), | |
| 433 | create_record_tuple(Fields,FieldVars,OldFields), | |
| 434 | ||
| 435 | % o = (f1,...,n,...,fn) | |
| 436 | create_texpr(equal(Out,NewFields),pred,[],OutEqual), | |
| 437 | update_record_tuple(Path,NewValue,OldFields,NewFields), | |
| 438 | ||
| 439 | % the creation of the exists has to be deferred until here as used identifiers are computed: | |
| 440 | create_exists([NewValue|FieldVars],ExistsPred,Pred), | |
| 441 | %translate:print_bexpr(CompSet),nl, | |
| 442 | ||
| 443 | % we need to call the clean-up functions because exists(...) needs more | |
| 444 | % information about which identifier are used and that information | |
| 445 | % is added by clean_up/3. | |
| 446 | clean_up(CompSet,[],Function). | |
| 447 | ||
| 448 | % for a fields, create an identifier with the corresponding type | |
| 449 | create_field_var(fieldloc(AccessorId,Domain,_Path),TId) :- | |
| 450 | create_var_for_field(AccessorId,Domain,'f$__',TId). | |
| 451 | create_var_for_field(AccessorId,Domain,Prefix,TId) :- | |
| 452 | get_texpr_id(AccessorId,FId), | |
| 453 | get_texpr_type(Domain,DType), | |
| 454 | is_set_type(DType,Type), | |
| 455 | atom_concat(Prefix,FId,VId), | |
| 456 | create_texpr(identifier(VId),Type,[],TId). | |
| 457 | ||
| 458 | % for a list of fields and identifiers f1,...,fn, create a tuple (f1,...,fn) | |
| 459 | create_record_tuple([],[],_). | |
| 460 | create_record_tuple([fieldloc(_,_,Path)|Frest],[FieldVar|Vrest],Tuple) :- | |
| 461 | create_record_tuple2(Path,FieldVar,Tuple), | |
| 462 | create_record_tuple(Frest,Vrest,Tuple). | |
| 463 | create_record_tuple2([],Expr,Expr). | |
| 464 | create_record_tuple2([Loc|Path],Expr,Tuple) :- | |
| 465 | ( Loc==left -> create_texpr(couple(Sub,_),couple(SubType,_),[],Tuple) | |
| 466 | ; Loc==right -> create_texpr(couple(_,Sub),couple(_,SubType),[],Tuple)), | |
| 467 | get_texpr_type(Sub,SubType), | |
| 468 | create_record_tuple2(Path,Expr,Sub). | |
| 469 | ||
| 470 | % in a tuple representing a record, replace one field | |
| 471 | update_record_tuple([],NewValue,_OldFields,NewValue). | |
| 472 | update_record_tuple([Loc|Path],NewValue,OldFields,NewFields) :- | |
| 473 | get_texpr_expr(OldFields,couple(A,B)), | |
| 474 | get_texpr_type(OldFields,Type), | |
| 475 | create_texpr(couple(X,Y),Type,[],NewFields), | |
| 476 | ( Loc==left -> X=NewSub,A=OldSub,Y=B | |
| 477 | ; Loc==right -> Y=NewSub,B=OldSub,X=A), | |
| 478 | update_record_tuple(Path,NewValue,OldSub,NewSub). | |
| 479 | ||
| 480 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 481 | % replace_type_in_machine(+T1,+T2,+Domain,+OldMachine,-NewMachine): | |
| 482 | % T1: The name of the global set that refers to the old type | |
| 483 | % T2: The new type that should replace global(T1) | |
| 484 | % Domain: A B expression that describes the domain of the record, used to | |
| 485 | % constrain introduced identifiers | |
| 486 | % OldMachine: The B machine before replacement | |
| 487 | % NewMachine: The resulting B machine | |
| 488 | replace_type_in_machine(T1,T2,Domain,OldMachine,NewMachine) :- | |
| 489 | extract_constants_that_are_types(OldMachine,RTT), | |
| 490 | (is_just_type(Domain,RTT) -> Domain2 = simple ; Domain2 = Domain), | |
| 491 | % add predicates to constrain variables or constants, if needed | |
| 492 | foldl(add_type_constraint_to_section(T1,Domain2), | |
| 493 | [decl([abstract_constants,concrete_constants],properties), | |
| 494 | decl([abstract_variables,concrete_variables],invariant)], | |
| 495 | OldMachine,Machine1), | |
| 496 | % replace the type in the expressions of the machine (identifier declarations | |
| 497 | % can be treated as expressions, too) | |
| 498 | foldl(replace_type_in_machine2(T1,T2,Domain2), | |
| 499 | [abstract_constants,concrete_constants, | |
| 500 | abstract_variables,concrete_variables, | |
| 501 | promoted, unpromoted, | |
| 502 | constraints, properties, invariant, assertions, | |
| 503 | initialisation, operation_bodies], | |
| 504 | Machine1,NewMachine). | |
| 505 | replace_type_in_machine2(T1,T2,Domain,Sec,In,Out) :- | |
| 506 | select_section_texprs(Sec,Old,New,In,Out), | |
| 507 | replace_type_in_exprs(Old,T1,T2,Domain,New). | |
| 508 | replace_type_in_expr(TExpr,T1,T2,Domain,New) :- | |
| 509 | % if identifiers (e.g. i) are declared in an expression (like in forall), we may have | |
| 510 | % to add an predicate (e.g. i:1..4) | |
| 511 | ( Domain=simple -> % the domain is a whole type, no additional constraint needed | |
| 512 | TExpr2 = TExpr | |
| 513 | ; has_declared_identifier(TExpr,Ids),contains_identifier_with_type(Ids,T1) -> | |
| 514 | % identifiers are introduced and one of them contains the record type | |
| 515 | add_type_declarations(TExpr,global(T1),Domain,Ids,TExpr2) | |
| 516 | ; % no identifiers introduced that use the record type | |
| 517 | TExpr = TExpr2 | |
| 518 | ), | |
| 519 | create_texpr(Expr,EType,Info,TExpr2), % de-assemble old expression | |
| 520 | replace_type(EType,T1,T2,NType), % replace type global(T1) by T2 in Type | |
| 521 | create_texpr(NExpr2,NType,Info,New), % assemble new expression with new type | |
| 522 | syntaxtransformation(Expr,Subs,_Names,NSubs,NExpr1), % continue with sub-expressions | |
| 523 | replace_type_in_exprs(Subs,T1,T2,Domain,NSubs), | |
| 524 | replace_hidden_type_paras(NExpr1,T1,T2,NExpr2). | |
| 525 | replace_type_in_exprs([],_T1,_T2,_Domain,[]). | |
| 526 | replace_type_in_exprs([Sub|Irest],T1,T2,Domain,[NSub|Orest]) :- | |
| 527 | replace_type_in_expr(Sub,T1,T2,Domain,NSub), | |
| 528 | replace_type_in_exprs(Irest,T1,T2,Domain,Orest). | |
| 529 | ||
| 530 | ||
| 531 | replace_hidden_type_paras(freetype_constructor(A,Case,Expr),T1,T2,freetype_constructor(RA,Case,Expr)) :- !, | |
| 532 | replace_freetype_paras(A,T1,T2,RA). | |
| 533 | replace_hidden_type_paras(freetype_destructor(A,Case,Expr),T1,T2,freetype_destructor(RA,Case,Expr)) :- !, | |
| 534 | replace_freetype_paras(A,T1,T2,RA). | |
| 535 | replace_hidden_type_paras(E,_,_,E). | |
| 536 | ||
| 537 | ||
| 538 | contains_identifier_with_type(Ids,T1) :- | |
| 539 | get_texpr_type(TId,Type), | |
| 540 | member(TId,Ids), | |
| 541 | contains_type(global(T1),Type). | |
| 542 | ||
| 543 | add_type_constraint_to_section(_ToReplace,simple,_Decl,In,Out) :- | |
| 544 | !,In=Out. | |
| 545 | add_type_constraint_to_section(ToReplace,Domain,decl(IdSecs,PredSec),In,Out) :- | |
| 546 | maplist(aux_get_section(In),IdSecs,IdsL),append(IdsL,Ids1), | |
| 547 | % do not add constraints for accessor and update functions for records: | |
| 548 | exclude(is_record_detection_expr,Ids1,Ids), | |
| 549 | ( contains_identifier_with_type(Ids,ToReplace) -> | |
| 550 | create_type_predicate(global(ToReplace),Domain,Ids,P), | |
| 551 | ( is_truth(P) -> In=Out % Trivial, nothing to do | |
| 552 | ; % Add the type constraint to the relevant predicate section | |
| 553 | select_section(PredSec,OldPred,NewPred,In,Out), | |
| 554 | conjunct_predicates([P,OldPred],NewPred) | |
| 555 | ) | |
| 556 | ; % Not applicable, nothing to do | |
| 557 | In=Out). | |
| 558 | aux_get_section(Machine,Sec,Content) :- % just to rearrange the parameters | |
| 559 | get_section(Sec,Machine,Content). | |
| 560 | ||
| 561 | is_record_detection_expr(Expr) :- | |
| 562 | get_texpr_info(Expr,Info),memberchk(record_detection(_),Info). | |
| 563 | ||
| 564 | replace_type(global(Type),Type,NewType,Result) :- !, NewType=Result. | |
| 565 | replace_type(couple(A,B),T1,T2,couple(RA,RB)) :- | |
| 566 | !, replace_type(A,T1,T2,RA), replace_type(B,T1,T2,RB). | |
| 567 | replace_type(set(A),T1,T2,set(RA)) :- | |
| 568 | !, replace_type(A,T1,T2,RA). | |
| 569 | replace_type(seq(A),T1,T2,seq(RA)) :- | |
| 570 | !, replace_type(A,T1,T2,RA). | |
| 571 | replace_type(freetype(A),T1,T2,freetype(RA)) :- | |
| 572 | !, replace_freetype_paras(A,T1,T2,RA). | |
| 573 | replace_type(T,_,_,T). % just skip types like integer, string, etc. | |
| 574 | ||
| 575 | % replace type inside freetype type parameters (relevant for Event-B polymorphic theories) | |
| 576 | replace_freetype_paras(A,T1,T2,RA) :- | |
| 577 | get_freetype_type_parameters(A,Func,TypeParas), | |
| 578 | maplist(mreplace(T1,T2),TypeParas,NewTypeParas), | |
| 579 | get_freetype_type_parameters(RA,Func,NewTypeParas). | |
| 580 | ||
| 581 | mreplace(T1,T2,Type,ReplacedType) :- replace_type(Type,T1,T2,ReplacedType). | |
| 582 | ||
| 583 | contains_type(Type,couple(A,_)) :- contains_type(Type,A). | |
| 584 | contains_type(Type,couple(_,B)) :- contains_type(Type,B). | |
| 585 | contains_type(Type,set(A)) :- contains_type(Type,A). | |
| 586 | contains_type(Type,seq(A)) :- contains_type(Type,A). | |
| 587 | contains_type(Type,freetype(A)) :- get_freetype_type_parameters(A,_,TypeParas), | |
| 588 | member(TP,TypeParas), contains_type(Type,TP). | |
| 589 | contains_type(Type,Type). | |
| 590 | ||
| 591 | add_type_declarations(TExpr,ToReplace,Set,Ids,Result) :- | |
| 592 | create_type_predicate(ToReplace,Set,Ids,P), | |
| 593 | ( is_truth(P) -> Result = TExpr % Nothing to do | |
| 594 | ; add_declaration_for_identifier(TExpr,P,Result)). | |
| 595 | ||
| 596 | create_type_predicate(ToReplace,Set,Ids,P) :- | |
| 597 | include(expr_contains_type(ToReplace),Ids,RelevantIds), | |
| 598 | maplist(create_type_membership(ToReplace,Set),RelevantIds,Preds), | |
| 599 | conjunct_predicates(Preds,P1), | |
| 600 | clean_up(P1,[],P). | |
| 601 | ||
| 602 | expr_contains_type(ToReplace,TExpr) :- | |
| 603 | get_texpr_type(TExpr,Type), | |
| 604 | contains_type(ToReplace,Type). | |
| 605 | ||
| 606 | create_type_membership(ToReplace,Set,TId,Membership) :- | |
| 607 | get_texpr_type(TId,Type), | |
| 608 | create_set_for_type(Type,ToReplace,Set,GenSet), | |
| 609 | create_texpr(member(TId,GenSet),pred,[],Membership). | |
| 610 | ||
| 611 | create_set_for_type(T,ToReplace,Set,R) :- | |
| 612 | T=ToReplace,!,R=Set. | |
| 613 | create_set_for_type(couple(A,B),ToReplace,Set,R) :- !, | |
| 614 | create_set_for_type(A,ToReplace,Set,SA), | |
| 615 | create_set_for_type(B,ToReplace,Set,SB), | |
| 616 | ( is_typeset(SA),is_typeset(SB) -> create_typeset(couple(A,B),R) | |
| 617 | ; create_texpr(cartesian_product(SA,SB),set(couple(A,B)),[],R)). | |
| 618 | create_set_for_type(set(A),ToReplace,Set,R) :- !, | |
| 619 | create_set_for_type(A,ToReplace,Set,SA), | |
| 620 | ( is_typeset(SA) -> create_typeset(set(A),R) | |
| 621 | ; create_texpr(pow_subset(SA),set(set(A)),[],R)). | |
| 622 | create_set_for_type(seq(A),ToReplace,Set,R) :- !, | |
| 623 | % for our purpose, we can ignore other constraints on sequences | |
| 624 | create_set_for_type(set(couple(integer,A)),ToReplace,Set,R). | |
| 625 | create_set_for_type(Type,_ToReplace,_Set,R) :- create_typeset(Type,R). | |
| 626 | is_typeset(Expr) :- get_texpr_expr(Expr,typeset). | |
| 627 | ||
| 628 | create_typeset(Type,Set) :- | |
| 629 | create_texpr(typeset,set(Type),[],Set). | |
| 630 | ||
| 631 | % extract_constants_that_are_types(+Machine,-RTT): | |
| 632 | % gives a list of identifiers of those constants that are defined to be the whole type. | |
| 633 | % This works only for constants that are defined with an equality, e.g. | |
| 634 | % c = BOOL**BOOL | |
| 635 | extract_constants_that_are_types(Machine,RTT) :- | |
| 636 | get_all_equalities_from_machine(Machine,Equalities), | |
| 637 | extract_type_references(Equalities,[],RTT). | |
| 638 | % get_all_equalities_from_properties(+Machine,-Equalities): | |
| 639 | % extract all equalities from the properties. | |
| 640 | % Equalities is a list of terms equal(Id,Expr) where Id denotes a | |
| 641 | % constant and Expr the expr on the other side of the equation. | |
| 642 | get_all_equalities_from_machine(Machine,Equalities) :- | |
| 643 | get_all_constants_from_machine(Machine,Constants), | |
| 644 | get_section(properties,Machine,Properties), | |
| 645 | conjunction_to_list(Properties,PList), | |
| 646 | convlist(is_equality_left(Constants), PList,LEqualities), | |
| 647 | convlist(is_equality_right(Constants),PList,REqualities), | |
| 648 | append(LEqualities,REqualities,Equalities). | |
| 649 | get_all_constants_from_machine(Machine,Constants) :- | |
| 650 | get_section(abstract_constants,Machine,AC), | |
| 651 | get_section(concrete_constants,Machine,CC), | |
| 652 | append(AC,CC,Constants). | |
| 653 | % extract_type_references(+Equalities,+RTT,-Result): | |
| 654 | % Equalities: A list of term of the form equal(Id,Expr), see get_all_equalities_from_properties/2 | |
| 655 | % RTT: An ordered list of already known constants that refer to types | |
| 656 | % Result: A ordered list of constants (their id, no types) that refer to types | |
| 657 | % The predicate works recursively because a definition like a = B**INTEGER can denote a | |
| 658 | % type if B also denotes a type which must be detected first. | |
| 659 | extract_type_references(Equalities,RTT,Result) :- | |
| 660 | convlist(equality_is_typedef(RTT),Equalities,ReferencesToTypes), | |
| 661 | list_to_ord_set(ReferencesToTypes,Sorted), | |
| 662 | ord_subtract(Sorted,RTT,Unique), | |
| 663 | ( Unique = [] -> Result=RTT % no new definitions found | |
| 664 | ; % new definitions found, continue recursively | |
| 665 | ord_union(Unique,RTT,NewRTT), | |
| 666 | extract_type_references(Equalities,NewRTT,Result) | |
| 667 | ). | |
| 668 | % Pred is of form Id=Expr where Id is a constant | |
| 669 | is_equality_left(Constants,Pred,equal(Id,Expr)) :- | |
| 670 | get_texpr_expr(Pred,equal(TId,Expr)), is_equality_aux(TId,Constants,Id). | |
| 671 | % Pred is of form Expr=Id where Id is a constant | |
| 672 | is_equality_right(Constants,Pred,equal(Id,Expr)) :- | |
| 673 | get_texpr_expr(Pred,equal(Expr,TId)), is_equality_aux(TId,Constants,Id). | |
| 674 | is_equality_aux(TId,Constants,Id) :- | |
| 675 | get_texpr_id(TId,Id),get_texpr_id(Constant,Id),memberchk(Constant,Constants). | |
| 676 | equality_is_typedef(RTT,equal(Id,Expr),Id) :- | |
| 677 | is_just_type(Expr,RTT). |