123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293openPpopenUtilopenNamesopenTableopenMinimlopenMlutilopenCommonletjson_strs=qssletjson_inti=intiletjson_boolb=ifbthenstr"true"elsestr"false"letjson_globaltabletypref=ifis_customrefthenjson_str(find_customref)elsejson_str(Common.pp_globaltabletypref)letjson_idid=json_str(Id.to_stringid)letjson_dict_one(k,v)=json_strk++str(": ")++vletjson_dict_openl=str("{")++fnl()++str(" ")++hov0(prlist_with_seppr_commajson_dict_onel)letjson_dictl=json_dict_openl++fnl()++str("}")letjson_listl=str("[")++fnl()++str(" ")++hov0(prlist_with_seppr_comma(funx->x)l)++fnl()++str("]")letjson_listarra=str("[")++fnl()++str(" ")++hov0(prvect_with_seppr_comma(funx->x)a)++fnl()++str("]")letpreambletablemod_namecommentused_modulesusf=(matchcommentwith|None->mt()|Somes->str"/* "++hov0s++str" */"++fnl())++json_dict_open[("what",json_str"module");("name",json_idmod_name);("need_magic",json_bool(usf.magic));("need_dummy",json_bool(usf.mldummy));("used_modules",json_list(List.map(funmf->json_str(file_of_modfile(State.get_tabletable)mf))used_modules))](*s Pretty-printing of types. *)letrecjson_typetablevl=function|Tmeta_|Tvar'_->assertfalse|Tvari->(tryletvarid=List.nthvl(predi)injson_dict[("what",json_str"type:var");("name",json_idvarid)]withFailure_->json_dict[("what",json_str"type:varidx");("name",json_inti)])|Tglob(r,l)->json_dict[("what",json_str"type:glob");("name",json_globaltableTyper);("args",json_list(List.map(json_typetablevl)l))]|Tarr(t1,t2)->json_dict[("what",json_str"type:arrow");("left",json_typetablevlt1);("right",json_typetablevlt2)]|Tdummy_->json_dict[("what",json_str"type:dummy")]|Tunknown->json_dict[("what",json_str"type:unknown")]|Taxiom->json_dict[("what",json_str"type:axiom")](*s Pretty-printing of expressions. *)letrecjson_exprtableenv=function|MLreln->json_dict[("what",json_str"expr:rel");("name",json_id(get_db_namenenv))]|MLapp(f,args)->json_dict[("what",json_str"expr:apply");("func",json_exprtableenvf);("args",json_list(List.map(json_exprtableenv)args))]|MLlam_asa->letfl,a'=collect_lamsainletfl,env'=push_vars(List.mapid_of_mlidfl)envinjson_dict[("what",json_str"expr:lambda");("argnames",json_list(List.mapjson_id(List.revfl)));("body",json_exprtableenv'a')]|MLletin(id,a1,a2)->leti,env'=push_vars[id_of_mlidid]envinjson_dict[("what",json_str"expr:let");("name",json_id(List.hdi));("nameval",json_exprtableenva1);("body",json_exprtableenv'a2)]|MLglobr->json_dict[("what",json_str"expr:global");("name",json_globaltableTermr)]|MLcons(_,r,a)->json_dict[("what",json_str"expr:constructor");("name",json_globaltableConsr);("args",json_list(List.map(json_exprtableenv)a))]|MLtuplel->json_dict[("what",json_str"expr:tuple");("items",json_list(List.map(json_exprtableenv)l))]|MLcase(typ,t,pv)->json_dict[("what",json_str"expr:case");("expr",json_exprtableenvt);("cases",json_listarr(Array.map(funx->json_one_pattableenvx)pv))]|MLfix(i,ids,defs)->letids',env'=push_vars(List.rev(Array.to_listids))envinletids'=Array.of_list(List.revids')injson_dict[("what",json_str"expr:fix");("funcs",json_listarr(Array.map(fun(fi,ti)->json_dict[("what",json_str"fix:item");("name",json_idfi);("body",json_functiontableenv'ti)])(Array.map2(funab->a,b)ids'defs)));("for",json_inti);]|MLexns->json_dict[("what",json_str"expr:exception");("msg",json_strs)]|MLdummy_->json_dict[("what",json_str"expr:dummy")]|MLmagica->json_dict[("what",json_str"expr:coerce");("value",json_exprtableenva)]|MLaxiom_->json_dict[("what",json_str"expr:axiom")]|MLuinti->json_dict[("what",json_str"expr:int");("int",json_str(Uint63.to_stringi))]|MLfloatf->json_dict[("what",json_str"expr:float");("float",json_str(Float64.to_stringf))]|MLstrings->json_dict[("what",json_str"expr:string");("string",json_str(Pstring.to_strings))]|MLparray(t,def)->json_dict[("what",json_str"expr:array");("elems",json_listarr(Array.map(json_exprtableenv)t));("default",json_exprtableenvdef)]andjson_one_pattableenv(ids,p,t)=letids',env'=push_vars(List.rev_mapid_of_mlidids)envinjson_dict[("what",json_str"case");("pat",json_gen_pattable(List.revids')env'p);("body",json_exprtableenv't)]andjson_gen_pattableidsenv=function|Pcons(r,l)->json_cons_pattabler(List.map(json_gen_pattableidsenv)l)|Pusualr->json_cons_pattabler(List.mapjson_idids)|Ptuplel->json_dict[("what",json_str"pat:tuple");("items",json_list(List.map(json_gen_pattableidsenv)l))]|Pwild->json_dict[("what",json_str"pat:wild")]|Preln->json_dict[("what",json_str"pat:rel");("name",json_id(get_db_namenenv))]andjson_cons_pattablerppl=json_dict[("what",json_str"pat:constructor");("name",json_globaltableConsr);("argnames",json_listppl)]andjson_functiontableenvt=letbl,t'=collect_lamstinletbl,env'=push_vars(List.mapid_of_mlidbl)envinjson_dict[("what",json_str"expr:lambda");("argnames",json_list(List.mapjson_id(List.revbl)));("body",json_exprtableenv't')](*s Pretty-printing of inductive types declaration. *)letjson_indtableipplcv=json_dict[("what",json_str"decl:ind");("name",json_globaltableTypeip.ip_typename_ref);("argnames",json_list(List.mapjson_idpl));("constructors",json_listarr(Array.mapi(funidxc->json_dict[("name",json_globaltableConsip.ip_consnames_ref.(idx));("argtypes",json_list(List.map(json_typetablepl)c))])cv))](*s Pretty-printing of a declaration. *)letpp_decltable=function|Dinddefs->prvecti_with_seppr_comma(funip->ifp.ip_logicalthenstr""elsejson_indtablepp.ip_varsp.ip_types)defs.ind_packets|Dtype(r,l,t)->json_dict[("what",json_str"decl:type");("name",json_globaltableTyper);("argnames",json_list(List.mapjson_idl));("value",json_typetablelt)]|Dfix(rv,defs,typs)->json_dict[("what",json_str"decl:fixgroup");("fixlist",json_listarr(Array.mapi(funir->json_dict[("what",json_str"fixgroup:item");("name",json_globaltableTermrv.(i));("type",json_typetable[]typs.(i));("value",json_functiontable(empty_envtable())defs.(i))])rv))]|Dterm(r,a,t)->json_dict[("what",json_str"decl:term");("name",json_globaltableTermr);("type",json_typetable[]t);("value",json_functiontable(empty_envtable())a)]letrecpp_structure_elemtable=function|(l,SEdecld)->[pp_decltabled]|(l,SEmodulem)->pp_module_exprtablem.ml_mod_expr|(l,SEmodtypem)->[](* for the moment we simply discard module type *)andpp_module_exprtable=function|MEstruct(mp,sel)->List.concat(List.map(pp_structure_elemtable)sel)|MEfunctor_->[](* for the moment we simply discard unapplied functors *)|MEident_|MEapply_->assertfalse(* should be expansed in extract_env *)letpp_structtablemls=letpp_sel(mp,sel)=State.with_visibilitytablemp[]beginfuntable->letp=prlist_with_seppr_commaidentity(List.concat(List.map(pp_structure_elemtable)sel))inpendinstr","++fnl()++str" "++qs"declarations"++str": ["++fnl()++str" "++hov0(prlist_with_seppr_commapp_selmls)++fnl()++str" ]"++fnl()++str"}"++fnl()letfile_namingstatemp=file_of_modfile(State.get_tablestate)mpletjson_descr={keywords=Id.Set.empty;file_suffix=".json";file_naming=file_naming;preamble=preamble;pp_struct=pp_struct;sig_suffix=None;sig_preamble=(fun_____->mt());pp_sig=(fun__->mt());pp_decl=pp_decl;}