src/Tools/code/code_target.ML
changeset 26998 2c4032d59586
parent 26753 094d70c81243
child 27000 e8a40d8b7897
equal deleted inserted replaced
26997:40552bbac005 26998:2c4032d59586
    30 
    30 
    31   val allow_exception: string -> theory -> theory;
    31   val allow_exception: string -> theory -> theory;
    32 
    32 
    33   type serializer;
    33   type serializer;
    34   val add_serializer: string * serializer -> theory -> theory;
    34   val add_serializer: string * serializer -> theory -> theory;
       
    35   val assert_serializer: theory -> string -> string;
    35   val get_serializer: theory -> string -> bool -> string option
    36   val get_serializer: theory -> string -> bool -> string option
    36     -> string option -> Args.T list
    37     -> string option -> Args.T list
    37     -> string list option -> CodeThingol.code -> unit;
    38     -> string list option -> CodeThingol.code -> unit;
    38   val assert_serializer: theory -> string -> string;
    39   val sml_of: theory -> string list -> CodeThingol.code -> string;
    39   val eval: theory -> (string * (unit -> 'a) option ref)
    40   val eval: theory -> (string * (unit -> 'a) option ref)
    40     -> CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
    41     -> CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
    41   val sml_of: theory -> string list -> CodeThingol.code -> string;
       
    42   val code_width: int ref;
    42   val code_width: int ref;
    43 
    43 
    44   val setup: theory -> theory;
    44   val setup: theory -> theory;
    45 end;
    45 end;
    46 
    46 
    59 val concat = Pretty.block o Pretty.breaks;
    59 val concat = Pretty.block o Pretty.breaks;
    60 val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
    60 val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
    61 fun semicolon ps = Pretty.block [concat ps, str ";"];
    61 fun semicolon ps = Pretty.block [concat ps, str ";"];
    62 fun enum_default default sep opn cls [] = str default
    62 fun enum_default default sep opn cls [] = str default
    63   | enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
    63   | enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
       
    64 
       
    65 val code_width = ref 80;
       
    66 fun code_source p = Pretty.setmp_margin (!code_width) Pretty.string_of p ^ "\n";
       
    67 fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p;
    64 
    68 
    65 
    69 
    66 (** syntax **)
    70 (** syntax **)
    67 
    71 
    68 datatype lrx = L | R | X;
    72 datatype lrx = L | R | X;
   908   ] @ content @ [
   912   ] @ content @ [
   909     str "",
   913     str "",
   910     str ("end;; (*struct " ^ name ^ "*)")
   914     str ("end;; (*struct " ^ name ^ "*)")
   911   ]);
   915   ]);
   912 
   916 
   913 val code_width = ref 80;
       
   914 fun code_output p = Pretty.setmp_margin (!code_width) Pretty.output p ^ "\n";
       
   915 
       
   916 fun seri_ml pr_def pr_modl module output labelled_name reserved_syms includes raw_module_alias
   917 fun seri_ml pr_def pr_modl module output labelled_name reserved_syms includes raw_module_alias
   917   (_ : string -> class_syntax option) tyco_syntax const_syntax cs code =
   918   (_ : string -> class_syntax option) tyco_syntax const_syntax cs code =
   918   let
   919   let
   919     val module_alias = if is_some module then K module else raw_module_alias;
   920     val module_alias = if is_some module then K module else raw_module_alias;
   920     val is_cons = CodeThingol.is_cons code;
   921     val is_cons = CodeThingol.is_cons code;
  1091   in output (map_filter deresolv cs, p) end;
  1092   in output (map_filter deresolv cs, p) end;
  1092 
  1093 
  1093 fun isar_seri_sml module file =
  1094 fun isar_seri_sml module file =
  1094   let
  1095   let
  1095     val output = case file
  1096     val output = case file
  1096      of NONE => use_text (1, "generated code") Output.ml_output false o code_output
  1097      of NONE => use_text (1, "generated code") Output.ml_output false o code_source
  1097       | SOME "-" => PrintMode.setmp [] writeln o code_output
  1098       | SOME "-" => code_writeln
  1098       | SOME file => File.write (Path.explode file) o code_output;
  1099       | SOME file => File.write (Path.explode file) o code_source;
  1099   in
  1100   in
  1100     parse_args (Scan.succeed ())
  1101     parse_args (Scan.succeed ())
  1101     #> (fn () => seri_ml pr_sml pr_sml_modl module (output o snd))
  1102     #> (fn () => seri_ml pr_sml pr_sml_modl module (output o snd))
  1102   end;
  1103   end;
  1103 
  1104 
  1104 fun eval_seri module file args =
  1105 fun eval_seri module file args =
  1105   seri_ml pr_sml (K Pretty.chunks) (SOME "Isabelle_Eval")
  1106   seri_ml pr_sml (K Pretty.chunks) (SOME "Isabelle_Eval")
  1106     (fn (cs, p) => "let\n" ^ code_output p ^ "\nin fn () => " ^ enclose "(" ")" (commas cs) ^ " end");
  1107     (fn (cs, p) => "let\n" ^ code_source p ^ "\nin fn () => " ^ enclose "(" ")" (commas cs) ^ " end");
  1107 
  1108 
  1108 fun isar_seri_ocaml module file =
  1109 fun isar_seri_ocaml module file =
  1109   let
  1110   let
  1110     val output = case file
  1111     val output = case file
  1111      of NONE => error "OCaml: no internal compilation"
  1112      of NONE => error "OCaml: no internal compilation"
  1112       | SOME "-" => PrintMode.setmp [] writeln o code_output
  1113       | SOME "-" => code_writeln
  1113       | SOME file => File.write (Path.explode file) o code_output;
  1114       | SOME file => File.write (Path.explode file) o code_source;
  1114     fun output_file file = File.write (Path.explode file) o code_output;
       
  1115     val output_diag = PrintMode.setmp [] writeln o code_output;
       
  1116   in
  1115   in
  1117     parse_args (Scan.succeed ())
  1116     parse_args (Scan.succeed ())
  1118     #> (fn () => seri_ml pr_ocaml pr_ocaml_modl module (output o snd))
  1117     #> (fn () => seri_ml pr_ocaml pr_ocaml_modl module (output o snd))
  1119   end;
  1118   end;
  1120 
  1119 
  1484              of "" => Path.explode "Main.hs"
  1483              of "" => Path.explode "Main.hs"
  1485               | _ => (Path.ext "hs" o Path.explode o implode o separate "/"
  1484               | _ => (Path.ext "hs" o Path.explode o implode o separate "/"
  1486                     o NameSpace.explode) modlname;
  1485                     o NameSpace.explode) modlname;
  1487             val pathname = Path.append destination filename;
  1486             val pathname = Path.append destination filename;
  1488             val _ = File.mkdir (Path.dir pathname);
  1487             val _ = File.mkdir (Path.dir pathname);
  1489           in File.write pathname end
  1488           in File.write pathname o code_source end
  1490       | write_modulefile NONE _ = PrintMode.setmp [] writeln;
  1489       | write_modulefile NONE _ = code_writeln;
  1491     fun write_module destination (modulename, content) =
  1490     fun write_module destination (modulename, content) =
  1492       Pretty.chunks [
  1491       Pretty.chunks [
  1493         str ("module " ^ modulename ^ " where {"),
  1492         str ("module " ^ modulename ^ " where {"),
  1494         str "",
  1493         str "",
  1495         content,
  1494         content,
  1496         str "",
  1495         str "",
  1497         str "}"
  1496         str "}"
  1498       ]
  1497       ]
  1499       |> code_output
       
  1500       |> write_modulefile destination modulename;
  1498       |> write_modulefile destination modulename;
  1501     fun seri_module (modlname', (imports, (defs, _))) =
  1499     fun seri_module (modlname', (imports, (defs, _))) =
  1502       let
  1500       let
  1503         val imports' =
  1501         val imports' =
  1504           imports
  1502           imports
  1562   in
  1560   in
  1563     []
  1561     []
  1564     |> Graph.fold (fn (name, (def, _)) =>
  1562     |> Graph.fold (fn (name, (def, _)) =>
  1565           case try pr (name, def) of SOME p => cons p | NONE => I) code
  1563           case try pr (name, def) of SOME p => cons p | NONE => I) code
  1566     |> Pretty.chunks2
  1564     |> Pretty.chunks2
  1567     |> code_output
  1565     |> code_writeln
  1568     |> PrintMode.setmp [] writeln
       
  1569   end;
  1566   end;
  1570 
  1567 
  1571 
  1568 
  1572 
  1569 
  1573 (** theory data **)
  1570 (** theory data **)