123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License as published *)(* by the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)openMopsa_utilsopenBotopenCore.AllmoduletypeELT=sigtypetvalcompare:t->t->intvalprint:Print.printer->t->unitend(** Powerset with lower and upper approximations *)moduleMake(Elt:ELT)=structmoduleSet=SetExt.Make(Elt)moduleUSet=Powerset.Make(Elt)(* Lower approximation *)typel=Set.t(* Upper approximation *)typeu=USet.t(* Powerset with lower and upper approximation *)typet=(l*u)with_botletempty:t=Nb(Set.empty,USet.empty)letbottom:t=BOTlettop:t=Nb(Set.empty,USet.top)letadd_u(e:Elt.t)(su:t):t=bot_lift1(fun(l,u)->Set.addel,USet.addeu)suletadd_o(e:Elt.t)(su:t):t=bot_lift1(fun(l,u)->l,USet.addeu)suletmem_u(e:Elt.t)(su:t):bool=bot_apply(fun_(l,u)->Set.memel)falsesuletmem_o(e:Elt.t)(su:t):bool=bot_apply(fun_(l,u)->USet.memeu)falsesuletremove(e:Elt.t)(su:t):t=bot_lift1(fun(l,u)->Set.removeel,USet.removeeu)suletis_empty(su:t):bool=bot_apply(fun_(l,u)->Set.is_emptyl&&USet.is_emptyu)falsesuletis_bottom=functionBOT->true|_->falseletis_top(su:t)=bot_apply(fun_(l,u)->Set.is_emptyl&&USet.is_topu)falsesuletsubset(su1:t)(su2:t):bool=bot_apply2falsefalse(fun(l1,u1)(l2,u2)->Set.subsetl1l2&&USet.subsetu1u2)su1su2letequal(su1:t)(su2:t):bool=bot_equal(fun(l1,u1)(l2,u2)->Set.equall1l2&&USet.equalu1u2)su1su2letjoin(su1:t)(su2:t):t=bot_neutral2(fun(l1,u1)(l2,u2)->Set.interl1l2,USet.joinu1u2)su1su2letmeet(su1:t)(su2:t):t=bot_absorb2(fun(l1,u1)(l2,u2)->Nb(Set.unionl1l2,USet.meetu1u2))su1su2letunion=joinletinter=meetletwidenctx(su1:t)(su2:t):t=bot_neutral2(fun(l1,u1)(l2,u2)->Set.interl1l2,USet.widenctxu1u2)su1su2letfold_u(f:Elt.t->'a->'a)(s:t)(init:'a):'a=bot_to_exns|>(fun(_,u)->USet.foldfuinit)letfold_o(f:Elt.t->'a->'a)(s:t)(init:'a):'a=bot_to_exns|>(fun(l,_)->Set.foldflinit)(* let fold_uo f s init = fold_o f s (fold_u f s init) (\* FIXME: double iteration on underapproximated elements *\) *)letprintprinter(su:t)=matchsuwith|BOT->pp_stringprinter"⊥"|Nb(l,u)(* lower, upper *)->letle=Set.elementslinpprint~path:[Key"U"]printer(pbox(funprinterle->ifle=[]thenpp_stringprinter"∅"elsepp_listElt.printprinterle~lopen:"{"~lsep:","~lclose:"}")le);letud=USet.diffu(Ntl)inifUSet.is_emptyudthenpp_string~path:[Key"O"]printer"U"elsepprint~path:[Key"O"]printer(List([String"U";pboxUSet.printud],{sopen="";ssep="∪";sclose="";sbind=""}))end