1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063openComponentexceptionInvalidatedtype('a,'b)or_replaced=Not_replacedof'a|Replacedof'btype'atype_or_replaced=('a,TypeExpr.t*TypeDecl.Equation.t)or_replacedtype'amodule_type_or_replaced=('a,ModuleType.expr)or_replacedletmap_replacedf=function|Not_replacedp->Not_replaced(fp)|Replaced_asr->ropenComponentopenSubstitutiontypenonrect=tletidentity={module_=ModuleMap.empty;module_type=ModuleTypeMap.empty;module_type_replacement=ModuleTypeMap.empty;type_=TypeMap.empty;class_type=TypeMap.empty;type_replacement=TypeMap.empty;path_invalidating_modules=[];unresolve_opaque_paths=false;}letppfmts=letpp_mappp_bindingbfmtmap=letpp_bfmt(id,v)=Format.fprintffmt"%a -> %a"Ident.fmtidpp_bindingvinFormat.fprintffmt"@[<hov 1>{%a}@]"(Format.pp_print_listpp_b)(bmap)inletpp_substpppfmtv=Format.fprintffmt"%s"(matchvwith|`Prefixed(p,_)->Format.asprintf"%a"pppp|`Renamedid'->Format.asprintf"%a"Ident.fmtid'|`Substituted->"<substituted>")inletpp_type_replacementfmt(te,eq)=Format.fprintffmt"(%a,%a)"Component.Fmt.(type_exprdefault)teComponent.Fmt.(type_equationdefault)eqinFormat.fprintffmt"{ module_ = %a;@ module_type = %a;@ type_ = %a;@ class_type = %a;@ \
type_replacement = %a;@ module_type_replacement = %a;@ \
path_invalidating_modules = [%a];@ unresolve_opaque_paths = %b }"(pp_map(pp_substComponent.Fmt.(module_pathdefault))ModuleMap.bindings)s.module_(pp_map(pp_substComponent.Fmt.(module_type_pathdefault))ModuleTypeMap.bindings)s.module_type(pp_map(pp_substComponent.Fmt.(type_pathdefault))TypeMap.bindings)s.type_(pp_map(pp_substComponent.Fmt.(class_type_pathdefault))TypeMap.bindings)s.class_type(pp_mappp_type_replacementTypeMap.bindings)s.type_replacement(pp_mapComponent.Fmt.(module_type_exprdefault)ModuleTypeMap.bindings)s.module_type_replacement(Format.pp_print_listIdent.fmt)s.path_invalidating_moduless.unresolve_opaque_pathsletunresolve_opaque_pathss={swithunresolve_opaque_paths=true}letpath_invalidate_moduleidt={twithpath_invalidating_modules=id::t.path_invalidating_modules}letadd_moduleidprpt={twithmodule_=ModuleMap.addid(`Prefixed(p,rp))t.module_}letadd_module_typeidprpt={twithmodule_type=ModuleTypeMap.addid(`Prefixed(p,rp))t.module_type;}letadd_type:Ident.type_->Cpath.type_->Cpath.Resolved.type_->t->t=funidprpt->{twithtype_=TypeMap.add(id:>Ident.type_)(`Prefixed(p,rp))t.type_}letadd_class:Ident.type_->Cpath.class_type->Cpath.Resolved.class_type->t->t=funidprpt->{twithtype_=TypeMap.add(id:>Ident.type_)(`Prefixed((p:>Cpath.type_),(rp:>Cpath.Resolved.type_)))t.type_;class_type=TypeMap.add(id:>Ident.type_)(`Prefixed(p,rp))t.class_type;}letadd_class_type:Ident.type_->Cpath.class_type->Cpath.Resolved.class_type->t->t=funidprpt->{twithtype_=TypeMap.add(id:>Ident.type_)(`Prefixed((p:>Cpath.type_),(rp:>Cpath.Resolved.type_)))t.type_;class_type=TypeMap.add(id:>Ident.type_)(`Prefixed(p,rp))t.class_type;}letadd_type_replacementidtexpequationt={twithtype_replacement=TypeMap.addid(texp,equation)t.type_replacement;}letadd_module_type_replacementpathmtyt={twithmodule_type_replacement=ModuleTypeMap.addpathmtyt.module_type_replacement;}letadd_module_substitution:Ident.module_->t->t=funidt->{twithpath_invalidating_modules=id::t.path_invalidating_modules;module_=ModuleMap.addid`Substitutedt.module_;}letrename_module:Ident.module_->Ident.module_->t->t=funidid't->{twithmodule_=ModuleMap.addid(`Renamedid')t.module_}letrename_module_type:Ident.module_type->Ident.module_type->t->t=funidid't->{twithmodule_type=ModuleTypeMap.addid(`Renamedid')t.module_type}letrename_type:Ident.type_->Ident.type_->t->t=funidid't->{twithtype_=TypeMap.addid(`Renamedid')t.type_}letrename_class_type:Ident.type_->Ident.type_->t->t=funidid't->{twithclass_type=TypeMap.addid(`Renamedid')t.class_type;type_=TypeMap.add(id:>Ident.type_)(`Renamed(id':>Ident.type_))t.type_;}letrecsubstitute_varsvarst=letopenTypeExprinmatchtwith|Vars->(tryList.assocsvarswithNot_found->t)|Any->Any|Alias(t,str)->Alias(substitute_varsvarst,str)|Arrow(lbl,t1,t2)->Arrow(lbl,substitute_varsvarst1,substitute_varsvarst2)|Tuplets->Tuple(List.map(fun(lbl,ty)->(lbl,substitute_varsvarsty))ts)|Unboxed_tuplets->Unboxed_tuple(List.map(fun(l,t)->(l,substitute_varsvarst))ts)|Constr(p,ts)->Constr(p,List.map(substitute_varsvars)ts)|Polymorphic_variantv->Polymorphic_variant(substitute_vars_poly_variantvarsv)|Objecto->Object(substitute_vars_type_objectvarso)|Class(p,ts)->Class(p,List.map(substitute_varsvars)ts)|Poly(strs,ts)->Poly(strs,substitute_varsvarsts)|Quotet->Quote(substitute_varsvarst)|Splicet->Splice(substitute_varsvarst)|Packagep->Package(substitute_vars_packagevarsp)|Arrow_functor(lbl,m_arg,t)->Arrow_functor(lbl,substitute_vars_module_argvarsm_arg,substitute_varsvarst)andsubstitute_vars_module_argvarsm_arg=letpackage=substitute_vars_packagevarsm_arg.packagein{m_argwithpackage}andsubstitute_vars_packagevarsp=letopenTypeExpr.Packageinletsubst_subst(p,t)=(p,substitute_varsvarst)in{pwithsubstitutions=List.mapsubst_substp.substitutions}andsubstitute_vars_type_objectvarso=letopenTypeExpr.Objectinletsubst_field=function|Methodm->Method{mwithtype_=substitute_varsvarsm.type_}|Inheritt->Inherit(substitute_varsvarst)in{owithfields=List.mapsubst_fieldo.fields}andsubstitute_vars_poly_variantvarsv=letopenTypeExpr.Polymorphic_variantinletsubst_element=function|Typet->Type(substitute_varsvarst)|Constructorc->letarguments=List.map(substitute_varsvars)c.Constructor.argumentsinConstructor{cwitharguments}in{vwithelements=List.mapsubst_elementv.elements}letrecresolved_module_path:t->Cpath.Resolved.module_->Cpath.Resolved.module_=funsp->matchpwith|`Localid->(ifList.memids.path_invalidating_modulesthenraiseInvalidated;matchtrySome(ModuleMap.find(id:>Ident.module_)s.module_)with_->Nonewith|Some(`Renamedx)->`Localx|Some(`Prefixed(_p,rp))->rp|Some`Substituted->`Substitutedp|None->p)|`Gpath_->p|`Apply(p1,p2)->`Apply(resolved_module_pathsp1,resolved_module_pathsp2)|`Substitutedp->`Substituted(resolved_module_pathsp)|`Module(p,n)->`Module(resolved_parent_pathsp,n)|`Alias(p1,p2,p3opt)->letp2'=module_pathsp2inletup2'=tryCpath.unresolve_module_pathp2'with_->p2'inletp3opt'=matchp3optwith|Somep3->Some(resolved_module_pathsp3)|None->Nonein`Alias(resolved_module_pathsp1,up2',p3opt')|`Subst(p1,p2)->letp1=matchresolved_module_type_pathsp1with|Replaced_->(* the left hand side of Subst is a named module type inside a module,
it cannot be substituted away *)assertfalse|Not_replacedp1->p1in`Subst(p1,resolved_module_pathsp2)|`Hiddenp1->`Hidden(resolved_module_pathsp1)|`Canonical(p1,p2)->`Canonical(resolved_module_pathsp1,p2)|`OpaqueModulem->ifs.unresolve_opaque_pathsthenraiseInvalidatedelse`OpaqueModule(resolved_module_pathsm)andresolved_parent_paths=function|`Modulem->`Module(resolved_module_pathsm)|`ModuleTypem->letp=matchresolved_module_type_pathsmwith|Replaced_->assertfalse|Not_replacedp1->p1in`ModuleTypep|`FragmentRootasx->xandmodule_path:t->Cpath.module_->Cpath.module_=funsp->matchpwith|`Resolvedp'->(try`Resolved(resolved_module_pathsp')withInvalidated->letpath'=Cpath.unresolve_resolved_module_pathp'inmodule_pathspath')|`Dot(p',str)->`Dot(module_pathsp',str)|`Module(p',str)->`Module(resolved_parent_pathsp',str)|`Apply(p1,p2)->`Apply(module_pathsp1,module_pathsp2)|`Local(id,b)->(matchtrySome(ModuleMap.find(id:>Ident.module_)s.module_)with_->Nonewith|Some(`Prefixed(p,_rp))->p|Some(`Renamedx)->`Local(x,b)|Some`Substituted->`Substitutedp|None->`Local(id,b))|`Identifier_->p|`Substitutedp->`Substituted(module_pathsp)|`Forward_->p|`Root_->pandresolved_module_type_path:t->Cpath.Resolved.module_type->(Cpath.Resolved.module_type,ModuleType.expr)or_replaced=funsp->matchpwith|`Localid->(ifModuleTypeMap.memids.module_type_replacementthenReplaced(ModuleTypeMap.findids.module_type_replacement)elsematchModuleTypeMap.findids.module_typewith|`Prefixed(_p,rp)->Not_replacedrp|`Renamedx->Not_replaced(`Localx)|exceptionNot_found->Not_replaced(`Localid))|`Gpath_->Not_replacedp|`Substitutedp->resolved_module_type_pathsp|>map_replaced(funp->`Substitutedp)|`ModuleType(p,n)->Not_replaced(`ModuleType(resolved_parent_pathsp,n))|`CanonicalModuleType(mt1,mt2)->(matchresolved_module_type_pathsmt1with|Not_replacedmt1'->Not_replaced(`CanonicalModuleType(mt1',mt2))|x->x)|`OpaqueModuleTypem->ifs.unresolve_opaque_pathsthenraiseInvalidatedelseresolved_module_type_pathsm|>map_replaced(funx->`OpaqueModuleTypex)|`SubstT(p1,p2)->(match(resolved_module_type_pathsp1,resolved_module_type_pathsp2)with|Not_replacedp1,Not_replacedp2->Not_replaced(`SubstT(p1,p2))|Replacedmt,_|_,Replacedmt->Replacedmt)|`AliasModuleType(p1,p2)->(match(resolved_module_type_pathsp1,resolved_module_type_pathsp2)with|Not_replacedp1,Not_replacedp2->Not_replaced(`AliasModuleType(p1,p2))|Replacedmt,_|_,Replacedmt->Replacedmt)andmodule_type_path:t->Cpath.module_type->Cpath.module_typemodule_type_or_replaced=funsp->matchpwith|`Resolvedr->(tryresolved_module_type_pathsr|>map_replaced(funr->`Resolvedr)withInvalidated->letpath'=Cpath.unresolve_resolved_module_type_pathrinmodule_type_pathspath')|`Substitutedp->module_type_pathsp|>map_replaced(funr->`Substitutedr)|`Local(id,b)->ifModuleTypeMap.memids.module_type_replacementthenReplaced(ModuleTypeMap.findids.module_type_replacement)elseletr=matchtrySome(ModuleTypeMap.findids.module_type)with_->Nonewith|Some(`Prefixed(p,_rp))->p|Some(`Renamedx)->`Local(x,b)|None->`Local(id,b)inNot_replacedr|`Identifier_->Not_replacedp|`DotMT(p,n)->Not_replaced(`DotMT(module_pathsp,n))|`ModuleType(p',str)->Not_replaced(`ModuleType(resolved_parent_pathsp',str))andresolved_type_path:t->Cpath.Resolved.type_->(Cpath.Resolved.type_,TypeExpr.t*TypeDecl.Equation.t)or_replaced=funsp->matchpwith|`CoreType_asc->Not_replacedc|`Localid->(ifTypeMap.memids.type_replacementthenReplaced(TypeMap.findids.type_replacement)elsematchtrySome(TypeMap.findids.type_)withNot_found->Nonewith|Some(`Prefixed(_p,rp))->Not_replacedrp|Some(`Renamedx)->Not_replaced(`Localx)|None->Not_replaced(`Localid))|`CanonicalType(t1,t2)->(matchresolved_type_pathst1with|Not_replacedt1'->Not_replaced(`CanonicalType(t1',t2))|x->x)|`Gpath_->Not_replacedp|`Substitutedp->resolved_type_pathsp|>map_replaced(funp->`Substitutedp)|`Type(p,n)->Not_replaced(`Type(resolved_parent_pathsp,n))|`ClassType(p,n)->Not_replaced(`ClassType(resolved_parent_pathsp,n))|`Class(p,n)->Not_replaced(`Class(resolved_parent_pathsp,n))andtype_path:t->Cpath.type_->Cpath.type_type_or_replaced=funsp->matchpwith|`Resolvedr->(tryresolved_type_pathsr|>map_replaced(funr->`Resolvedr)withInvalidated->letpath'=Cpath.unresolve_resolved_type_pathrintype_pathspath')|`Substitutedp->type_pathsp|>map_replaced(funr->`Substitutedr)|`Local(id,b)->(ifTypeMap.memids.type_replacementthenReplaced(TypeMap.findids.type_replacement)elsematchtrySome(TypeMap.findids.type_)withNot_found->Nonewith|Some(`Prefixed(p,_rp))->Not_replacedp|Some(`Renamedx)->Not_replaced(`Local(x,b))|None->Not_replaced(`Local(id,b)))|`Identifier_->Not_replacedp|`DotT(p,n)->Not_replaced(`DotT(module_pathsp,n))|`Type(p,n)->Not_replaced(`Type(resolved_parent_pathsp,n))|`Class(p,n)->Not_replaced(`Class(resolved_parent_pathsp,n))|`ClassType(p,n)->Not_replaced(`ClassType(resolved_parent_pathsp,n))andresolved_class_type_path:t->Cpath.Resolved.class_type->Cpath.Resolved.class_type=funsp->matchpwith|`Localid->(matchtrySome(TypeMap.findids.class_type)with_->Nonewith|Some(`Prefixed(_p,rp))->rp|Some(`Renamedx)->`Localx|None->`Localid)|`Gpath_->p|`Substitutedp->`Substituted(resolved_class_type_pathsp)|`ClassType(p,n)->`ClassType(resolved_parent_pathsp,n)|`Class(p,n)->`Class(resolved_parent_pathsp,n)andclass_type_path:t->Cpath.class_type->Cpath.class_type=funsp->matchpwith|`Resolvedr->(try`Resolved(resolved_class_type_pathsr)withInvalidated->letpath'=Cpath.unresolve_resolved_class_type_pathrinclass_type_pathspath')|`Local(id,b)->(matchtrySome(TypeMap.findids.class_type)with_->Nonewith|Some(`Prefixed(p,_rp))->p|Some(`Renamedx)->`Local(x,b)|None->`Local(id,b))|`Identifier_->p|`Substitutedp->`Substituted(class_type_pathsp)|`DotT(p,n)->`DotT(module_pathsp,n)|`Class(p,n)->`Class(resolved_parent_pathsp,n)|`ClassType(p,n)->`ClassType(resolved_parent_pathsp,n)letrecresolved_signature_fragment:t->Cfrag.resolved_signature->Cfrag.resolved_signature=funtr->matchrwith|`Root(`ModuleTypep)->letp=matchresolved_module_type_pathtpwith|Not_replacedp->p|Replaced_->(* The resolved root module type of the fragment is being
substituted away. The resolved form is no longer valid —
unresolve and re-resolve against the post-substitution context
*)raiseInvalidatedin`Root(`ModuleTypep)|`Root(`Modulep)->`Root(`Module(resolved_module_pathtp))|(`Subst_|`Alias_|`OpaqueModule_|`Module_)asx->(resolved_module_fragmenttx:>Cfrag.resolved_signature)andresolved_module_fragment:t->Cfrag.resolved_module->Cfrag.resolved_module=funtr->matchrwith|`Subst(mty,f)->letp=matchresolved_module_type_pathtmtywith|Not_replacedp->p|Replaced_->(* the left hand side of subst is a named module type inside a module,
it cannot be substituted *)assertfalsein`Subst(p,resolved_module_fragmenttf)|`Alias(m,f)->`Alias(resolved_module_pathtm,resolved_module_fragmenttf)|`Module(sg,n)->`Module(resolved_signature_fragmenttsg,n)|`OpaqueModulem->`OpaqueModule(resolved_module_fragmenttm)andresolved_module_type_fragment:t->Cfrag.resolved_module_type->Cfrag.resolved_module_type=funtr->matchrwith|`ModuleType(s,n)->`ModuleType(resolved_signature_fragmentts,n)andresolved_type_fragment:t->Cfrag.resolved_type->Cfrag.resolved_type=funtr->matchrwith|`Type(s,n)->`Type(resolved_signature_fragmentts,n)|`ClassType(s,n)->`ClassType(resolved_signature_fragmentts,n)|`Class(s,n)->`Class(resolved_signature_fragmentts,n)letrecsignature_fragment:t->Cfrag.signature->Cfrag.signature=funtr->matchrwith|`Resolvedf->`Resolved(resolved_signature_fragmenttf)|`Dot(sg,n)->`Dot(signature_fragmenttsg,n)|`Root->`Rootletrecmodule_fragment:t->Cfrag.module_->Cfrag.module_=funtr->matchrwith|`Resolvedr->(try`Resolved(resolved_module_fragmenttr)withInvalidated->letfrag'=Cfrag.unresolve_modulerinmodule_fragmenttfrag')|`Dot(sg,n)->`Dot(signature_fragmenttsg,n)letrecmodule_type_fragment:t->Cfrag.module_type->Cfrag.module_type=funtr->matchrwith|`Resolvedr->(try`Resolved(resolved_module_type_fragmenttr)withInvalidated->letfrag'=Cfrag.unresolve_module_typerinmodule_type_fragmenttfrag')|`Dot(sg,n)->`Dot(signature_fragmenttsg,n)letrectype_fragment:t->Cfrag.type_->Cfrag.type_=funtr->matchrwith|`Resolvedr->(try`Resolved(resolved_type_fragmenttr)withInvalidated->letfrag'=Cfrag.unresolve_typerintype_fragmenttfrag')|`Dot(sg,n)->`Dot(signature_fragmenttsg,n)letoption_convsx=matchxwithSomex->Some(convsx)|None->Noneletlistconvsxs=List.map(convs)xsletrectype_st=letopenComponent.TypeDeclinletrepresentation=option_type_decl_representationst.representationin{twithequation=type_decl_equationst.equation;representation}andtype_decl_representationst=letopenComponent.TypeDecl.Representationinmatchtwith|Variantcs->Variant(List.map(type_decl_constructors)cs)|Recordfs->Record(List.map(type_decl_fields)fs)|Record_unboxed_productfs->Record_unboxed_product(List.map(type_decl_unboxed_fields)fs)|Extensible->tandtype_decl_constructorst=letopenComponent.TypeDecl.Constructorinletargs=type_decl_constructor_argst.argsinletres=option_type_exprst.resin{twithargs;res}andtype_poly_varsv=letopenComponent.TypeExpr.Polymorphic_variantinletmap_constrc=letopenConstructorin{name=c.name;constant=c.constant;arguments=List.map(type_exprs)c.arguments;doc=c.doc;}inletmap_element=function|Typet->(matchtype_exprstwith|Polymorphic_variantv->v.elements|x->[Typex])|Constructorc->[Constructor(map_constrc)]in{kind=v.kind;elements=List.flatten(List.mapmap_elementv.elements)}andtype_objectso=letopenComponent.TypeExpr.Objectinletmap_field=function|Methodm->Method{mwithtype_=type_exprsm.type_}|Inheritt->Inherit(type_exprst)in{fields=List.mapmap_fieldo.fields;open_=o.open_}andtype_packagesp=letopenComponent.TypeExpr.Packageinletsub(x,y)=(type_fragmentsx,type_exprsy)in{path=(matchmodule_type_pathsp.pathwith|Not_replacedp->p|Replaced(Pathp)->p.p_path|Replaced_->(* substituting away a packed module type by a non-path module type is a type error *)assertfalse);substitutions=List.mapsubp.substitutions;}andtype_exprst=letopenComponent.TypeExprinmatchtwith|Vars->Vars|Any->Any|Alias(t,str)->Alias(type_exprst,str)|Arrow(lbl,t1,t2)->Arrow(lbl,type_exprst1,type_exprst2)|Tuplets->Tuple(List.map(fun(lbl,ty)->(lbl,type_exprsty))ts)|Unboxed_tuplets->Unboxed_tuple(List.map(fun(l,t)->(l,type_exprst))ts)|Constr(p,ts)->(matchtype_pathspwith|Replaced(t,eq)->letmk_varaccpexprparam=matchparam.Odoc_model.Lang.TypeDecl.descwith|Any->acc|Varn->(n,type_exprspexpr)::accinifList.lengthts<>List.lengtheq.paramsthen(Format.eprintf"Type substitution error: eq.params length=%d ts length=%d@."(List.lengtheq.params)(List.lengthts);assertfalse);letvars=List.fold_left2mk_var[]tseq.paramsinsubstitute_varsvarst|Not_replacedp->Constr(p,List.map(type_exprs)ts))|Polymorphic_variantv->Polymorphic_variant(type_poly_varsv)|Objecto->Object(type_objectso)|Class(p,ts)->Class(class_type_pathsp,List.map(type_exprs)ts)|Poly(strs,ts)->Poly(strs,type_exprsts)|Quotet->Quote(type_exprst)|Splicet->Splice(type_exprst)|Packagep->Package(type_packagesp)|Arrow_functor(lbl,m_arg,t)->Arrow_functor(lbl,type_module_argsm_arg,type_exprst)andtype_module_argsm_arg=letpackage=type_packagesm_arg.packagein{m_argwithpackage}andsimple_expansion:t->Component.ModuleType.simple_expansion->Component.ModuleType.simple_expansion=funst->letopenComponent.ModuleTypeinmatchtwith|Signaturesg->Signature(signaturessg)|Functor(arg,sg)->Functor(functor_parametersarg,simple_expansionssg)andmodule_typest=letopenComponent.ModuleTypeinletexpr=matcht.exprwithSomem->Some(module_type_exprsm)|None->Nonein{expr;source_loc=t.source_loc;doc=t.doc;canonical=t.canonical}andmodule_type_substitutionst=letopenComponent.ModuleTypeSubstitutioninletmanifest=module_type_exprst.manifestin{manifest;doc=t.doc}andfunctor_parameterst=letopenComponent.FunctorParameterinmatchtwith|Namedarg->Named{argwithexpr=module_type_exprsarg.expr}|Unit->Unitandmodule_type_type_of_descst=letopenComponent.ModuleTypeinmatchtwith|ModPathp->ModPath(module_pathsp)|StructIncludep->StructInclude(module_pathsp)andu_module_type_exprst=letopenComponent.ModuleType.Uinmatchtwith|Pathp->(matchmodule_type_pathspwith|Not_replacedp->Pathp|Replacedeqn->(matcheqnwith|Pathp->Pathp.p_path|Signatures->Signatures|TypeOft->TypeOf(t.t_desc,t.t_original_path)|Withw->With(w.w_substitutions,w.w_expr)|Functor_->(* non functor cannot be substituted away to a functor *)assertfalse|Strengthens->Strengthen(s.s_expr,s.s_path,s.s_aliasable)))|Signaturesg->Signature(signaturessg)|With(subs,e)->With(List.map(with_module_type_substitutions)subs,u_module_type_exprse)|TypeOf(t_desc,t_original_path)->TypeOf(module_type_type_of_descst_desc,t_original_path)|Strengthen(expr,path,aliasable)->letexpr=u_module_type_exprsexprinletpath=module_pathspathinStrengthen(expr,path,aliasable)andmodule_type_exprst=letopenComponent.ModuleTypeinmatchtwith|Path{p_path;p_expansion}->(matchmodule_type_pathsp_pathwith|Not_replacedp_path->Path{p_path;p_expansion=option_simple_expansionsp_expansion}|Replaceds->s)|Signaturesg->Signature(signaturessg)|Functor(arg,expr)->Functor(functor_parametersarg,module_type_exprsexpr)|With{w_substitutions;w_expansion;w_expr}->With{w_substitutions=List.map(with_module_type_substitutions)w_substitutions;w_expansion=option_simple_expansionsw_expansion;w_expr=u_module_type_exprsw_expr;}|TypeOft->TypeOf{twitht_desc=module_type_type_of_descst.t_desc;t_expansion=option_simple_expansionst.t_expansion;}|Strengthen{s_expr;s_path;s_aliasable;s_expansion}->Strengthen{s_expr=u_module_type_exprss_expr;s_path=module_pathss_path;s_aliasable;s_expansion=option_simple_expansionss_expansion;}andwith_module_type_substitutionssub=letopenComponent.ModuleTypeinmatchsubwith|ModuleEq(f,m)->ModuleEq(module_fragmentsf,module_declsm)|ModuleSubst(f,p)->ModuleSubst(module_fragmentsf,module_pathsp)|TypeEq(f,eq)->TypeEq(type_fragmentsf,type_decl_equationseq)|TypeSubst(f,eq)->TypeSubst(type_fragmentsf,type_decl_equationseq)|ModuleTypeEq(f,eq)->ModuleTypeEq(module_type_fragmentsf,module_type_exprseq)|ModuleTypeSubst(f,eq)->ModuleTypeSubst(module_type_fragmentsf,module_type_exprseq)andmodule_declst=matchtwith|Alias(p,e)->Alias(module_pathsp,option_simple_expansionse)|ModuleTypet->ModuleType(module_type_exprst)andinclude_declst=matchtwith|Include.Aliasp->Include.Alias(module_pathsp)|ModuleTypet->ModuleType(u_module_type_exprst)andmodule_st=letopenComponent.Moduleinlettype_=module_declst.type_inletcanonical=t.canonicalin{twithtype_;canonical}andmodule_substitutionsm=letopenComponent.ModuleSubstitutioninletmanifest=module_pathsm.manifestin{manifest;doc=m.doc}andtype_decl_fieldsf=letopenComponent.TypeDecl.Fieldin{fwithtype_=type_exprsf.type_}andtype_decl_unboxed_fieldsf=letopenComponent.TypeDecl.UnboxedFieldin{fwithtype_=type_exprsf.type_}andtype_decl_constructor_argsa=letopenComponent.TypeDecl.Constructorinmatchawith|Tuplets->Tuple(listtype_exprsts)|Recordfs->Record(listtype_decl_fieldsfs)andtype_decl_equationst=letopenComponent.TypeDecl.Equationin{twithmanifest=option_type_exprst.manifest;constraints=List.map(fun(x,y)->(type_exprsx,type_exprsy))t.constraints;}andexception_se=letopenComponent.Exceptioninletres=option_type_exprse.resinletargs=type_decl_constructor_argse.argsin{ewithargs;res}andextension_constructorsc=letopenComponent.Extension.Constructorin{cwithargs=type_decl_constructor_argsc.args;res=option_type_exprsc.res;}andextensionse=letopenComponent.Extensioninlettype_path=matchtype_pathse.type_pathwith|Not_replacedp->p|Replaced(TypeExpr.Constr(p,_),_)->p|Replaced_->(* What else is possible ? *)assertfalseandconstructors=List.map(extension_constructors)e.constructorsin{ewithtype_path;constructors}andinclude_si=letopenComponent.Includein{iwithdecl=include_declsi.decl;strengthened=option_module_pathsi.strengthened;expansion_=apply_sig_map_sgsi.expansion_;}andopen_so=letopenComponent.Openin{expansion=apply_sig_map_sgso.expansion;doc=o.doc}andvaluesv=letopenComponent.Valuein{vwithtype_=type_exprsv.type_}andclass_sc=letopenComponent.Classinletexpansion=option_class_signaturesc.expansionin{cwithtype_=class_declsc.type_;expansion}andclass_decls=letopenComponent.Classinfunction|ClassTypee->ClassType(class_type_exprse)|Arrow(lbl,t,d)->Arrow(lbl,type_exprst,class_declsd)andclass_type_exprs=letopenComponent.ClassTypeinfunction|Constr(p,ts)->Constr(class_type_pathsp,List.map(type_exprs)ts)|Signaturesg->Signature(class_signaturessg)andclass_typesc=letopenComponent.ClassTypeinletexpansion=option_class_signaturesc.expansionin{cwithexpr=class_type_exprsc.expr;expansion}andclass_signature_items=letopenComponent.ClassSignatureinfunction|Method(id,m)->Method(id,method_sm)|InstanceVariable(id,i)->InstanceVariable(id,instance_variablesi)|Constraintcst->Constraint(class_constraintscst)|Inherite->Inherit(inherit_se)|Comment_asy->yandclass_signaturessg=letopenComponent.ClassSignaturein{sgwithself=option_type_exprssg.self;items=List.map(class_signature_items)sg.items;}andmethod_sm=letopenComponent.Methodin{mwithtype_=type_exprsm.type_}andinstance_variablesi=letopenComponent.InstanceVariablein{iwithtype_=type_exprsi.type_}andclass_constraintscst=letopenComponent.ClassSignature.Constraintin{cstwithleft=type_exprscst.left;right=type_exprscst.right}andinherit_sih=letopenComponent.ClassSignature.Inheritin{ihwithexpr=class_type_exprsih.expr}andrename_bound_identsssg=letopenComponent.Signatureinletnew_module_idid=trymatchModuleMap.find(id:>Ident.module_)s.module_with|`Renamed(`LModule_asx)->x|`Prefixed(_,_)->(* This is unusual but can happen when we have TypeOf expressions. It means
we're already prefixing this module path, hence we can essentially rename
it to whatever we like because it's never going to be referred to. *)Ident.Rename.module_id|_->failwith"Error"withNot_found->Ident.Rename.module_idinletnew_module_type_idid=trymatchModuleTypeMap.findids.module_typewith|`Renamedx->x|`Prefixed(_,_)->Ident.Rename.module_typeidwithNot_found->Ident.Rename.module_typeidinletnew_type_idid=trymatchTypeMap.find(id:>Ident.type_)s.type_with|`Renamed(`LType_asx)->x|`Prefixed(_,_)->Ident.Rename.type_idwithNot_found->Ident.Rename.type_idinletnew_class_idid=trymatchTypeMap.find(id:>Ident.type_)s.class_typewith|`Renamed(`LType_asx)->x|`Prefixed(_,_)->Ident.Rename.type_idwithNot_found->Ident.Rename.type_idinletnew_class_type_idid=trymatchTypeMap.find(id:>Ident.type_)s.class_typewith|`Renamed(`LType_asx)->x|`Prefixed(_,_)->Ident.Rename.type_idwithNot_found->Ident.Rename.type_idinfunction|[]->(s,List.revsg)|Module(id,r,m)::rest->letid'=new_module_ididinrename_bound_idents(rename_module(id:>Ident.module_)(id':>Ident.module_)s)(Module(id',r,m)::sg)rest|ModuleSubstitution(id,m)::rest->letid'=new_module_ididinrename_bound_idents(rename_module(id:>Ident.module_)(id':>Ident.module_)s)(ModuleSubstitution(id',m)::sg)rest|ModuleType(id,mt)::rest->letid'=new_module_type_ididinrename_bound_idents(rename_module_typeidid's)(ModuleType(id',mt)::sg)rest|ModuleTypeSubstitution(id,mt)::rest->letid'=new_module_type_ididinrename_bound_idents(rename_module_typeidid's)(ModuleTypeSubstitution(id',mt)::sg)rest|Type(id,r,t)::rest->letid'=new_type_ididinrename_bound_idents(rename_type(id:>Ident.type_)(id':>Ident.type_)s)(Type(id',r,t)::sg)rest|TypeSubstitution(id,t)::rest->letid'=new_type_ididinrename_bound_idents(rename_type(id:>Ident.type_)(id':>Ident.type_)s)(TypeSubstitution(id',t)::sg)rest|Exception(id,e)::rest->letid'=Ident.Rename.exception_idinrename_bound_identss(Exception(id',e)::sg)rest|TypExte::rest->rename_bound_identss(TypExte::sg)rest|Value(id,v)::rest->letid'=Ident.Rename.valueidinrename_bound_identss(Value(id',v)::sg)rest|Class(id,r,c)::rest->letid'=new_class_ididinrename_bound_idents(rename_class_type(id:>Ident.type_)(id':>Ident.type_)s)(Class(id',r,c)::sg)rest|ClassType(id,r,c)::rest->letid'=new_class_type_ididinrename_bound_idents(rename_class_type(id:>Ident.type_)(id':>Ident.type_)s)(ClassType(id',r,c)::sg)rest|Include({expansion_;_}asi)::rest->lets,items=rename_bound_identss[]expansion_.itemsinrename_bound_identss(Include{iwithexpansion_={expansion_withitems;removed=[]}}::sg)rest|Open{expansion;doc}::rest->lets,items=rename_bound_identss[]expansion.itemsinrename_bound_identss(Open{expansion={expansionwithitems;removed=[]};doc}::sg)rest|(Comment_asitem)::rest->rename_bound_identss(item::sg)restandremoved_itemssitems=letopenComponent.SignatureinList.map(function|RModule(id,p)->RModule(id,module_pathsp)|RType(id,exp,eqn)->RType(id,type_exprsexp,type_decl_equationseqn)|RModuleType(id,mty)->RModuleType(id,module_type_exprsmty))itemsandsignaturessg=lets,items=rename_bound_identss[]sg.itemsinletitems,removed,dont_recompile=apply_sig_mapsitemssg.removedin{sgwithitems;removed;compiled=sg.compiled&&dont_recompile}andapply_sig_map_sgs(sg:Component.Signature.t)=letitems,removed,dont_recompile=apply_sig_mapssg.itemssg.removedin{sgwithitems;removed;compiled=sg.compiled&&dont_recompile}andapply_sig_map_itemsitem=letopenComponent.Signatureinmatchitemwith|Module(id,r,m)->Module(id,r,Component.Delayed.put(fun()->module_s(Component.Delayed.getm)))|ModuleSubstitution(id,m)->ModuleSubstitution(id,module_substitutionsm)|ModuleType(id,mt)->ModuleType(id,Component.Delayed.put(fun()->module_types(Component.Delayed.getmt)))|ModuleTypeSubstitution(id,mt)->ModuleTypeSubstitution(id,module_type_substitutionsmt)|Type(id,r,t)->Type(id,r,Component.Delayed.put(fun()->type_s(Component.Delayed.gett)))|TypeSubstitution(id,t)->TypeSubstitution(id,type_st)|Exception(id,e)->Exception(id,exception_se)|TypExte->TypExt(extensionse)|Value(id,v)->Value(id,Component.Delayed.put(fun()->values(Component.Delayed.getv)))|Class(id,r,c)->Class(id,r,class_sc)|ClassType(id,r,c)->ClassType(id,r,class_typesc)|Includei->Include(include_si)|Openo->Open(open_so)|Commentc->Commentcandapply_sig_map_itemssitems=List.rev_map(apply_sig_map_items)items|>List.revandapply_sig_mapsitemsremoved=letdont_recompile=List.lengths.path_invalidating_modules=0in(apply_sig_map_itemssitems,removed_itemssremoved,dont_recompile)