src/Tools/Code/code_ml.ML
changeset 55147 bce3dbc11f95
parent 55145 2bb3cd36bcf7
child 55150 0940309ed8f1
equal deleted inserted replaced
55146:525309c2e4ee 55147:bce3dbc11f95
    26 val target_SML = "SML";
    26 val target_SML = "SML";
    27 val target_OCaml = "OCaml";
    27 val target_OCaml = "OCaml";
    28 
    28 
    29 datatype ml_binding =
    29 datatype ml_binding =
    30     ML_Function of string * (typscheme * ((iterm list * iterm) * (thm option * bool)) list)
    30     ML_Function of string * (typscheme * ((iterm list * iterm) * (thm option * bool)) list)
    31   | ML_Instance of string * { class: string, tyco: string, vs: (vname * sort) list,
    31   | ML_Instance of (string * class) * { class: class, tyco: string, vs: (vname * sort) list,
    32         superinsts: (class * (string * (string * dict list list))) list,
    32         superinsts: (class * dict list list) list,
    33         inst_params: ((string * (const * int)) * (thm * bool)) list,
    33         inst_params: ((string * (const * int)) * (thm * bool)) list,
    34         superinst_params: ((string * (const * int)) * (thm * bool)) list };
    34         superinst_params: ((string * (const * int)) * (thm * bool)) list };
    35 
    35 
    36 datatype ml_stmt =
    36 datatype ml_stmt =
    37     ML_Exc of string * (typscheme * int)
    37     ML_Exc of string * (typscheme * int)
    38   | ML_Val of ml_binding
    38   | ML_Val of ml_binding
    39   | ML_Funs of ml_binding list * string list
    39   | ML_Funs of ml_binding list * Code_Symbol.symbol list
    40   | ML_Datas of (string * (vname list * ((string * vname list) * itype list) list)) list
    40   | ML_Datas of (string * (vname list * ((string * vname list) * itype list) list)) list
    41   | ML_Class of string * (vname * ((class * string) list * (string * itype) list));
    41   | ML_Class of string * (vname * ((class * class) list * (string * itype) list));
    42 
    42 
    43 fun print_product _ [] = NONE
    43 fun print_product _ [] = NONE
    44   | print_product print [x] = SOME (print x)
    44   | print_product print [x] = SOME (print x)
    45   | print_product print xs = (SOME o enum " *" "" "") (map print xs);
    45   | print_product print xs = (SOME o enum " *" "" "") (map print xs);
    46 
    46 
    51 
    51 
    52 (** SML serializer **)
    52 (** SML serializer **)
    53 
    53 
    54 fun print_sml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
    54 fun print_sml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
    55   let
    55   let
    56     fun print_tyco_expr (tyco, []) = (str o deresolve) tyco
    56     val deresolve_const = deresolve o Code_Symbol.Constant;
    57       | print_tyco_expr (tyco, [ty]) =
    57     val deresolve_tyco = deresolve o Code_Symbol.Type_Constructor;
    58           concat [print_typ BR ty, (str o deresolve) tyco]
    58     val deresolve_class = deresolve o Code_Symbol.Type_Class;
    59       | print_tyco_expr (tyco, tys) =
    59     val deresolve_classrel = deresolve o Code_Symbol.Class_Relation;
    60           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
    60     val deresolve_inst = deresolve o Code_Symbol.Class_Instance;
       
    61     fun print_tyco_expr (sym, []) = (str o deresolve) sym
       
    62       | print_tyco_expr (sym, [ty]) =
       
    63           concat [print_typ BR ty, (str o deresolve) sym]
       
    64       | print_tyco_expr (sym, tys) =
       
    65           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym]
    61     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
    66     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
    62          of NONE => print_tyco_expr (tyco, tys)
    67          of NONE => print_tyco_expr (Code_Symbol.Type_Constructor tyco, tys)
    63           | SOME (_, print) => print print_typ fxy tys)
    68           | SOME (_, print) => print print_typ fxy tys)
    64       | print_typ fxy (ITyVar v) = str ("'" ^ v);
    69       | print_typ fxy (ITyVar v) = str ("'" ^ v);
    65     fun print_dicttyp (class, ty) = print_tyco_expr (class, [ty]);
    70     fun print_dicttyp (class, ty) = print_tyco_expr (Code_Symbol.Type_Class class, [ty]);
    66     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
    71     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
    67       (map_filter (fn (v, sort) =>
    72       (map_filter (fn (v, sort) =>
    68         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
    73         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
    69     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
    74     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
    70     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
    75     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
    71     fun print_classrels fxy [] ps = brackify fxy ps
    76     fun print_classrels fxy [] ps = brackify fxy ps
    72       | print_classrels fxy [classrel] ps = brackify fxy [(str o deresolve) classrel, brackify BR ps]
    77       | print_classrels fxy [classrel] ps = brackify fxy [(str o deresolve_classrel) classrel, brackify BR ps]
    73       | print_classrels fxy classrels ps =
    78       | print_classrels fxy classrels ps =
    74           brackify fxy [enum " o" "(" ")" (map (str o deresolve) classrels), brackify BR ps]
    79           brackify fxy [enum " o" "(" ")" (map (str o deresolve_classrel) classrels), brackify BR ps]
    75     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
    80     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
    76       print_classrels fxy classrels (print_plain_dict is_pseudo_fun fxy x)
    81       print_classrels fxy classrels (print_plain_dict is_pseudo_fun fxy x)
    77     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
    82     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
    78           ((str o deresolve) inst ::
    83           ((str o deresolve_inst) inst ::
    79             (if is_pseudo_fun inst then [str "()"]
    84             (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
    80             else map_filter (print_dicts is_pseudo_fun BR) dss))
    85             else map_filter (print_dicts is_pseudo_fun BR) dss))
    81       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
    86       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
    82           [str (if k = 1 then first_upper v ^ "_"
    87           [str (if k = 1 then first_upper v ^ "_"
    83             else first_upper v ^ string_of_int (i+1) ^ "_")]
    88             else first_upper v ^ string_of_int (i+1) ^ "_")]
    84     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
    89     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
   103               #>> (fn p => concat [str "fn", p, str "=>"]);
   108               #>> (fn p => concat [str "fn", p, str "=>"]);
   104             val (ps, vars') = fold_map print_abs binds vars;
   109             val (ps, vars') = fold_map print_abs binds vars;
   105           in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end
   110           in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end
   106       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   111       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   107           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   112           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   108            of SOME (app as ({ name = c, ... }, _)) => if is_none (const_syntax c)
   113            of SOME (app as ({ sym = Code_Symbol.Constant const, ... }, _)) =>
       
   114                 if is_none (const_syntax const)
   109                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   115                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   110                 else print_app is_pseudo_fun some_thm vars fxy app
   116                 else print_app is_pseudo_fun some_thm vars fxy app
   111             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   117             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   112     and print_app_expr is_pseudo_fun some_thm vars (app as ({ name = c, dicts = dss, dom = dom, ... }, ts)) =
   118     and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) =
   113       if is_constr c then
   119       if is_constr sym then
   114         let val k = length dom in
   120         let val k = length dom in
   115           if k < 2 orelse length ts = k
   121           if k < 2 orelse length ts = k
   116           then (str o deresolve) c
   122           then (str o deresolve) sym
   117             :: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts)
   123             :: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts)
   118           else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)]
   124           else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)]
   119         end
   125         end
   120       else if is_pseudo_fun c
   126       else if is_pseudo_fun sym
   121         then (str o deresolve) c @@ str "()"
   127         then (str o deresolve) sym @@ str "()"
   122       else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) dss
   128       else (str o deresolve) sym :: map_filter (print_dicts is_pseudo_fun BR) dss
   123         @ map (print_term is_pseudo_fun some_thm vars BR) ts
   129         @ map (print_term is_pseudo_fun some_thm vars BR) ts
   124     and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
   130     and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
   125       (print_term is_pseudo_fun) const_syntax some_thm vars
   131       (print_term is_pseudo_fun) const_syntax some_thm vars
   126     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   132     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   127     and print_case is_pseudo_fun some_thm vars fxy { clauses = [], ... } =
   133     and print_case is_pseudo_fun some_thm vars fxy { clauses = [], ... } =
   156               :: print_term is_pseudo_fun some_thm vars NOBR t
   162               :: print_term is_pseudo_fun some_thm vars NOBR t
   157               :: print_select "of" clause
   163               :: print_select "of" clause
   158               :: map (print_select "|") clauses
   164               :: map (print_select "|") clauses
   159             )
   165             )
   160           end;
   166           end;
   161     fun print_val_decl print_typscheme (name, typscheme) = concat
   167     fun print_val_decl print_typscheme (sym, typscheme) = concat
   162       [str "val", str (deresolve name), str ":", print_typscheme typscheme];
   168       [str "val", str (deresolve sym), str ":", print_typscheme typscheme];
   163     fun print_datatype_decl definer (tyco, (vs, cos)) =
   169     fun print_datatype_decl definer (tyco, (vs, cos)) =
   164       let
   170       let
   165         fun print_co ((co, _), []) = str (deresolve co)
   171         fun print_co ((co, _), []) = str (deresolve_const co)
   166           | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
   172           | print_co ((co, _), tys) = concat [str (deresolve_const co), str "of",
   167               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   173               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   168       in
   174       in
   169         concat (
   175         concat (
   170           str definer
   176           str definer
   171           :: print_tyco_expr (tyco, map ITyVar vs)
   177           :: print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs)
   172           :: str "="
   178           :: str "="
   173           :: separate (str "|") (map print_co cos)
   179           :: separate (str "|") (map print_co cos)
   174         )
   180         )
   175       end;
   181       end;
   176     fun print_def is_pseudo_fun needs_typ definer
   182     fun print_def is_pseudo_fun needs_typ definer
   177           (ML_Function (name, (vs_ty as (vs, ty), eq :: eqs))) =
   183           (ML_Function (const, (vs_ty as (vs, ty), eq :: eqs))) =
   178           let
   184           let
   179             fun print_eqn definer ((ts, t), (some_thm, _)) =
   185             fun print_eqn definer ((ts, t), (some_thm, _)) =
   180               let
   186               let
   181                 val vars = reserved
   187                 val vars = reserved
   182                   |> intro_base_names_for (is_none o const_syntax)
   188                   |> intro_base_names_for (is_none o const_syntax)
   183                        deresolve (t :: ts)
   189                        deresolve (t :: ts)
   184                   |> intro_vars ((fold o Code_Thingol.fold_varnames)
   190                   |> intro_vars ((fold o Code_Thingol.fold_varnames)
   185                        (insert (op =)) ts []);
   191                        (insert (op =)) ts []);
   186                 val prolog = if needs_typ then
   192                 val prolog = if needs_typ then
   187                   concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty]
   193                   concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty]
   188                     else (concat o map str) [definer, deresolve name];
   194                     else (concat o map str) [definer, deresolve_const const];
   189               in
   195               in
   190                 concat (
   196                 concat (
   191                   prolog
   197                   prolog
   192                   :: (if is_pseudo_fun name then [str "()"]
   198                   :: (if is_pseudo_fun (Code_Symbol.Constant const) then [str "()"]
   193                       else print_dict_args vs
   199                       else print_dict_args vs
   194                         @ map (print_term is_pseudo_fun some_thm vars BR) ts)
   200                         @ map (print_term is_pseudo_fun some_thm vars BR) ts)
   195                   @ str "="
   201                   @ str "="
   196                   @@ print_term is_pseudo_fun some_thm vars NOBR t
   202                   @@ print_term is_pseudo_fun some_thm vars NOBR t
   197                 )
   203                 )
   198               end
   204               end
   199             val shift = if null eqs then I else
   205             val shift = if null eqs then I else
   200               map (Pretty.block o single o Pretty.block o single);
   206               map (Pretty.block o single o Pretty.block o single);
   201           in pair
   207           in pair
   202             (print_val_decl print_typscheme (name, vs_ty))
   208             (print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty))
   203             ((Pretty.block o Pretty.fbreaks o shift) (
   209             ((Pretty.block o Pretty.fbreaks o shift) (
   204               print_eqn definer eq
   210               print_eqn definer eq
   205               :: map (print_eqn "|") eqs
   211               :: map (print_eqn "|") eqs
   206             ))
   212             ))
   207           end
   213           end
   208       | print_def is_pseudo_fun _ definer
   214       | print_def is_pseudo_fun _ definer
   209           (ML_Instance (inst, { class, tyco, vs, superinsts, inst_params, ... })) =
   215           (ML_Instance (inst as (tyco, class), { vs, superinsts, inst_params, ... })) =
   210           let
   216           let
   211             fun print_super_instance (_, (classrel, x)) =
   217             fun print_super_instance (super_class, x) =
   212               concat [
   218               concat [
   213                 (str o Long_Name.base_name o deresolve) classrel,
   219                 (str o Long_Name.base_name o deresolve_classrel) (class, super_class),
   214                 str "=",
   220                 str "=",
   215                 print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const x))
   221                 print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const ((tyco, super_class), x)))
   216               ];
   222               ];
   217             fun print_classparam_instance ((classparam, (const, _)), (thm, _)) =
   223             fun print_classparam_instance ((classparam, (const, _)), (thm, _)) =
   218               concat [
   224               concat [
   219                 (str o Long_Name.base_name o deresolve) classparam,
   225                 (str o Long_Name.base_name o deresolve_const) classparam,
   220                 str "=",
   226                 str "=",
   221                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   227                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   222               ];
   228               ];
   223           in pair
   229           in pair
   224             (print_val_decl print_dicttypscheme
   230             (print_val_decl print_dicttypscheme
   225               (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   231               (Code_Symbol.Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   226             (concat (
   232             (concat (
   227               str definer
   233               str definer
   228               :: (str o deresolve) inst
   234               :: (str o deresolve_inst) inst
   229               :: (if is_pseudo_fun inst then [str "()"]
   235               :: (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
   230                   else print_dict_args vs)
   236                   else print_dict_args vs)
   231               @ str "="
   237               @ str "="
   232               :: enum "," "{" "}"
   238               :: enum "," "{" "}"
   233                 (map print_super_instance superinsts
   239                 (map print_super_instance superinsts
   234                   @ map print_classparam_instance inst_params)
   240                   @ map print_classparam_instance inst_params)
   235               :: str ":"
   241               :: str ":"
   236               @@ print_tyco_expr (class, [tyco `%% map (ITyVar o fst) vs])
   242               @@ print_dicttyp (class, tyco `%% map (ITyVar o fst) vs)
   237             ))
   243             ))
   238           end;
   244           end;
   239     fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
   245     fun print_stmt (ML_Exc (const, (vs_ty, n))) = pair
   240           [print_val_decl print_typscheme (name, vs_ty)]
   246           [print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty)]
   241           ((semicolon o map str) (
   247           ((semicolon o map str) (
   242             (if n = 0 then "val" else "fun")
   248             (if n = 0 then "val" else "fun")
   243             :: deresolve name
   249             :: deresolve_const const
   244             :: replicate n "_"
   250             :: replicate n "_"
   245             @ "="
   251             @ "="
   246             :: "raise"
   252             :: "raise"
   247             :: "Fail"
   253             :: "Fail"
   248             @@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name
   254             @@ ML_Syntax.print_string const
   249           ))
   255           ))
   250       | print_stmt (ML_Val binding) =
   256       | print_stmt (ML_Val binding) =
   251           let
   257           let
   252             val (sig_p, p) = print_def (K false) true "val" binding
   258             val (sig_p, p) = print_def (K false) true "val" binding
   253           in pair
   259           in pair
   255             (semicolon [p])
   261             (semicolon [p])
   256           end
   262           end
   257       | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
   263       | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
   258           let
   264           let
   259             val print_def' = print_def (member (op =) pseudo_funs) false;
   265             val print_def' = print_def (member (op =) pseudo_funs) false;
   260             fun print_pseudo_fun name = concat [
   266             fun print_pseudo_fun sym = concat [
   261                 str "val",
   267                 str "val",
   262                 (str o deresolve) name,
   268                 (str o deresolve) sym,
   263                 str "=",
   269                 str "=",
   264                 (str o deresolve) name,
   270                 (str o deresolve) sym,
   265                 str "();"
   271                 str "();"
   266               ];
   272               ];
   267             val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
   273             val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
   268               (print_def' "fun" binding :: map (print_def' "and") bindings);
   274               (print_def' "fun" binding :: map (print_def' "and") bindings);
   269             val pseudo_ps = map print_pseudo_fun pseudo_funs;
   275             val pseudo_ps = map print_pseudo_fun pseudo_funs;
   271             sig_ps
   277             sig_ps
   272             (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
   278             (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
   273           end
   279           end
   274      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   280      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   275           let
   281           let
   276             val ty_p = print_tyco_expr (tyco, map ITyVar vs);
   282             val ty_p = print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs);
   277           in
   283           in
   278             pair
   284             pair
   279             [concat [str "type", ty_p]]
   285             [concat [str "type", ty_p]]
   280             (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
   286             (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
   281           end
   287           end
   286             val (ps, p) = split_last sig_ps;
   292             val (ps, p) = split_last sig_ps;
   287           in pair
   293           in pair
   288             sig_ps
   294             sig_ps
   289             (Pretty.chunks (ps @| semicolon [p]))
   295             (Pretty.chunks (ps @| semicolon [p]))
   290           end
   296           end
   291      | print_stmt (ML_Class (class, (v, (super_classes, classparams)))) =
   297      | print_stmt (ML_Class (class, (v, (classrels, classparams)))) =
   292           let
   298           let
   293             fun print_field s p = concat [str s, str ":", p];
   299             fun print_field s p = concat [str s, str ":", p];
   294             fun print_proj s p = semicolon
   300             fun print_proj s p = semicolon
   295               (map str ["val", s, "=", "#" ^ s, ":"] @| p);
   301               (map str ["val", s, "=", "#" ^ s, ":"] @| p);
   296             fun print_super_class_decl (super_class, classrel) =
   302             fun print_super_class_decl (classrel as (_, super_class)) =
   297               print_val_decl print_dicttypscheme
   303               print_val_decl print_dicttypscheme
   298                 (classrel, ([(v, [class])], (super_class, ITyVar v)));
   304                 (Code_Symbol.Class_Relation classrel, ([(v, [class])], (super_class, ITyVar v)));
   299             fun print_super_class_field (super_class, classrel) =
   305             fun print_super_class_field (classrel as (_, super_class)) =
   300               print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v));
   306               print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v));
   301             fun print_super_class_proj (super_class, classrel) =
   307             fun print_super_class_proj (classrel as (_, super_class)) =
   302               print_proj (deresolve classrel)
   308               print_proj (deresolve_classrel classrel)
   303                 (print_dicttypscheme ([(v, [class])], (super_class, ITyVar v)));
   309                 (print_dicttypscheme ([(v, [class])], (super_class, ITyVar v)));
   304             fun print_classparam_decl (classparam, ty) =
   310             fun print_classparam_decl (classparam, ty) =
   305               print_val_decl print_typscheme
   311               print_val_decl print_typscheme
   306                 (classparam, ([(v, [class])], ty));
   312                 (Code_Symbol.Constant classparam, ([(v, [class])], ty));
   307             fun print_classparam_field (classparam, ty) =
   313             fun print_classparam_field (classparam, ty) =
   308               print_field (deresolve classparam) (print_typ NOBR ty);
   314               print_field (deresolve_const classparam) (print_typ NOBR ty);
   309             fun print_classparam_proj (classparam, ty) =
   315             fun print_classparam_proj (classparam, ty) =
   310               print_proj (deresolve classparam)
   316               print_proj (deresolve_const classparam)
   311                 (print_typscheme ([(v, [class])], ty));
   317                 (print_typscheme ([(v, [class])], ty));
   312           in pair
   318           in pair
   313             (concat [str "type", print_dicttyp (class, ITyVar v)]
   319             (concat [str "type", print_dicttyp (class, ITyVar v)]
   314               :: map print_super_class_decl super_classes
   320               :: map print_super_class_decl classrels
   315               @ map print_classparam_decl classparams)
   321               @ map print_classparam_decl classparams)
   316             (Pretty.chunks (
   322             (Pretty.chunks (
   317               concat [
   323               concat [
   318                 str ("type '" ^ v),
   324                 str ("type '" ^ v),
   319                 (str o deresolve) class,
   325                 (str o deresolve_class) class,
   320                 str "=",
   326                 str "=",
   321                 enum "," "{" "};" (
   327                 enum "," "{" "};" (
   322                   map print_super_class_field super_classes
   328                   map print_super_class_field classrels
   323                   @ map print_classparam_field classparams
   329                   @ map print_classparam_field classparams
   324                 )
   330                 )
   325               ]
   331               ]
   326               :: map print_super_class_proj super_classes
   332               :: map print_super_class_proj classrels
   327               @ map print_classparam_proj classparams
   333               @ map print_classparam_proj classparams
   328             ))
   334             ))
   329           end;
   335           end;
   330   in print_stmt end;
   336   in print_stmt end;
   331 
   337 
   351 
   357 
   352 (** OCaml serializer **)
   358 (** OCaml serializer **)
   353 
   359 
   354 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
   360 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
   355   let
   361   let
   356     fun print_tyco_expr (tyco, []) = (str o deresolve) tyco
   362     val deresolve_const = deresolve o Code_Symbol.Constant;
   357       | print_tyco_expr (tyco, [ty]) =
   363     val deresolve_tyco = deresolve o Code_Symbol.Type_Constructor;
   358           concat [print_typ BR ty, (str o deresolve) tyco]
   364     val deresolve_class = deresolve o Code_Symbol.Type_Class;
   359       | print_tyco_expr (tyco, tys) =
   365     val deresolve_classrel = deresolve o Code_Symbol.Class_Relation;
   360           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
   366     val deresolve_inst = deresolve o Code_Symbol.Class_Instance;
       
   367     fun print_tyco_expr (sym, []) = (str o deresolve) sym
       
   368       | print_tyco_expr (sym, [ty]) =
       
   369           concat [print_typ BR ty, (str o deresolve) sym]
       
   370       | print_tyco_expr (sym, tys) =
       
   371           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym]
   361     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   372     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   362          of NONE => print_tyco_expr (tyco, tys)
   373          of NONE => print_tyco_expr (Code_Symbol.Type_Constructor tyco, tys)
   363           | SOME (_, print) => print print_typ fxy tys)
   374           | SOME (_, print) => print print_typ fxy tys)
   364       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   375       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   365     fun print_dicttyp (class, ty) = print_tyco_expr (class, [ty]);
   376     fun print_dicttyp (class, ty) = print_tyco_expr (Code_Symbol.Type_Class class, [ty]);
   366     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   377     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   367       (map_filter (fn (v, sort) =>
   378       (map_filter (fn (v, sort) =>
   368         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   379         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   369     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
   380     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
   370     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   381     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   371     val print_classrels =
   382     val print_classrels =
   372       fold_rev (fn classrel => fn p => Pretty.block [p, str ".", (str o deresolve) classrel])
   383       fold_rev (fn classrel => fn p => Pretty.block [p, str ".", (str o deresolve_classrel) classrel])
   373     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
   384     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
   374       print_plain_dict is_pseudo_fun fxy x
   385       print_plain_dict is_pseudo_fun fxy x
   375       |> print_classrels classrels
   386       |> print_classrels classrels
   376     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
   387     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
   377           brackify BR ((str o deresolve) inst ::
   388           brackify BR ((str o deresolve_inst) inst ::
   378             (if is_pseudo_fun inst then [str "()"]
   389             (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
   379             else map_filter (print_dicts is_pseudo_fun BR) dss))
   390             else map_filter (print_dicts is_pseudo_fun BR) dss))
   380       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
   391       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
   381           str (if k = 1 then "_" ^ first_upper v
   392           str (if k = 1 then "_" ^ first_upper v
   382             else "_" ^ first_upper v ^ string_of_int (i+1))
   393             else "_" ^ first_upper v ^ string_of_int (i+1))
   383     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
   394     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
   399             val (binds, t') = Code_Thingol.unfold_pat_abs t;
   410             val (binds, t') = Code_Thingol.unfold_pat_abs t;
   400             val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars;
   411             val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars;
   401           in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end
   412           in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end
   402       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   413       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   403           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   414           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   404            of SOME (app as ({ name = c, ... }, _)) => if is_none (const_syntax c)
   415            of SOME (app as ({ sym = Code_Symbol.Constant const, ... }, _)) =>
       
   416                 if is_none (const_syntax const)
   405                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   417                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   406                 else print_app is_pseudo_fun some_thm vars fxy app
   418                 else print_app is_pseudo_fun some_thm vars fxy app
   407             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   419             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   408     and print_app_expr is_pseudo_fun some_thm vars (app as ({ name = c, dicts = dss, dom = dom, ... }, ts)) =
   420     and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) =
   409       if is_constr c then
   421       if is_constr sym then
   410         let val k = length dom in
   422         let val k = length dom in
   411           if length ts = k
   423           if length ts = k
   412           then (str o deresolve) c
   424           then (str o deresolve) sym
   413             :: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts)
   425             :: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts)
   414           else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)]
   426           else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)]
   415         end
   427         end
   416       else if is_pseudo_fun c
   428       else if is_pseudo_fun sym
   417         then (str o deresolve) c @@ str "()"
   429         then (str o deresolve) sym @@ str "()"
   418       else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) dss
   430       else (str o deresolve) sym :: map_filter (print_dicts is_pseudo_fun BR) dss
   419         @ map (print_term is_pseudo_fun some_thm vars BR) ts
   431         @ map (print_term is_pseudo_fun some_thm vars BR) ts
   420     and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
   432     and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
   421       (print_term is_pseudo_fun) const_syntax some_thm vars
   433       (print_term is_pseudo_fun) const_syntax some_thm vars
   422     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   434     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   423     and print_case is_pseudo_fun some_thm vars fxy { clauses = [], ... } =
   435     and print_case is_pseudo_fun some_thm vars fxy { clauses = [], ... } =
   447               :: print_term is_pseudo_fun some_thm vars NOBR t
   459               :: print_term is_pseudo_fun some_thm vars NOBR t
   448               :: print_select "with" clause
   460               :: print_select "with" clause
   449               :: map (print_select "|") clauses
   461               :: map (print_select "|") clauses
   450             )
   462             )
   451           end;
   463           end;
   452     fun print_val_decl print_typscheme (name, typscheme) = concat
   464     fun print_val_decl print_typscheme (sym, typscheme) = concat
   453       [str "val", str (deresolve name), str ":", print_typscheme typscheme];
   465       [str "val", str (deresolve sym), str ":", print_typscheme typscheme];
   454     fun print_datatype_decl definer (tyco, (vs, cos)) =
   466     fun print_datatype_decl definer (tyco, (vs, cos)) =
   455       let
   467       let
   456         fun print_co ((co, _), []) = str (deresolve co)
   468         fun print_co ((co, _), []) = str (deresolve_const co)
   457           | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
   469           | print_co ((co, _), tys) = concat [str (deresolve_const co), str "of",
   458               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   470               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   459       in
   471       in
   460         concat (
   472         concat (
   461           str definer
   473           str definer
   462           :: print_tyco_expr (tyco, map ITyVar vs)
   474           :: print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs)
   463           :: str "="
   475           :: str "="
   464           :: separate (str "|") (map print_co cos)
   476           :: separate (str "|") (map print_co cos)
   465         )
   477         )
   466       end;
   478       end;
   467     fun print_def is_pseudo_fun needs_typ definer
   479     fun print_def is_pseudo_fun needs_typ definer
   468           (ML_Function (name, (vs_ty as (vs, ty), eqs))) =
   480           (ML_Function (const, (vs_ty as (vs, ty), eqs))) =
   469           let
   481           let
   470             fun print_eqn ((ts, t), (some_thm, _)) =
   482             fun print_eqn ((ts, t), (some_thm, _)) =
   471               let
   483               let
   472                 val vars = reserved
   484                 val vars = reserved
   473                   |> intro_base_names_for (is_none o const_syntax)
   485                   |> intro_base_names_for (is_none o const_syntax)
   527                       :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
   539                       :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
   528                            o single o print_eqn) eqs'
   540                            o single o print_eqn) eqs'
   529                     )
   541                     )
   530                   end;
   542                   end;
   531             val prolog = if needs_typ then
   543             val prolog = if needs_typ then
   532               concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty]
   544               concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty]
   533                 else (concat o map str) [definer, deresolve name];
   545                 else (concat o map str) [definer, deresolve_const const];
   534           in pair
   546           in pair
   535             (print_val_decl print_typscheme (name, vs_ty))
   547             (print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty))
   536             (concat (
   548             (concat (
   537               prolog
   549               prolog
   538               :: print_dict_args vs
   550               :: print_dict_args vs
   539               @| print_eqns (is_pseudo_fun name) eqs
   551               @| print_eqns (is_pseudo_fun (Code_Symbol.Constant const)) eqs
   540             ))
   552             ))
   541           end
   553           end
   542       | print_def is_pseudo_fun _ definer
   554       | print_def is_pseudo_fun _ definer
   543           (ML_Instance (inst, { class, tyco, vs, superinsts, inst_params, ... })) =
   555           (ML_Instance (inst as (tyco, class), { vs, superinsts, inst_params, ... })) =
   544           let
   556           let
   545             fun print_super_instance (_, (classrel, x)) =
   557             fun print_super_instance (super_class, x) =
   546               concat [
   558               concat [
   547                 (str o deresolve) classrel,
   559                 (str o deresolve_classrel) (class, super_class),
   548                 str "=",
   560                 str "=",
   549                 print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const x))
   561                 print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const ((tyco, super_class), x)))
   550               ];
   562               ];
   551             fun print_classparam_instance ((classparam, (const, _)), (thm, _)) =
   563             fun print_classparam_instance ((classparam, (const, _)), (thm, _)) =
   552               concat [
   564               concat [
   553                 (str o deresolve) classparam,
   565                 (str o deresolve_const) classparam,
   554                 str "=",
   566                 str "=",
   555                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   567                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   556               ];
   568               ];
   557           in pair
   569           in pair
   558             (print_val_decl print_dicttypscheme
   570             (print_val_decl print_dicttypscheme
   559               (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   571               (Code_Symbol.Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   560             (concat (
   572             (concat (
   561               str definer
   573               str definer
   562               :: (str o deresolve) inst
   574               :: (str o deresolve_inst) inst
   563               :: (if is_pseudo_fun inst then [str "()"]
   575               :: (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
   564                   else print_dict_args vs)
   576                   else print_dict_args vs)
   565               @ str "="
   577               @ str "="
   566               @@ brackets [
   578               @@ brackets [
   567                 enum_default "()" ";" "{" "}" (map print_super_instance superinsts
   579                 enum_default "()" ";" "{" "}" (map print_super_instance superinsts
   568                   @ map print_classparam_instance inst_params),
   580                   @ map print_classparam_instance inst_params),
   569                 str ":",
   581                 str ":",
   570                 print_tyco_expr (class, [tyco `%% map (ITyVar o fst) vs])
   582                 print_dicttyp (class, tyco `%% map (ITyVar o fst) vs)
   571               ]
   583               ]
   572             ))
   584             ))
   573           end;
   585           end;
   574      fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
   586      fun print_stmt (ML_Exc (const, (vs_ty, n))) = pair
   575           [print_val_decl print_typscheme (name, vs_ty)]
   587           [print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty)]
   576           ((doublesemicolon o map str) (
   588           ((doublesemicolon o map str) (
   577             "let"
   589             "let"
   578             :: deresolve name
   590             :: deresolve_const const
   579             :: replicate n "_"
   591             :: replicate n "_"
   580             @ "="
   592             @ "="
   581             :: "failwith"
   593             :: "failwith"
   582             @@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name
   594             @@ ML_Syntax.print_string const
   583           ))
   595           ))
   584       | print_stmt (ML_Val binding) =
   596       | print_stmt (ML_Val binding) =
   585           let
   597           let
   586             val (sig_p, p) = print_def (K false) true "let" binding
   598             val (sig_p, p) = print_def (K false) true "let" binding
   587           in pair
   599           in pair
   589             (doublesemicolon [p])
   601             (doublesemicolon [p])
   590           end
   602           end
   591       | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
   603       | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
   592           let
   604           let
   593             val print_def' = print_def (member (op =) pseudo_funs) false;
   605             val print_def' = print_def (member (op =) pseudo_funs) false;
   594             fun print_pseudo_fun name = concat [
   606             fun print_pseudo_fun sym = concat [
   595                 str "let",
   607                 str "let",
   596                 (str o deresolve) name,
   608                 (str o deresolve) sym,
   597                 str "=",
   609                 str "=",
   598                 (str o deresolve) name,
   610                 (str o deresolve) sym,
   599                 str "();;"
   611                 str "();;"
   600               ];
   612               ];
   601             val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
   613             val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
   602               (print_def' "let rec" binding :: map (print_def' "and") bindings);
   614               (print_def' "let rec" binding :: map (print_def' "and") bindings);
   603             val pseudo_ps = map print_pseudo_fun pseudo_funs;
   615             val pseudo_ps = map print_pseudo_fun pseudo_funs;
   605             sig_ps
   617             sig_ps
   606             (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
   618             (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
   607           end
   619           end
   608      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   620      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   609           let
   621           let
   610             val ty_p = print_tyco_expr (tyco, map ITyVar vs);
   622             val ty_p = print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs);
   611           in
   623           in
   612             pair
   624             pair
   613             [concat [str "type", ty_p]]
   625             [concat [str "type", ty_p]]
   614             (concat [str "type", ty_p, str "=", str "EMPTY__"])
   626             (concat [str "type", ty_p, str "=", str "EMPTY__"])
   615           end
   627           end
   620             val (ps, p) = split_last sig_ps;
   632             val (ps, p) = split_last sig_ps;
   621           in pair
   633           in pair
   622             sig_ps
   634             sig_ps
   623             (Pretty.chunks (ps @| doublesemicolon [p]))
   635             (Pretty.chunks (ps @| doublesemicolon [p]))
   624           end
   636           end
   625      | print_stmt (ML_Class (class, (v, (super_classes, classparams)))) =
   637      | print_stmt (ML_Class (class, (v, (classrels, classparams)))) =
   626           let
   638           let
   627             fun print_field s p = concat [str s, str ":", p];
   639             fun print_field s p = concat [str s, str ":", p];
   628             fun print_super_class_field (super_class, classrel) =
   640             fun print_super_class_field (classrel as (_, super_class)) =
   629               print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v));
   641               print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v));
   630             fun print_classparam_decl (classparam, ty) =
   642             fun print_classparam_decl (classparam, ty) =
   631               print_val_decl print_typscheme
   643               print_val_decl print_typscheme
   632                 (classparam, ([(v, [class])], ty));
   644                 (Code_Symbol.Constant classparam, ([(v, [class])], ty));
   633             fun print_classparam_field (classparam, ty) =
   645             fun print_classparam_field (classparam, ty) =
   634               print_field (deresolve classparam) (print_typ NOBR ty);
   646               print_field (deresolve_const classparam) (print_typ NOBR ty);
   635             val w = "_" ^ first_upper v;
   647             val w = "_" ^ first_upper v;
   636             fun print_classparam_proj (classparam, _) =
   648             fun print_classparam_proj (classparam, _) =
   637               (concat o map str) ["let", deresolve classparam, w, "=",
   649               (concat o map str) ["let", deresolve_const classparam, w, "=",
   638                 w ^ "." ^ deresolve classparam ^ ";;"];
   650                 w ^ "." ^ deresolve_const classparam ^ ";;"];
   639             val type_decl_p = concat [
   651             val type_decl_p = concat [
   640                 str ("type '" ^ v),
   652                 str ("type '" ^ v),
   641                 (str o deresolve) class,
   653                 (str o deresolve_class) class,
   642                 str "=",
   654                 str "=",
   643                 enum_default "unit" ";" "{" "}" (
   655                 enum_default "unit" ";" "{" "}" (
   644                   map print_super_class_field super_classes
   656                   map print_super_class_field classrels
   645                   @ map print_classparam_field classparams
   657                   @ map print_classparam_field classparams
   646                 )
   658                 )
   647               ];
   659               ];
   648           in pair
   660           in pair
   649             (type_decl_p :: map print_classparam_decl classparams)
   661             (type_decl_p :: map print_classparam_decl classparams)
   692 
   704 
   693 
   705 
   694 
   706 
   695 (** SML/OCaml generic part **)
   707 (** SML/OCaml generic part **)
   696 
   708 
   697 fun ml_program_of_program ctxt symbol_of module_name reserved identifiers program =
   709 fun ml_program_of_program ctxt module_name reserved identifiers program =
   698   let
   710   let
   699     fun namify_const upper base (nsp_const, nsp_type) =
   711     fun namify_const upper base (nsp_const, nsp_type) =
   700       let
   712       let
   701         val (base', nsp_const') =
   713         val (base', nsp_const') =
   702           Name.variant (if upper then first_upper base else base) nsp_const
   714           Name.variant (if upper then first_upper base else base) nsp_const
   710       | namify_stmt (Code_Thingol.Datatypecons _) = namify_const true
   722       | namify_stmt (Code_Thingol.Datatypecons _) = namify_const true
   711       | namify_stmt (Code_Thingol.Class _) = namify_type
   723       | namify_stmt (Code_Thingol.Class _) = namify_type
   712       | namify_stmt (Code_Thingol.Classrel _) = namify_const false
   724       | namify_stmt (Code_Thingol.Classrel _) = namify_const false
   713       | namify_stmt (Code_Thingol.Classparam _) = namify_const false
   725       | namify_stmt (Code_Thingol.Classparam _) = namify_const false
   714       | namify_stmt (Code_Thingol.Classinst _) = namify_const false;
   726       | namify_stmt (Code_Thingol.Classinst _) = namify_const false;
   715     fun ml_binding_of_stmt (name, Code_Thingol.Fun (_, ((tysm as (vs, ty), raw_eqs), _))) =
   727     fun ml_binding_of_stmt (sym as Code_Symbol.Constant const, Code_Thingol.Fun ((tysm as (vs, ty), raw_eqs), _)) =
   716           let
   728           let
   717             val eqs = filter (snd o snd) raw_eqs;
   729             val eqs = filter (snd o snd) raw_eqs;
   718             val (eqs', some_value_name) = if null (filter_out (null o snd) vs) then case eqs
   730             val (eqs', some_sym) = if null (filter_out (null o snd) vs) then case eqs
   719                of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
   731                of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
   720                   then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE)
   732                   then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE)
   721                   else (eqs, SOME (name, member (op =) (Code_Thingol.add_constnames t []) name))
   733                   else (eqs, SOME (sym, member (op =) (Code_Thingol.add_constsyms t []) sym))
   722                 | _ => (eqs, NONE)
   734                 | _ => (eqs, NONE)
   723               else (eqs, NONE)
   735               else (eqs, NONE)
   724           in (ML_Function (name, (tysm, eqs')), some_value_name) end
   736           in (ML_Function (const, (tysm, eqs')), some_sym) end
   725       | ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as { vs, ... })) =
   737       | ml_binding_of_stmt (sym as Code_Symbol.Class_Instance inst, Code_Thingol.Classinst (stmt as { vs, ... })) =
   726           (ML_Instance (name, stmt), if forall (null o snd) vs then SOME (name, false) else NONE)
   738           (ML_Instance (inst, stmt), if forall (null o snd) vs then SOME (sym, false) else NONE)
   727       | ml_binding_of_stmt (name, _) =
   739       | ml_binding_of_stmt (sym, _) =
   728           error ("Binding block containing illegal statement: " ^ 
   740           error ("Binding block containing illegal statement: " ^ 
   729             (Code_Symbol.quote_symbol ctxt o symbol_of) name)
   741             Code_Symbol.quote ctxt sym)
   730     fun modify_fun (name, stmt) =
   742     fun modify_fun (sym, stmt) =
   731       let
   743       let
   732         val (binding, some_value_name) = ml_binding_of_stmt (name, stmt);
   744         val (binding, some_value_sym) = ml_binding_of_stmt (sym, stmt);
   733         val ml_stmt = case binding
   745         val ml_stmt = case binding
   734          of ML_Function (name, ((vs, ty), [])) =>
   746          of ML_Function (const, ((vs, ty), [])) =>
   735               ML_Exc (name, ((vs, ty),
   747               ML_Exc (const, ((vs, ty),
   736                 (length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty))
   748                 (length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty))
   737           | _ => case some_value_name
   749           | _ => case some_value_sym
   738              of NONE => ML_Funs ([binding], [])
   750              of NONE => ML_Funs ([binding], [])
   739               | SOME (name, true) => ML_Funs ([binding], [name])
   751               | SOME (sym, true) => ML_Funs ([binding], [sym])
   740               | SOME (name, false) => ML_Val binding
   752               | SOME (sym, false) => ML_Val binding
   741       in SOME ml_stmt end;
   753       in SOME ml_stmt end;
   742     fun modify_funs stmts = single (SOME
   754     fun modify_funs stmts = single (SOME
   743       (ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst)))
   755       (ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst)))
   744     fun modify_datatypes stmts = single (SOME
   756     fun modify_datatypes stmts = single (SOME
   745       (ML_Datas (map_filter
   757       (ML_Datas (map_filter
   746         (fn (name, Code_Thingol.Datatype (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts)))
   758         (fn (Code_Symbol.Type_Constructor tyco, Code_Thingol.Datatype stmt) => SOME (tyco, stmt) | _ => NONE) stmts)))
   747     fun modify_class stmts = single (SOME
   759     fun modify_class stmts = single (SOME
   748       (ML_Class (the_single (map_filter
   760       (ML_Class (the_single (map_filter
   749         (fn (name, Code_Thingol.Class (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts))))
   761         (fn (Code_Symbol.Type_Class class, Code_Thingol.Class stmt) => SOME (class, stmt) | _ => NONE) stmts))))
   750     fun modify_stmts ([stmt as (_, stmt' as Code_Thingol.Fun _)]) =
   762     fun modify_stmts ([stmt as (_, stmt' as Code_Thingol.Fun _)]) =
   751           if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
   763           if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
   752       | modify_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) =
   764       | modify_stmts ((stmts as (_, Code_Thingol.Fun _) :: _)) =
   753           modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
   765           modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
   754       | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) =
   766       | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _) :: _)) =
   755           modify_datatypes stmts
   767           modify_datatypes stmts
   756       | modify_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) =
   768       | modify_stmts ((stmts as (_, Code_Thingol.Datatype _) :: _)) =
   757           modify_datatypes stmts
   769           modify_datatypes stmts
   758       | modify_stmts ((stmts as (_, Code_Thingol.Class _)::_)) =
   770       | modify_stmts ((stmts as (_, Code_Thingol.Class _) :: _)) =
   759           modify_class stmts
   771           modify_class stmts
   760       | modify_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) =
   772       | modify_stmts ((stmts as (_, Code_Thingol.Classrel _) :: _)) =
   761           modify_class stmts
   773           modify_class stmts
   762       | modify_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) =
   774       | modify_stmts ((stmts as (_, Code_Thingol.Classparam _) :: _)) =
   763           modify_class stmts
   775           modify_class stmts
   764       | modify_stmts ([stmt as (_, Code_Thingol.Classinst _)]) =
   776       | modify_stmts ([stmt as (_, Code_Thingol.Classinst _)]) =
   765           [modify_fun stmt]
   777           [modify_fun stmt]
   766       | modify_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) =
   778       | modify_stmts ((stmts as (_, Code_Thingol.Classinst _) :: _)) =
   767           modify_funs stmts
   779           modify_funs stmts
   768       | modify_stmts stmts = error ("Illegal mutual dependencies: " ^
   780       | modify_stmts stmts = error ("Illegal mutual dependencies: " ^
   769           (Library.commas o map (Code_Symbol.quote_symbol ctxt o symbol_of o fst)) stmts);
   781           (Library.commas o map (Code_Symbol.quote ctxt o fst)) stmts);
   770   in
   782   in
   771     Code_Namespace.hierarchical_program ctxt symbol_of {
   783     Code_Namespace.hierarchical_program ctxt {
   772       module_name = module_name, reserved = reserved, identifiers = identifiers,
   784       module_name = module_name, reserved = reserved, identifiers = identifiers,
   773       empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt,
   785       empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt,
   774       cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program
   786       cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program
   775   end;
   787   end;
   776 
   788 
   777 fun serialize_ml print_ml_module print_ml_stmt with_signatures ctxt
   789 fun serialize_ml print_ml_module print_ml_stmt with_signatures ctxt
   778     { symbol_of, module_name, reserved_syms, identifiers, includes,
   790     { module_name, reserved_syms, identifiers, includes,
   779       class_syntax, tyco_syntax, const_syntax } program =
   791       class_syntax, tyco_syntax, const_syntax } program =
   780   let
   792   let
   781 
   793 
   782     (* build program *)
   794     (* build program *)
   783     val { deresolver, hierarchical_program = ml_program } =
   795     val { deresolver, hierarchical_program = ml_program } =
   784       ml_program_of_program ctxt symbol_of module_name (Name.make_context reserved_syms) identifiers program;
   796       ml_program_of_program ctxt module_name (Name.make_context reserved_syms) identifiers program;
   785 
   797 
   786     (* print statements *)
   798     (* print statements *)
   787     fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt
   799     fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt
   788       tyco_syntax const_syntax (make_vars reserved_syms)
   800       tyco_syntax const_syntax (make_vars reserved_syms)
   789       (Code_Thingol.is_constr program) (deresolver prefix_fragments) stmt
   801       (Code_Thingol.is_constr program) (deresolver prefix_fragments) stmt
   801       @ map snd (Code_Namespace.print_hierarchical {
   813       @ map snd (Code_Namespace.print_hierarchical {
   802         print_module = print_module, print_stmt = print_stmt,
   814         print_module = print_module, print_stmt = print_stmt,
   803         lift_markup = apsnd } ml_program));
   815         lift_markup = apsnd } ml_program));
   804     fun write width NONE = writeln o format [] width
   816     fun write width NONE = writeln o format [] width
   805       | write width (SOME p) = File.write p o format [] width;
   817       | write width (SOME p) = File.write p o format [] width;
   806     fun prepare names width p = ([("", format names width p)], try (deresolver []));
   818     fun prepare syms width p = ([("", format syms width p)], try (deresolver []));
   807   in
   819   in
   808     Code_Target.serialization write prepare p
   820     Code_Target.serialization write prepare p
   809   end;
   821   end;
   810 
   822 
   811 val serializer_sml : Code_Target.serializer =
   823 val serializer_sml : Code_Target.serializer =