kodkod_rewrite_pre(let_expression(Ids,Exprs,Body),_,Res) :- !, % expand LET expression
replace_ids_by_exprs(Body,Ids,Exprs,b(NewBody,_,I2)),
(kodkod_rewrite_pre(NewBody,I2,Res2) -> Res=Res2; Res = NewBody).
kodkod_rewrite_pre(let_predicate(Ids,Exprs,Body),_,Res) :- !, % expand LET predicate
replace_ids_by_exprs(Body,Ids,Exprs,b(NewBody,_,I2)),
(kodkod_rewrite_pre(NewBody,I2,Res2) -> Res=Res2; Res = NewBody).
kodkod_rewrite_pre(not_equal(A,B),_,negation(Equal)) :- !,
create_kk_tpred(equal(A,B),Equal).
kodkod_rewrite_pre(member(Elem,Powset),_,Result) :-
get_texpr_expr(Powset,PExpr),
get_texpr_info(Elem,ElemInfo),
kodkod_rewrite_membership(PExpr,Elem,ElemInfo,Result),
!.
kodkod_rewrite_pre(subset(Subset,Powset),_,forall([Elem],TPre,TCond)) :-
get_texpr_info(Subset,Info),
get_texpr_set_type(Subset,ElemType),
unique_identifier(TId),
element_analysis(Info,ElemInfo),
create_texpr(identifier(TId),ElemType,ElemInfo,Elem),
get_texpr_expr(Powset,PExpr),
kodkod_rewrite_membership(PExpr,Elem,ElemInfo,Cond),!,
create_texpr(member(Elem,Subset),pred,[],TPre),
create_texpr(Cond,pred,[],TCond).
kodkod_rewrite_pre(size(Seq),_,card(Seq)) :- !.