123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136open!ImportincludeClause_syntax_intfmoduleVariant=structtypeast=constructor_declarationtypet={ast:ast;position:int}letcreate_listlist=List.mapilist~f:(funpositionast->letloc=ast.pcd_locinmatchast.pcd_reswith|Some_->unsupported~loc"GADT"|None->{ast;position})letsaltt=Somet.positionletlocationt=t.ast.pcd_locletcore_type_listt=matcht.ast.pcd_argswith|Pcstr_tuplelist->list|Pcstr_recordlabel_decl_list->List.maplabel_decl_list~f:(funlabel_decl->label_decl.pld_type)letpatternt~locpat_list=letarg=matcht.ast.pcd_argswith|Pcstr_tuple_->beginmatchpat_listwith|[]->None|[pat]->Somepat|_->Some(ppat_tuple~locpat_list)end|Pcstr_recordlabel_decl_list->letalist=List.map2_exnlabel_decl_listpat_list~f:(funlabel_declpat->lident_loclabel_decl.pld_name,pat)inSome(ppat_record~localistClosed)inppat_construct~loc(lident_loct.ast.pcd_name)argletexpressiont~loc_expr_list=letarg=matcht.ast.pcd_argswith|Pcstr_tuple_->beginmatchexpr_listwith|[]->None|[expr]->Someexpr|_->Some(pexp_tuple~locexpr_list)end|Pcstr_recordlabel_decl_list->letalist=List.map2_exnlabel_decl_listexpr_list~f:(funlabel_declexpr->lident_loclabel_decl.pld_name,expr)inSome(pexp_record~localistNone)inpexp_construct~loc(lident_loct.ast.pcd_name)argendmodulePolymorphic_variant=structtypeast=row_fieldtypet=astletcreate_list=Fn.idletsaltt=matchtwith|Rtag(label,_,_,_)->Some(Ocaml_common.Btype.hash_variantlabel.txt)|Rinherit_->Noneletlocationt=matchtwith|Rtag(label,_,_,_)->label.loc|Rinheritcore_type->core_type.ptyp_locletcore_type_listt=matchtwith|Rtag(_,_,_,core_type_list)->core_type_list|Rinheritcore_type->[core_type]letpatternt~locpat_list=matcht,pat_listwith|Rtag(label,_,true,[]),[]->ppat_variant~loclabel.txtNone|Rtag(label,_,false,[_]),[pat]->ppat_variant~loclabel.txt(Somepat)|Rtag(label,_,false,[_]),(_::_::_)->ppat_variant~loclabel.txt(Some(ppat_tuple~locpat_list))|Rinherit{ptyp_desc;_},[{ppat_desc;_}]->beginmatchptyp_descwith|Ptyp_constr(id,[])->beginmatchppat_descwith|Ppat_varvar->ppat_alias~loc(ppat_type~locid)var|_->internal_error~loc"cannot bind a #<type> pattern to anything other than a variable"end|_->unsupported~loc"inherited polymorphic variant type that is not a type name"end|Rtag(_,_,true,_::_),_|Rtag(_,_,false,([]|_::_::_)),_->unsupported~loc"intersection type"|Rtag(_,_,true,[]),_::_|Rtag(_,_,false,[_]),[]|Rinherit_,([]|_::_::_)->internal_error~loc"wrong number of arguments for variant clause"letexpressiont~loccore_typeexpr_list=matcht,expr_listwith|Rtag(label,_,true,[]),[]->pexp_variant~loclabel.txtNone|Rtag(label,_,false,[_]),[expr]->pexp_variant~loclabel.txt(Someexpr)|Rtag(label,_,false,[_]),(_::_::_)->pexp_variant~loclabel.txt(Some(pexp_tuple~locexpr_list))|Rinheritinherited_type,[expr]->pexp_coerce~locexpr(Someinherited_type)core_type|Rtag(_,_,true,_::_),_|Rtag(_,_,false,([]|_::_::_)),_->unsupported~loc"intersection type"|Rtag(_,_,true,[]),_::_|Rtag(_,_,false,[_]),[]|Rinherit_,([]|_::_::_)->internal_error~loc"wrong number of arguments for variant clause"end