src/Tools/Code/code_ml.ML
changeset 47609 b3dab1892cda
parent 47576 b32aae03e3d6
child 48003 1d11af40b106
equal deleted inserted replaced
47608:572d7e51de4d 47609:b3dab1892cda
    37   | ML_Val of ml_binding
    37   | ML_Val of ml_binding
    38   | ML_Funs of ml_binding list * string list
    38   | ML_Funs of ml_binding list * string list
    39   | ML_Datas of (string * ((vname * sort) list * ((string * vname list) * itype list) list)) list
    39   | ML_Datas of (string * ((vname * sort) list * ((string * vname list) * itype list) list)) list
    40   | ML_Class of string * (vname * ((class * string) list * (string * itype) list));
    40   | ML_Class of string * (vname * ((class * string) list * (string * itype) list));
    41 
    41 
    42 fun stmt_name_of_binding (ML_Function (name, _)) = name
       
    43   | stmt_name_of_binding (ML_Instance (name, _)) = name;
       
    44 
       
    45 fun print_product _ [] = NONE
    42 fun print_product _ [] = NONE
    46   | print_product print [x] = SOME (print x)
    43   | print_product print [x] = SOME (print x)
    47   | print_product print xs = (SOME o enum " *" "" "") (map print xs);
    44   | print_product print xs = (SOME o enum " *" "" "") (map print xs);
    48 
    45 
    49 fun tuplify _ _ [] = NONE
    46 fun tuplify _ _ [] = NONE
    53 
    50 
    54 (** SML serializer **)
    51 (** SML serializer **)
    55 
    52 
    56 fun print_sml_stmt tyco_syntax const_syntax reserved is_cons deresolve =
    53 fun print_sml_stmt tyco_syntax const_syntax reserved is_cons deresolve =
    57   let
    54   let
    58     fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
    55     fun print_tyco_expr (tyco, []) = (str o deresolve) tyco
    59       | print_tyco_expr fxy (tyco, [ty]) =
    56       | print_tyco_expr (tyco, [ty]) =
    60           concat [print_typ BR ty, (str o deresolve) tyco]
    57           concat [print_typ BR ty, (str o deresolve) tyco]
    61       | print_tyco_expr fxy (tyco, tys) =
    58       | print_tyco_expr (tyco, tys) =
    62           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
    59           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
    63     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
    60     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
    64          of NONE => print_tyco_expr fxy (tyco, tys)
    61          of NONE => print_tyco_expr (tyco, tys)
    65           | SOME (i, print) => print print_typ fxy tys)
    62           | SOME (_, print) => print print_typ fxy tys)
    66       | print_typ fxy (ITyVar v) = str ("'" ^ v);
    63       | print_typ fxy (ITyVar v) = str ("'" ^ v);
    67     fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
    64     fun print_dicttyp (class, ty) = print_tyco_expr (class, [ty]);
    68     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
    65     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
    69       (map_filter (fn (v, sort) =>
    66       (map_filter (fn (v, sort) =>
    70         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
    67         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
    71     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
    68     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
    72     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
    69     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   127       (print_term is_pseudo_fun) const_syntax some_thm vars
   124       (print_term is_pseudo_fun) const_syntax some_thm vars
   128     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   125     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   129     and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
   126     and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
   130           let
   127           let
   131             val (binds, body) = Code_Thingol.unfold_let (ICase cases);
   128             val (binds, body) = Code_Thingol.unfold_let (ICase cases);
   132             fun print_match ((pat, ty), t) vars =
   129             fun print_match ((pat, _), t) vars =
   133               vars
   130               vars
   134               |> print_bind is_pseudo_fun some_thm NOBR pat
   131               |> print_bind is_pseudo_fun some_thm NOBR pat
   135               |>> (fn p => semicolon [str "val", p, str "=",
   132               |>> (fn p => semicolon [str "val", p, str "=",
   136                     print_term is_pseudo_fun some_thm vars NOBR t])
   133                     print_term is_pseudo_fun some_thm vars NOBR t])
   137             val (ps, vars') = fold_map print_match binds vars;
   134             val (ps, vars') = fold_map print_match binds vars;
   168           | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
   165           | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
   169               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   166               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   170       in
   167       in
   171         concat (
   168         concat (
   172           str definer
   169           str definer
   173           :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
   170           :: print_tyco_expr (tyco, map (ITyVar o fst) vs)
   174           :: str "="
   171           :: str "="
   175           :: separate (str "|") (map print_co cos)
   172           :: separate (str "|") (map print_co cos)
   176         )
   173         )
   177       end;
   174       end;
   178     fun print_def is_pseudo_fun needs_typ definer
   175     fun print_def is_pseudo_fun needs_typ definer
   234               @ str "="
   231               @ str "="
   235               :: enum "," "{" "}"
   232               :: enum "," "{" "}"
   236                 (map print_super_instance super_instances
   233                 (map print_super_instance super_instances
   237                   @ map print_classparam_instance classparam_instances)
   234                   @ map print_classparam_instance classparam_instances)
   238               :: str ":"
   235               :: str ":"
   239               @@ print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
   236               @@ print_tyco_expr (class, [tyco `%% map (ITyVar o fst) vs])
   240             ))
   237             ))
   241           end;
   238           end;
   242     fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
   239     fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
   243           [print_val_decl print_typscheme (name, vs_ty)]
   240           [print_val_decl print_typscheme (name, vs_ty)]
   244           ((semicolon o map str) (
   241           ((semicolon o map str) (
   274             sig_ps
   271             sig_ps
   275             (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
   272             (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
   276           end
   273           end
   277      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   274      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   278           let
   275           let
   279             val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
   276             val ty_p = print_tyco_expr (tyco, map (ITyVar o fst) vs);
   280           in
   277           in
   281             pair
   278             pair
   282             [concat [str "type", ty_p]]
   279             [concat [str "type", ty_p]]
   283             (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
   280             (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
   284           end
   281           end
   357 
   354 
   358 (** OCaml serializer **)
   355 (** OCaml serializer **)
   359 
   356 
   360 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_cons deresolve =
   357 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_cons deresolve =
   361   let
   358   let
   362     fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
   359     fun print_tyco_expr (tyco, []) = (str o deresolve) tyco
   363       | print_tyco_expr fxy (tyco, [ty]) =
   360       | print_tyco_expr (tyco, [ty]) =
   364           concat [print_typ BR ty, (str o deresolve) tyco]
   361           concat [print_typ BR ty, (str o deresolve) tyco]
   365       | print_tyco_expr fxy (tyco, tys) =
   362       | print_tyco_expr (tyco, tys) =
   366           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
   363           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
   367     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   364     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   368          of NONE => print_tyco_expr fxy (tyco, tys)
   365          of NONE => print_tyco_expr (tyco, tys)
   369           | SOME (_, print) => print print_typ fxy tys)
   366           | SOME (_, print) => print print_typ fxy tys)
   370       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   367       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   371     fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
   368     fun print_dicttyp (class, ty) = print_tyco_expr (class, [ty]);
   372     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   369     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   373       (map_filter (fn (v, sort) =>
   370       (map_filter (fn (v, sort) =>
   374         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   371         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   375     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
   372     fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
   376     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   373     fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
   463           | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
   460           | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
   464               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   461               enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
   465       in
   462       in
   466         concat (
   463         concat (
   467           str definer
   464           str definer
   468           :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
   465           :: print_tyco_expr (tyco, map (ITyVar o fst) vs)
   469           :: str "="
   466           :: str "="
   470           :: separate (str "|") (map print_co cos)
   467           :: separate (str "|") (map print_co cos)
   471         )
   468         )
   472       end;
   469       end;
   473     fun print_def is_pseudo_fun needs_typ definer
   470     fun print_def is_pseudo_fun needs_typ definer
   574               @ str "="
   571               @ str "="
   575               @@ brackets [
   572               @@ brackets [
   576                 enum_default "()" ";" "{" "}" (map print_super_instance super_instances
   573                 enum_default "()" ";" "{" "}" (map print_super_instance super_instances
   577                   @ map print_classparam_instance classparam_instances),
   574                   @ map print_classparam_instance classparam_instances),
   578                 str ":",
   575                 str ":",
   579                 print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
   576                 print_tyco_expr (class, [tyco `%% map (ITyVar o fst) vs])
   580               ]
   577               ]
   581             ))
   578             ))
   582           end;
   579           end;
   583      fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
   580      fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
   584           [print_val_decl print_typscheme (name, vs_ty)]
   581           [print_val_decl print_typscheme (name, vs_ty)]
   614             sig_ps
   611             sig_ps
   615             (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
   612             (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
   616           end
   613           end
   617      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   614      | print_stmt (ML_Datas [(tyco, (vs, []))]) =
   618           let
   615           let
   619             val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
   616             val ty_p = print_tyco_expr (tyco, map (ITyVar o fst) vs);
   620           in
   617           in
   621             pair
   618             pair
   622             [concat [str "type", ty_p]]
   619             [concat [str "type", ty_p]]
   623             (concat [str "type", ty_p, str "=", str "EMPTY__"])
   620             (concat [str "type", ty_p, str "=", str "EMPTY__"])
   624           end
   621           end