src/Tools/Code/code_ml.ML
changeset 47576 b32aae03e3d6
parent 45009 99e1965f9c21
child 47609 b3dab1892cda
equal deleted inserted replaced
47575:b90cd7016d4f 47576:b32aae03e3d6
    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
    42 fun stmt_name_of_binding (ML_Function (name, _)) = name
    43   | stmt_name_of_binding (ML_Instance (name, _)) = name;
    43   | stmt_name_of_binding (ML_Instance (name, _)) = name;
    44 
    44 
    45 fun stmt_names_of (ML_Exc (name, _)) = [name]
       
    46   | stmt_names_of (ML_Val binding) = [stmt_name_of_binding binding]
       
    47   | stmt_names_of (ML_Funs (bindings, _)) = map stmt_name_of_binding bindings
       
    48   | stmt_names_of (ML_Datas ds) = map fst ds
       
    49   | stmt_names_of (ML_Class (name, _)) = [name];
       
    50 
       
    51 fun print_product _ [] = NONE
    45 fun print_product _ [] = NONE
    52   | print_product print [x] = SOME (print x)
    46   | print_product print [x] = SOME (print x)
    53   | print_product print xs = (SOME o enum " *" "" "") (map print xs);
    47   | print_product print xs = (SOME o enum " *" "" "") (map print xs);
    54 
    48 
    55 fun tuplify _ _ [] = NONE
    49 fun tuplify _ _ [] = NONE
    57   | tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs));
    51   | tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs));
    58 
    52 
    59 
    53 
    60 (** SML serializer **)
    54 (** SML serializer **)
    61 
    55 
    62 fun print_sml_stmt labelled_name tyco_syntax const_syntax reserved is_cons deresolve =
    56 fun print_sml_stmt tyco_syntax const_syntax reserved is_cons deresolve =
    63   let
    57   let
    64     fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
    58     fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
    65       | print_tyco_expr fxy (tyco, [ty]) =
    59       | print_tyco_expr fxy (tyco, [ty]) =
    66           concat [print_typ BR ty, (str o deresolve) tyco]
    60           concat [print_typ BR ty, (str o deresolve) tyco]
    67       | print_tyco_expr fxy (tyco, tys) =
    61       | print_tyco_expr fxy (tyco, tys) =
   361 };
   355 };
   362 
   356 
   363 
   357 
   364 (** OCaml serializer **)
   358 (** OCaml serializer **)
   365 
   359 
   366 fun print_ocaml_stmt labelled_name tyco_syntax const_syntax reserved is_cons deresolve =
   360 fun print_ocaml_stmt tyco_syntax const_syntax reserved is_cons deresolve =
   367   let
   361   let
   368     fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
   362     fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
   369       | print_tyco_expr fxy (tyco, [ty]) =
   363       | print_tyco_expr fxy (tyco, [ty]) =
   370           concat [print_typ BR ty, (str o deresolve) tyco]
   364           concat [print_typ BR ty, (str o deresolve) tyco]
   371       | print_tyco_expr fxy (tyco, tys) =
   365       | print_tyco_expr fxy (tyco, tys) =
   372           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
   366           concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
   373     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   367     and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
   374          of NONE => print_tyco_expr fxy (tyco, tys)
   368          of NONE => print_tyco_expr fxy (tyco, tys)
   375           | SOME (i, print) => print print_typ fxy tys)
   369           | SOME (_, print) => print print_typ fxy tys)
   376       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   370       | print_typ fxy (ITyVar v) = str ("'" ^ v);
   377     fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
   371     fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
   378     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   372     fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
   379       (map_filter (fn (v, sort) =>
   373       (map_filter (fn (v, sort) =>
   380         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   374         (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
   433       (print_term is_pseudo_fun) const_syntax some_thm vars
   427       (print_term is_pseudo_fun) const_syntax some_thm vars
   434     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   428     and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
   435     and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
   429     and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
   436           let
   430           let
   437             val (binds, body) = Code_Thingol.unfold_let (ICase cases);
   431             val (binds, body) = Code_Thingol.unfold_let (ICase cases);
   438             fun print_let ((pat, ty), t) vars =
   432             fun print_let ((pat, _), t) vars =
   439               vars
   433               vars
   440               |> print_bind is_pseudo_fun some_thm NOBR pat
   434               |> print_bind is_pseudo_fun some_thm NOBR pat
   441               |>> (fn p => concat
   435               |>> (fn p => concat
   442                   [str "let", p, str "=", print_term is_pseudo_fun some_thm vars NOBR t, str "in"])
   436                   [str "let", p, str "=", print_term is_pseudo_fun some_thm vars NOBR t, str "in"])
   443             val (ps, vars') = fold_map print_let binds vars;
   437             val (ps, vars') = fold_map print_let binds vars;
   762       (ML_Datas (map_filter
   756       (ML_Datas (map_filter
   763         (fn (name, Code_Thingol.Datatype (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts)))
   757         (fn (name, Code_Thingol.Datatype (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts)))
   764     fun modify_class stmts = single (SOME
   758     fun modify_class stmts = single (SOME
   765       (ML_Class (the_single (map_filter
   759       (ML_Class (the_single (map_filter
   766         (fn (name, Code_Thingol.Class (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts))))
   760         (fn (name, Code_Thingol.Class (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts))))
   767     fun modify_stmts ([stmt as (name, stmt' as Code_Thingol.Fun _)]) =
   761     fun modify_stmts ([stmt as (_, stmt' as Code_Thingol.Fun _)]) =
   768           if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
   762           if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
   769       | modify_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) =
   763       | modify_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) =
   770           modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
   764           modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
   771       | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) =
   765       | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) =
   772           modify_datatypes stmts
   766           modify_datatypes stmts
   788     Code_Namespace.hierarchical_program labelled_name { module_alias = module_alias, reserved = reserved,
   782     Code_Namespace.hierarchical_program labelled_name { module_alias = module_alias, reserved = reserved,
   789       empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt,
   783       empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt,
   790       cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program
   784       cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program
   791   end;
   785   end;
   792 
   786 
   793 fun serialize_ml target print_ml_module print_ml_stmt with_signatures
   787 fun serialize_ml print_ml_module print_ml_stmt with_signatures
   794     { labelled_name, reserved_syms, includes, module_alias,
   788     { labelled_name, reserved_syms, includes, module_alias,
   795       class_syntax, tyco_syntax, const_syntax } program =
   789       class_syntax, tyco_syntax, const_syntax } program =
   796   let
   790   let
   797 
   791 
   798     (* build program *)
   792     (* build program *)
   799     val { deresolver, hierarchical_program = ml_program } =
   793     val { deresolver, hierarchical_program = ml_program } =
   800       ml_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program;
   794       ml_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program;
   801 
   795 
   802     (* print statements *)
   796     (* print statements *)
   803     fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt
   797     fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt
   804       labelled_name tyco_syntax const_syntax (make_vars reserved_syms)
   798       tyco_syntax const_syntax (make_vars reserved_syms)
   805       (Code_Thingol.is_cons program) (deresolver prefix_fragments) stmt
   799       (Code_Thingol.is_cons program) (deresolver prefix_fragments) stmt
   806       |> apfst SOME;
   800       |> apfst SOME;
   807 
   801 
   808     (* print modules *)
   802     (* print modules *)
   809     fun print_module prefix_fragments base _ xs =
   803     fun print_module _ base _ xs =
   810       let
   804       let
   811         val (raw_decls, body) = split_list xs;
   805         val (raw_decls, body) = split_list xs;
   812         val decls = if with_signatures then SOME (maps these raw_decls) else NONE 
   806         val decls = if with_signatures then SOME (maps these raw_decls) else NONE 
   813       in (NONE, print_ml_module base decls body) end;
   807       in (NONE, print_ml_module base decls body) end;
   814 
   808 
   823     Code_Target.serialization write (rpair (try (deresolver [])) ooo format) p
   817     Code_Target.serialization write (rpair (try (deresolver [])) ooo format) p
   824   end;
   818   end;
   825 
   819 
   826 val serializer_sml : Code_Target.serializer =
   820 val serializer_sml : Code_Target.serializer =
   827   Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
   821   Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
   828   >> (fn with_signatures => serialize_ml target_SML
   822   >> (fn with_signatures => serialize_ml print_sml_module print_sml_stmt with_signatures));
   829       print_sml_module print_sml_stmt with_signatures));
       
   830 
   823 
   831 val serializer_ocaml : Code_Target.serializer =
   824 val serializer_ocaml : Code_Target.serializer =
   832   Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
   825   Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
   833   >> (fn with_signatures => serialize_ml target_OCaml
   826   >> (fn with_signatures => serialize_ml print_ocaml_module print_ocaml_stmt with_signatures));
   834       print_ocaml_module print_ocaml_stmt with_signatures));
       
   835 
   827 
   836 
   828 
   837 (** Isar setup **)
   829 (** Isar setup **)
   838 
   830 
   839 val setup =
   831 val setup =