src/Tools/Code/code_target.ML
author haftmann
Thu Dec 04 16:51:54 2014 +0100 (2014-12-04)
changeset 59100 ad09649f6f50
parent 59099 62ee9676b7ed
child 59101 6dc48c98303c
permissions -rw-r--r--
tuned
haftmann@37745
     1
(*  Title:      Tools/Code/code_target.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@38910
     4
Generic infrastructure for target language data.
haftmann@24219
     5
*)
haftmann@24219
     6
haftmann@24219
     7
signature CODE_TARGET =
haftmann@24219
     8
sig
haftmann@55757
     9
  val cert_tyco: Proof.context -> string -> string
haftmann@55757
    10
  val read_tyco: Proof.context -> string -> string
haftmann@36471
    11
haftmann@55757
    12
  val export_code_for: Proof.context -> Path.T option -> string -> int option -> string -> Token.T list
haftmann@55683
    13
    -> Code_Thingol.program -> bool -> Code_Symbol.T list -> unit
haftmann@55757
    14
  val produce_code_for: Proof.context -> string -> int option -> string -> Token.T list
haftmann@55683
    15
    -> Code_Thingol.program -> bool -> Code_Symbol.T list -> (string * string) list * string option list
haftmann@55757
    16
  val present_code_for: Proof.context -> string -> int option -> string -> Token.T list
haftmann@55150
    17
    -> Code_Thingol.program -> Code_Symbol.T list * Code_Symbol.T list -> string
haftmann@55757
    18
  val check_code_for: Proof.context -> string -> bool -> Token.T list
haftmann@55683
    19
    -> Code_Thingol.program -> bool -> Code_Symbol.T list -> unit
haftmann@38918
    20
haftmann@55757
    21
  val export_code: Proof.context -> bool -> string list
haftmann@38933
    22
    -> (((string * string) * Path.T option) * Token.T list) list -> unit
haftmann@55757
    23
  val produce_code: Proof.context -> bool -> string list
haftmann@48568
    24
    -> string -> int option -> string -> Token.T list -> (string * string) list * string option list
haftmann@55757
    25
  val present_code: Proof.context -> string list -> Code_Symbol.T list
haftmann@38933
    26
    -> string -> int option -> string -> Token.T list -> string
haftmann@55757
    27
  val check_code: Proof.context -> bool -> string list
haftmann@38918
    28
    -> ((string * bool) * Token.T list) list -> unit
haftmann@38918
    29
haftmann@48568
    30
  val generatedN: string
haftmann@55757
    31
  val evaluator: Proof.context -> string -> Code_Thingol.program
haftmann@55684
    32
    -> Code_Symbol.T list -> bool -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
haftmann@48568
    33
    -> (string * string) list * string
haftmann@41344
    34
haftmann@28054
    35
  type serializer
haftmann@34152
    36
  type literals = Code_Printer.literals
haftmann@37822
    37
  val add_target: string * { serializer: serializer, literals: literals,
wenzelm@41940
    38
    check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }
wenzelm@41940
    39
    -> theory -> theory
haftmann@28663
    40
  val extend_target: string *
