normalize_ids_in_b_ast(Env, b(truth,pred,Info), NAst, NewEnv) :-
!,
NAst = b(truth,pred,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(falsity,pred,Info), NAst, NewEnv) :-
!,
NAst = b(falsity,pred,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, pred_true, NAst, NewEnv) :-
!,
NAst = b(value(pred_true),boolean,[]),
NewEnv = Env.
normalize_ids_in_b_ast(Env, pred_false, NAst, NewEnv) :-
!,
NAst = b(value(pred_false),boolean,[]),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(integer(Value),integer,Info), NAst, NewEnv) :-
!,
NAst = b(integer(Value),integer,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(identifier(op(Name)),_,_), NOperationName, NewEnv) :-
!,
Env = [_,_,_,NOperationNames,_],
member((b(identifier(op(Name)),_,_),NOperationName), NOperationNames),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NId, NewEnv) :-
Node =.. [function|Args],
!,
map_normalize_ids_in_b_ast(Env, Args, NArgs, NewEnv),
NNode =.. [function|NArgs],
NId = b(NNode,Type,Info).
normalize_ids_in_b_ast(Env, b(operation(OperationName,ReturnIds,Params,Body),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, OperationName, NOperationName, _),
map_normalize_ids_in_b_ast(Env, ReturnIds, NReturnIds, _),
map_normalize_ids_in_b_ast(Env, Params, NParams, _),
normalize_ids_in_b_ast(Env, Body, NBody, NewEnv),
NNode = operation(NOperationName,NReturnIds,NParams,NBody),
NAst = b(NNode,Type,Info).
normalize_ids_in_b_ast(Env, b(Node,subst,Info), NAst, NewEnv) :-
Node =.. [Functor,Assignments],
( Functor = select
; Functor = parallel
; Functor = sequence
),
!,
map_normalize_ids_in_b_ast(Env, Assignments, NAssignments, NewEnv),
NNode =.. [Functor,NAssignments],
NAst = b(NNode,subst,Info).
normalize_ids_in_b_ast(Env, b(struct(Record),Type,Info), b(struct(NRecord),NType,NInfo), NewEnv) :-
normalize_ids_in_b_ast(Env, Record, NRecord, NewEnv),
NewEnv = [_,_,NormalizedIds,_,_],
normalize_id_type(NormalizedIds, Type, NType),
adapt_synthesis_ast_info(NewEnv, Info, NInfo).
normalize_ids_in_b_ast(Env, b(rec(Fields),Type,Info), b(rec(NFields),NType,NInfo), NewEnv) :-
normalize_record_fields(Env, Fields, NFields, NewEnv),
NewEnv = [_,_,NormalizedIds,_,_],
normalize_id_type(NormalizedIds, Type, NType),
adapt_synthesis_ast_info(NewEnv, Info, NInfo).
normalize_ids_in_b_ast(Env, b(record_field(Id,Field),Type,Info), b(record_field(NId,NField),NType,NInfo), NewEnv) :-
normalize_ids_in_b_ast(Env, Id, NId, Env1),
normalize_ids_in_b_ast(Env1, Field, NField, NewEnv),
NewEnv = [_,_,NormalizedIds,_,_],
normalize_id_type(NormalizedIds, Type, NType),
adapt_synthesis_ast_info(NewEnv, Info, NInfo).
normalize_ids_in_b_ast(Env, b(assign(Ids,Assignments),subst,Info), b(assign(NIds,NAssignments),subst,Info), NewEnv) :-
map_normalize_ids_in_b_ast(Env, Ids, NIds, _),
map_normalize_ids_in_b_ast(Env, Assignments, NAssignments, NewEnv).
normalize_ids_in_b_ast(Env, b(assign_single_id(Id,Assignment),subst,Info), b(assign_single_id(NId,NAssignment),subst,Info), NewEnv) :-
normalize_ids_in_b_ast(Env, Id, NId, _),
normalize_ids_in_b_ast(Env, Assignment, NAssignment, NewEnv).
normalize_ids_in_b_ast(Env, b(value(global_set(SetName)),_,_), NId, NewEnv) :-
!,
Env = [_,NormalizedSets,_,_,_],
member((b(identifier(SetName),_,_),NId), NormalizedSets),
NewEnv = Env.
normalize_ids_in_b_ast(Env, In, NewId, NewEnv) :-
( In = string(String), Info = []
; In = b(string(String),string,Info)),
normalize_strings,
!,
NewId = b(string(NString),string,NInfo),
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
normalize_string(NormalizedStrings, String, NString, NewNormalizedStrings),
NewEnv = [NewNormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
adapt_synthesis_ast_info(NewEnv, Info, NInfo).
normalize_ids_in_b_ast(Env, b(string(String),string,Info), Out, NewEnv) :-
!,
Out = b(string(String),string,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, string(String), Out, NewEnv) :-
!,
Out = b(string(String),string,[]),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(EmptySet,set(Type),Info), b(empty_set,set(NType),NInfo), Env) :-
(EmptySet = set_extension([]); EmptySet = empty_set),
adapt_synthesis_ast_info(Env, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType).
normalize_ids_in_b_ast(Env, b(EmptySeq,Type,Info), b(empty_sequence,NType,NInfo), Env) :-
(EmptySeq = sequence_extension([]); EmptySeq = empty_sequence),
adapt_synthesis_ast_info(Env, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType).
normalize_ids_in_b_ast(Env, List, NAst, NewEnv) :-
List \= [],
is_list(List),
!,
map_normalize_ids_in_b_ast(Env, List, NewList, NewEnv),
NewList = [b(_,Type,_)|_],
NAst = b(set_extension(NewList),set(Type),[]).
normalize_ids_in_b_ast(Env, Ast, NewSetNode, NewEnv) :-
( Ast = b(value(avl_set(Avl)),set(Type),Info)
; Ast = avl_set(Avl),
Type = set(any),
Info = []
),
!,
avl_to_list(Avl, TList),
findall(L, member(L-_, TList), List),
map_normalize_ids_in_b_ast(Env, List, NewList, NewEnv),
NewSetNode = b(set_extension(NewList),set(NType),NInfo),
adapt_synthesis_ast_info(NewEnv, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType).
normalize_ids_in_b_ast(Env, b(identifier(Name),_,_), NId, NewEnv) :-
Env = [_,NormalizedSets,_,_,_],
member((b(identifier(Name),_,_),NId), NormalizedSets),
!,
NewEnv = Env.
normalize_ids_in_b_ast(Env, Name, NewName, NewEnv) :-
Env = [_,_,_,_,NRecordFieldNames],
member((Name,NName), NRecordFieldNames),
!,
NewName = NName,
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(identifier(Name),Type,Info), NId, NewEnv) :-
Env = [_,_,_,_,NRecordFieldNames],
member((Name,NName), NRecordFieldNames),
!,
normalize_id_type(NRecordFieldNames, Type, NType),
NId = b(identifier(NName),NType,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(identifier(Name),_,_), NId, NewEnv) :-
Env = [_,_,NormalizedIds,_,_],
member((b(identifier(Name),_,_),NId), NormalizedIds),
!,
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(identifier(Name),_,_), NAst, NewEnv) :-
Env = [_,NormalizedSets,_,_,_],
atom_concat(SetName, Nr, Name),
member((b(identifier(SetName),_,_),NId), NormalizedSets),
!,
NId = b(identifier(NSetName),NType,NInfo),
atom_concat(NSetName, e, NNSetName),
atom_concat(NNSetName, Nr, NName),
NAst = b(identifier(NName),NType,NInfo),
NewEnv = Env.
normalize_ids_in_b_ast(Env, (A1,A2), NAst, NewEnv) :-
!,
% value couple
normalize_ids_in_b_ast(Env, A1, NA1, Env1),
normalize_ids_in_b_ast(Env1, A2, NA2, NewEnv),
NA1 = b(_,T1,_),
NA2 = b(_,T2,_),
NAst = b(couple(NA1,NA2),couple(T1,T2),[]).
normalize_ids_in_b_ast(Env, b(couple(A1,A2),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, A1, NA1, Env1),
normalize_ids_in_b_ast(Env1, A2, NA2, NewEnv),
adapt_synthesis_ast_info(Env, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType),
NAst = b(couple(NA1,NA2),NType,NInfo).
normalize_ids_in_b_ast(Env, b(value(fd(Nr,SetName)),_,Info), b(Node,Type,NInfo), NewEnv) :-
normalize_ids_in_b_ast(Env, fd(Nr,SetName), TAst, NewEnv),
adapt_synthesis_ast_info(NewEnv, Info, NInfo),
TAst = b(Node,Type,_).
normalize_ids_in_b_ast(Env, fd(Nr,SetName), NAst, NewEnv) :-
Env = [_,NormalizedSets,_,_,_],
member((b(identifier(SetName),_,_),NId), NormalizedSets),
!,
NId = b(identifier(NSetName),set(NType),_),
atom_concat(NSetName, e, TempName),
number_codes(Nr, NC),
atom_codes(AN, NC),
atom_concat(TempName, AN, NName),
NAst = b(identifier(NName),NType,[]),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(identifier(Name),_,_), NAst, NewEnv) :-
Env = [_,NormalizedSets,NormalizedIds,_,_],
atom(Name),
atom_concat(NameP, Prime, Name),
( Prime == '__prime'
; Prime == '\''
),
( member((b(identifier(NameP),_,_),b(identifier(TName),NType,NInfo)), NormalizedSets)
;
member((b(identifier(NameP),_,_),b(identifier(TName),NType,NInfo)), NormalizedIds)
),
!,
atom_concat(TName, Prime, NName),
NAst = b(identifier(NName),NType,NInfo),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node =.. [Functor,Ids,Pred],
( Functor == exists
; Functor == comprehension_set
),
!,
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
append(EIds, NormalizedIds, NNormalizedIds),
normalize_ids_in_b_ast([NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames], Pred, NPred, Env1),
adapt_synthesis_ast_info(Env1, Info, NInfo),
NNode =.. [Functor,NIds,NPred],
NAst = b(NNode,Type,NInfo),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(forall(Ids,LHS,RHS),Type,Info), NAst, NewEnv) :-
!,
% TO DO: refactor creation of local ids
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
append(EIds, NormalizedIds, NNormalizedIds),
normalize_ids_in_b_ast([NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames], LHS, NLHS, Env1),
normalize_ids_in_b_ast(Env1, RHS, NRHS, Env2),
adapt_synthesis_ast_info(Env2, Info, NInfo),
NAst = b(forall(NIds,NLHS,NRHS),Type,NInfo),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node =.. [Functor,Ids,Pred,Int],
( Functor == general_sum
; Functor == general_product
),
!,
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
append(EIds, NormalizedIds, NNormalizedIds),
normalize_ids_in_b_ast([NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames], Pred, NPred, Env1),
normalize_ids_in_b_ast(Env1, Int, NInt, _),
NNode =.. [Functor,NIds,NPred,NInt],
NewEnv = Env,
NAst = b(NNode,Type,Info).
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node =.. [Functor,Ids,PredOrExpr],
( Functor == becomes_such
; Functor == becomes_element_of
),
!,
map_normalize_ids_in_b_ast(Env, Ids, NIds, Env1),
normalize_ids_in_b_ast(Env1, PredOrExpr, NPredOrExpr, NewEnv),
NNode =.. [Functor,NIds,NPredOrExpr],
NAst = b(NNode,Type,Info).
normalize_ids_in_b_ast(Env, b(iteration(Id,Int),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, Id, NId, NewEnv),
NAst = b(iteration(NId,Int),Type,Info).
normalize_ids_in_b_ast(Env, b(choice(Substs),Type,Info), NAst, NewEnv) :-
!,
map_normalize_ids_in_b_ast(Env, Substs, NSubsts, NewEnv),
NAst = b(choice(NSubsts),Type,Info).
normalize_ids_in_b_ast(Env, b(kodkod(Int,List),Type,Info), NAst, NewEnv) :-
!,
map_normalize_ids_in_b_ast(Env, List, NList, NewEnv),
NAst = b(kodkod(Int,NList),Type,Info).
normalize_ids_in_b_ast(Env, b(assertion_expression(Expr1,Msg,Expr2),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, Expr1, NExpr1, Env1),
normalize_ids_in_b_ast(Env1, Expr2, NExpr2, NewEnv),
NAst = b(assertion_expression(NExpr1,Msg,NExpr2),Type,Info).
normalize_ids_in_b_ast(Env, b(external_function_call(FuncName,Exprs),Type,Info), NAst, NewEnv) :-
!,
map_normalize_ids_in_b_ast(Env, Exprs, NExprs, NewEnv),
NAst = b(external_function_call(FuncName,NExprs),Type,Info).
normalize_ids_in_b_ast(Env, b(operation_call(Id,L1,L2),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, Id, NId, Env1),
map_normalize_ids_in_b_ast(Env1, L1, NL1, Env2),
map_normalize_ids_in_b_ast(Env2, L2, NL2, NewEnv),
NAst = b(operation_call(NId,NL1,NL2),Type,Info).
normalize_ids_in_b_ast(Env, b(while(Cond,Subst,Invariant,Variant),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, Cond, NCond, Env1),
normalize_ids_in_b_ast(Env1, Subst, NSubst, Env2),
normalize_ids_in_b_ast(Env2, Invariant, NInvariant, Env3),
normalize_ids_in_b_ast(Env3, Variant, NVariant, NewEnv),
NAst = b(while(NCond,NSubst,NInvariant,NVariant),Type,Info).
normalize_ids_in_b_ast(Env, b(partition(Id,List),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, Id, NId, Env1),
map_normalize_ids_in_b_ast(Env1, List, NList, NewEnv),
NAst = b(partition(NId,NList),Type,Info).
normalize_ids_in_b_ast(Env, b(if(IfElsifs),Type,Info), NAst, NewEnv) :-
!,
map_normalize_ids_in_b_ast(Env, IfElsifs, NIfElsifs, NewEnv),
NAst = b(if(NIfElsifs),Type,Info).
normalize_ids_in_b_ast(Env, b(var(Ids,Subst),Type,Info), NAst, NewEnv) :-
!,
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
append(EIds, NormalizedIds, NNormalizedIds),
Env1 = [NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames],
normalize_ids_in_b_ast(Env1, Subst, NSubst, _),
NAst = b(var(NIds,NSubst),Type,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(any(Ids,Pred,Subst),Type,Info), NAst, NewEnv) :-
!,
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
append(EIds, NormalizedIds, NNormalizedIds),
normalize_ids_in_b_ast([NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames], Pred, NPred, Env1),
normalize_ids_in_b_ast(Env1, Subst, NSubst, _),
NAst = b(any(NIds,NPred,NSubst),Type,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(lazy_lookup_expr(RawIdName),Type,Info), NAst, NewEnv) :-
atom(RawIdName),
!,
Env = [_,_,NormalizedIds,_,_],
member((b(identifier(RawIdName),_,_),b(identifier(NRawIdName),_,_)), NormalizedIds),
NAst = b(lazy_lookup_expr(NRawIdName),Type,Info),
NewEnv = Env.
normalize_ids_in_b_ast(Env, b(recursive_let(Id,Expr),Type,Info), NAst, NewEnv) :-
!,
normalize_ids_in_b_ast(Env, Id, NId, Env1),
normalize_ids_in_b_ast(Env1, Expr, NExpr, _),
NewEnv = Env,
NAst = b(recursive_let(NId,NExpr),Type,Info).
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node =.. [Functor,Ids,PredOrExprs,Sub],
( Functor == let_expression
; Functor == let_predicate
; Functor == let
),
!,
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
append(EIds, NormalizedIds, NNormalizedIds),
Env0 = [NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames],
( is_list(PredOrExprs)
->
map_normalize_ids_in_b_ast(Env0, PredOrExprs, NExprs, Env1)
; normalize_ids_in_b_ast(Env0, PredOrExprs, NExprs, Env1)
),
normalize_ids_in_b_ast(Env1, Sub, NSub, _),
NNode =.. [Functor,NIds,NExprs,NSub],
NAst = b(NNode,Type,Info),
NewEnv = Env. % normalized ids are local
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node =.. [Functor,Id,PredOrExprs,Sub],
( Functor == lazy_let_pred
; Functor == lazy_let_expr
; Functor == reorder_lazy_let_pred
; Functor == reorder_lazy_let_expr
),
!,
Env = [NormalizedStrings,NormalizedSets,NormalizedIds,NOperationNames,NRecordFieldNames],
Ids = [Id],
enumerate_ids(NormalizedSets, 'l', Ids, EIds),
findall(NId, member((_,NId), EIds), NIds),
NIds = [NId],
append(EIds, NormalizedIds, NNormalizedIds),
Env0 = [NormalizedStrings,NormalizedSets,NNormalizedIds,NOperationNames,NRecordFieldNames],
( is_list(PredOrExprs)
->
map_normalize_ids_in_b_ast(Env0, PredOrExprs, NExprs, Env1)
; normalize_ids_in_b_ast(Env0, PredOrExprs, NExprs, Env1)
),
normalize_ids_in_b_ast(Env1, Sub, NSub, _),
NNode =.. [Functor,NId,NExprs,NSub],
NAst = b(NNode,Type,Info),
NewEnv = Env. % normalized ids are local
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node = value(Arg),
is_list(Arg),
!,
map_normalize_ids_in_b_ast(Env, Arg, NArg, NewEnv),
adapt_synthesis_ast_info(NewEnv, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType),
NNode = set_extension(NArg),
NAst = b(NNode,NType,NInfo).
normalize_ids_in_b_ast(Env, b(value(Value),_Type,Info), b(Node,NType,NInfo), Env) :-
normalize_ids_in_b_ast(Env, Value, NAst, NewEnv),
NAst = b(Node,NType,_),
adapt_synthesis_ast_info(NewEnv, Info, NInfo).
normalize_ids_in_b_ast(Env, b(set_extension(List),Type,Info), NAst, NewEnv) :-
!,
map_normalize_ids_in_b_ast(Env, List, NList, NewEnv),
adapt_synthesis_ast_info(NewEnv, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType),
NAst = b(set_extension(NList),NType,NInfo).
normalize_ids_in_b_ast(Env, b(sequence_extension(List),Type,Info), NAst, NewEnv) :-
!,
map_normalize_ids_in_b_ast(Env, List, NList, NewEnv),
adapt_synthesis_ast_info(NewEnv, Info, NInfo),
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType),
NAst = b(sequence_extension(NList),NType,NInfo).
normalize_ids_in_b_ast(Env, b(Node,Type,Info), NAst, NewEnv) :-
Node =.. [Functor|Args],
b_component(Functor),
!,
map_normalize_ids_in_b_ast(Env, Args, NArgs, NewEnv),
adapt_synthesis_ast_info(NewEnv, Info, NInfo),
NNode =.. [Functor|NArgs],
Env = [_,NormalizedSets,_,_,_],
normalize_id_type(NormalizedSets, Type, NType),
NAst = b(NNode,NType,NInfo).
normalize_ids_in_b_ast(Env, [], NAst, NewEnv) :-
!,
NAst = b(empty_set,set(any),[]),
NewEnv = Env.
normalize_ids_in_b_ast(Env, Value, Ast, NewEnv) :-
value_to_ast(Value, Ast),
!,
Env = NewEnv.
normalize_ids_in_b_ast(Env, Ast, NAst, NewEnv) :-
whitelist(Ast),
!,
NAst = Ast,
NewEnv = Env.
normalize_ids_in_b_ast(Env, Ast, Ast, Env) :-
nl,
print(Ast),
nl,
add_warning(normalize_ids_in_b_ast, 'Skipped AST for normalization:', Ast).