123456789101112131415161718192021222324252627282930313233343536373839404142434445openTypesletterrt=raise(TypeError("expected a value of type: "^t))letexpectte=ift=ethen()elseterrtlettypecheck(x:evt)(t:string)=matchxwith|EvtInt_->expectt"int"|EvtFloat_->expectt"float"|EvtComplex_->expectt"complex"|EvtBool_->expectt"bool"|EvtString_->expectt"string"|EvtUnit->expectt"unit"|EvtList_->expectt"list"|EvtDict_->expectt"dict"|Closure_->expectt"fun"|RecClosure_->expectt"fun"|PrimitiveAbstraction_->expectt"fun"(** Unpacking functions: extract a value or throw an err *)letunpack_intx=(matchxwithEvtInti->i|_->terr"int")letunpack_floatx=(matchxwithEvtFloati->i|_->terr"float")letunpack_complexx=(matchxwithEvtComplexi->i|_->terr"complex")letunpack_boolx=(matchxwithEvtBooli->i|_->terr"bool")letunpack_stringx=(matchxwithEvtStringi->i|_->terr"string")letunpack_listx=(matchxwithEvtListi->i|_->terr"list")letunpack_dictx=(matchxwithEvtDicti->i|_->terr"dict")letunpack_closurex=(matchxwithClosure(p,b,e)->(p,b,e)|_->terr"fun")letunpack_recclosurex=(matchxwith|RecClosure(i,p,b,e)->(i,p,b,e)|_->terr"fun")letunpack_anyfunx=matchxwith|RecClosure(i,p,b,e)->(i,p,b,e)|Closure(p,b,e)->("",p,b,e)|_->terr"fun"(* Check if first elem of tuple is an allowed type for dict key and return tuple *)letisvalidkey(x,y)=((matchxwith|EvtInt_->x|EvtBool_->x|EvtString_->x|_->failwith"value not allowed as dictionary key"),y)