haftmann@55147
    41
      (string * (Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    42
    -> theory -> theory
haftmann@55757
    43
  val assert_target: Proof.context -> string -> string
haftmann@55757
    44
  val the_literals: Proof.context -> string -> literals
haftmann@28054
    45
  type serialization
wenzelm@36959
    46
  val parse_args: 'a parser -> Token.T list -> 'a
haftmann@38916
    47
  val serialization: (int -> Path.T option -> 'a -> unit)
haftmann@55150
    48
    -> (Code_Symbol.T list -> int -> 'a -> (string * string) list * (Code_Symbol.T -> string option))
haftmann@38925
    49
    -> 'a -> serialization
haftmann@38918
    50
  val set_default_code_width: int -> theory -> theory
haftmann@38917
    51
haftmann@52137
    52
  type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
haftmann@52138
    53
  type identifier_data
haftmann@52138
    54
  val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
haftmann@52138
    55
    -> theory -> theory
haftmann@55372
    56
  val set_printings: (Code_Printer.raw_const_syntax, Code_Printer.tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
haftmann@52137
    57
    -> theory -> theory
haftmann@28054
    58
  val add_reserved: string -> string -> theory -> theory
haftmann@39646
    59
haftmann@39750
    60
  val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
wenzelm@43564
    61
wenzelm@43564
    62
  val setup: theory -> theory
haftmann@24219
    63
end;
haftmann@24219
    64
haftmann@28054
    65
structure Code_Target : CODE_TARGET =
haftmann@24219
    66
struct
haftmann@24219
    67
haftmann@55150
    68
open Basic_Code_Symbol;
haftmann@28054
    69
open Basic_Code_Thingol;
haftmann@34152
    70
haftmann@34152
    71
type literals = Code_Printer.literals;
haftmann@52137
    72
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl =
haftmann@52137
    73
  (string * (string * 'a option) list, string * (string * 'b option) list,
haftmann@52137
    74
    class * (string * 'c option) list, (class * class) * (string * 'd option) list,
haftmann@52137
    75
    (class * string) * (string * 'e option) list,
haftmann@52137
    76
    string * (string * 'f option) list) Code_Symbol.attr;
haftmann@55291
    77
type identifier_data = (string list * string, string list * string, string list * string, string list * string,
haftmann@55291
    78
  string list * string, string list * string) Code_Symbol.data;
haftmann@52137
    79
haftmann@34152
    80
type tyco_syntax = Code_Printer.tyco_syntax;
haftmann@55153
    81
type raw_const_syntax = Code_Printer.raw_const_syntax;
haftmann@34152
    82
haftmann@24219
    83
haftmann@52377
    84
(** checking and parsing of symbols **)
haftmann@52377
    85
haftmann@55757
    86
fun cert_const ctxt const =
haftmann@52377
    87
  let
haftmann@55757
    88
    val _ = if Sign.declared_const (Proof_Context.theory_of ctxt) const then ()
haftmann@52377
    89
      else error ("No such constant: " ^ quote const);
haftmann@52377
    90
  in const end;
haftmann@52377
    91
haftmann@55757
    92
fun read_const ctxt = Code.read_const (Proof_Context.theory_of ctxt);
haftmann@55757
    93
haftmann@55757
    94
fun cert_tyco ctxt tyco =
haftmann@52377
    95
  let
haftmann@55757
    96
    val _ = if Sign.declared_tyname (Proof_Context.theory_of ctxt) tyco then ()
haftmann@52377
    97
      else error ("No such type constructor: " ^ quote tyco);
haftmann@52377
    98
  in tyco end;
haftmann@52377
    99
wenzelm@55951
   100
fun read_tyco ctxt =
wenzelm@56002
   101
  #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = true} ctxt;
haftmann@52377
   102
haftmann@55757
   103
fun cert_class ctxt class =
haftmann@52377
   104
  let
haftmann@55757
   105
    val _ = Axclass.get_info (Proof_Context.theory_of ctxt) class;
haftmann@52377
   106
  in class end;
haftmann@52377
   107
haftmann@52377
   108
val parse_classrel_ident = Parse.class --| @{keyword "<"} -- Parse.class;
haftmann@52377
   109
haftmann@55757
   110
fun cert_inst ctxt (class, tyco) =
haftmann@55757
   111
  (cert_class ctxt class, cert_tyco ctxt tyco);
haftmann@52377
   112
haftmann@55757
   113
fun read_inst ctxt (raw_tyco, raw_class) =
haftmann@55757
   114
  (read_tyco ctxt raw_tyco, Proof_Context.read_class ctxt raw_class);
haftmann@52377
   115
haftmann@52377
   116
val parse_inst_ident = Parse.xname --| @{keyword "::"} -- Parse.class;
haftmann@52377
   117
haftmann@55757
   118
fun cert_syms ctxt =
haftmann@55757
   119
  Code_Symbol.map_attr (apfst (cert_const ctxt)) (apfst (cert_tyco ctxt))
wenzelm@59058
   120
    (apfst (cert_class ctxt)) ((apfst o apply2) (cert_class ctxt)) (apfst (cert_inst ctxt)) I;
haftmann@52377
   121
haftmann@55757
   122
fun read_syms ctxt =
haftmann@55757
   123
  Code_Symbol.map_attr (apfst (read_const ctxt)) (apfst (read_tyco ctxt))
wenzelm@59058
   124
    (apfst (Proof_Context.read_class ctxt)) ((apfst o apply2) (Proof_Context.read_class ctxt)) (apfst (read_inst ctxt)) I;
haftmann@52377
   125
haftmann@52377
   126
fun check_name is_module s =
haftmann@52377
   127
  let
haftmann@52377
   128
    val _ = if s = "" then error "Bad empty code name" else ();
haftmann@52377
   129
    val xs = Long_Name.explode s;
haftmann@52377
   130
    val xs' = if is_module
haftmann@56826
   131
        then map (Name.desymbolize NONE) xs
haftmann@52377
   132
      else if length xs < 2
haftmann@52377
   133
        then error ("Bad code name without module component: " ^ quote s)
haftmann@52377
   134
      else
haftmann@52377
   135
        let
haftmann@52377
   136
          val (ys, y) = split_last xs;
haftmann@56826
   137
          val ys' = map (Name.desymbolize NONE) ys;
haftmann@56826
   138
          val y' = Name.desymbolize NONE y;
haftmann@52377
   139
        in ys' @ [y'] end;
haftmann@52377
   140
  in if xs' = xs
haftmann@55291
   141
    then if is_module then (xs, "") else split_last xs
haftmann@52377
   142
    else error ("Invalid code name: " ^ quote s ^ "\n"
haftmann@52377
   143
      ^ "better try " ^ quote (Long_Name.implode xs'))
haftmann@52377
   144
  end;
haftmann@52377
   145
haftmann@52377
   146
haftmann@52137
   147
(** serializations and serializer **)
haftmann@52137
   148
haftmann@52137
   149
(* serialization: abstract nonsense to cover different destinies for generated code *)
haftmann@24219
   150
haftmann@55150
   151
datatype destination = Export of Path.T option | Produce | Present of Code_Symbol.T list;
haftmann@55150
   152
type serialization = int -> destination -> ((string * string) list * (Code_Symbol.T -> string option)) option;
haftmann@27014
   153
haftmann@39659
   154
fun serialization output _ content width (Export some_path) =
haftmann@39661
   155
      (output width some_path content; NONE)
haftmann@39661
   156
  | serialization _ string content width Produce =
haftmann@39661
   157
      string [] width content |> SOME
haftmann@55147
   158
  | serialization _ string content width (Present syms) =
haftmann@55147
   159
     string syms width content
haftmann@48568
   160
     |> (apfst o map o apsnd) (Pretty.output (SOME width) o Pretty.str)
haftmann@39661
   161
     |> SOME;
haftmann@39659
   162
haftmann@39661
   163
fun export some_path f = (f (Export some_path); ());
haftmann@39661
   164
fun produce f = the (f Produce);
haftmann@55147
   165
fun present syms f = space_implode "\n\n" (map snd (fst (the (f (Present syms)))));
haftmann@38917
   166
haftmann@24219
   167
haftmann@52137
   168
(* serializers: functions producing serializations *)
haftmann@28054
   169
haftmann@39142
   170
type serializer = Token.T list
haftmann@52138
   171
  -> Proof.context
haftmann@39142
   172
  -> {
haftmann@52138
   173
    module_name: string,
haftmann@38926
   174
    reserved_syms: string list,
haftmann@52138
   175
    identifiers: identifier_data,
haftmann@38926
   176
    includes: (string * Pretty.T) list,
haftmann@38926
   177
    class_syntax: string -> string option,
haftmann@38926
   178
    tyco_syntax: string -> Code_Printer.tyco_syntax option,
haftmann@55153
   179
    const_syntax: string -> Code_Printer.const_syntax option }
haftmann@55683
   180
  -> Code_Symbol.T list
haftmann@41342
   181
  -> Code_Thingol.program
haftmann@28054
   182
  -> serialization;
haftmann@27000
   183
haftmann@59099
   184
type fundamental = { serializer: serializer, literals: literals,
haftmann@59099
   185
  check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }; 
haftmann@59099
   186
haftmann@59099
   187
datatype language = Fundamental of fundamental 
haftmann@59099
   188
  | Extension of string * (Code_Thingol.program -> Code_Thingol.program);
haftmann@28054
   189
haftmann@52137
   190
haftmann@52137
   191
(** theory data **)
haftmann@52137
   192
haftmann@28054
   193
datatype target = Target of {
haftmann@28054
   194
  serial: serial,
haftmann@59099
   195
  language: language,
haftmann@28054
   196
  reserved: string list,
haftmann@52138
   197
  identifiers: identifier_data,
haftmann@55153
   198
  printings: (Code_Printer.const_syntax, Code_Printer.tyco_syntax, string, unit, unit,
haftmann@52137
   199
    (Pretty.T * string list)) Code_Symbol.data
haftmann@28054
   200
};
haftmann@27103
   201
haftmann@59099
   202
fun make_target ((serial, language), (reserved, (identifiers, printings))) =
haftmann@59099
   203
  Target { serial = serial, language = language, reserved = reserved,
haftmann@52138
   204
    identifiers = identifiers, printings = printings };
haftmann@59099
   205
fun map_target f (Target { serial, language, reserved, identifiers, printings }) =
haftmann@59099
   206
  make_target (f ((serial, language), (reserved, (identifiers, printings))));
haftmann@59099
   207
fun merge_target strict target_name (Target { serial = serial1, language = language,
haftmann@52138
   208
  reserved = reserved1, identifiers = identifiers1, printings = printings1 },
haftmann@59099
   209
    Target { serial = serial2, language = _,
haftmann@52138
   210
      reserved = reserved2, identifiers = identifiers2, printings = printings2 }) =
haftmann@28054
   211
  if serial1 = serial2 orelse not strict then
haftmann@59099
   212
    make_target ((serial1, language), (merge (op =) (reserved1, reserved2),
haftmann@52138
   213
      (Code_Symbol.merge_data (identifiers1, identifiers2),
haftmann@52138
   214
        Code_Symbol.merge_data (printings1, printings2))))
haftmann@28054
   215
  else
haftmann@59099
   216
    error ("Incompatible targets: " ^ quote target_name);
haftmann@27103
   217
haftmann@59099
   218
fun the_language (Target { language, ... }) = language;
haftmann@28054
   219
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@52138
   220
fun the_identifiers (Target { identifiers , ... }) = identifiers;
haftmann@52137
   221
fun the_printings (Target { printings, ... }) = printings;
haftmann@27437
   222
haftmann@34248
   223
structure Targets = Theory_Data
haftmann@34071
   224
(
haftmann@54890
   225
  type T = target Symtab.table * int;
haftmann@54890
   226
  val empty = (Symtab.empty, 80);
haftmann@34071
   227
  val extend = I;
haftmann@54890
   228
  fun merge ((target1, width1), (target2, width2)) : T =
haftmann@54890
   229
    (Symtab.join (merge_target true) (target1, target2), Int.max (width1, width2));
haftmann@34071
   230
);
haftmann@27436
   231
haftmann@59100
   232
fun lookup_target thy = Symtab.lookup (fst (Targets.get thy));
haftmann@59100
   233
haftmann@59099
   234
fun assert_target ctxt target_name =
haftmann@59099
   235
  if Symtab.defined (fst (Targets.get (Proof_Context.theory_of ctxt))) target_name
haftmann@59099
   236
  then target_name
haftmann@59099
   237
  else error ("Unknown code target language: " ^ quote target_name);
haftmann@28054
   238
haftmann@59099
   239
fun put_target (target_name, target_language) thy =
haftmann@27304
   240
  let
haftmann@59099
   241
    val _ = case target_language
haftmann@59100
   242
     of Extension (super, _) => if is_some (lookup_target thy super) then ()
haftmann@28054
   243
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   244
      | _ => ();
haftmann@59100
   245
    val overwriting = case (Option.map the_language o lookup_target thy) target_name
haftmann@28663
   246
     of NONE => false
haftmann@37821
   247
      | SOME (Extension _) => true
haftmann@59099
   248
      | SOME (Fundamental _) => (case target_language
haftmann@59099
   249
         of Extension _ => error ("Illegal attempt to overwrite existing target " ^ quote target_name)
haftmann@28663
   250
          | _ => true);
haftmann@28663
   251
    val _ = if overwriting
haftmann@59099
   252
      then warning ("Overwriting existing target " ^ quote target_name)
wenzelm@43564
   253
      else ();
haftmann@28054
   254
  in
haftmann@28054
   255
    thy
haftmann@54890
   256
    |> (Targets.map o apfst o Symtab.update)
haftmann@59099
   257
        (target_name, make_target ((serial (), target_language),
haftmann@52138
   258
          ([], (Code_Symbol.empty_data, Code_Symbol.empty_data))))
haftmann@28054
   259
  end;
haftmann@27436
   260
haftmann@59100
   261
fun add_target (target_name, fundamental) =
haftmann@59100
   262
  put_target (target_name, Fundamental fundamental);
haftmann@59099
   263
fun extend_target (target_name, (super, modify)) =
haftmann@59099
   264
  put_target (target_name, Extension (super, modify));
haftmann@27436
   265
haftmann@59099
   266
fun map_target_data target_name f thy =
haftmann@27436
   267
  let
haftmann@59099
   268
    val _ = assert_target (Proof_Context.init_global thy) target_name;
haftmann@28054
   269
  in
haftmann@28054
   270
    thy
haftmann@59099
   271
    |> (Targets.map o apfst o Symtab.map_entry target_name o map_target o apsnd) f
haftmann@28054
   272
  end;
haftmann@27304
   273
haftmann@59099
   274
fun map_reserved target_name =
haftmann@59099
   275
  map_target_data target_name o apfst;
haftmann@59099
   276
fun map_identifiers target_name =
haftmann@59099
   277
  map_target_data target_name o apsnd o apfst;
haftmann@59099
   278
fun map_printings target_name =
haftmann@59099
   279
  map_target_data target_name o apsnd o apsnd;
haftmann@27000
   280
haftmann@34248
   281
fun set_default_code_width k = (Targets.map o apsnd) (K k);
haftmann@34071
   282
haftmann@27000
   283
haftmann@34021
   284
(** serializer usage **)
haftmann@34021
   285
haftmann@34021
   286
(* montage *)
haftmann@34021
   287
haftmann@55757
   288
fun the_fundamental ctxt =
haftmann@34021
   289
  let
haftmann@59100
   290
    val thy = Proof_Context.theory_of ctxt;
haftmann@59100
   291
    fun fundamental target_name = case lookup_target thy target_name
haftmann@59099
   292
     of SOME target => (case the_language target
haftmann@59099
   293
         of Fundamental fundamental => fundamental
haftmann@37822
   294
          | Extension (super, _) => fundamental super)
haftmann@59099
   295
      | NONE => error ("Unknown code target language: " ^ quote target_name);
haftmann@37822
   296
  in fundamental end;
haftmann@37822
   297
haftmann@55757
   298
fun the_literals ctxt = #literals o the_fundamental ctxt;
haftmann@34021
   299
haftmann@55757
   300
fun collapse_hierarchy ctxt =
haftmann@38927
   301
  let
haftmann@59100
   302
    val thy = Proof_Context.theory_of ctxt;
haftmann@59099
   303
    fun collapse target_name =
haftmann@38927
   304
      let
haftmann@59100
   305
        val target = case lookup_target thy target_name
haftmann@59099
   306
         of SOME target => target
haftmann@59099
   307
          | NONE => error ("Unknown code target language: " ^ quote target_name);
haftmann@59099
   308
      in case the_language target
haftmann@59099
   309
       of Fundamental _ => (I, target)
haftmann@38927
   310
        | Extension (super, modify) => let
haftmann@59099
   311
            val (modify', target') = collapse super
haftmann@59099
   312
          in (modify' #> modify, merge_target false target_name (target', target)) end
haftmann@38927
   313
      end;
haftmann@39817
   314
  in collapse end;
haftmann@39817
   315
haftmann@39817
   316
local
haftmann@39817
   317
haftmann@59099
   318
fun activate_target ctxt target_name =
haftmann@39817
   319
  let
haftmann@59100
   320
    val (_, default_width) = Targets.get (Proof_Context.theory_of ctxt);
haftmann@59099
   321
    val (modify, target) = collapse_hierarchy ctxt target_name;
haftmann@59099
   322
  in (default_width, target, modify) end;
haftmann@38927
   323
haftmann@55757
   324
fun project_program ctxt syms_hidden syms1 program2 =
haftmann@38927
   325
  let
haftmann@55147
   326
    val syms2 = subtract (op =) syms_hidden syms1;
haftmann@55147
   327
    val program3 = Code_Symbol.Graph.restrict (not o member (op =) syms_hidden) program2;
haftmann@55147
   328
    val syms4 = Code_Symbol.Graph.all_succs program3 syms2;
haftmann@54890
   329
    val unimplemented = Code_Thingol.unimplemented program3;
wenzelm@42359
   330
    val _ =
haftmann@54889
   331
      if null unimplemented then ()
wenzelm@42359
   332
      else error ("No code equations for " ^
wenzelm@55304
   333
        commas (map (Proof_Context.markup_const ctxt) unimplemented));
haftmann@55147
   334
    val program4 = Code_Symbol.Graph.restrict (member (op =) syms4) program3;
haftmann@55147
   335
  in (syms4, program4) end;
haftmann@38927
   336
haftmann@55757
   337
fun prepare_serializer ctxt (serializer : serializer) reserved identifiers
haftmann@55147
   338
    printings module_name args proto_program syms =
haftmann@38927
   339
  let
haftmann@55149
   340
    val syms_hidden = Code_Symbol.symbols_of printings;
haftmann@55757
   341
    val (syms_all, program) = project_program ctxt syms_hidden syms proto_program;
haftmann@39484
   342
    fun select_include (name, (content, cs)) =
haftmann@55150
   343
      if null cs orelse exists (fn c => member (op =) syms_all (Constant c)) cs
haftmann@39484
   344
      then SOME (name, content) else NONE;
haftmann@52137
   345
    val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
haftmann@34021
   346
  in
haftmann@55757
   347
    (serializer args ctxt {
haftmann@52138
   348
      module_name = module_name,
haftmann@38926
   349
      reserved_syms = reserved,
haftmann@52138
   350
      identifiers = identifiers,
haftmann@38926
   351
      includes = includes,
haftmann@55149
   352
      const_syntax = Code_Symbol.lookup_constant_data printings,
haftmann@55149
   353
      tyco_syntax = Code_Symbol.lookup_type_constructor_data printings,
haftmann@55149
   354
      class_syntax = Code_Symbol.lookup_type_class_data printings },
haftmann@55776
   355
      (subtract (op =) syms_hidden syms, program))
haftmann@34021
   356
  end;
haftmann@34021
   357
haftmann@59099
   358
fun mount_serializer ctxt target_name some_width module_name args program syms =
haftmann@34021
   359
  let
haftmann@59099
   360
    val (default_width, target, modify) = activate_target ctxt target_name;
haftmann@59099
   361
    val serializer = case the_language target
haftmann@38927
   362
     of Fundamental seri => #serializer seri;
haftmann@55683
   363
    val (prepared_serializer, (prepared_syms, prepared_program)) =
haftmann@55757
   364
      prepare_serializer ctxt serializer
haftmann@59099
   365
        (the_reserved target) (the_identifiers target) (the_printings target)
haftmann@55147
   366
        module_name args (modify program) syms
haftmann@34071
   367
    val width = the_default default_width some_width;
haftmann@55683
   368
  in (fn program => fn syms => prepared_serializer syms program width, (prepared_syms, prepared_program)) end;
haftmann@41344
   369
haftmann@59099
   370
fun invoke_serializer ctxt target_name some_width raw_module_name args program all_public syms =
haftmann@41344
   371
  let
haftmann@55291
   372
    val module_name = if raw_module_name = "" then ""
haftmann@55291
   373
      else (check_name true raw_module_name; raw_module_name)
haftmann@55757
   374
    val (mounted_serializer, (prepared_syms, prepared_program)) =
haftmann@59099
   375
      mount_serializer ctxt target_name some_width module_name args program syms;
haftmann@55776
   376
  in mounted_serializer prepared_program (if all_public then [] else prepared_syms) end;
haftmann@34021
   377
haftmann@52138
   378
fun assert_module_name "" = error "Empty module name not allowed here"
haftmann@38933
   379
  | assert_module_name module_name = module_name;
haftmann@38933
   380
haftmann@55757
   381
fun using_master_directory ctxt =
wenzelm@56208
   382
  Option.map (Path.append (File.pwd ()) o
wenzelm@56208
   383
    Path.append (Resources.master_directory (Proof_Context.theory_of ctxt)));
haftmann@48371
   384
haftmann@34021
   385
in
haftmann@34021
   386
haftmann@48568
   387
val generatedN = "Generated_Code";
haftmann@48568
   388
haftmann@59099
   389
fun export_code_for ctxt some_path target_name some_width module_name args =
haftmann@55757
   390
  export (using_master_directory ctxt some_path)
haftmann@59099
   391
  ooo invoke_serializer ctxt target_name some_width module_name args;
haftmann@38918
   392
haftmann@59099
   393
fun produce_code_for ctxt target_name some_width module_name args =
haftmann@39102
   394
  let
haftmann@59099
   395
    val serializer = invoke_serializer ctxt target_name some_width (assert_module_name module_name) args;
haftmann@55683
   396
  in fn program => fn all_public => fn syms =>
haftmann@55683
   397
    produce (serializer program all_public syms) |> apsnd (fn deresolve => map deresolve syms)
haftmann@39484
   398
  end;
haftmann@38929
   399
haftmann@59099
   400
fun present_code_for ctxt target_name some_width module_name args =
haftmann@39484
   401
  let
haftmann@59099
   402
    val serializer = invoke_serializer ctxt target_name some_width (assert_module_name module_name) args;
haftmann@55147
   403
  in fn program => fn (syms, selects) =>
haftmann@55683
   404
    present selects (serializer program false syms)
haftmann@39484
   405
  end;
haftmann@38918
   406
haftmann@59099
   407
fun check_code_for ctxt target_name strict args program all_public syms =
haftmann@37824
   408
  let
haftmann@37824
   409
    val { env_var, make_destination, make_command } =
haftmann@59099
   410
      (#check o the_fundamental ctxt) target_name;
wenzelm@41939
   411
    fun ext_check p =
wenzelm@43564
   412
      let
haftmann@37824
   413
        val destination = make_destination p;
haftmann@59099
   414
        val _ = export (SOME destination) (invoke_serializer ctxt target_name (SOME 80)
haftmann@55683
   415
          generatedN args program all_public syms);
haftmann@48568
   416
        val cmd = make_command generatedN;
wenzelm@43850
   417
      in
wenzelm@43850
   418
        if Isabelle_System.bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
haftmann@59099
   419
        then error ("Code check failed for " ^ target_name ^ ": " ^ cmd)
haftmann@37824
   420
        else ()
haftmann@37824
   421
      end;
wenzelm@43850
   422
  in
wenzelm@43850
   423
    if getenv env_var = ""
haftmann@37825
   424
    then if strict
haftmann@59099
   425
      then error (env_var ^ " not set; cannot check code for " ^ target_name)
haftmann@59099
   426
      else warning (env_var ^ " not set; skipped checking code for " ^ target_name)
wenzelm@41939
   427
    else Isabelle_System.with_tmp_dir "Code_Test" ext_check
haftmann@37824
   428
  end;
haftmann@37824
   429
haftmann@56920
   430
fun subevaluator mounted_serializer prepared_program syms all_public ((vs, ty), t) =
haftmann@41344
   431
  let
haftmann@41344
   432
    val _ = if Code_Thingol.contains_dict_var t then
haftmann@41344
   433
      error "Term to be evaluated contains free dictionaries" else ();
wenzelm@43324
   434
    val v' = singleton (Name.variant_list (map fst vs)) "a";
haftmann@41344
   435
    val vs' = (v', []) :: vs;
haftmann@55147
   436
    val ty' = ITyVar v' `-> ty;
haftmann@41344
   437
    val program = prepared_program
haftmann@55147
   438
      |> Code_Symbol.Graph.new_node (Code_Symbol.value,
haftmann@57249
   439
          Code_Thingol.Fun (((vs', ty'), [(([IVar (SOME "dummy")], t), (NONE, true))]), NONE))
haftmann@55147
   440
      |> fold (curry (perhaps o try o
haftmann@55147
   441
          Code_Symbol.Graph.add_edge) Code_Symbol.value) syms;
haftmann@55683
   442
    val (program_code, deresolve) =
haftmann@55684
   443
      produce (mounted_serializer program (if all_public then [] else [Code_Symbol.value]));
haftmann@55147
   444
    val value_name = the (deresolve Code_Symbol.value);
haftmann@55147
   445
  in (program_code, value_name) end;
haftmann@41344
   446
haftmann@59099
   447
fun evaluator ctxt target_name program syms =
haftmann@41344
   448
  let
haftmann@55683
   449
    val (mounted_serializer, (_, prepared_program)) =
haftmann@59099
   450
      mount_serializer ctxt target_name NONE generatedN [] program syms;
haftmann@56920
   451
  in subevaluator mounted_serializer prepared_program syms end;
haftmann@41344
   452
haftmann@34021
   453
end; (* local *)
haftmann@34021
   454
haftmann@34021
   455
haftmann@34021
   456
(* code generation *)
haftmann@34021
   457
haftmann@38918
   458
fun prep_destination "" = NONE
haftmann@38918
   459
  | prep_destination s = SOME (Path.explode s);
haftmann@38918
   460
haftmann@55757
   461
fun export_code ctxt all_public cs seris =
haftmann@34021
   462
  let
haftmann@55757
   463
    val thy = Proof_Context.theory_of ctxt;
haftmann@55188
   464
    val program = Code_Thingol.consts_program thy cs;
haftmann@59099
   465
    val _ = map (fn (((target_name, module_name), some_path), args) =>
haftmann@59099
   466
      export_code_for ctxt some_path target_name NONE module_name args program all_public (map Constant cs)) seris;
haftmann@34021
   467
  in () end;
haftmann@34021
   468
haftmann@55757
   469
fun export_code_cmd all_public raw_cs seris ctxt =
haftmann@55757
   470
  export_code ctxt all_public
haftmann@55757
   471
    (Code_Thingol.read_const_exprs ctxt raw_cs)
haftmann@55683
   472
    ((map o apfst o apsnd) prep_destination seris);
haftmann@38918
   473
haftmann@59099
   474
fun produce_code ctxt all_public cs target_name some_width some_module_name args =
haftmann@38918
   475
  let
haftmann@55757
   476
    val thy = Proof_Context.theory_of ctxt;
haftmann@55188
   477
    val program = Code_Thingol.consts_program thy cs;
haftmann@59099
   478
  in produce_code_for ctxt target_name some_width some_module_name args program all_public (map Constant cs) end;
haftmann@38929
   479
haftmann@59099
   480
fun present_code ctxt cs syms target_name some_width some_module_name args =
haftmann@38929
   481
  let
haftmann@55757
   482
    val thy = Proof_Context.theory_of ctxt;
haftmann@55188
   483
    val program = Code_Thingol.consts_program thy cs;
haftmann@59099
   484
  in present_code_for ctxt target_name some_width some_module_name args program (map Constant cs, syms) end;
haftmann@34021
   485
haftmann@55757
   486
fun check_code ctxt all_public cs seris =
haftmann@37824
   487
  let
haftmann@55757
   488
    val thy = Proof_Context.theory_of ctxt;
haftmann@55188
   489
    val program = Code_Thingol.consts_program thy cs;
haftmann@59099
   490
    val _ = map (fn ((target_name, strict), args) =>
haftmann@59099
   491
      check_code_for ctxt target_name strict args program all_public (map Constant cs)) seris;
haftmann@37824
   492
  in () end;
haftmann@37824
   493
haftmann@55757
   494
fun check_code_cmd all_public raw_cs seris ctxt =
haftmann@55757
   495
  check_code ctxt all_public
haftmann@55757
   496
    (Code_Thingol.read_const_exprs ctxt raw_cs) seris;
haftmann@37824
   497
haftmann@39480
   498
local
haftmann@39480
   499
haftmann@39480
   500
val parse_const_terms = Scan.repeat1 Args.term
haftmann@55757
   501
  >> (fn ts => fn ctxt => map (Code.check_const (Proof_Context.theory_of ctxt)) ts);
haftmann@39480
   502
haftmann@55147
   503
fun parse_names category parse internalize mark_symbol =
haftmann@39480
   504
  Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
haftmann@55757
   505
  >> (fn xs => fn ctxt => map (mark_symbol o internalize ctxt) xs);
wenzelm@43564
   506
haftmann@39480
   507
val parse_consts = parse_names "consts" Args.term
haftmann@55757
   508
  (Code.check_const o Proof_Context.theory_of) Constant;
haftmann@39480
   509
haftmann@39480
   510
val parse_types = parse_names "types" (Scan.lift Args.name)
haftmann@55757
   511
  (Sign.intern_type o Proof_Context.theory_of) Type_Constructor;
haftmann@39480
   512
haftmann@39480
   513
val parse_classes = parse_names "classes" (Scan.lift Args.name)
haftmann@55757
   514
  (Sign.intern_class o Proof_Context.theory_of) Type_Class;
haftmann@39480
   515
haftmann@39480
   516
val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@55757
   517
  (fn ctxt => fn (raw_tyco, raw_class) =>
haftmann@55757
   518
    let
haftmann@55757
   519
      val thy = Proof_Context.theory_of ctxt;
haftmann@55757
   520
    in (Sign.intern_class thy raw_tyco, Sign.intern_type thy raw_class) end) Class_Instance;
haftmann@39480
   521
haftmann@39480
   522
in
haftmann@39480
   523
wenzelm@43564
   524
val antiq_setup =
wenzelm@43564
   525
  Thy_Output.antiquotation @{binding code_stmts}
wenzelm@43564
   526
    (parse_const_terms --
wenzelm@43564
   527
      Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances)
wenzelm@43564
   528
      -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
haftmann@59099
   529
    (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target_name, some_width)) =>
haftmann@55757
   530
        present_code ctxt (mk_cs ctxt)
haftmann@55757
   531
          (maps (fn f => f ctxt) mk_stmtss)
haftmann@59099
   532
          target_name some_width "Example" []);
haftmann@39480
   533
haftmann@39480
   534
end;
haftmann@39480
   535
haftmann@34021
   536
haftmann@28054
   537
(** serializer configuration **)
haftmann@27000
   538
haftmann@52138
   539
(* reserved symbol names *)
haftmann@52138
   540
haftmann@59099
   541
fun add_reserved target_name sym thy =
haftmann@52138
   542
  let
haftmann@59099
   543
    val (_, target) = collapse_hierarchy (Proof_Context.init_global thy) target_name;
haftmann@59099
   544
    val _ = if member (op =) (the_reserved target) sym
haftmann@52138
   545
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@52138
   546
      else ();
haftmann@52138
   547
  in
haftmann@52138
   548
    thy
haftmann@59099
   549
    |> map_reserved target_name (insert (op =) sym)
haftmann@52138
   550
  end;
haftmann@52138
   551
haftmann@52138
   552
haftmann@52377
   553
(* checking of syntax *)
haftmann@52138
   554
haftmann@59099
   555
fun check_const_syntax ctxt target_name c syn =
haftmann@55757
   556
  if Code_Printer.requires_args syn > Code.args_number (Proof_Context.theory_of ctxt) c
haftmann@52137
   557
  then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@59099
   558
  else Code_Printer.prep_const_syntax (Proof_Context.theory_of ctxt) (the_literals ctxt target_name) c syn;
haftmann@52137
   559
haftmann@59099
   560
fun check_tyco_syntax ctxt target_name tyco syn =
haftmann@55757
   561
  if fst syn <> Sign.arity_number (Proof_Context.theory_of ctxt) tyco
haftmann@52137
   562
  then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@52137
   563
  else syn;
haftmann@34071
   564
haftmann@52138
   565
haftmann@52138
   566
(* custom symbol names *)
haftmann@52138
   567
wenzelm@52218
   568
fun arrange_name_decls x =
haftmann@52138
   569
  let
haftmann@52138
   570
    fun arrange is_module (sym, target_names) = map (fn (target, some_name) =>
haftmann@52138
   571
      (target, (sym, Option.map (check_name is_module) some_name))) target_names;
haftmann@52138
   572
  in
haftmann@52138
   573
    Code_Symbol.maps_attr' (arrange false) (arrange false) (arrange false)
wenzelm@52218
   574
      (arrange false) (arrange false) (arrange true) x
haftmann@52138
   575
  end;
haftmann@52138
   576
haftmann@55757
   577
fun cert_name_decls ctxt = cert_syms ctxt #> arrange_name_decls;
haftmann@52138
   578
haftmann@55757
   579
fun read_name_decls ctxt = read_syms ctxt #> arrange_name_decls;
haftmann@52138
   580
haftmann@59099
   581
fun set_identifier (target_name, sym_name) = map_identifiers target_name (Code_Symbol.set_data sym_name);
haftmann@52138
   582
haftmann@52138
   583
fun gen_set_identifiers prep_name_decl raw_name_decls thy =
haftmann@55757
   584
  fold set_identifier (prep_name_decl (Proof_Context.init_global thy) raw_name_decls) thy;
haftmann@52138
   585
haftmann@52138
   586
val set_identifiers = gen_set_identifiers cert_name_decls;
haftmann@52138
   587
val set_identifiers_cmd = gen_set_identifiers read_name_decls;
haftmann@52138
   588
haftmann@52138
   589
haftmann@52138
   590
(* custom printings *)
haftmann@52138
   591
haftmann@55757
   592
fun arrange_printings prep_const ctxt =
haftmann@52137
   593
  let
haftmann@52137
   594
    fun arrange check (sym, target_syns) =
haftmann@59099
   595
      map (fn (target_name, some_syn) =>
haftmann@59099
   596
        (target_name, (sym, Option.map (check ctxt target_name sym) some_syn))) target_syns;
haftmann@52137
   597
  in
haftmann@52137
   598
    Code_Symbol.maps_attr'
haftmann@52137
   599
      (arrange check_const_syntax) (arrange check_tyco_syntax)
haftmann@55149
   600
        (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) (arrange ((K o K o K) I))
haftmann@55757
   601
        (arrange (fn ctxt => fn _ => fn _ => fn (raw_content, raw_cs) =>
haftmann@55757
   602
          (Code_Printer.str raw_content, map (prep_const ctxt) raw_cs)))
haftmann@52137
   603
  end;
haftmann@52137
   604
haftmann@55757
   605
fun cert_printings ctxt = cert_syms ctxt #> arrange_printings cert_const ctxt;
haftmann@24219
   606
haftmann@55757
   607
fun read_printings ctxt = read_syms ctxt #> arrange_printings read_const ctxt;
haftmann@52137
   608
haftmann@59099
   609
fun set_printing (target_name, sym_syn) = map_printings target_name (Code_Symbol.set_data sym_syn);
haftmann@52137
   610
haftmann@52137
   611
fun gen_set_printings prep_print_decl raw_print_decls thy =
haftmann@55757
   612
  fold set_printing (prep_print_decl (Proof_Context.init_global thy) raw_print_decls) thy;
haftmann@52137
   613
haftmann@52137
   614
val set_printings = gen_set_printings cert_printings;
haftmann@52137
   615
val set_printings_cmd = gen_set_printings read_printings;
haftmann@52137
   616
haftmann@52137
   617
haftmann@27103
   618
(* concrete syntax *)
haftmann@27000
   619
haftmann@28054
   620
fun parse_args f args =
wenzelm@36959
   621
  case Scan.read Token.stopper f args
haftmann@28054
   622
   of SOME x => x
haftmann@28054
   623
    | NONE => error "Bad serializer arguments";
haftmann@28054
   624
haftmann@28054
   625
haftmann@27304
   626
(** Isar setup **)
haftmann@27103
   627
haftmann@52137
   628
fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
haftmann@52434
   629
  parse_keyword |-- Parse.!!! (parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"})
haftmann@52434
   630
    -- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target)));
haftmann@52137
   631
haftmann@52137
   632
fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
wenzelm@52801
   633
  parse_single_symbol_pragma @{keyword "constant"} Parse.term parse_const
haftmann@55150
   634
    >> Constant
haftmann@52377
   635
  || parse_single_symbol_pragma @{keyword "type_constructor"} Parse.type_const parse_tyco
haftmann@55150
   636
    >> Type_Constructor
haftmann@52377
   637
  || parse_single_symbol_pragma @{keyword "type_class"} Parse.class parse_class
haftmann@55150
   638
    >> Type_Class
haftmann@52137
   639
  || parse_single_symbol_pragma @{keyword "class_relation"} parse_classrel_ident parse_classrel
haftmann@55150
   640
    >> Class_Relation
haftmann@52137
   641
  || parse_single_symbol_pragma @{keyword "class_instance"} parse_inst_ident parse_inst
haftmann@55150
   642
    >> Class_Instance
haftmann@52137
   643
  || parse_single_symbol_pragma @{keyword "code_module"} Parse.name parse_module
haftmann@52137
   644
    >> Code_Symbol.Module;
haftmann@52137
   645
haftmann@52137
   646
fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@52137
   647
  Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
haftmann@52137
   648
    (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
haftmann@52137
   649
wenzelm@56201
   650
val code_expr_argsP = Scan.optional (@{keyword "("} |-- Parse.args --| @{keyword ")"}) [];
haftmann@52138
   651
haftmann@55683
   652
fun code_expr_inP all_public raw_cs =
haftmann@52434
   653
  Scan.repeat (@{keyword "in"} |-- Parse.!!! (Parse.name
haftmann@52434
   654
    -- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
haftmann@52434
   655
    -- Scan.optional (@{keyword "file"} |-- Parse.name) ""
haftmann@52434
   656
    -- code_expr_argsP))
haftmann@55683
   657
      >> (fn seri_args => export_code_cmd all_public raw_cs seri_args);
haftmann@52434
   658
haftmann@55683
   659
fun code_expr_checkingP all_public raw_cs =
haftmann@52434
   660
  (@{keyword "checking"} |-- Parse.!!!
haftmann@52434
   661
    (Scan.repeat (Parse.name -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true)
haftmann@52434
   662
    -- code_expr_argsP)))
haftmann@55683
   663
      >> (fn seri_args => check_code_cmd all_public raw_cs seri_args);
haftmann@52434
   664
haftmann@55683
   665
val code_exprP = (Scan.optional (@{keyword "open"} |-- Scan.succeed true) false
haftmann@55683
   666
  -- Scan.repeat1 Parse.term)
haftmann@55683
   667
  :|-- (fn (all_public, raw_cs) => (code_expr_checkingP all_public raw_cs || code_expr_inP all_public raw_cs));
haftmann@52138
   668
haftmann@52138
   669
val _ =
haftmann@52138
   670
  Outer_Syntax.command @{command_spec "code_reserved"}
haftmann@52138
   671
    "declare words as reserved for target language"
haftmann@52138
   672
    (Parse.name -- Scan.repeat1 Parse.name
haftmann@52138
   673
      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
haftmann@52138
   674
haftmann@52138
   675
val _ =
haftmann@52138
   676
  Outer_Syntax.command @{command_spec "code_identifier"} "declare mandatory names for code symbols"
haftmann@52138
   677
    (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
haftmann@52138
   678
      >> (Toplevel.theory o fold set_identifiers_cmd));
haftmann@52138
   679
haftmann@52138
   680
val _ =
haftmann@52137
   681
  Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
haftmann@52137
   682
    (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
haftmann@52137
   683
      Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
haftmann@52137
   684
      (Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
haftmann@52137
   685
      >> (Toplevel.theory o fold set_printings_cmd));
haftmann@52137
   686
haftmann@52137
   687
val _ =
wenzelm@46961
   688
  Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
haftmann@55757
   689
    (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.context_of)));
haftmann@30494
   690
haftmann@39646
   691
haftmann@39646
   692
(** external entrance point -- for codegen tool **)
haftmann@39646
   693
haftmann@39750
   694
fun codegen_tool thyname cmd_expr =
haftmann@39646
   695
  let
wenzelm@58928
   696
    val thy = Thy_Info.get_theory thyname;
wenzelm@58928
   697
    val ctxt = Proof_Context.init_global thy;
haftmann@39646
   698
    val parse = Scan.read Token.stopper (Parse.!!! code_exprP) o
wenzelm@59083
   699
      (filter Token.is_proper o Token.explode (Thy_Header.get_keywords thy) Position.none);
haftmann@39646
   700
  in case parse cmd_expr
haftmann@55757
   701
   of SOME f => (writeln "Now generating code..."; f ctxt)
haftmann@39646
   702
    | NONE => error ("Bad directive " ^ quote cmd_expr)
haftmann@39646
   703
  end;
haftmann@39646
   704
wenzelm@43564
   705
wenzelm@43564
   706
(** theory setup **)
wenzelm@43564
   707
wenzelm@43564
   708
val setup = antiq_setup;
wenzelm@43564
   709
haftmann@24219
   710
end; (*struct*)