12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318openCommon(*
let attr_nobuiltin : (Ppxlib.core_type, unit -> unit) Ppxlib.Attribute.t =
Ppxlib.Attribute.declare "refl.nobuiltin" Core_type
(Ppxlib.Ast_pattern.(pstr nil))
Fun.id
let attr_opaque : (Ppxlib.core_type, unit -> unit) Ppxlib.Attribute.t =
Ppxlib.Attribute.declare "refl.opaque" Core_type
(Ppxlib.Ast_pattern.(pstr nil))
Fun.id
*)letmap_lidentf(ident:Longident.t):Longident.t=matchidentwith|Ldot(prefix,name)->Ldot(prefix,fname)|Lidentident->Lident(fident)|_->assertfalseletrecremove_ident_prefix_opt(prefix:Longident.t)(ident:Longident.t):Longident.toption=matchidentwith|Ldot(prefix',name)->ifprefix=prefix'thenSome(Lidentname)elseOption.map(funnew_prefix->Longident.Ldot(new_prefix,name))(remove_ident_prefix_optprefixprefix')|Lapply(a,b)->beginmatcha,remove_ident_prefix_optprefixa,b,remove_ident_prefix_optprefixbwith|_,Somea,_,Someb|a,None,_,Someb|_,Somea,b,None->Some(Lapply(a,b))|_,None,_,None->Noneend|Lident_->Noneletident_of_str(x:Ast_helper.str):Ppxlib.expression=Ppxlib.Ast_helper.Exp.ident~loc:x.loc(Metapp.lid_of_strx)letitemi=Printf.sprintf"item%d"iletreciterateaccfi=ifi>0theniterate(facc)f(predi)elseaccletreciterate_i_auxaccfij=ifi<jtheniterate_i_aux(fiacc)f(succi)jelseaccletiterate_iaccfi=iterate_i_auxaccf0iletpeano_type_of_inti=iterate[%type:[`Zero]](funt->[%type:[`Succof[%tt]]])iletrefl_dotfield:Longident.t=Ldot(Lident"Refl",field)letreccutl=matchlwith|even::odd::tail->leteven_tail,odd_tail=cuttailineven::even_tail,odd::odd_tail|[even]->[even],[]|[]->[],[]letrecbinary_of_intzeroonefinalilength=iflength>1thenifimod2=0thenzero(binary_of_intzeroonefinal(i/2)((length+1)/2))elseone(binary_of_intzeroonefinal(i/2)(length-(length+1)/2))elsefinalletbinary_type_of_intilength=binary_of_int(funtailk->tail[%type:[`Zeroof[%tk]]])(funtailk->tail[%type:[`Oneof[%tk]]])Fun.idilength[%type:[`Start]]moduleReflValue(Value:Metapp.ValueS)=structincludeValueletpeano_of_intzerosucci=iteratezero(funarg->constructsucc[arg])ilettyped_vector_of_listnilconsl=letadd_itemitemacc=constructcons[record[Lident"head",item;Lident"tail",acc]]inList.fold_rightadd_itemlnilletsequence_of_listl=letadd_itemitemacc=tuple[item;acc]inList.fold_rightadd_iteml(construct(Lident"()")[])lettnil=refl_dot"TNil"lettcons=refl_dot"TCons"lettuple_of_listl=typed_vector_of_list(constructtnil[])tconslletrnil=refl_dot"RNil"letrcons=refl_dot"RCons"letrecord_of_listl=typed_vector_of_list(constructrnil[])rconslletcnode=refl_dot"CNode"letcleaf=refl_dot"CLeaf"letrecbinary_choices_of_listl=matchlwith|[]->assertfalse|[leaf]->constructcleaf[leaf]|_->leteven,odd=cutlinconstructcnode[record[(Lident"zero",binary_choices_of_listeven);(Lident"one",binary_choices_of_listodd)]]letvcnil=refl_dot"VCNil"letvccons=refl_dot"VCCons"letvariant_choices_of_listl=typed_vector_of_list(constructvcnil[])vcconslletonil=refl_dot"ONil"letocons=refl_dot"OCons"letobject_methods_of_listl=typed_vector_of_list(constructonil[])oconslletvnil=refl_dot"VNil"letvcons=refl_dot"VCons"letvector_of_listl=typed_vector_of_list(constructvnil[])vconslletsfirst=refl_dot"Start"letsnext=refl_dot"Next"letselection_of_inti=peano_of_int(constructsfirst[])snextiletvfirst=refl_dot"VFirst"letvnext=refl_dot"VNext"letvariable_of_inti=peano_of_int(constructvfirst[])vnextiletcfirst=refl_dot"CFirst"letcnext=refl_dot"CNext"letchoice_of_intisequence=peano_of_int(constructcfirst[sequence])cnextiletcend=refl_dot"CEnd"letczero=refl_dot"CZero"letcone=refl_dot"COne"letbinary_choice_of_intilengthsequence=binary_of_int(funtail->constructczero[tail])(funtail->constructcone[tail])(constructcend[sequence])ilengthletbinary_start=refl_dot"BinaryStart"letzero=refl_dot"Zero"letone=refl_dot"One"letselect=refl_dot"Select"letbinary_selection_of_intilength=binary_of_int(funtailk->tail(constructzero[k]))(funtailk->tail(constructone[k]))(funk->constructselect[k])ilength(constructbinary_start[])lets_zero=refl_dot"Zero"lets_succ=refl_dot"Succ"letlength_of_inti=peano_of_int(constructs_zero[])s_succilets_nil=refl_dot"Nil"lets_append=refl_dot"Add"letappend_of_inti=peano_of_int(constructs_nil[])s_appendiletvtanil=refl_dot"VTANil"letvtacons=refl_dot"VTACons"lettransfer_arguments_of_listl=typed_vector_of_list(constructvtanil[])vtaconslletvtnil=refl_dot"VTNil"letvtcons=refl_dot"VTCons"lettransfer_of_listl=typed_vector_of_list(constructvtnil[])vtconslletenil=refl_dot"ENil"letecons=refl_dot"ECons"letequalities_of_listl=typed_vector_of_list(constructenil[])econslendletappend_type_sequence_of_listle=List.fold_right(funtyacc->[%type:[%tty]*[%tacc]])lelettype_sequence_of_listl=append_type_sequence_of_listl[%type:unit]letrecbinary_type_of_listl=matchlwith|[]->[%type:unit]|[leaf]->[%type:[%tleaf]ref]|_->leteven,odd=cutlin[%type:[%tbinary_type_of_listeven]*[%tbinary_type_of_listodd]]typetype_info={desc_name:string;arity:int;td:Ppxlib.type_declaration;recursive:Ppxlib.Asttypes.rec_flagref;}letrefl_names=s^"_refl"letstructure_names=s^"__structure"letrec_group_names=s^"__rec_group"letarity_names=s^"__arity"letkinds_names=s^"__kinds"letgadt_names=s^"__gadt"lettype_refl_ctors="Refl_"^stypetype_names={refl:string;structure:string;rec_group:string;arity:string;kinds:string;gadt:string;refl_ctor:string;}lettype_names_of_type_nametype_name={refl=refl_nametype_name;structure=structure_nametype_name;rec_group=rec_group_nametype_name;arity=arity_nametype_name;kinds=kinds_nametype_name;gadt=gadt_nametype_name;refl_ctor=type_refl_ctortype_name;}lettype_info_of_type_declarationrecursive(td:Ppxlib.type_declaration)={td;desc_name=refl_nametd.ptype_name.txt;arity=List.lengthtd.ptype_params;recursive;}typefree_variable={index:int;name:string;mutablebound:bool;}moduleStringIndexer=Indexer.Make(String)typecontext={name:stringoption;rec_types:(int*int*type_info)StringMap.toption;vars:StringIndexer.t;fresh_counter:intref;free_var_table:free_variableStringHashtbl.t;free_vars:free_variablelistref;rec_type_refs:IntSet.tref;constraints:Constraints.tref;origin:Constraints.Variables.Path.origin;selector:Constraints.Variables.Path.selector;rev_eqs:Ppxlib.core_typelistref;eqs_counter:intref;type_names:type_names;type_args:stringlist;type_vars:Ppxlib.core_typelist;type_expr:Ppxlib.core_type;exists:Constraints.Transfer.toptionref;gadt_args:Ppxlib.core_typelist;original_vars:Ppxlib.core_typelist;}letvar_of_core_type_opt(ty:Ppxlib.core_type)=matchtywith|[%type:_]->SomeNone|{ptyp_desc=Ptyp_vars;_}->Some(Somes)|_->Noneletvar_of_core_type(ty:Ppxlib.core_type)=matchvar_of_core_type_opttywith|Somevar->var|None->Location.raise_errorf~loc:!Ast_helper.default_loc"Type variable expected but '%a' found"Ppxlib.Pprintast.core_typetyletmake_index(f:'a->stringoption)(l:'alist)(count:int):(int*int*'a)StringMap.t=letadd_type_argiaccarg=letacc=matchfargwith|None->acc|Somevar->StringMap.addvar(i,count,arg)accinaccinListExt.fold_leftiadd_type_argStringMap.emptyllettype_argi=Printf.sprintf"a%d"ilettype_constr_of_string?(args=[])s=Ppxlib.Ast_helper.Typ.constr(Metapp.mkloc(Longident.Lidents))argsletmake_context?namerec_typesoriginal_varsvars=lettype_args=List.init(StringIndexer.countvars)type_arginlettype_vars=List.mapPpxlib.Ast_helper.Typ.vartype_argsinletname_default=Option.value~default:""nameinlettype_expr=type_constr_of_stringname_default~args:type_varsin{name;rec_types;vars;fresh_counter=ref0;free_var_table=StringHashtbl.create7;free_vars=ref[];rec_type_refs=refIntSet.empty;constraints=refConstraints.bottom;origin=[];selector=Direct;rev_eqs=ref[];eqs_counter=ref0;type_names=type_names_of_type_namename_default;type_args;type_vars;type_expr;exists=refNone;gadt_args=type_vars;original_vars;}letcontext_of_type_declaration(td:Ppxlib.type_declaration)rec_types:context=letvars=StringIndexer.of_list(td.ptype_params|>List.map(fun(ty,_)->var_of_core_typety))inmake_context~name:td.ptype_name.txtrec_types(List.mapfsttd.ptype_params)varsletbuiltins_dotfield:Longident.t=Ldot(refl_dot"Builtins",field)letirrefutable()=[Ppxlib.Ast_helper.Exp.case[%pat?_](Ppxlib.Ast_helper.Exp.unreachable())]letstructure_of_tuplestructure_of_typecontext(types:Ppxlib.core_typelist):Ppxlib.core_type*Ppxlib.expression=letarity=List.lengthtypesinlettypes,descs=List.split(List.map(structure_of_typecontext)types)inletmoduleValues(Value:Metapp.ValueS)=structincludeReflValue(Value)letitems=List.initarity(funi->var(itemi))letsequence=sequence_of_listitemslettuple=tupleitemsendinletmoduleValuesExp=Values(Metapp.Exp)inletmoduleValuesPat=Values(Metapp.Pat)inletconstruct=Ppxlib.Ast_helper.Exp.caseValuesPat.sequenceValuesExp.tuple::irrefutable()inletdestruct=Ppxlib.Ast_helper.Exp.caseValuesPat.tupleValuesExp.sequencein[%type:[`Tupleof[%ttype_sequence_of_listtypes]]],[%exprRefl.Tuple{structure=[%eValuesExp.tuple_of_listdescs];construct=[%ePpxlib.Ast_helper.Exp.function_construct];destruct=[%ePpxlib.Ast_helper.Exp.function_[destruct]];}]letrecfor_alli_fromindexplist=matchlistwith|[]->true|hd::tl->pindexhd&&for_alli_from(succindex)ptlletfor_alliplist=for_alli_from0plistlettype_args_regularcontext(args:Ppxlib.core_typelist)=List.lengthargs=StringIndexer.countcontext.vars&&args|>for_allibeginfuni(arg:Ppxlib.core_type)->matchargwith|{ptyp_desc=Ptyp_vars;_}->beginmatchStringIndexer.find_optscontext.varswith|Somej->i=j|None->falseend|_->falseend(*
let lzero = refl_dot "LZero"
let lsucc = refl_dot "LSucc"
let sequence_length_of_int i =
peano_of_int (construct lzero) lsucc i
*)(*
let anil = refl_dot "ANil"
let acons = refl_dot "ACons"
let rec_group_of_list l =
typed_vector_of_list (construct anil) acons l
*)letmake_transferpresentunknowncompose(transfer:Constraints.Variables.transfer)=matchtransferwith|Present->present|Dependdepend->letadd_dependunknownlongident_list=letadd_longidentpresenttxt=composetxtpresentunknowninList.fold_leftadd_longidentpresentlongident_listinList.fold_leftadd_dependunknowndependletcompose_transfertxtpresentunknown=Constraints.Transfer.Constr(txt,present,unknown)letcompose_typetxtpresentunknown=Ppxlib.Ast_helper.Typ.constr(Metapp.mkloctxt)[present;unknown]letcompose_exprtxtpresentunknown=[%expr[%ePpxlib.Ast_helper.Exp.ident(Metapp.mkloctxt)][%epresent][%eunknown]]letvariable_typestype_namearitynameabsent=type_sequence_of_list(List.initaritybeginfuni->Ppxlib.Ast_helper.Typ.constr(Metapp.mkloc(subst_ident(funs->namesi)type_name))[[%type:[`Present]];absenti]end)moduleReflValueExp=ReflValue(Metapp.Exp)moduleReflValuePat=ReflValue(Metapp.Pat)moduleReflValueVal=ReflValue(Metapp.Value)letsubst_free_variables(f:Location.t->stringoption->Ppxlib.core_type)(ty:Ppxlib.core_type):Ppxlib.core_type=letmapper=objectinheritPpxlib.Ast_traverse.mapassupermethod!core_type(ty:Ppxlib.core_type):Ppxlib.core_type=matchvar_of_core_type_opttywith|None->super#core_typety|Somevar->fty.ptyp_locvarendinmapper#core_typetyexceptionExistsofLocation.t*stringoptionlet()=Printexc.register_printer(funexc->matchexcwith|Exists(loc,name)->Some(Format.asprintf"@[Exists@ (@[%a@],@[%a@])@]"Location.print_locloc(Format.pp_print_optionFormat.pp_print_string)name)|_->None)letsubst_type_vars_optmap_locname=Option.bindname@@funname->Option.bind(StringMap.find_optnamemap)@@funindex->Some(Ppxlib.Ast_helper.Typ.var(type_argindex))letsubst_type_varsmaplocname=matchsubst_type_vars_optmaplocnamewith|None->raise(Exists(loc,name))|Someresult->result(*
let subst_type_vars_exists map loc name =
match subst_type_vars_opt map loc name with
| None -> Ppxlib.Ast_helper.Typ.any ()
| Some result -> result
*)letinstantiate_with_freeaccumap_locname=matchnamewith|None->invalid_arg"subst_type_vars_with_free"|Somename->matchStringMap.find_optnamemapwith|Someindex->type_constr_of_string(type_argindex)|None->accu|>Metapp.mutate(StringSet.addname);type_constr_of_stringnameletinstantiate_locvar=matchvarwith|None->failwith"Not implemented: instantiate"|Somevar->type_constr_of_stringvarletstructure_of_constrstructure_of_typecontext?rec_type(constr:Longident.t)(args:Ppxlib.core_typelist):Ppxlib.core_type*Ppxlib.expression=lett,desc=matchrec_typewith|None->context.constraints|>Metapp.mutate(Constraints.add_inherited_kind(subst_identkinds_nameconstr));letstructure=Ppxlib.Ast_helper.Typ.constr(Metapp.mkloc(subst_identstructure_nameconstr))[]inletrec_group_type=Ppxlib.Ast_helper.Typ.constr(Metapp.mkloc(subst_identrec_group_nameconstr))[]inletunwrapped_desc=Ppxlib.Ast_helper.Exp.ident(Metapp.mkloc(subst_identrefl_nameconstr))inlett,desc=[%type:[`RecGroupof[%tstructure]*[%trec_group_type]]],[%exprRecGroup{desc=[%eunwrapped_desc](*;
rec_group = [%e rec_group_expr] *)}]inletarrow=[%type:[%ttype_constr_of_stringcontext.type_names.gadt~args:context.gadt_args]->[%tPpxlib.Ast_helper.Typ.constr(Metapp.mkloc(subst_identgadt_nameconstr))args]]in[%type:[`SubGADTof[%tt]]],[%exprRefl.SubGADT([%edesc]:[%tarrow])]|Some(index,length,{desc_name;recursive;_})->recursive:=Recursive;letarrow=[%type:[%ttype_constr_of_stringcontext.type_names.gadt~args:context.gadt_args]->[%tPpxlib.Ast_helper.Typ.constr(Metapp.mkloc(subst_identgadt_nameconstr))args]]incontext.rec_type_refs|>Metapp.mutate(IntSet.addindex);[%type:[`SubGADTof[`Recof[%tbinary_type_of_intindexlength]]]],[%exprRefl.SubGADT(Refl.Rec{index=[%eReflValueExp.binary_selection_of_intindexlength];desc=[%eident_of_str(Metapp.mklocdesc_name)]}:[%tarrow])]in(*
let t, desc =
match rec_type with
| Some _ -> t, desc
| None ->
begin
let ty =
Ppxlib.Ast_helper.Typ.constr (Metapp.mkloc (subst_ident gadt_name constr))
args in
let ty =
subst_free_variables (subst_type_vars_exists context.vars.map) ty in
let eq_index = !(context.eqs_counter) in
context.eqs_counter := succ eq_index;
context.rev_eqs := ty :: !(context.rev_eqs);
[%type: [`SelectGADT of [%t t] * [%t peano_type_of_int eq_index]]],
[%expr Refl.SelectGADT {
index = [%e ReflValueExp.selection_of_int (succ eq_index)];
desc = [%e desc]
}]
end in
*)lett,desc=iftype_args_regularcontextargs&&matchcontext.namewith|None->true|Somename->constr=Lidentnamethenbeginifrec_type=Nonethencontext.constraints|>Metapp.mutate(funconstraints->args|>ListExt.fold_lefti(funi(constraints:Constraints.t)arg->Constraints.add_variablei([(constr,i)],Direct)constraints)constraints);t,descendelseletargs=args|>List.mapbeginfunarg->letold_ref=context.constraintsinletold_kinds,old_variables=!old_refinletconstraints'=ref(old_kinds,Constraints.Variables.bottom)inletcontext={contextwithconstraints=constraints';origin=[];selector=Direct;}inletstructure=structure_of_typecontextarginletkinds,variables=!constraints'inold_ref:=(kinds,old_variables);structure,variablesendinletargs,variables=List.splitargsinletargs_type,args_expr=List.splitargsinletargs_type=type_sequence_of_listargs_typeinlettransfer_argumentstransfer=ReflValueExp.transfer_arguments_of_list(List.init(StringIndexer.countcontext.vars)transfer)inlettransfer_matrixvariables=lettransfer_positive=transfer_argumentsbeginfunj->Constraints.Variables.make_transfervariablesRightj|>make_transfer[%exprRefl.Transfer][%exprRefl.Skip]compose_exprendinlettransfer_negative=transfer_argumentsbeginfunj->Constraints.Variables.make_transfervariablesLeftj|>make_transfer[%exprRefl.Transfer][%exprRefl.Skip]compose_exprendinlettransfer_direct=transfer_argumentsbeginfunj->Constraints.Variables.make_transfervariablesDirectj|>make_transfer[%exprRefl.Transfer][%exprRefl.Skip]compose_exprendin[%expr{pp=[%etransfer_positive];pn=[%etransfer_negative];np=[%etransfer_negative];nn=[%etransfer_positive];},[%etransfer_direct]]inlettransfer=ReflValueExp.transfer_of_list(List.maptransfer_matrixvariables)inletargs_count=List.lengthargsinletskip_itemnamei=[%exprfun()->[%ePpxlib.Ast_helper.Exp.ident(Metapp.mkloc(map_lident(funconstr->nameconstri)constr))]Refl.VKeepRefl.VSkip]inletmake_skip_vectorlist=ReflValueExp.typed_vector_of_list(ReflValueExp.construct(refl_dot"SKNil")[])(refl_dot"SKCons")listinletskip_positive=make_skip_vector(List.initargs_count(funi->skip_itemConstraints.Variables.positive_namei))inletskip_negative=make_skip_vector(List.initargs_count(funi->skip_itemConstraints.Variables.negative_namei))inletskip_direct=make_skip_vector(List.initargs_count(funi->skip_itemConstraints.Variables.direct_namei))inlettransfer=[%exprTransfer_skip{transfer_vector=[%etransfer];skip_positive=[%eskip_positive];skip_negative=[%eskip_negative];skip_direct=[%eskip_direct]}]inletnb_args=List.lengthargsinletvariable_typesname=variable_typesconstrnb_argsname(fun_->[%type:[`Absent]])inletsubpositive=variable_typesConstraints.Variables.positive_nameinletsubnegative=variable_typesConstraints.Variables.negative_nameinletsubdirect=variable_typesConstraints.Variables.direct_nameinletarity=StringIndexer.countcontext.varsinletarguments=type_sequence_of_list(variables|>List.mapbeginfunvariables->letargument_positive=type_sequence_of_list(List.initaritybeginfunj->Constraints.Variables.make_transfervariablesRightj|>make_transfer[%type:[`Present]][%type:[`Absent]]compose_typeend)inletargument_negative=type_sequence_of_list(List.initaritybeginfunj->Constraints.Variables.make_transfervariablesLeftj|>make_transfer[%type:[`Present]][%type:[`Absent]]compose_typeend)inletargument_direct=type_sequence_of_list(List.initaritybeginfunj->Constraints.Variables.make_transfervariablesDirectj|>make_transfer[%type:[`Present]][%type:[`Absent]]compose_typeend)in[%type:[%targument_positive]*[%targument_negative]*[%targument_direct]]end)inlett=[%type:[`Applyof[%tt]*[%targs_type]*[%tsubpositive]*[%tsubnegative]*[%tsubdirect]*[%targuments]]]inletdesc=[%exprRefl.Apply{arguments=[%eReflValueExp.vector_of_listargs_expr];desc=[%edesc];transfer=[%etransfer];}]inifrec_type=Nonethencontext.constraints|>Metapp.mutate(funconstraints->variables|>ListExt.fold_lefti(funi(constraints:Constraints.t)variables->IntMap.fold(funjpath_setconstraints->Constraints.Variables.PathSet.fold(fun(origin,selector)->letorigin=(constr,i)::origininConstraints.add_variablej(origin,selector))path_setconstraints)variablesconstraints)constraints);t,descint,descletexpr_of_strings=Ppxlib.Ast_helper.Exp.constant(Ppxlib.Ast_helper.Const.strings)letstructure_of_row_fieldstructure_of_typecontext(row_field:Ppxlib.row_field):Ppxlib.core_type*Ppxlib.expression=Ppxlib.Ast_helper.with_default_loc(Metapp.Rf.to_locrow_field)@@fun()->matchMetapp.Rf.destructrow_fieldwith|Rtag(label,_,args)->letstructure,desc=matchargswith|[]->[%type:unit],[%exprVNone]|arg::_->letstructure,desc=structure_of_typecontextargin[%type:[%tstructure]*unit],[%exprVSome[%edesc]]in[%type:[`Constrof[%tstructure]]],[%exprRefl.VConstructor{name=[%eexpr_of_stringlabel.txt];argument=[%edesc]}]|Rinheritty->letstructure,desc=structure_of_typecontexttyin[%type:[`Inheritof[%tstructure]]],[%exprRefl.VInherit[%edesc]]letaccessors_of_row_field(ty:Ppxlib.core_typeLazy.t)i(row_field:Ppxlib.row_field):Ppxlib.case*Ppxlib.case=letarg="arg"inPpxlib.Ast_helper.with_default_loc(Metapp.Rf.to_locrow_field)@@fun()->letmoduleValues(Value:Metapp.ValueS)=structincludeReflValue(Value)letsequence,variant=matchMetapp.Rf.destructrow_fieldwith|Rtag(label,_,[])->sequence_of_list[],variantlabel.txtNone|Rtag(label,_,_)->letident=vararginsequence_of_list[ident],variantlabel.txt(Someident)|Rinherit{ptyp_desc=Ptyp_constr(type_name,_);_}->letpat()=Ppxlib.Ast_helper.Pat.alias(Ppxlib.Ast_helper.Pat.type_type_name)(Metapp.mklocarg)inletexpr()=[%expr([%eReflValueExp.vararg]:>[%tLazy.forcety])]invararg,choiceexprpat|_->Location.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"refl cannot be derived for such polymorphic variants"letchoice=choice_of_intisequenceendinletmoduleValuesExp=Values(Metapp.Exp)inletmoduleValuesPat=Values(Metapp.Pat)inPpxlib.Ast_helper.Exp.caseValuesPat.choiceValuesExp.variant,Ppxlib.Ast_helper.Exp.caseValuesPat.variantValuesExp.choiceletstructure_of_variantstructure_of_typecontext(fields:Ppxlib.row_fieldlist):Ppxlib.core_type*Ppxlib.expression=letcases=List.map(structure_of_row_fieldstructure_of_typecontext)fieldsinlettypes,descs=List.splitcasesinletty=lazybeginletfields=fields|>List.mapbeginfun(field:Ppxlib.row_field)->matchMetapp.Rf.destructfieldwith|Rtag(label,_,list)->letlist=matchlistwith|[]->[]|_::_->[[%type:_]]inMetapp.Rf.taglabelfalselist|Rinherit_->fieldendinPpxlib.Ast_helper.Typ.variantfieldsClosedNoneendinletaccessors=List.mapi(accessors_of_row_fieldty)fieldsinletconstruct,destruct=List.splitaccessorsinletconstruct=construct@irrefutable()in[%type:[`Variantof[%ttype_sequence_of_listtypes]]],[%exprRefl.Variant{constructors=[%eReflValueExp.variant_choices_of_listdescs];construct=[%ePpxlib.Ast_helper.Exp.function_construct];destruct=[%ePpxlib.Ast_helper.Exp.function_destruct];}]letstructure_of_builtins_or_constrstructure_of_typecontext(ty:Ppxlib.core_type)(constr:Longident.t)(args:Ppxlib.core_typelist):Ppxlib.core_type*Ppxlib.expression=letty=matchty.ptyp_descwith|Ptyp_constr(lid,args)->beginmatchremove_ident_prefix_opt(Lident"Stdlib")lid.txtwith|None->ty|Sometxt->{tywithptyp_desc=Ptyp_constr({lidwithtxt},args)}end|_->tyinmatch{tywithptyp_attributes=[]}with|[%type:bool]|[%type:Bool.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Bool");[%type:[`Builtinof[`Bool]]],[%exprRefl.BuiltinRefl.Bool]|[%type:bytes]|[%type:Bytes.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Bytes");[%type:[`Builtinof[`Bytes]]],[%exprRefl.BuiltinRefl.Bytes]|[%type:char]|[%type:Char.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Char");[%type:[`Builtinof[`Char]]],[%exprRefl.BuiltinRefl.Char]|[%type:float]|[%type:Float.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Float");[%type:[`Builtinof[`Float]]],[%exprRefl.BuiltinRefl.Float]|[%type:int]|[%type:Int.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Int");[%type:[`Builtinof[`Int]]],[%exprRefl.BuiltinRefl.Int]|[%type:int32]|[%type:Int32.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Int32");[%type:[`Builtinof[`Int32]]],[%exprRefl.BuiltinRefl.Int32]|[%type:int64]|[%type:Int64.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Int64");[%type:[`Builtinof[`Int64]]],[%exprRefl.BuiltinRefl.Int64]|[%type:nativeint]|[%type:Nativeint.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Nativeint");[%type:[`Builtinof[`Nativeint]]],[%exprRefl.BuiltinRefl.Nativeint]|[%type:string]|[%type:String.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"String");[%type:[`Builtinof[`String]]],[%exprRefl.BuiltinRefl.String]|[%type:unit]->structure_of_constrstructure_of_typecontext(builtins_dot"unit")args|[%type:[%t?subtype]array]|[%type:[%t?subtype]Array.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Array");letstructure,desc=structure_of_typecontextsubtypein[%type:[`Arrayof[%tstructure]]],[%exprArray[%edesc]]|[%type:[%t?_]list]|[%type:[%t?_]List.t]->structure_of_constrstructure_of_typecontext(builtins_dot"list")args|[%type:([%t?_],[%t?_])result]|[%type:([%t?_],[%t?_])Result.t]->structure_of_constrstructure_of_typecontext(builtins_dot"result")args|[%type:[%t?_]option]|[%type:[%t?_]Option.t]->structure_of_constrstructure_of_typecontext(builtins_dot"option")args|[%type:[%t?_]ref]->structure_of_constrstructure_of_typecontext(builtins_dot"ref")args|[%type:[%t?ty]Lazy.t]->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Lazy");letty,desc=structure_of_typecontexttyin[%type:[`Lazyof[%tty]]],[%exprRefl.Lazy[%edesc]]|_->structure_of_constrstructure_of_typecontextconstrargsletfind_rec_typecontextconstr=matchcontext.rec_types,constrwith|Somerec_types,Longident.Lidentname->StringMap.find_optnamerec_types|_->Noneletfree_variablecontext=letindex=!(context.fresh_counter)incontext.fresh_counter:=succindex;letvar={index;name=Printf.sprintf"free%d"index;bound=false}incontext.free_vars:=var::!(context.free_vars);varletname_free_variablecontexts=matchStringHashtbl.find_optcontext.free_var_tableswith|Somevar->var|None->letvar=free_variablecontextinStringHashtbl.addcontext.free_var_tablesvar;varletstructure_of_arrowstructure_of_typecontext(label:Ppxlib.Asttypes.arg_label)parameterresult=letlabel_desc=matchlabelwith|Nolabel->None|Labelleds->Some(false,s)|Optionals->Some(true,s)inletparameter=matchlabelwith|Nolabel|Labelled_->parameter|Optional_->[%type:[%tparameter]option]inletparameter_structure,parameter_desc=letcontext={contextwithselector=Constraints.Variables.Path.leftcontext.selector}instructure_of_typecontextparameterinletresult_structure,result_desc=letcontext={contextwithselector=Constraints.Variables.Path.rightcontext.selector}instructure_of_typecontextresultinbeginmatchlabel_descwith|None->[%type:[`Arrowof[%tparameter_structure]->[%tresult_structure]]],[%exprRefl.Arrow{parameter=[%eparameter_desc];result=[%eresult_desc];}]|Some(optional,s)->[%type:[`LabelledArrowof[%tparameter_structure]->[%tresult_structure]]],[%exprRefl.LabelledArrow{label=[%eexpr_of_strings];optional=[%eifoptionalthen[%exprtrue]else[%exprfalse]];parameter=[%eparameter_desc];result=[%eresult_desc];wrap=(funf->[%ePpxlib.Ast_helper.Exp.fun_labelNone[%pat?x][%exprfx]]);unwrap=(funfx->[%ePpxlib.Ast_helper.Exp.apply[%exprf][label,[%exprx]]]);}]endletstructure_of_object_fieldstructure_of_typecontext(object_field:Metapp.Of.t):(Ppxlib.core_type*Ppxlib.expression)*((Ppxlib.pattern*Ppxlib.class_field)*Ppxlib.expression)=letloc=Metapp.Of.to_locobject_fieldinPpxlib.Ast_helper.with_default_locloc@@fun()->matchMetapp.Of.destructobject_fieldwith|Otag(label,argument)->letstructure,desc=structure_of_typecontextargumentinletstructure=[%type:[`Methodof[%tstructure]]],[%exprRefl.OMethod{name=[%eexpr_of_stringlabel.txt];desc=[%edesc]}]inletconstruct=((Ppxlib.Ast_helper.Pat.varlabel,Ppxlib.Ast_helper.Cf.method_labelPublic(Ppxlib.Ast_helper.Cf.concreteFresh[%expr[%ePpxlib.Ast_helper.Exp.ident(Metapp.lid_of_strlabel)]()])),[%exprfun()->[%eMetapp.Exp.send[%exprc]label]])instructure,construct|Oinherit_->Location.raise_errorf~loc"ppx_refl does not support object inheritance"letdelays_dot=refl_dot"Delays"letstructure_of_objectstructure_of_typecontext(fields:Metapp.Of.tlist):Ppxlib.core_type*Ppxlib.expression=letmethods=List.map(structure_of_object_fieldstructure_of_typecontext)fieldsinletstructures,constructs=List.splitmethodsinlettypes,descs=List.splitstructuresinletconstruct,destruct=List.splitconstructsinletpatterns,results=List.splitconstructinletconstruct=[Ppxlib.Ast_helper.Exp.case(ReflValuePat.list~prefix:delays_dotpatterns)(Ppxlib.Ast_helper.Exp.object_(Ppxlib.Ast_helper.Cstr.mk[%pat?_]results))]inletdestruct=[Ppxlib.Ast_helper.Exp.case(ReflValuePat.var"c")(ReflValueExp.list~prefix:delays_dotdestruct)]in[%type:[`Objectof[%ttype_sequence_of_listtypes]]],[%exprRefl.Object{methods=[%eReflValueExp.object_methods_of_listdescs];construct=[%ePpxlib.Ast_helper.Exp.function_construct];destruct=[%ePpxlib.Ast_helper.Exp.function_destruct];}]letmake_variablesvariable_countvariablesselectore=letlist=List.initvariable_countbeginfuni->Constraints.Variables.make_transfervariablesselectori|>make_transfer[%type:[`Present]][%type:[`Absent]]compose_typeendinappend_type_sequence_of_listlisteletmake_presencesvariable_countvariables=iterate_i[%exprRefl.Presences](funiacc->Constraints.Variables.make_transfervariablesDirecti|>make_transfer[%exprRefl.AddPresent[%eacc]][%exprRefl.AddAbsent[%eacc]]compose_expr)variable_countletconstructor_of_attr_nameattr_name=Printf.sprintf"Attribute_%s"attr_nameletreclid_of_rev_pathrev_pathname:Longident.t=matchrev_pathwith|[]->Lidentname|head::tail->Ldot(lid_of_rev_pathtailhead,name)letlid_of_attr_nameattr_name=matchList.rev(String.split_on_char'.'attr_name)with|[]->assertfalse|head::tail->lid_of_rev_path(List.mapString.capitalize_asciitail)(constructor_of_attr_namehead)letmake_arity_typesarity=type_sequence_of_list(List.initarity(funi->type_constr_of_string(type_argi)))letmake_attributescontexttyattributes:Ppxlib.expression=letcases=attributes|>List.mapbeginfun(attribute:Ppxlib.attribute)->letname=lid_of_attr_name(Metapp.Attr.nameattribute).txtinletname:Longident.t=matchnamewith|Ldot(Lident"Ocaml",attr)->Ldot(Ldot(Lident"Refl","Ocaml_attributes"),attr)|_->nameinletexpr=Metapp.Exp.of_payload(Metapp.Attr.payloadattribute)inPpxlib.Ast_helper.Exp.case(Metapp.Pat.constructname[])[%exprSome[%eexpr]]endinletcases=cases@[Ppxlib.Ast_helper.Exp.case(Ppxlib.Ast_helper.Pat.any())[%exprNone]]inletaccu=refStringSet.emptyinletty=subst_free_variables(instantiate_with_freeaccucontext.vars.map)tyinletarity_types=make_arity_types(StringIndexer.countcontext.vars)inletforall_types=List.mapMetapp.mkloc("__attribute"::StringSet.elements!accu)in[%expr{Refl.typed=[%eList.fold_rightMetapp.Exp.newtypeforall_types(Ppxlib.Ast_helper.Exp.constraint_(Ppxlib.Ast_helper.Exp.function_cases)[%type:([%tty],[%tarity_types],__attribute)Refl.typed_attribute_kind->__attributeoption])]}]lettransform_attrcontextstructuredesc(ty:Ppxlib.core_type)=matchty.ptyp_attributeswith|[]->structure,desc|attr->letattributes=make_attributescontexttyattrin[%type:[`Attributesof[%tstructure]]],[%exprRefl.Attributes{attributes=[%eattributes];desc=[%edesc];}]letrecstructure_of_typecontext(ty:Ppxlib.core_type):Ppxlib.core_type*Ppxlib.expression=Ppxlib.Ast_helper.with_default_locty.ptyp_loc@@fun()->lettransformty=matchtywith|[%type:_]->letvar=free_variablecontextincontext.constraints|>Metapp.mutatebeginConstraints.add_variablevar.index(context.origin,context.selector)end;Ppxlib.Ast_helper.Typ.varvar.name,ident_of_str(Metapp.mklocvar.name)|{ptyp_desc=Ptyp_vars;_}->beginmatchStringIndexer.find_optscontext.varswith|Somei->context.constraints|>Metapp.mutatebeginfunc->c|>Constraints.add_direct_kind"Variable"|>Constraints.add_variablei(context.origin,context.selector)end;[%type:[`Variableof[%tpeano_type_of_inti]]],[%exprRefl.Variable[%eReflValueExp.variable_of_inti]]|None->letvar=name_free_variablecontextsincontext.constraints|>Metapp.mutatebeginConstraints.add_variablevar.index(context.origin,context.selector)end;Ppxlib.Ast_helper.Typ.varvar.name,ident_of_str(Metapp.mklocvar.name)end|{ptyp_desc=Ptyp_arrow(label,parameter,result);_}->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Arrow");structure_of_arrowstructure_of_typecontextlabelparameterresult|{ptyp_desc=Ptyp_tupletypes;_}->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Tuple");structure_of_tuplestructure_of_typecontexttypes|{ptyp_desc=Ptyp_constr(constr,args);_}->beginmatchfind_rec_typecontextconstr.txt,Metapp.Attr.find"nobuiltin"ty.ptyp_attributeswith|None,None->structure_of_builtins_or_constrstructure_of_typecontexttyconstr.txtargs|rec_type,_->structure_of_constrstructure_of_typecontextconstr.txtargs?rec_typeend|{ptyp_desc=Ptyp_variant(fields,_,_);_}->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Variant");structure_of_variantstructure_of_typecontextfields|{ptyp_desc=Ptyp_object(methods,closed_flag);_}->ifclosed_flag=OpenthenLocation.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"Open object types are not supported by ppx_refl";context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Object");structure_of_objectstructure_of_typecontextmethods|{ptyp_desc=Ptyp_alias(ty,name);_}->letvar=name_free_variablecontextnameinvar.bound<-true;letstructure,desc=structure_of_typecontexttyinPpxlib.Ast_helper.Typ.aliasstructurevar.name,Ppxlib.Ast_helper.Exp.let_Recursive[Ppxlib.Ast_helper.Vb.mk(Metapp.Pat.varvar.name)desc](Metapp.Exp.varvar.name)|{ptyp_desc=Ptyp_poly(vars,ty);_}->letcontext={contextwithvars=context.vars|>StringIndexer.union(StringIndexer.of_list(vars|>List.map(funvar->Some(Metapp.Typ.poly_namevar))))}instructure_of_typecontextty|_->Location.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"Unsupported type"inmatchMetapp.Attr.chop"opaque"ty.ptyp_attributeswith|Some(_,attributes)->letty={tywithptyp_attributes=attributes}inletty=subst_free_variables(subst_type_varscontext.vars.map)tyincontext.constraints|>Metapp.mutate(Constraints.add_direct_kind"Opaque");leteq_index=!(context.eqs_counter)incontext.eqs_counter:=succeq_index;context.rev_eqs:=ty::!(context.rev_eqs);letstructure=[%type:[`Opaqueof[%tpeano_type_of_inteq_index]]]inletdesc=[%exprRefl.Opaque[%eReflValueExp.selection_of_int(succeq_index)]]intransform_attrcontextstructuredescty|_->matchMetapp.Attr.chop"mapopaque"ty.ptyp_attributeswith|None->letstructure,desc=transformtyintransform_attrcontextstructuredescty|Some(_,attributes)->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"MapOpaque");letkinds=fst!(context.constraints)inletty={tywithptyp_attributes=attributes}inletstructure,desc=transformtyinletstructure,desc=transform_attrcontextstructuredesctyincontext.constraints:=(kinds,snd!(context.constraints));letvariable_count=StringIndexer.countcontext.varsinletvariables=snd!(context.constraints)inletdirect_type=make_variablesvariable_countvariablesDirect[%type:unit]in[%type:[`MapOpaqueof[%tstructure]*[%tdirect_type]]],[%exprRefl.MapOpaque{desc=[%edesc]}]letfold_free_variables(f:Location.t->stringoption->'acc->'acc)(ty:Ppxlib.core_type)(acc:'acc):'acc=letfold=objectinherit['acc]Ppxlib.Ast_traverse.foldassupermethod!core_typetyacc=matchvar_of_core_type_opttywith|None->super#core_typetyacc|Somevar->fty.ptyp_locvaraccendinfold#core_typetyaccletfold_map_free_variables(f:Location.t->stringoption->'acc->Ppxlib.core_type*'acc)(ty:Ppxlib.core_type)(acc:'acc):Ppxlib.core_type*'acc=letfold_map=objectinherit['acc]Ppxlib.Ast_traverse.fold_mapassupermethod!core_typetyacc=matchvar_of_core_type_opttywith|None->super#core_typetyacc|Somevar->fty.ptyp_locvaraccendinfold_map#core_typetyaccletextract_gadt_equalitiescontext(constructor:Ppxlib.constructor_declaration)=matchconstructor.pcd_reswith|None->[],context|Somety->letargs=matchtywith|{ptyp_desc=Ptyp_constr({txt=Lidentname;_},args);_}whenSomename=context.name->args|_->Location.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"Type constructor '%s' expected"(Option.getcontext.name)inletarg_count=List.lengthargsinletarity=StringIndexer.countcontext.varsinifarg_count<>aritythenLocation.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"Type constructor '%s' has %d parameters but %d arguments given"(Option.getcontext.name)arityarg_count;letadd_eq(eqs,vars)arg=matchvar_of_core_type_optargwith|SomeNone->let(_,vars)=StringIndexer.freshvarsin(eqs,vars)|Some(Somevar)whennot(StringIndexer.memvarvars)->let(_,vars)=StringIndexer.force_addvarvarsin(eqs,vars)|_->let(index,vars)=StringIndexer.freshvarsin((index,arg)::eqs,vars)inleteqs,vars=List.fold_leftadd_eq([],StringIndexer.empty)argsinleteqs=eqs|>List.mapbeginfun(index,ty)->leta=Ppxlib.Ast_helper.Typ.var(type_argindex)inletb=subst_free_variables(subst_type_varsvars.map)tyin[%type:([%ta],[%tb])Refl.eq]endinifeqs<>[]thencontext.constraints|>Metapp.mutate(Constraints.add_direct_kind"GADT");eqs,{contextwithvars}letargs_of_constructor(constructor:Ppxlib.constructor_declaration):Ppxlib.core_typelist=matchconstructor.pcd_argswith|Pcstr_tupleitems->items|Pcstr_recordlabels->List.map(fun(label:Ppxlib.label_declaration)->label.pld_type)labelsletvariables_typenamearitysign=type_sequence_of_list(List.initarity(funi->type_constr_of_string(signnamei)~args:[[%type:[`Present]];[%type:[`Absent]]]))typevariables_structure={arity_types:Ppxlib.core_type;count_length:Metapp.value;count_append:Metapp.value;variables:Ppxlib.expression;positives:Ppxlib.core_type;negatives:Ppxlib.core_type;directs:Ppxlib.core_type;positive:Ppxlib.core_type;negative:Ppxlib.core_type;direct:Ppxlib.core_type;}letmake_variables_structurecontextvariable_countvariables=letpresences=make_presencesvariable_countvariablesinletcount_length=ReflValueVal.length_of_intvariable_countinletcount_append=ReflValueVal.append_of_intvariable_countinletinitial_arity=StringIndexer.countcontext.varsinletarity_types=make_arity_typesinitial_arityin{arity_types;count_length;count_append;variables=[%expr{presences=[%epresences];positive_count=[%ecount_length.exp];positive=[%ecount_append.exp];negative_count=[%ecount_length.exp];negative=[%ecount_append.exp];direct_count=[%ecount_length.exp];direct=[%ecount_append.exp];}];positives=make_variablesvariable_countvariablesRight[%type:unit];negatives=make_variablesvariable_countvariablesLeft[%type:unit];directs=make_variablesvariable_countvariablesDirect[%type:unit];positive=make_variablesvariable_countvariablesRight(variables_type(Option.getcontext.name)initial_arityConstraints.Variables.positive_name);negative=make_variablesvariable_countvariablesLeft(variables_type(Option.getcontext.name)initial_arityConstraints.Variables.negative_name);direct=make_variablesvariable_countvariablesDirect(variables_type(Option.getcontext.name)initial_arityConstraints.Variables.direct_name);}letis_singletonlist=matchlistwith|[_]->true|_->falseletempty_type_annotation=Ppxlib.Asttypes.NoVariance,Ppxlib.Asttypes.NoInjectivityletstructure_of_label_declarationcontextprefixsingle_label(label:Ppxlib.label_declaration)item=matchlabel.pld_typewith|{ptyp_desc=Ptyp_poly(vars,field_type);_}->context.constraints|>Metapp.mutate(Constraints.add_direct_kind"Poly");letfree_variables=StringIndexer.of_list(List.map(funv->Some(Metapp.Typ.poly_namev))vars)inletvars=context.vars|>StringIndexer.unionfree_variablesinletcontext'={contextwithvars;constraints=refConstraints.bottom}inletfield_structure,field_desc=structure_of_typecontext'field_typeinletvariables=snd!(context'.constraints)incontext.constraints:=Constraints.union!(context.constraints)!(context'.constraints);letcount=StringIndexer.countfree_variablesinletcount_type=peano_type_of_intcountinlet{arity_types;count_length;count_append;variables;positives;negatives;directs;positive;negative;direct}=make_variables_structurecontextcountvariablesinletstructure=[%type:[`Polyof[%tfield_structure]*[%tcount_type]*[%tpositives]*[%tnegatives]*[%tdirects]]]inlettype_args=List.maptype_constr_of_stringcontext.type_argsinletkinds=type_constr_of_stringcontext.type_names.kindsinletrec_group=type_constr_of_stringcontext.type_names.rec_groupinletgadt=type_constr_of_stringcontext.type_names.gadt~args:type_argsinletinternal_name,internal_label,type_declarations=ifsingle_labelthenOption.getcontext.name,label.pld_name,[]elseletinternal_name=Printf.sprintf"%s__%s"prefixlabel.pld_name.txtinletinternal_name_str=Metapp.mklocinternal_nameinlettype_declaration=Ppxlib.Ast_helper.Type.mkinternal_name_str~params:(List.map(funx->x,empty_type_annotation)context.original_vars)~kind:(Ptype_record[Ppxlib.Ast_helper.Type.fieldinternal_name_strlabel.pld_type])ininternal_name,internal_name_str,[type_declaration]inletdestructed=ReflValueVal.record[Lidentinternal_label.txt,item]inletinternal_type=type_constr_of_stringinternal_name~args:type_argsinletdesc=[%exprletsubstructure=[%efield_desc]inletforall_destruct:typeforallsubarity.([%tcount_type],forall)Refl.length->(forall,[%tarity_types],subarity)Refl.append->([%tinternal_type],[%tfield_structure],subarity,[%trec_group],[>[%tkinds]],[%tpositive],[%tnegative],[%tdirect],[%tgadt])Refl.forall_destruct_result=fun[%pcount_length.pat][%pcount_append.pat]->Refl.ForallDestruct{desc=substructure;destruct=fun[%pPpxlib.Ast_helper.Pat.record[Metapp.lid_of_strinternal_label,Ppxlib.Ast_helper.Pat.varinternal_label]Closed]->[%ePpxlib.Ast_helper.Exp.ident(Metapp.lid_of_strinternal_label)];}inRefl.Poly{label=[%eexpr_of_stringlabel.pld_name.txt];variables=[%evariables];destruct={forall_destruct};construct=fun{forall_construct}->[%ePpxlib.Ast_helper.Exp.record[Metapp.lid_of_strinternal_label,[%exprfunx->forall_construct[%ecount_length.exp][%ecount_append.exp]substructurex]]None]}]in(structure,desc),((destructed,internal_type),type_declarations)|field_type->letfield_structure,field_desc=structure_of_typecontextfield_typeinletstructure=[%type:[`Monoof[%tfield_structure]]]inletdesc=[%exprRefl.Mono{label=[%eexpr_of_stringlabel.pld_name.txt];desc=[%efield_desc];attributes=[%emake_attributescontextfield_typelabel.pld_attributes];}]in(structure,desc),((item,field_type),[])letmake_constructor_kindcontext(constructor:Ppxlib.constructor_declaration)(args:Ppxlib.core_typelist):Metapp.valuelist*Ppxlib.core_type*Ppxlib.core_typelist*Ppxlib.expression*Metapp.valuelist*Ppxlib.type_declarationlist=letitems=List.mapi(funi_->Metapp.Value.var(itemi))argsinmatchconstructor.pcd_argswith|Pcstr_tuple_->letstructures=List.map(structure_of_typecontext)argsinlettypes,descs=List.splitstructuresinletarg_types=args|>List.map(subst_free_variables(subst_type_varscontext.vars.map))initems,[%type:[`Tupleof[%ttype_sequence_of_listtypes]]],arg_types,[%exprRefl.CTuple[%eReflValueExp.tuple_of_listdescs]],items,[]|Pcstr_recordlabels->letsingle_label=is_singletonlabelsinletprefix=Printf.sprintf"%s__%s"(Option.getcontext.name)constructor.pcd_name.txtinletstructures=List.map2(structure_of_label_declarationcontextprefixsingle_label)labelsitemsinletstructures,destructs=List.splitstructuresinlettypes,descs=List.splitstructuresinletdestructs,type_declarations=List.splitdestructsinletdestructs,arg_types=List.splitdestructsinlettype_declarations=List.flattentype_declarationsinletarg_types=arg_types|>List.map(subst_free_variables(subst_type_varscontext.vars.map))initems,[%type:[`Recordof[%ttype_sequence_of_listtypes]]],arg_types,[%exprRefl.CRecord[%eReflValueExp.record_of_listdescs]],destructs,type_declarationsletmake_constructor_args(constructor:Ppxlib.constructor_declaration)items=matchitemswith|[]->None|_::_->letargs=matchconstructor.pcd_argswith|Pcstr_tuple_->Metapp.Value.tupleitems|Pcstr_recordlabels->letfields=List.map2beginfun(label:Ppxlib.label_declaration)x->Longident.Lidentlabel.pld_name.txt,xendlabelsitemsinMetapp.Value.recordfieldsinSomeargslettuple_of_typestypes=matchtypeswith|[]->[%type:unit]|[ty]->ty|_->Ppxlib.Ast_helper.Typ.tupletypesletrecfold_map_auxflistacc_listaccu=matchlistwith|[]->(List.revacc_list,accu)|head::tail->let(value,accu)=fheadaccuinfold_map_auxftail(value::acc_list)acculetfold_mapflistaccu=fold_map_auxflist[]acculetstructure_of_existssingle_constructorctor_counticontext(constructor:Ppxlib.constructor_declaration)(result:Ppxlib.core_type):(((Ppxlib.core_type*Ppxlib.expression)*Ppxlib.core_type)*(Ppxlib.case*Ppxlib.case))*(Ppxlib.type_declarationlist*Ppxlib.type_extensionlist)=letresult_args=matchresultwith|{ptyp_desc=Ptyp_constr(_,args);_}->args|_->assertfalseinletadd_arg(parameters,vars)arg=matchvar_of_core_type_optargwith|SomeNone->let(_,vars)=StringIndexer.freshvarsin(parameters,vars)|Some(Somevar)->beginmatchStringIndexer.find_optvarvarswith|None->let(_,vars)=StringIndexer.force_addvarvarsin(parameters,vars)|Someindex'->let(index,vars)=StringIndexer.freshvarsin((index,arg)::(index',arg)::parameters,vars)end|_->let(index,vars)=StringIndexer.freshvarsin((index,arg)::parameters,vars)inlet(parameters,vars)=List.fold_leftadd_arg([],StringIndexer.empty)result_argsinletcheck_free_variable_locvarindexer=let(var,indexer)=matchvarwith|None->letvar=Printf.sprintf"free_var__%d"(StringIndexer.countindexer)inlet(_,indexer)=StringIndexer.force_addvarindexerin(var,indexer)|Somevar->ifStringIndexer.memvarvarsthen(var,indexer)elselet(_,indexer)=StringIndexer.addvarindexerin(var,indexer)in(Ppxlib.Ast_helper.Typ.varvar,indexer)inletargs=args_of_constructorconstructorinlet(parameters,renamed_args),free_variables=let(parameters,indexer)=fold_mapbeginfun(index,arg)indexer->let(arg,indexer)=fold_map_free_variablescheck_free_variableargindexerin((index,arg),indexer)endparametersStringIndexer.emptyinlet(renamed_args,indexer)=fold_map(fold_map_free_variablescheck_free_variable)argsindexerin((parameters,renamed_args),indexer)inletvars=vars|>StringIndexer.unionfree_variablesinletbranch_name=Printf.sprintf"%s_%s"(Option.getcontext.name)constructor.pcd_name.txtinletcontext'={contextwithvars;constraints=refConstraints.bottom;gadt_args=result_args;}inletitems,structure,_types,kind,destructs,type_declarations=make_constructor_kindcontext'constructorrenamed_argsinletfree_variable_count=StringIndexer.countfree_variablesincontext.constraints:=Constraints.union!(context.constraints)(Constraints.offset_variablesfree_variable_count!(context'.constraints));leteq_index=!(context.eqs_counter)incontext.eqs_counter:=eq_index+1;letconstructor_args=make_constructor_argsconstructoritemsinletconstructor_with_args=Metapp.Value.force_construct(Metapp.mkloc(Longident.Lidentconstructor.pcd_name.txt))constructor_argsinletcount=peano_type_of_intfree_variable_countinletcomposed,value_type_name,type_declarations=ifsingle_constructorthenconstructor_with_args,Option.getcontext.name,type_declarationselseletbranch_constructor=String.capitalize_asciibranch_nameinletres=type_constr_of_stringbranch_name~args:result_argsinletkind=Ppxlib.Ptype_variant[Ppxlib.Ast_helper.Type.constructor(Metapp.mklocbranch_constructor)~args:(Pcstr_tupleargs)~res]inMetapp.Value.construct(Lidentbranch_constructor)items,branch_name,Ppxlib.Ast_helper.Type.mk(Metapp.mklocbranch_name)~kind~params:(List.map(funx->x,empty_type_annotation)context.type_vars)::type_declarationsinlettype_args=List.maptype_constr_of_stringcontext.type_argsinletvalue_type=type_constr_of_stringvalue_type_name~args:type_argsinletparameter_types=parameters|>List.mapbeginfun(index,_)->type_constr_of_string(type_argindex)endinletparameter_type_tuple=tuple_of_typesparameter_typesinletdecomposed=ReflValueVal.sequence_of_listdestructsinletparameter_tuple=tuple_of_types(parameters|>List.map(fun(_index,parameter)->subst_free_variables(subst_type_varsvars.map)parameter))inletparameter_sequence=type_sequence_of_list(List.initfree_variable_count(funi->Ppxlib.Ast_helper.Typ.var(type_argi)))inlettype_extensions,constraints_pattern=ifparameters=[]then[],ReflValueVal.construct(refl_dot"NoConstraints")[]elseletconstraints=Printf.sprintf"Constraints_%s"branch_nameinletconstraints_pattern=ReflValueVal.construct(Lidentconstraints)[]in[(Ppxlib.Ast_helper.Te.mk(Metapp.mkloc(refl_dot"gadt_constraints"))~params:[[%type:_],empty_type_annotation;[%type:_],empty_type_annotation][Ppxlib.Ast_helper.Te.decl(Metapp.mklocconstraints)~res:[%type:([%tparameter_tuple],[%tparameter_sequence])Refl.gadt_constraints]])],constraints_patterninletparameter_type_vars=parameters|>List.mapbeginfun(index,_)->Ppxlib.Ast_helper.Typ.var(type_argindex)endinletparameter_type_vars_tuple=tuple_of_typesparameter_type_varsincontext.rev_eqs:=parameter_type_vars_tuple::!(context.rev_eqs);letkinds=type_constr_of_stringcontext.type_names.kindsinletrec_group=type_constr_of_stringcontext.type_names.rec_groupinletgadt=type_constr_of_stringcontext.type_names.gadt~args:type_argsinletvariables=snd!(context'.constraints)incontext.exists:=Somebeginletprevious=match!(context.exists)with|Someprevious->previous|None->Absentiniterate_iprevious(funiacc->Constraints.Variables.make_transfervariablesDirecti|>make_transferConstraints.Transfer.Presentacccompose_transfer)free_variable_countend;letpresence_type=iterate_i[%type:[`Absent]](funiacc->Constraints.Variables.make_transfervariablesDirecti|>make_transfer[%type:[`Present]]acccompose_type)free_variable_countinletpresence_expr=iterate_i[%exprRefl.Absent](funiacc->Constraints.Variables.make_transfervariablesDirecti|>make_transfer[%exprRefl.Present]acccompose_expr)free_variable_countinlet{arity_types;count_length;count_append;variables;positives;negatives;directs;positive;negative;direct}=make_variables_structurecontextfree_variable_countvariablesinletty=[%type:[`Existsof[%tpeano_type_of_inteq_index]*[%tpeano_type_of_intfree_variable_count]*[%tstructure]*[%tpresence_type]*[%tpositives]*[%tnegatives]*[%tdirects]]]inletmatch_constraintsexpr=ifparameters=[]thenexprelse[%exprmatch_constraintswith|[%pconstraints_pattern.pat]->[%eexpr]|_->assertfalse]inletdesc=[%exprletkind=[%ekind]inletconstruct:typeexistssubarity.([%tcount],exists)Refl.length->([%tparameter_type_tuple],exists)Refl.gadt_constraints->(exists,[%tarity_types],subarity)Refl.append->([%tvalue_type],[%tstructure],subarity,[%trec_group],[>[%tkinds]],[%tpositive],[%tnegative],[%tdirect],[%tgadt])Refl.exists_construct=funexists_count_constraintsexists->let[%pcount_length.pat]=exists_countinlet[%pcount_append.pat]=existsin[%ematch_constraints[%exprRefl.ExistsConstruct{kind;construct=[%ePpxlib.Ast_helper.Exp.function_[Ppxlib.Ast_helper.Exp.casedecomposed.patcomposed.exp]];}]]inletdestruct=[%eList.fold_right(funtxte->Metapp.Exp.newtype(Metapp.mkloctxt)e)context.type_args[%expr(fun([%pcomposed.pat]:[%tvalue_type]):([%tcount],[%tparameter_type_tuple],[%tvalue_type],[%tstructure],[%tarity_types],[%trec_group],[>[%tkinds]],[%tpositive],[%tnegative],[%tdirect],[%tgadt])Refl.exists_destruct->Refl.ExistsDestruct{exists_count=[%ecount_length.exp];exists=[%ecount_append.exp];constraints=[%econstraints_pattern.exp];kind;values=[%edecomposed.exp]})]]inRefl.Exists{name=[%eexpr_of_stringconstructor.pcd_name.txt];construct;destruct;selection=[%eReflValueExp.selection_of_int(succeq_index)];presence=[%epresence_expr];variables=[%evariables];}]incontext.constraints|>Metapp.mutate(Constraints.add_direct_kind"Exists");letchoice=ReflValueVal.binary_choice_of_intictor_countcomposedinletsignature=(type_declarations,type_extensions)in(((ty,desc),value_type),(Ppxlib.Ast_helper.Exp.casechoice.patconstructor_with_args.exp,Ppxlib.Ast_helper.Exp.caseconstructor_with_args.patchoice.exp)),signatureletstructure_of_constructorsingle_constructorcontextcounti(constructor:Ppxlib.constructor_declaration):(((Ppxlib.core_type*Ppxlib.expression)*Ppxlib.core_type)*(Ppxlib.case*Ppxlib.case))*(Ppxlib.type_declarationlist*Ppxlib.type_extensionlist)=tryleteqs,context=extract_gadt_equalitiescontextconstructorinletargs=args_of_constructorconstructorinletitems,ty,types,kind,destructs,type_declarations=make_constructor_kindcontextconstructorargsinletbase_eq_index=!(context.eqs_counter)inleteq_count=List.lengtheqsincontext.eqs_counter:=base_eq_index+eq_count;context.rev_eqs:=List.rev_appendeqs!(context.rev_eqs);letgadt_indexes=List.initeq_count(funi->i+base_eq_index)inleteq_refs=ReflValueExp.equalities_of_list(List.mapReflValueExp.selection_of_int(List.mapsuccgadt_indexes))inletgadt=type_sequence_of_list(List.mappeano_type_of_intgadt_indexes)inletty=[%type:[`Constructorof[%tty]*[%tgadt]]]inletattributes=matchconstructor.pcd_argswith|Pcstr_recordlabelswhenList.exists(fun(label:Ppxlib.label_declaration)->matchlabel.pld_typewith|{ptyp_desc=Ptyp_poly_;_}->true|_->false)labels->[%exprRefl.Tools.attributes_empty]|_->make_attributescontext(type_sequence_of_listargs)constructor.pcd_attributesinletdesc=[%exprRefl.Constructor{name=[%eexpr_of_stringconstructor.pcd_name.txt];kind=[%ekind];eqs=[%eeq_refs];attributes=[%eattributes];}]inletvalue_eqs=List.initeq_count(fun_->Metapp.Value.construct(refl_dot"Eq")[])inletsequence=Metapp.Value.tuple[ReflValueVal.sequence_of_listdestructs;ReflValueVal.sequence_of_listvalue_eqs]inletchoice=ReflValueVal.binary_choice_of_inticountsequenceinletargs=make_constructor_argsconstructoritemsinletconstruct=Metapp.Value.force_construct(Metapp.mkloc(Longident.Lidentconstructor.pcd_name.txt))argsinletchoice_ty=[%type:[%ttype_sequence_of_listtypes]*[%ttype_sequence_of_listeqs]]in(((ty,desc),choice_ty),(Ppxlib.Ast_helper.Exp.casechoice.patconstruct.exp,Ppxlib.Ast_helper.Exp.caseconstruct.patchoice.exp)),(type_declarations,[])with(Exists(loc,name))->matchconstructor.pcd_reswith|Somety->structure_of_existssingle_constructorcounticontextconstructorty|None->matchnamewith|None->Location.raise_errorf~loc"Free variable types are not allowed outside GADT constructors"|Somename->Location.raise_errorf~loc"The type variable '%s is unbound in this type declaration."namelettemp_binding_count=ref0letstructure_of_constrcontext(constructors:Ppxlib.constructor_declarationlist):(Ppxlib.core_type*Ppxlib.expression)*(Ppxlib.type_declarationlist*Ppxlib.type_extensionlist*Ppxlib.structure)=letsingle_constructor=is_singletonconstructorsinletcount=List.lengthconstructorsinletcases=List.mapi(structure_of_constructorsingle_constructorcontextcount)constructorsinletcases,signature=List.splitcasesinletcases,accessors=List.splitcasesinletstructures,choices=List.splitcasesinlettypes,descs=List.splitstructuresinletconstruct,destruct=List.splitaccessorsinletconstruct=construct@irrefutable()inletchoice_ty=[%type:[%tbinary_type_of_listchoices]Refl.binary_choice]inletmake_fun_typeleftrightcases=letleft=subst_free_variablesinstantiateleftinletright=subst_free_variablesinstantiaterightinletarrow_ty=[%type:[%tleft]->[%tright]]in[%expr([%ePpxlib.Ast_helper.Exp.function_cases]:[%tarrow_ty])]inletbindings,descs=ifcontext.type_vars=[]thenList.fold_left_map(funbindings((cstr:Ppxlib.constructor_declaration),desc)->ifcstr.pcd_args=Pcstr_tuple[]&&cstr.pcd_res=Nonethenlettemp_binding_index=!temp_binding_countintemp_binding_count:=succtemp_binding_index;letconstruct_name=Printf.sprintf"constructor%d"temp_binding_indexin[%strilet[%pMetapp.Pat.varconstruct_name]=[%edesc](*assert false
let module M = struct let v = [%e desc] end in
let module T = struct module type S = module type of M end in
let module (N : T.S) = struct let v = assert false end in
let (m : (module T.S)) = if Random.int 2 < 1 then (module M) else (module N) in
let module M' = (val m) in
M'.v*)]::bindings,Metapp.Exp.varconstruct_nameelsebindings,desc)[](List.combineconstructorsdescs)else[],descsincontext.constraints|>Metapp.mutate(Constraints.add_direct_kind"Constr");letexpr=[%exprRefl.Constr{constructors=[%eReflValueExp.binary_choices_of_listdescs];construct=[%emake_fun_typechoice_tycontext.type_exprconstruct];destruct=[%emake_fun_typecontext.type_exprchoice_tydestruct];}]inlettype_declarations,type_extensions=List.splitsignatureinlettype_declarations=List.flattentype_declarationsinlettype_extensions=List.flattentype_extensionsin([%type:[`Constrof[%tbinary_type_of_listtypes]]],expr),(type_declarations,type_extensions,bindings)letstructure_of_recordcontext(labels:Ppxlib.label_declarationlist):(Ppxlib.core_type*Ppxlib.expression)*(Ppxlib.type_declarationlist*Ppxlib.type_extensionlist*'alist)=letitems=List.init(List.lengthlabels)(funi->ReflValueVal.var(itemi))incontext.constraints|>Metapp.mutate(Constraints.add_direct_kind"Record");letsingle_label=is_singletonlabelsinletstructures=List.map2(structure_of_label_declarationcontext(Option.getcontext.name)single_label)labelsitemsinletstructures,destructs=List.splitstructuresinlettypes,descs=List.splitstructuresinletdestructs,type_declarations=List.splitdestructsinlettype_declarations=List.flattentype_declarationsinletdestructs,_=List.splitdestructsinletsequence=ReflValueVal.sequence_of_listdestructsinletrecord=ReflValueVal.record(List.map2(fun(label:Ppxlib.label_declaration)item->(Longident.Lidentlabel.pld_name.txt,item))labelsitems)inletexpr=[%exprRefl.Record{structure=[%eReflValueExp.record_of_listdescs];construct=[%ePpxlib.Ast_helper.Exp.function_[Ppxlib.Ast_helper.Exp.casesequence.patrecord.exp]];destruct=[%ePpxlib.Ast_helper.Exp.function_[Ppxlib.Ast_helper.Exp.caserecord.patsequence.exp]];}]in([%type:[`Recordof[%ttype_sequence_of_listtypes]]],expr),(type_declarations,[],[])letstructure_of_type_declarationcontext(td:Ppxlib.type_declaration):(Ppxlib.core_type*Ppxlib.expression)*(Ppxlib.type_declarationlist*Ppxlib.type_extensionlist*Ppxlib.structure_itemlist)=Ppxlib.Ast_helper.with_default_loctd.ptype_loc@@fun()->let(structure,unwrapped_desc),sides=matchtd.ptype_kindwith|Ptype_variantconstructors->structure_of_constrcontextconstructors|Ptype_recordlabels->structure_of_recordcontextlabels|Ptype_abstract->beginmatchtd.ptype_manifestwith|None->Location.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"refl cannot be derived for fully abstract types"|Somety->(structure_of_typecontextty),([],[],[])end|Ptype_open->Location.raise_errorf~loc:!Ppxlib.Ast_helper.default_loc"refl cannot be derived for open types"inletstructure=[%type:[`Nameof[%tstructure]]]inletunwrapped_desc=[%exprRefl.Name{refl=[%eMetapp.Exp.construct(Lident(type_refl_ctortd.ptype_name.txt))[]];name=[%eMetapp.Exp.of_stringtd.ptype_name.txt];desc=[%eunwrapped_desc];}]in(structure,unwrapped_desc),sidestypetype_structure={type_info:type_info;context:context;arity_type:Ppxlib.core_type;structure:Ppxlib.core_type;unwrapped_desc:Ppxlib.expression;constraints:Constraints.t;rec_type_refs:IntSet.t;}letsubgadt_mappercontexttype_extensions=lettype_extension_count=ref0inletmapper=objectinheritPpxlib.Ast_traverse.mapassupermethod!core_typety=matchsuper#core_typetywith|[%type:[`SubGADTof[%t?ty']]]->ifcontext.name=Nonethenty'elsety|ty->tymethod!expressione=matchsuper#expressionewith|[%exprRefl.SubGADT([%e?desc]:[%t?base]->[%t?sub])]->beginmatchcontext.namewith|None->desc|Somename->letindex=!type_extension_countintype_extension_count:=succindex;letconstructor_name=Printf.sprintf"%s__sub_%d"(String.capitalize_asciiname)indexinletconstructor=Metapp.Value.construct(Lidentconstructor_name)[]intype_extensions:=Ppxlib.Ast_helper.Te.mk(Metapp.mkloc(refl_dot"sub_gadt_ext"))~params:[[%type:_],empty_type_annotation;[%type:_],empty_type_annotation][Ppxlib.Ast_helper.Te.decl(Metapp.mklocconstructor_name)~res:[%type:([%tbase],[%tsub])Refl.sub_gadt_ext]]::!type_extensions;[%exprletsub_gadt_functional:typegadtsub_gadt0sub_gadt1.(gadt,sub_gadt0)Refl.sub_gadt_ext->(gadt,sub_gadt1)Refl.sub_gadt_ext->(sub_gadt0,sub_gadt1)Refl.eq=funsubsub'->matchsub,sub'with|[%pconstructor.pat],[%pconstructor.pat]->Eq|_->assertfalseinRefl.SubGADT{desc=[%edesc];sub_gadt={Refl.sub_gadt_ext=[%econstructor.exp];sub_gadt_functional}}]end|e->eendinmapperlettype_structure_of_type_inforec_typestype_info=let{arity;td;_}=type_infoinPpxlib.Ast_helper.with_default_loctd.ptype_loc@@fun()->letcontext=context_of_type_declarationtdrec_typesinlet(structure,unwrapped_desc),(type_declarations,type_extensions,value_bindings)=structure_of_type_declarationcontexttdinletarity_type=peano_type_of_intarityinlettype_extensions=reftype_extensionsinletmapper=subgadt_mappercontexttype_extensionsinletunwrapped_desc=mapper#expressionunwrapped_descinletstructure=mapper#core_typestructureinletdeclarations=[Ppxlib.Ast_helper.Type.mk(Metapp.mkloccontext.type_names.arity)~manifest:arity_type;Ppxlib.Ast_helper.Type.mk(Metapp.mkloccontext.type_names.structure)~manifest:structure;]inletarity_type=type_constr_of_stringcontext.type_names.arityinletstructure=type_constr_of_stringcontext.type_names.structureinletconstraints=!(context.constraints)inletconstraints=match!(context.exists)with|None->constraints|Someexists->Constraints.add_exists_kindexistsconstraintsinlettype_extensions=!type_extensionsinlettype_extensions=Ppxlib.Ast_helper.Te.mk(Metapp.mkloc(refl_dot"refl"))~params:[[%type:_],empty_type_annotation][Ppxlib.Ast_helper.Te.decl(Metapp.mkloccontext.type_names.refl_ctor)~res:[%type:[%tcontext.type_expr]Refl.refl]]::type_extensionsin((declarations@type_declarations),(type_extensions,value_bindings)),{type_info;context;arity_type;structure;unwrapped_desc;constraints;rec_type_refs=!(context.rec_type_refs)}lettypes_of_transferstransfers=letpresent=[%type:'present]inletunknown=[%type:'unknown]inletparams=[present,empty_type_annotation;unknown,empty_type_annotation]intransfers|>List.mapbeginfun(name,transfer)->letmanifest=transfer|>make_transferpresentunknowncompose_typeinPpxlib.Ast_helper.Type.mk~params(Metapp.mklocname)~manifestendletfuns_of_transferstransfers=transfers|>List.mapbeginfun(name,transfer)->letstr=Metapp.mklocnameinPpxlib.Ast_helper.Val.mkstr[%type:'present->'unknown->[%tPpxlib.Ast_helper.Typ.constr(Metapp.lid_of_strstr)[[%type:'present];[%type:'unknown]]]],Ppxlib.Ast_helper.Vb.mk(Ppxlib.Ast_helper.Pat.varstr)[%exprfunrefl__presentrefl__absent->[%etransfer|>make_transfer[%exprrefl__present][%exprrefl__absent]compose_expr]]~attrs:[Metapp.Attr.mk(Metapp.mkloc"ocaml.warning")(PStr[%str"-27-32"])]endletmodule_of_type_structurerec_groupconstraintsitype_structure:((Ppxlib.type_declarationlist*Ppxlib.type_declarationlist)*(Ppxlib.value_description*Ppxlib.value_binding)list)*(Ppxlib.signature_item*Ppxlib.value_binding)=let{type_info={td;desc_name;arity;_};structure;unwrapped_desc;context;_}=type_structureinPpxlib.Ast_helper.with_default_loctd.ptype_loc@@fun()->lettypes=type_sequence_of_listcontext.type_varsinletrec_group_type=type_constr_of_stringcontext.type_names.rec_groupinletrec_group_decl=let(declared,manifest)=!rec_groupinifnotdeclaredthenrec_group:=(true,rec_group_type);Ppxlib.Ast_helper.Type.mk(Metapp.mkloccontext.type_names.rec_group)~manifestinletconstraints=constraintsiinletkinds=type_constr_of_stringcontext.type_names.kindsinletkinds_decl=letmanifest=Constraints.Kinds.to_type(fstconstraints)inPpxlib.Ast_helper.Type.mk(Metapp.mkloccontext.type_names.kinds)~manifestinletvariable_transfers=Constraints.Variables.make_transferstd.ptype_name.txtarity(sndconstraints)inlettransfers_types=types_of_transfersvariable_transfersinlettransfers_funs=funs_of_transfersvariable_transfersinletvariable_typesname_absent=variable_types(Lidenttd.ptype_name.txt)arityname(fun_i->[%type:[`Absent]])inletgadt=type_constr_of_stringcontext.type_names.gadt~args:context.type_varsinletgadt_decl=letparams=context.type_vars|>List.map(funty->(ty,empty_type_annotation))inletmanifest=type_sequence_of_list(List.rev!(context.rev_eqs))inPpxlib.Ast_helper.Type.mk(Metapp.mkloccontext.type_names.gadt)~manifest~paramsinletdesc_type=[%type:([%tcontext.type_expr],[%tstructure],[%ttypes],[%trec_group_type],[>[%tkinds]],[%tvariable_typesConstraints.Variables.positive_name(funi->"absent_positive"^string_of_inti)],[%tvariable_typesConstraints.Variables.negative_name(funi->"absent_negative"^string_of_inti)],[%tvariable_typesConstraints.Variables.direct_name(funi->"absent_direct"^string_of_inti)],[%tgadt])Refl.desc]inletdesc_sig=Ppxlib.Ast_helper.Sig.value(Ppxlib.Ast_helper.Val.mk(Metapp.mklocdesc_name)desc_type)inlettype_loc=List.mapMetapp.mkloccontext.type_argsinletdesc=List.fold_rightMetapp.Exp.newtypetype_locunwrapped_descinletdesc_def=Ppxlib.Ast_helper.Vb.mk[%pat?([%pMetapp.Pat.vardesc_name]:[%tMetapp.Typ.polytype_locdesc_type])]desc~attrs:[Metapp.Attr.mk(Metapp.mkloc"ocaml.warning")(PStr[%str"-32-34"])]in((transfers_types,[rec_group_decl;kinds_decl;gadt_decl]),transfers_funs),(desc_sig,desc_def)letrec_types_of_type_info(rec_flag:Ppxlib.Asttypes.rec_flag)type_infos=matchrec_flagwith|Nonrecursive->None|Recursive->letcount=List.lengthtype_infosinSome(make_index(fun{td;_}->Sometd.ptype_name.txt)type_infoscount)typemodules={desc_sig:Ppxlib.signature;desc_def:Ppxlib.structure;}letmodules_of_type_declarations(rec_flag,tds)=letrecursive=refPpxlib.Asttypes.Nonrecursiveinlettype_infos=tds|>List.map(type_info_of_type_declarationrecursive)inletrec_types=rec_types_of_type_inforec_flagtype_infosinlettype_structures=List.map(type_structure_of_type_inforec_types)type_infosinletsignature,type_structures=List.splittype_structuresinletindexed_type_structures=Array.of_listtype_structuresinletmoduleF=Fix.Fix.ForType(Int)(Constraints)inletconstraints=F.lfpbeginfuniconstraints->lettype_structure=indexed_type_structures.(i)inletunionjcstr=Constraints.union(constraintsj)cstrinIntSet.folduniontype_structure.rec_type_refs(Constraints.union(constraintsi)type_structure.constraints)endinletrec_group_type=binary_type_of_list(type_structures|>List.mapbeginfun(type_structure:type_structure):Ppxlib.core_type->[%type:[%ttype_structure.arity_type]*[%ttype_structure.structure]]end)in(*
let rec_group_expr =
rec_group_of_list (type_structures |> List.map begin
fun (type_structure : type_structure) ->
exp [%expr [%e expression_of_value
(length_of_int type_structure.type_info.arity)],
[%e Ppxlib.Ast_helper.Exp.ident { loc; txt =
Lident (type_structure.type_info.desc_name)}]]
end) in
*)lettype_declarations,type_extensions=List.splitsignatureinlettype_declarations=List.flattentype_declarationsinlettype_extensions,value_bindings=List.splittype_extensionsinlettype_extensions=List.flattentype_extensionsinletvalue_bindings=List.flattenvalue_bindingsinletdesc=List.mapi(module_of_type_structure(ref(false,rec_group_type))constraints)type_structuresinletdecls,desc=List.splitdescinlettypes,vals=List.splitdeclsinlettransfers,types=List.splittypesinlettype_declarations=List.flattentransfers@type_declarations@List.flattentypesinletdesc_sig,desc_bindings=List.splitdescin(*
let rec_group_name = (List.hd type_structures).context.type_names.rec_group in
let desc_sig =
Ppxlib.Ast_helper.Sig.value (Ppxlib.Ast_helper.Value.mk { loc; txt = rec_group_name }
(Ppxlib.Ast_helper.Typ.constr { loc; txt = (refl_dot "rec_group") }
[type_constr_of_string rec_group_name;
type_constr_of_string rec_group_name])) :: desc_sig in
let desc_bindings =
Ppxlib.Ast_helper.Vb.mk (Ppxlib.Ast_helper.Pat.var { loc; txt = rec_group_name })
(expression_of_value rec_group_expr) :: desc_bindings in
*)letval_desc,val_bindings=List.split(List.flattenvals)inletval_sig=List.mapPpxlib.Ast_helper.Sig.valueval_descinletdesc_def=Ppxlib.Ast_helper.Str.type_Recursivetype_declarations::List.mapPpxlib.Ast_helper.Str.type_extensiontype_extensions@value_bindings@[Ppxlib.Ast_helper.Str.value!recursivedesc_bindings]inletdesc_sig=Ppxlib.Ast_helper.Sig.type_Recursivetype_declarations::List.mapPpxlib.Ast_helper.Sig.type_extensiontype_extensions@val_sig@desc_siginletdesc_def=ifval_bindings=[]thendesc_defelsePpxlib.Ast_helper.Str.valueNonrecursiveval_bindings::desc_defin{desc_sig;desc_def}letmake_str~loctype_declarations:Ppxlib.structure=Ppxlib.Ast_helper.with_default_locloc@@fun()->let{desc_def;_}=modules_of_type_declarationstype_declarationsinletstop_doc=[%str(**/**)]instop_doc@desc_def@stop_doc(*
let str_type_decl =
Ppxlib.Deriving.Generator.make_noarg make_str
*)letmake_sig~loctype_declarations:Ppxlib.signature=let{desc_sig;_}=modules_of_type_declarationstype_declarationsinletstop_doc=[%sig:(**/**)]instop_doc@desc_sig@stop_doc(*
let sig_type_decl = Ppxlib.Deriving.Generator.make_noarg make_sig
*)letenumerate_free_variables(ty:Ppxlib.core_type):StringSet.t*int=fold_free_variablesbeginfun_locvar(names,anonymous)->matchvarwith|None->names,anonymous+1|Somename->StringSet.addnamenames,anonymousendty(StringSet.empty,0)letextensionty:Ppxlib.expression=letnames,anonymous=enumerate_free_variablestyinletarity=StringSet.cardinalnames+anonymousinletcontext=make_contextNone[](StringIndexer.of_fresharity)inlet_structure,expr=structure_of_typecontexttyinletmapper=subgadt_mappercontext(ref[])inletexpr=mapper#expressionexprinletexpr=match!(context.free_vars)with|[]->expr|free_vars->letbindings=List.revfree_vars|>List.filter(funvar->notvar.bound)|>List.mapibeginfuni(var:free_variable)->Ppxlib.Ast_helper.Vb.mk(Metapp.Pat.varvar.name)[%exprRefl.Variable[%eReflValueExp.variable_of_inti]]endinPpxlib.Ast_helper.Exp.let_Nonrecursivebindingsexprinexprletsig_type_decl=Ppxlib.Deriving.Generator.makePpxlib.Deriving.Args.empty(fun~loc~path:_->make_sig~loc)letstr_type_decl=Ppxlib.Deriving.Generator.makePpxlib.Deriving.Args.empty(fun~loc~path:_->make_str~loc)let()=Ppxlib.Deriving.add"refl"~sig_type_decl~str_type_decl~extension:(fun~loc:_~path:_->extension)|>Ppxlib.Deriving.ignore(*
let var_list = ref [] in
let var_counter = ref 0 in
let fresh_var () =
let index = !var_counter in
let var_name = Printf.sprintf "free%d" index in
var_list := var_name :: !var_list;
Ppxlib.Ast_helper.Typ.var var_name in
let table = StringHashtbl.create 17 in
let f _ var =
match var with
| None -> fresh_var ()
| Some name ->
try StringHashtbl.find table name
with Not_found ->
let result = fresh_var () in
StringHashtbl.add table name result;
result in
let target_type = subst_free_variables f ty in
let var_list = !var_list in
let arity = type_sequence_of_list (List.map Ppxlib.Ast_helper.Typ.var var_list) in
let ty = [%type: (
[%t target_type], _, [%t arity], _, _, _, _, _) Refl.desc] in
let ty =
if var_list = [] then
ty
else
Ppxlib.Ast_helper.Typ.poly (List.map (fun txt -> { loc; txt }) var_list) ty in
[%expr let result : [%t ty] = [%e expr] in result]
*)(*
let deriver =
Ppxlib.Deriving.add "refl" ~str_type_decl ~sig_type_decl ~extension
*)