123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120(****************************************************************************)(* *)(* 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/>. *)(* *)(****************************************************************************)(** Utility functions for ['a option] type. *)letabsorb(f:'a->'boption)(a:'aoption):'boption=matchawithNone->None|Somex->fxletabsorb2(f:'a->'b->'coption)(a:'aoption)(b:'boption):'coption=matcha,bwithNone,_|_,None->None|Somex,Somey->fxyletneutral2(f:'a->'a->'a)(a:'aoption)(b:'aoption):'aoption=matcha,bwithNone,_->b|_,None->a|Somex,Somey->Some(fxy)letmerge(f1:'b->'a)(f2:'c->'a)(f12:'b->'c->'a)(none:'a)(b1:'boption)(b2:'coption):'a=matchb1,b2with|None,None->none|Somex1,None->f1x1|None,Somex2->f2x2|Somex1,Somex2->f12x1x2letequal(f:'a->'b->bool)(a:'aoption)(b:'boption):bool=matcha,bwithNone,None->true|Somex,Somey->fxy|_->falseletincluded(f:'a->'b->bool)(a:'aoption)(b:'boption):bool=matcha,bwithNone,_->true|Somex,Somey->fxy|_->falseletapply(f:'b->'a)(none:'a)(b:'boption):'a=matchbwithNone->none|Somex->fxletapply2(f:'a->'b->'c)(none:'c)(a:'aoption)(b:'boption):'c=matcha,bwithNone,_|_,None->none|Somex,Somey->fxyletdefault(none:'a)(a:'aoption):'a=apply(funx->x)nonealetcompare(cmp:'a->'a->int)(a:'aoption)(b:'aoption):int=ifa==bthen0elsematcha,bwith|None,None->0|None,Some_->-1|Some_,None->1|Somex,Somey->cmpxyexceptionFound_Noneletraise_none()=raiseFound_Noneletcatch_none(dfl:'b)(f:'a->'b)(a:'a):'b=tryfawithFound_None->dflletnone_to_exn(a:'aoption):'a=matchawithNone->raiseFound_None|Somex->xletexn_to_none(f:'a->'b)(x:'a):'boption=trySome(fx)withFound_None->Noneletprint?(none="None")?(some="")ppfmtx=matchxwith|None->Format.fprintffmt"%s"none|Somea->Format.fprintffmt"%s%a"someppaletreturnx=Somexletbind(f:'a->'boption)(x:'aoption):'boption=matchxwith|None->None|Somex->fxletlift(f:'a->'b)(a:'aoption):'boption=matchawithNone->None|Somex->Some(fx)letlift2(f:'a->'b->'c)(a:'aoption)(b:'boption):'coption=matcha,bwithNone,_|_,None->None|Somex,Somey->Some(fxy)letlift_list(l:'aoptionlist):'alistoption=letrecaux=function|[]->Some[]|None::_->None|Somex::tl->auxtl|>lift(funtl->x::tl)inauxl