123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475openTypesopenTypecheckopenUtil(** Insert a key-value pair in a dictionary *)letinsert_dictargs=let(k,v,d)=(matchargswith|[k;v;d]->(unpack_stringk,v,unpack_dictd)|_->iraiseWrongPrimitiveArgs)inEvtDict((k,v)::(Dict.deletekd))(** Remove a key-value pair from a dictionary *)letdelete_dictargs=let(key,ed)=(matchargswith|[key;d]->(unpack_stringkey,unpack_dictd)|_->iraiseWrongPrimitiveArgs)inifnot(Dict.existskeyed)theniraise(DictError"key not found")elseEvtDict(Dict.deletekeyed)(** Check if a key-value pair is in a dictionary *)lethaskeyargs=let(key,ed)=(matchargswith|[key;d]->(unpack_stringkey,unpack_dictd)|_->iraiseWrongPrimitiveArgs)inEvtBool(Dict.existskeyed)(** Check if a dict contains a key *)letgetkeyargs=let(key,ed)=(matchargswith|[key;d]->(unpack_stringkey,unpack_dictd)|_->iraiseWrongPrimitiveArgs)inmatchDict.getkeyedwith|None->iraise(DictError"key not found")|Somevalue->value(** Get a list of the keys *)letgetkeysargs=leted=(matchargswith|[d]->unpack_dictd|_->iraiseWrongPrimitiveArgs)inEvtList(List.map(funx->EvtStringx)(Dict.getkeysed))(** Get a list of the values *)letgetvaluesargs=leted=(matchargswith|[d]->unpack_dictd|_->iraiseWrongPrimitiveArgs)inEvtList(Dict.getvaluesed)(** Check if a dict contains a key *)letfilterkeysargs=let(kll,ed)=(matchargswith|[kl;d]->(List.map(unpack_string)(unpack_listkl),unpack_dictd)|_->iraiseWrongPrimitiveArgs)inEvtDict(Dict.filterklled)(** Build a dictionary from two lists *)letdict_from_listsargs=let(kl,vl)=(matchargswith|[kl;vl]->(List.map(unpack_string)(unpack_listkl),unpack_listvl)|_->iraiseWrongPrimitiveArgs)inEvtDict(zipklvl)lettable=[("insert",Primitive(insert_dict,("insert",3,Pure)));("remove",Primitive(delete_dict,("remove",2,Pure)));("haskey",Primitive(haskey,("haskey",2,Pure)));("getkey",Primitive(getkey,("getkey",2,Pure)));("getkeys",Primitive(getkeys,("getkeys",1,Pure)));("getvalues",Primitive(getvalues,("getvalues",1,Pure)));("dictfromlists",Primitive(dict_from_lists,("dictfromlists",2,Pure)));("filterkeys",Primitive(filterkeys,("filterkeys",2,Pure)))]