src/Tools/Code/code_ml.ML
changeset 55150 0940309ed8f1
parent 55147 bce3dbc11f95
child 55657 d5ad50aea356
equal deleted inserted replaced
55149:626d8f08d479 55150:0940309ed8f1
    12 end;
    12 end;
    13 
    13 
    14 structure Code_ML : CODE_ML =
    14 structure Code_ML : CODE_ML =
    15 struct
    15 struct
    16 
    16 
       
    17 open Basic_Code_Symbol;
    17 open Basic_Code_Thingol;
    18 open Basic_Code_Thingol;
    18 open Code_Printer;
    19 open Code_Printer;
    19 
    20 
    20 infixr 5 @@;
    21 infixr 5 @@;
    21 infixr 5 @|;
    22 infixr 5 @|;
    34         superinst_params: ((string * (const * int)) * (thm * bool)) list };
    35         superinst_params: ((string * (const * int)) * (thm * bool)) list };
    35 
    36 
    36 datatype ml_stmt =
    37 datatype ml_stmt =
    37     ML_Exc of string * (typscheme * int)
    38     ML_Exc of string * (typscheme * int)
    38   | ML_Val of ml_binding
    39   | ML_Val of ml_binding
    39   | ML_Funs of ml_binding list * Code_Symbol.symbol list
    40   | ML_Funs of ml_binding list * Code_Symbol.T list
    40   | ML_Datas of (string * (vname list * ((string * vname list) * itype list) list)) list
    41   | ML_Datas of (string * (vname list * ((string * vname list) * itype list) list)) list
    41   | ML_Class of string * (vname * ((class * class) list * (string * itype) list));
    42   | ML_Class of string * (vname * ((class * class) list * (string * itype) list));
    42 
    43 
    43 fun print_product _ [] = NONE
    44 fun print_product _ [] = NONE
    44   | print_product print [x] = SOME (print x)
    45   | print_product print [x] = SOME (print x)
    51 
    52 
    52 (** SML serializer **)
    53 (** SML serializer **)
    53 
    54 
    54 fun print_sml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
    55 fun print_sml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
    55   let
    56   let
    56     val deresolve_const = deresolve o Code_Symbol.Constant;
    57     val deresolve_const = deresolve o Constant;
    57     val deresolve_tyco = deresolve o Code_Symbol.Type_Constructor;
    58     val deresolve_tyco = deresolve o Type_Constructor;
    58     val deresolve_class = deresolve o Code_Symbol.Type_Class;
    59     val deresolve_class = deresolve o Type_Class;
    59     val deresolve_classrel = deresolve o Code_Symbol.Class_Relation;
    60     val deresolve_classrel = deresolve o Class_Relation;
    60     val deresolve_inst = deresolve o Code_Symbol.Class_Instance;
    61     val deresolve_inst = deresolve o Class_Instance;
    61     fun print_tyco_expr (sym, []) = (str o deresolve) sym
    62     fun print_tyco_expr (sym, []) = (str o deresolve) sym
    62       | print_tyco_expr (sym, [ty]) =
    63       | print_tyco_expr (sym, [ty]) =
    63           concat [print_typ BR ty, (str o deresolve) sym]
    64           concat [print_typ BR ty, (str o deresolve) sym]
    64       | print_tyco_expr (sym, tys) =
    65       | print_tyco_expr (sym, tys) =
    65           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym]
    66           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym]
    66     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
    67     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
    67          of NONE => print_tyco_expr (Code_Symbol.Type_Constructor tyco, tys)
    68          of NONE => print_tyco_expr (Type_Constructor tyco, tys)
    68           | SOME (_, print) => print print_typ fxy tys)
    69           | SOME (_, print) => print print_typ fxy tys)
    69       | print_typ fxy (ITyVar v) = str ("'" ^ v);
    70       | print_typ fxy (ITyVar v) = str ("'" ^ v);
    70     fun print_dicttyp (class, ty) = print_tyco_expr (Code_Symbol.Type_Class class, [ty]);
    71     fun print_dicttyp (class, ty) = print_tyco_expr (Type_Class class, [ty]);
    71     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
    72     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
    72       (map_filter (fn (v, sort) =>
    73       (map_filter (fn (v, sort) =>
    73         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
    74         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
    74     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
    75     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
    75     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
    76     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
    79           brackify fxy [enum " o" "(" ")" (map (str o deresolve_classrel) classrels), brackify BR ps]
    80           brackify fxy [enum " o" "(" ")" (map (str o deresolve_classrel) classrels), brackify BR ps]
    80     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
    81     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
    81       print_classrels fxy classrels (print_plain_dict is_pseudo_fun fxy x)
    82       print_classrels fxy classrels (print_plain_dict is_pseudo_fun fxy x)
    82     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
    83     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
    83           ((str o deresolve_inst) inst ::
    84           ((str o deresolve_inst) inst ::
    84             (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
    85             (if is_pseudo_fun (Class_Instance inst) then [str "()"]
    85             else map_filter (print_dicts is_pseudo_fun BR) dss))
    86             else map_filter (print_dicts is_pseudo_fun BR) dss))
    86       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
    87       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
    87           [str (if k = 1 then first_upper v ^ "_"
    88           [str (if k = 1 then first_upper v ^ "_"
    88             else first_upper v ^ string_of_int (i+1) ^ "_")]
    89             else first_upper v ^ string_of_int (i+1) ^ "_")]
    89     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
    90     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
   108               #>> (fn p => concat [str "fn", p, str "=>"]);
   109               #>> (fn p => concat [str "fn", p, str "=>"]);
   109             val (ps, vars') = fold_map print_abs binds vars;
   110             val (ps, vars') = fold_map print_abs binds vars;
   110           in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end
   111           in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end
   111       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   112       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   112           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   113           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   113            of SOME (app as ({ sym = Code_Symbol.Constant const, ... }, _)) =>
   114            of SOME (app as ({ sym = Constant const, ... }, _)) =>
   114                 if is_none (const_syntax const)
   115                 if is_none (const_syntax const)
   115                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   116                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   116                 else print_app is_pseudo_fun some_thm vars fxy app
   117                 else print_app is_pseudo_fun some_thm vars fxy app
   117             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   118             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   118     and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) =
   119     and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) =
   172           | print_co ((co, _), tys) = concat [str (deresolve_const co), str "of",
   173           | print_co ((co, _), tys) = concat [str (deresolve_const co), str "of",
   173               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   174               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   174       in
   175       in
   175         concat (
   176         concat (
   176           str definer
   177           str definer
   177           :: print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs)
   178           :: print_tyco_expr (Type_Constructor tyco, map ITyVar vs)
   178           :: str "="
   179           :: str "="
   179           :: separate (str "|") (map print_co cos)
   180           :: separate (str "|") (map print_co cos)
   180         )
   181         )
   181       end;
   182       end;
   182     fun print_def is_pseudo_fun needs_typ definer
   183     fun print_def is_pseudo_fun needs_typ definer
   193                   concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty]
   194                   concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty]
   194                     else (concat o map str) [definer, deresolve_const const];
   195                     else (concat o map str) [definer, deresolve_const const];
   195               in
   196               in
   196                 concat (
   197                 concat (
   197                   prolog
   198                   prolog
   198                   :: (if is_pseudo_fun (Code_Symbol.Constant const) then [str "()"]
   199                   :: (if is_pseudo_fun (Constant const) then [str "()"]
   199                       else print_dict_args vs
   200                       else print_dict_args vs
   200                         @ map (print_term is_pseudo_fun some_thm vars BR) ts)
   201                         @ map (print_term is_pseudo_fun some_thm vars BR) ts)
   201                   @ str "="
   202                   @ str "="
   202                   @@ print_term is_pseudo_fun some_thm vars NOBR t
   203                   @@ print_term is_pseudo_fun some_thm vars NOBR t
   203                 )
   204                 )
   204               end
   205               end
   205             val shift = if null eqs then I else
   206             val shift = if null eqs then I else
   206               map (Pretty.block o single o Pretty.block o single);
   207               map (Pretty.block o single o Pretty.block o single);
   207           in pair
   208           in pair
   208             (print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty))
   209             (print_val_decl print_typscheme (Constant const, vs_ty))
   209             ((Pretty.block o Pretty.fbreaks o shift) (
   210             ((Pretty.block o Pretty.fbreaks o shift) (
   210               print_eqn definer eq
   211               print_eqn definer eq
   211               :: map (print_eqn "|") eqs
   212               :: map (print_eqn "|") eqs
   212             ))
   213             ))
   213           end
   214           end
   226                 str "=",
   227                 str "=",
   227                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   228                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   228               ];
   229               ];
   229           in pair
   230           in pair
   230             (print_val_decl print_dicttypscheme
   231             (print_val_decl print_dicttypscheme
   231               (Code_Symbol.Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   232               (Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   232             (concat (
   233             (concat (
   233               str definer
   234               str definer
   234               :: (str o deresolve_inst) inst
   235               :: (str o deresolve_inst) inst
   235               :: (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
   236               :: (if is_pseudo_fun (Class_Instance inst) then [str "()"]
   236                   else print_dict_args vs)
   237                   else print_dict_args vs)
   237               @ str "="
   238               @ str "="
   238               :: enum "," "{" "}"
   239               :: enum "," "{" "}"
   239                 (map print_super_instance superinsts
   240                 (map print_super_instance superinsts
   240                   @ map print_classparam_instance inst_params)
   241                   @ map print_classparam_instance inst_params)
   241               :: str ":"
   242               :: str ":"
   242               @@ print_dicttyp (class, tyco `%% map (ITyVar o fst) vs)
   243               @@ print_dicttyp (class, tyco `%% map (ITyVar o fst) vs)
   243             ))
   244             ))
   244           end;
   245           end;
   245     fun print_stmt (ML_Exc (const, (vs_ty, n))) = pair
   246     fun print_stmt (ML_Exc (const, (vs_ty, n))) = pair
   246           [print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty)]
   247           [print_val_decl print_typscheme (Constant const, vs_ty)]
   247           ((semicolon o map str) (
   248           ((semicolon o map str) (
   248             (if n = 0 then "val" else "fun")
   249             (if n = 0 then "val" else "fun")
   249             :: deresolve_const const
   250             :: deresolve_const const
   250             :: replicate n "_"
   251             :: replicate n "_"
   251             @ "="
   252             @ "="
   277             sig_ps
   278             sig_ps
   278             (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
   279             (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
   279           end
   280           end
   280      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   281      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   281           let
   282           let
   282             val ty_p = print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs);
   283             val ty_p = print_tyco_expr (Type_Constructor tyco, map ITyVar vs);
   283           in
   284           in
   284             pair
   285             pair
   285             [concat [str "type", ty_p]]
   286             [concat [str "type", ty_p]]
   286             (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
   287             (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
   287           end
   288           end
   299             fun print_field s p = concat [str s, str ":", p];
   300             fun print_field s p = concat [str s, str ":", p];
   300             fun print_proj s p = semicolon
   301             fun print_proj s p = semicolon
   301               (map str ["val", s, "=", "#" ^ s, ":"] @| p);
   302               (map str ["val", s, "=", "#" ^ s, ":"] @| p);
   302             fun print_super_class_decl (classrel as (_, super_class)) =
   303             fun print_super_class_decl (classrel as (_, super_class)) =
   303               print_val_decl print_dicttypscheme
   304               print_val_decl print_dicttypscheme
   304                 (Code_Symbol.Class_Relation classrel, ([(v, [class])], (super_class, ITyVar v)));
   305                 (Class_Relation classrel, ([(v, [class])], (super_class, ITyVar v)));
   305             fun print_super_class_field (classrel as (_, super_class)) =
   306             fun print_super_class_field (classrel as (_, super_class)) =
   306               print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v));
   307               print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v));
   307             fun print_super_class_proj (classrel as (_, super_class)) =
   308             fun print_super_class_proj (classrel as (_, super_class)) =
   308               print_proj (deresolve_classrel classrel)
   309               print_proj (deresolve_classrel classrel)
   309                 (print_dicttypscheme ([(v, [class])], (super_class, ITyVar v)));
   310                 (print_dicttypscheme ([(v, [class])], (super_class, ITyVar v)));
   310             fun print_classparam_decl (classparam, ty) =
   311             fun print_classparam_decl (classparam, ty) =
   311               print_val_decl print_typscheme
   312               print_val_decl print_typscheme
   312                 (Code_Symbol.Constant classparam, ([(v, [class])], ty));
   313                 (Constant classparam, ([(v, [class])], ty));
   313             fun print_classparam_field (classparam, ty) =
   314             fun print_classparam_field (classparam, ty) =
   314               print_field (deresolve_const classparam) (print_typ NOBR ty);
   315               print_field (deresolve_const classparam) (print_typ NOBR ty);
   315             fun print_classparam_proj (classparam, ty) =
   316             fun print_classparam_proj (classparam, ty) =
   316               print_proj (deresolve_const classparam)
   317               print_proj (deresolve_const classparam)
   317                 (print_typscheme ([(v, [class])], ty));
   318                 (print_typscheme ([(v, [class])], ty));
   357 
   358 
   358 (** OCaml serializer **)
   359 (** OCaml serializer **)
   359 
   360 
   360 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
   361 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_constr deresolve =
   361   let
   362   let
   362     val deresolve_const = deresolve o Code_Symbol.Constant;
   363     val deresolve_const = deresolve o Constant;
   363     val deresolve_tyco = deresolve o Code_Symbol.Type_Constructor;
   364     val deresolve_tyco = deresolve o Type_Constructor;
   364     val deresolve_class = deresolve o Code_Symbol.Type_Class;
   365     val deresolve_class = deresolve o Type_Class;
   365     val deresolve_classrel = deresolve o Code_Symbol.Class_Relation;
   366     val deresolve_classrel = deresolve o Class_Relation;
   366     val deresolve_inst = deresolve o Code_Symbol.Class_Instance;
   367     val deresolve_inst = deresolve o Class_Instance;
   367     fun print_tyco_expr (sym, []) = (str o deresolve) sym
   368     fun print_tyco_expr (sym, []) = (str o deresolve) sym
   368       | print_tyco_expr (sym, [ty]) =
   369       | print_tyco_expr (sym, [ty]) =
   369           concat [print_typ BR ty, (str o deresolve) sym]
   370           concat [print_typ BR ty, (str o deresolve) sym]
   370       | print_tyco_expr (sym, tys) =
   371       | print_tyco_expr (sym, tys) =
   371           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym]
   372           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym]
   372     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   373     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   373          of NONE => print_tyco_expr (Code_Symbol.Type_Constructor tyco, tys)
   374          of NONE => print_tyco_expr (Type_Constructor tyco, tys)
   374           | SOME (_, print) => print print_typ fxy tys)
   375           | SOME (_, print) => print print_typ fxy tys)
   375       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   376       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   376     fun print_dicttyp (class, ty) = print_tyco_expr (Code_Symbol.Type_Class class, [ty]);
   377     fun print_dicttyp (class, ty) = print_tyco_expr (Type_Class class, [ty]);
   377     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   378     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   378       (map_filter (fn (v, sort) =>
   379       (map_filter (fn (v, sort) =>
   379         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   380         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   380     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
   381     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
   381     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   382     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   384     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
   385     fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) =
   385       print_plain_dict is_pseudo_fun fxy x
   386       print_plain_dict is_pseudo_fun fxy x
   386       |> print_classrels classrels
   387       |> print_classrels classrels
   387     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
   388     and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) =
   388           brackify BR ((str o deresolve_inst) inst ::
   389           brackify BR ((str o deresolve_inst) inst ::
   389             (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
   390             (if is_pseudo_fun (Class_Instance inst) then [str "()"]
   390             else map_filter (print_dicts is_pseudo_fun BR) dss))
   391             else map_filter (print_dicts is_pseudo_fun BR) dss))
   391       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
   392       | print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) =
   392           str (if k = 1 then "_" ^ first_upper v
   393           str (if k = 1 then "_" ^ first_upper v
   393             else "_" ^ first_upper v ^ string_of_int (i+1))
   394             else "_" ^ first_upper v ^ string_of_int (i+1))
   394     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
   395     and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
   410             val (binds, t') = Code_Thingol.unfold_pat_abs t;
   411             val (binds, t') = Code_Thingol.unfold_pat_abs t;
   411             val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars;
   412             val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars;
   412           in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end
   413           in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end
   413       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   414       | print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) =
   414           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   415           (case Code_Thingol.unfold_const_app (#primitive case_expr)
   415            of SOME (app as ({ sym = Code_Symbol.Constant const, ... }, _)) =>
   416            of SOME (app as ({ sym = Constant const, ... }, _)) =>
   416                 if is_none (const_syntax const)
   417                 if is_none (const_syntax const)
   417                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   418                 then print_case is_pseudo_fun some_thm vars fxy case_expr
   418                 else print_app is_pseudo_fun some_thm vars fxy app
   419                 else print_app is_pseudo_fun some_thm vars fxy app
   419             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   420             | NONE => print_case is_pseudo_fun some_thm vars fxy case_expr)
   420     and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) =
   421     and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) =
   469           | print_co ((co, _), tys) = concat [str (deresolve_const co), str "of",
   470           | print_co ((co, _), tys) = concat [str (deresolve_const co), str "of",
   470               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   471               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   471       in
   472       in
   472         concat (
   473         concat (
   473           str definer
   474           str definer
   474           :: print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs)
   475           :: print_tyco_expr (Type_Constructor tyco, map ITyVar vs)
   475           :: str "="
   476           :: str "="
   476           :: separate (str "|") (map print_co cos)
   477           :: separate (str "|") (map print_co cos)
   477         )
   478         )
   478       end;
   479       end;
   479     fun print_def is_pseudo_fun needs_typ definer
   480     fun print_def is_pseudo_fun needs_typ definer
   542                   end;
   543                   end;
   543             val prolog = if needs_typ then
   544             val prolog = if needs_typ then
   544               concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty]
   545               concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty]
   545                 else (concat o map str) [definer, deresolve_const const];
   546                 else (concat o map str) [definer, deresolve_const const];
   546           in pair
   547           in pair
   547             (print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty))
   548             (print_val_decl print_typscheme (Constant const, vs_ty))
   548             (concat (
   549             (concat (
   549               prolog
   550               prolog
   550               :: print_dict_args vs
   551               :: print_dict_args vs
   551               @| print_eqns (is_pseudo_fun (Code_Symbol.Constant const)) eqs
   552               @| print_eqns (is_pseudo_fun (Constant const)) eqs
   552             ))
   553             ))
   553           end
   554           end
   554       | print_def is_pseudo_fun _ definer
   555       | print_def is_pseudo_fun _ definer
   555           (ML_Instance (inst as (tyco, class), { vs, superinsts, inst_params, ... })) =
   556           (ML_Instance (inst as (tyco, class), { vs, superinsts, inst_params, ... })) =
   556           let
   557           let
   566                 str "=",
   567                 str "=",
   567                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   568                 print_app (K false) (SOME thm) reserved NOBR (const, [])
   568               ];
   569               ];
   569           in pair
   570           in pair
   570             (print_val_decl print_dicttypscheme
   571             (print_val_decl print_dicttypscheme
   571               (Code_Symbol.Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   572               (Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
   572             (concat (
   573             (concat (
   573               str definer
   574               str definer
   574               :: (str o deresolve_inst) inst
   575               :: (str o deresolve_inst) inst
   575               :: (if is_pseudo_fun (Code_Symbol.Class_Instance inst) then [str "()"]
   576               :: (if is_pseudo_fun (Class_Instance inst) then [str "()"]
   576                   else print_dict_args vs)
   577                   else print_dict_args vs)
   577               @ str "="
   578               @ str "="
   578               @@ brackets [
   579               @@ brackets [
   579                 enum_default "()" ";" "{" "}" (map print_super_instance superinsts
   580                 enum_default "()" ";" "{" "}" (map print_super_instance superinsts
   580                   @ map print_classparam_instance inst_params),
   581                   @ map print_classparam_instance inst_params),
   582                 print_dicttyp (class, tyco `%% map (ITyVar o fst) vs)
   583                 print_dicttyp (class, tyco `%% map (ITyVar o fst) vs)
   583               ]
   584               ]
   584             ))
   585             ))
   585           end;
   586           end;
   586      fun print_stmt (ML_Exc (const, (vs_ty, n))) = pair
   587      fun print_stmt (ML_Exc (const, (vs_ty, n))) = pair
   587           [print_val_decl print_typscheme (Code_Symbol.Constant const, vs_ty)]
   588           [print_val_decl print_typscheme (Constant const, vs_ty)]
   588           ((doublesemicolon o map str) (
   589           ((doublesemicolon o map str) (
   589             "let"
   590             "let"
   590             :: deresolve_const const
   591             :: deresolve_const const
   591             :: replicate n "_"
   592             :: replicate n "_"
   592             @ "="
   593             @ "="
   617             sig_ps
   618             sig_ps
   618             (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
   619             (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
   619           end
   620           end
   620      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   621      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   621           let
   622           let
   622             val ty_p = print_tyco_expr (Code_Symbol.Type_Constructor tyco, map ITyVar vs);
   623             val ty_p = print_tyco_expr (Type_Constructor tyco, map ITyVar vs);
   623           in
   624           in
   624             pair
   625             pair
   625             [concat [str "type", ty_p]]
   626             [concat [str "type", ty_p]]
   626             (concat [str "type", ty_p, str "=", str "EMPTY__"])
   627             (concat [str "type", ty_p, str "=", str "EMPTY__"])
   627           end
   628           end
   639             fun print_field s p = concat [str s, str ":", p];
   640             fun print_field s p = concat [str s, str ":", p];
   640             fun print_super_class_field (classrel as (_, super_class)) =
   641             fun print_super_class_field (classrel as (_, super_class)) =
   641               print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v));
   642               print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v));
   642             fun print_classparam_decl (classparam, ty) =
   643             fun print_classparam_decl (classparam, ty) =
   643               print_val_decl print_typscheme
   644               print_val_decl print_typscheme
   644                 (Code_Symbol.Constant classparam, ([(v, [class])], ty));
   645                 (Constant classparam, ([(v, [class])], ty));
   645             fun print_classparam_field (classparam, ty) =
   646             fun print_classparam_field (classparam, ty) =
   646               print_field (deresolve_const classparam) (print_typ NOBR ty);
   647               print_field (deresolve_const classparam) (print_typ NOBR ty);
   647             val w = "_" ^ first_upper v;
   648             val w = "_" ^ first_upper v;
   648             fun print_classparam_proj (classparam, _) =
   649             fun print_classparam_proj (classparam, _) =
   649               (concat o map str) ["let", deresolve_const classparam, w, "=",
   650               (concat o map str) ["let", deresolve_const classparam, w, "=",
   722       | namify_stmt (Code_Thingol.Datatypecons _) = namify_const true
   723       | namify_stmt (Code_Thingol.Datatypecons _) = namify_const true
   723       | namify_stmt (Code_Thingol.Class _) = namify_type
   724       | namify_stmt (Code_Thingol.Class _) = namify_type
   724       | namify_stmt (Code_Thingol.Classrel _) = namify_const false
   725       | namify_stmt (Code_Thingol.Classrel _) = namify_const false
   725       | namify_stmt (Code_Thingol.Classparam _) = namify_const false
   726       | namify_stmt (Code_Thingol.Classparam _) = namify_const false
   726       | namify_stmt (Code_Thingol.Classinst _) = namify_const false;
   727       | namify_stmt (Code_Thingol.Classinst _) = namify_const false;
   727     fun ml_binding_of_stmt (sym as Code_Symbol.Constant const, Code_Thingol.Fun ((tysm as (vs, ty), raw_eqs), _)) =
   728     fun ml_binding_of_stmt (sym as Constant const, Code_Thingol.Fun ((tysm as (vs, ty), raw_eqs), _)) =
   728           let
   729           let
   729             val eqs = filter (snd o snd) raw_eqs;
   730             val eqs = filter (snd o snd) raw_eqs;
   730             val (eqs', some_sym) = if null (filter_out (null o snd) vs) then case eqs
   731             val (eqs', some_sym) = if null (filter_out (null o snd) vs) then case eqs
   731                of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
   732                of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
   732                   then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE)
   733                   then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE)
   733                   else (eqs, SOME (sym, member (op =) (Code_Thingol.add_constsyms t []) sym))
   734                   else (eqs, SOME (sym, member (op =) (Code_Thingol.add_constsyms t []) sym))
   734                 | _ => (eqs, NONE)
   735                 | _ => (eqs, NONE)
   735               else (eqs, NONE)
   736               else (eqs, NONE)
   736           in (ML_Function (const, (tysm, eqs')), some_sym) end
   737           in (ML_Function (const, (tysm, eqs')), some_sym) end
   737       | ml_binding_of_stmt (sym as Code_Symbol.Class_Instance inst, Code_Thingol.Classinst (stmt as { vs, ... })) =
   738       | ml_binding_of_stmt (sym as Class_Instance inst, Code_Thingol.Classinst (stmt as { vs, ... })) =
   738           (ML_Instance (inst, stmt), if forall (null o snd) vs then SOME (sym, false) else NONE)
   739           (ML_Instance (inst, stmt), if forall (null o snd) vs then SOME (sym, false) else NONE)
   739       | ml_binding_of_stmt (sym, _) =
   740       | ml_binding_of_stmt (sym, _) =
   740           error ("Binding block containing illegal statement: " ^ 
   741           error ("Binding block containing illegal statement: " ^ 
   741             Code_Symbol.quote ctxt sym)
   742             Code_Symbol.quote ctxt sym)
   742     fun modify_fun (sym, stmt) =
   743     fun modify_fun (sym, stmt) =
   753       in SOME ml_stmt end;
   754       in SOME ml_stmt end;
   754     fun modify_funs stmts = single (SOME
   755     fun modify_funs stmts = single (SOME
   755       (ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst)))
   756       (ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst)))
   756     fun modify_datatypes stmts = single (SOME
   757     fun modify_datatypes stmts = single (SOME
   757       (ML_Datas (map_filter
   758       (ML_Datas (map_filter
   758         (fn (Code_Symbol.Type_Constructor tyco, Code_Thingol.Datatype stmt) => SOME (tyco, stmt) | _ => NONE) stmts)))
   759         (fn (Type_Constructor tyco, Code_Thingol.Datatype stmt) => SOME (tyco, stmt) | _ => NONE) stmts)))
   759     fun modify_class stmts = single (SOME
   760     fun modify_class stmts = single (SOME
   760       (ML_Class (the_single (map_filter
   761       (ML_Class (the_single (map_filter
   761         (fn (Code_Symbol.Type_Class class, Code_Thingol.Class stmt) => SOME (class, stmt) | _ => NONE) stmts))))
   762         (fn (Type_Class class, Code_Thingol.Class stmt) => SOME (class, stmt) | _ => NONE) stmts))))
   762     fun modify_stmts ([stmt as (_, stmt' as Code_Thingol.Fun _)]) =
   763     fun modify_stmts ([stmt as (_, stmt' as Code_Thingol.Fun _)]) =
   763           if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
   764           if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
   764       | modify_stmts ((stmts as (_, Code_Thingol.Fun _) :: _)) =
   765       | modify_stmts ((stmts as (_, Code_Thingol.Fun _) :: _)) =
   765           modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
   766           modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
   766       | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _) :: _)) =
   767       | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _) :: _)) =
   848   #> Code_Target.add_target
   849   #> Code_Target.add_target
   849     (target_OCaml, { serializer = serializer_ocaml, literals = literals_ocaml,
   850     (target_OCaml, { serializer = serializer_ocaml, literals = literals_ocaml,
   850       check = { env_var = "ISABELLE_OCAML",
   851       check = { env_var = "ISABELLE_OCAML",
   851         make_destination = fn p => Path.append p (Path.explode "ROOT.ocaml"),
   852         make_destination = fn p => Path.append p (Path.explode "ROOT.ocaml"),
   852         make_command = fn _ => "\"$ISABELLE_OCAML\" -w pu nums.cma ROOT.ocaml" } })
   853         make_command = fn _ => "\"$ISABELLE_OCAML\" -w pu nums.cma ROOT.ocaml" } })
   853   #> Code_Target.set_printings (Code_Symbol.Type_Constructor ("fun",
   854   #> Code_Target.set_printings (Type_Constructor ("fun",
   854     [(target_SML, SOME (2, fun_syntax)), (target_OCaml, SOME (2, fun_syntax))]))
   855     [(target_SML, SOME (2, fun_syntax)), (target_OCaml, SOME (2, fun_syntax))]))
   855   #> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names
   856   #> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names
   856   #> fold (Code_Target.add_reserved target_SML)
   857   #> fold (Code_Target.add_reserved target_SML)
   857       ["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*),
   858       ["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*),
   858         "Fail", "div", "mod" (*standard infixes*), "IntInf"]
   859         "Fail", "div", "mod" (*standard infixes*), "IntInf"]