src/Tools/Code/code_target.ML
author haftmann
Wed, 01 Jan 2014 01:05:48 +0100
changeset 56232 cb892d835803
parent 56231 4121d64fde90
child 56329 3f561ee3d998
permissions -rw-r--r--
fundamental treatment of undefined vs. universally partial replaces code_abort
haftmann@37744
     1
(*  Title:      Tools/Code/code_target.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@39136
     4
Generic infrastructure for target language data.
haftmann@24219
     5
*)
haftmann@24219
     6
haftmann@24219
     7
signature CODE_TARGET =
haftmann@24219
     8
sig
haftmann@36467
     9
  val cert_tyco: theory -> string -> string
haftmann@36467
    10
  val read_tyco: theory -> string -> string
haftmann@39291
    11
  val read_const_exprs: theory -> string list -> string list
haftmann@36467
    12
haftmann@39159
    13
  val export_code_for: theory -> Path.T option -> string -> int option -> string -> Token.T list
haftmann@39144
    14
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
haftmann@39159
    15
  val produce_code_for: theory -> string -> int option -> string -> Token.T list
haftmann@49583
    16
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> (string * string) list * string option list
haftmann@39159
    17
  val present_code_for: theory -> string -> int option -> string -> Token.T list
haftmann@39155
    18
    -> Code_Thingol.naming -> Code_Thingol.program -> string list * string list -> string
haftmann@39144
    19
  val check_code_for: theory -> string -> bool -> Token.T list
haftmann@39144
    20
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
haftmann@39144
    21
haftmann@39144
    22
  val export_code: theory -> string list
haftmann@39159
    23
    -> (((string * string) * Path.T option) * Token.T list) list -> unit
haftmann@39155
    24
  val produce_code: theory -> string list
haftmann@49583
    25
    -> string -> int option -> string -> Token.T list -> (string * string) list * string option list
haftmann@39155
    26
  val present_code: theory -> string list -> (Code_Thingol.naming -> string list)
haftmann@39159
    27
    -> string -> int option -> string -> Token.T list -> string
haftmann@39144
    28
  val check_code: theory -> string list
haftmann@39144
    29
    -> ((string * bool) * Token.T list) list -> unit
haftmann@39144
    30
haftmann@49583
    31
  val generatedN: string
haftmann@41592
    32
  val evaluator: theory -> string -> Code_Thingol.naming -> Code_Thingol.program
haftmann@41592
    33
    -> string list -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
haftmann@49583
    34
    -> (string * string) list * string
haftmann@41592
    35
haftmann@28054
    36
  type serializer
haftmann@34141
    37
  type literals = Code_Printer.literals
haftmann@37822
    38
  val add_target: string * { serializer: serializer, literals: literals,
wenzelm@42811
    39
    check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }
wenzelm@42811
    40
    -> theory -> theory
haftmann@28663
    41
  val extend_target: string *
haftmann@28663
    42
      (string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    43
    -> theory -> theory
haftmann@28054
    44
  val assert_target: theory -> string -> string
haftmann@39144
    45
  val the_literals: theory -> string -> literals
haftmann@28054
    46
  type serialization
wenzelm@36969
    47
  val parse_args: 'a parser -> Token.T list -> 'a
haftmann@39142
    48
  val serialization: (int -> Path.T option -> 'a -> unit)
haftmann@49583
    49
    -> (string list -> int -> 'a -> (string * string) list * (string -> string option))
haftmann@39151
    50
    -> 'a -> serialization
haftmann@39144
    51
  val set_default_code_width: int -> theory -> theory
haftmann@39143
    52
haftmann@53274
    53
  type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
haftmann@53275
    54
  type identifier_data
haftmann@53275
    55
  val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
haftmann@53275
    56
    -> theory -> theory
haftmann@53274
    57
  type const_syntax = Code_Printer.const_syntax
haftmann@34141
    58
  type tyco_syntax = Code_Printer.tyco_syntax
haftmann@53274
    59
  val set_printings: (const_syntax, tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
haftmann@53274
    60
    -> theory -> theory
haftmann@53274
    61
  val add_const_syntax: string -> string -> const_syntax option -> theory -> theory
haftmann@53274
    62
  val add_tyco_syntax: string -> string -> tyco_syntax option -> theory -> theory
haftmann@39149
    63
  val add_class_syntax: string -> class -> string option -> theory -> theory
haftmann@39149
    64
  val add_instance_syntax: string -> class * string -> unit option -> theory -> theory
haftmann@28054
    65
  val add_reserved: string -> string -> theory -> theory
haftmann@33968
    66
  val add_include: string -> string * (string * string list) option -> theory -> theory
haftmann@39870
    67
haftmann@39983
    68
  val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
wenzelm@44450
    69
wenzelm@44450
    70
  val setup: theory -> theory
haftmann@24219
    71
end;
haftmann@24219
    72
haftmann@28054
    73
structure Code_Target : CODE_TARGET =
haftmann@24219
    74
struct
haftmann@24219
    75
haftmann@28054
    76
open Basic_Code_Thingol;
haftmann@34141
    77
haftmann@34141
    78
type literals = Code_Printer.literals;
haftmann@53274
    79
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl =
haftmann@53274
    80
  (string * (string * 'a option) list, string * (string * 'b option) list,
haftmann@53274
    81
    class * (string * 'c option) list, (class * class) * (string * 'd option) list,
haftmann@53274
    82
    (class * string) * (string * 'e option) list,
haftmann@53274
    83
    string * (string * 'f option) list) Code_Symbol.attr;
haftmann@53275
    84
type identifier_data = (string, string, string, string, string, string) Code_Symbol.data;
haftmann@53274
    85
haftmann@34141
    86
type tyco_syntax = Code_Printer.tyco_syntax;
haftmann@37849
    87
type const_syntax = Code_Printer.const_syntax;
haftmann@34141
    88
haftmann@24219
    89
haftmann@53514
    90
(** checking and parsing of symbols **)
haftmann@53514
    91
haftmann@53514
    92
fun cert_const thy const =
haftmann@53514
    93
  let
haftmann@53514
    94
    val _ = if Sign.declared_const thy const then ()
haftmann@53514
    95
      else error ("No such constant: " ^ quote const);
haftmann@53514
    96
  in const end;
haftmann@53514
    97
haftmann@53514
    98
fun cert_tyco thy tyco =
haftmann@53514
    99
  let
haftmann@53514
   100
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@53514
   101
      else error ("No such type constructor: " ^ quote tyco);
haftmann@53514
   102
  in tyco end;
haftmann@53514
   103
haftmann@53514
   104
fun read_tyco thy = #1 o dest_Type
haftmann@53514
   105
  o Proof_Context.read_type_name_proper (Proof_Context.init_global thy) true;
haftmann@53514
   106
haftmann@53514
   107
fun cert_class thy class =
haftmann@53514
   108
  let
haftmann@53514
   109
    val _ = Axclass.get_info thy class;
haftmann@53514
   110
  in class end;
haftmann@53514
   111
haftmann@53514
   112
fun read_class thy = Proof_Context.read_class (Proof_Context.init_global thy);
haftmann@53514
   113
haftmann@53514
   114
val parse_classrel_ident = Parse.class --| @{keyword "<"} -- Parse.class;
haftmann@53514
   115
haftmann@53514
   116
fun cert_inst thy (class, tyco) =
haftmann@53514
   117
  (cert_class thy class, cert_tyco thy tyco);
haftmann@53514
   118
haftmann@53514
   119
fun read_inst thy (raw_tyco, raw_class) =
haftmann@53514
   120
  (read_class thy raw_class, read_tyco thy raw_tyco);
haftmann@53514
   121
haftmann@53514
   122
val parse_inst_ident = Parse.xname --| @{keyword "::"} -- Parse.class;
haftmann@53514
   123
haftmann@53514
   124
fun cert_syms thy =
haftmann@53514
   125
  Code_Symbol.map_attr (apfst (cert_const thy)) (apfst (cert_tyco thy))
haftmann@53514
   126
    (apfst (cert_class thy)) ((apfst o pairself) (cert_class thy)) (apfst (cert_inst thy)) I;
haftmann@53514
   127
haftmann@53514
   128
fun read_syms thy =
haftmann@53514
   129
  Code_Symbol.map_attr (apfst (Code.read_const thy)) (apfst (read_tyco thy))
haftmann@53514
   130
    (apfst (read_class thy)) ((apfst o pairself) (read_class thy)) (apfst (read_inst thy)) I;
haftmann@53514
   131
haftmann@53514
   132
fun check_name is_module s =
haftmann@53514
   133
  let
haftmann@53514
   134
    val _ = if s = "" then error "Bad empty code name" else ();
haftmann@53514
   135
    val xs = Long_Name.explode s;
haftmann@53514
   136
    val xs' = if is_module
haftmann@53514
   137
        then map (Name.desymbolize true) xs
haftmann@53514
   138
      else if length xs < 2
haftmann@53514
   139
        then error ("Bad code name without module component: " ^ quote s)
haftmann@53514
   140
      else
haftmann@53514
   141
        let
haftmann@53514
   142
          val (ys, y) = split_last xs;
haftmann@53514
   143
          val ys' = map (Name.desymbolize true) ys;
haftmann@53514
   144
          val y' = Name.desymbolize false y;
haftmann@53514
   145
        in ys' @ [y'] end;
haftmann@53514
   146
  in if xs' = xs
haftmann@53514
   147
    then s
haftmann@53514
   148
    else error ("Invalid code name: " ^ quote s ^ "\n"
haftmann@53514
   149
      ^ "better try " ^ quote (Long_Name.implode xs'))
haftmann@53514
   150
  end;
haftmann@53514
   151
haftmann@53514
   152
haftmann@53274
   153
(** serializations and serializer **)
haftmann@53274
   154
haftmann@53274
   155
(* serialization: abstract nonsense to cover different destinies for generated code *)
haftmann@24219
   156
haftmann@39155
   157
datatype destination = Export of Path.T option | Produce | Present of string list;
haftmann@49583
   158
type serialization = int -> destination -> ((string * string) list * (string -> string option)) option;
haftmann@27014
   159
haftmann@39885
   160
fun serialization output _ content width (Export some_path) =
haftmann@39885
   161
      (output width some_path content; NONE)
haftmann@39885
   162
  | serialization _ string content width Produce =
haftmann@39885
   163
      string [] width content |> SOME
haftmann@39885
   164
  | serialization _ string content width (Present stmt_names) =
haftmann@39885
   165
     string stmt_names width content
haftmann@49583
   166
     |> (apfst o map o apsnd) (Pretty.output (SOME width) o Pretty.str)
haftmann@39885
   167
     |> SOME;
haftmann@24219
   168
haftmann@39885
   169
fun export some_path f = (f (Export some_path); ());
haftmann@39885
   170
fun produce f = the (f Produce);
haftmann@49583
   171
fun present stmt_names f = space_implode "\n\n" (map snd (fst (the (f (Present stmt_names)))));
haftmann@39143
   172
haftmann@24219
   173
haftmann@53274
   174
(* serializers: functions producing serializations *)
haftmann@27103
   175
haftmann@39381
   176
type serializer = Token.T list
haftmann@53275
   177
  -> Proof.context
haftmann@39381
   178
  -> {
haftmann@53275
   179
    symbol_of: string -> Code_Symbol.symbol,
haftmann@53275
   180
    module_name: string,
haftmann@39152
   181
    reserved_syms: string list,
haftmann@53275
   182
    identifiers: identifier_data,
haftmann@39152
   183
    includes: (string * Pretty.T) list,
haftmann@39152
   184
    class_syntax: string -> string option,
haftmann@39152
   185
    tyco_syntax: string -> Code_Printer.tyco_syntax option,
haftmann@41590
   186
    const_syntax: string -> Code_Printer.activated_const_syntax option }
haftmann@41590
   187
  -> Code_Thingol.program
haftmann@28054
   188
  -> serialization;
haftmann@27103
   189
haftmann@53274
   190
datatype description =
haftmann@53274
   191
    Fundamental of { serializer: serializer,
haftmann@39136
   192
      literals: literals,
haftmann@37822
   193
      check: { env_var: string, make_destination: Path.T -> Path.T,
wenzelm@42811
   194
        make_command: string -> string } }
haftmann@39135
   195
  | Extension of string *
haftmann@39135
   196
      (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
haftmann@27103
   197
haftmann@53274
   198
haftmann@53274
   199
(** theory data **)
haftmann@53274
   200
haftmann@28054
   201
datatype target = Target of {
haftmann@28054
   202
  serial: serial,
haftmann@37821
   203
  description: description,
haftmann@28054
   204
  reserved: string list,
haftmann@53275
   205
  identifiers: identifier_data,
haftmann@53274
   206
  printings: (Code_Printer.const_syntax, Code_Printer.tyco_syntax, string, unit, unit,
haftmann@53274
   207
    (Pretty.T * string list)) Code_Symbol.data
haftmann@28054
   208
};
haftmann@27103
   209
haftmann@53275
   210
fun make_target ((serial, description), (reserved, (identifiers, printings))) =
wenzelm@44450
   211
  Target { serial = serial, description = description, reserved = reserved,
haftmann@53275
   212
    identifiers = identifiers, printings = printings };
haftmann@53275
   213
fun map_target f (Target { serial, description, reserved, identifiers, printings }) =
haftmann@53275
   214
  make_target (f ((serial, description), (reserved, (identifiers, printings))));
haftmann@37821
   215
fun merge_target strict target (Target { serial = serial1, description = description,
haftmann@53275
   216
  reserved = reserved1, identifiers = identifiers1, printings = printings1 },
haftmann@37821
   217
    Target { serial = serial2, description = _,
haftmann@53275
   218
      reserved = reserved2, identifiers = identifiers2, printings = printings2 }) =
haftmann@28054
   219
  if serial1 = serial2 orelse not strict then
haftmann@53275
   220
    make_target ((serial1, description), (merge (op =) (reserved1, reserved2),
haftmann@53275
   221
      (Code_Symbol.merge_data (identifiers1, identifiers2),
haftmann@53275
   222
        Code_Symbol.merge_data (printings1, printings2))))
haftmann@28054
   223
  else
haftmann@37821
   224
    error ("Incompatible targets: " ^ quote target);
haftmann@27436
   225
haftmann@37821
   226
fun the_description (Target { description, ... }) = description;
haftmann@28054
   227
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@53275
   228
fun the_identifiers (Target { identifiers , ... }) = identifiers;
haftmann@53274
   229
fun the_printings (Target { printings, ... }) = printings;
haftmann@27436
   230
haftmann@34243
   231
structure Targets = Theory_Data
haftmann@34071
   232
(
haftmann@56232
   233
  type T = target Symtab.table * int;
haftmann@56232
   234
  val empty = (Symtab.empty, 80);
haftmann@34071
   235
  val extend = I;
haftmann@56232
   236
  fun merge ((target1, width1), (target2, width2)) : T =
haftmann@56232
   237
    (Symtab.join (merge_target true) (target1, target2), Int.max (width1, width2));
haftmann@34071
   238
);
haftmann@28054
   239
haftmann@56232
   240
fun assert_target thy target = if Symtab.defined (fst (Targets.get thy)) target
haftmann@33968
   241
  then target
haftmann@33968
   242
  else error ("Unknown code target language: " ^ quote target);
haftmann@28054
   243
haftmann@28054
   244
fun put_target (target, seri) thy =
haftmann@27437
   245
  let
haftmann@56232
   246
    val lookup_target = Symtab.lookup (fst (Targets.get thy));
haftmann@28054
   247
    val _ = case seri
haftmann@37821
   248
     of Extension (super, _) => if is_some (lookup_target super) then ()
haftmann@28054
   249
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   250
      | _ => ();
haftmann@37821
   251
    val overwriting = case (Option.map the_description o lookup_target) target
haftmann@28663
   252
     of NONE => false
haftmann@37821
   253
      | SOME (Extension _) => true
haftmann@37821
   254
      | SOME (Fundamental _) => (case seri
haftmann@37821
   255
         of Extension _ => error ("Will not overwrite existing target " ^ quote target)
haftmann@28663
   256
          | _ => true);
haftmann@28663
   257
    val _ = if overwriting
haftmann@28054
   258
      then warning ("Overwriting existing target " ^ quote target)
wenzelm@44450
   259
      else ();
haftmann@27103
   260
  in
haftmann@28054
   261
    thy
haftmann@56232
   262
    |> (Targets.map o apfst o Symtab.update)
haftmann@53275
   263
        (target, make_target ((serial (), seri),
haftmann@53275
   264
          ([], (Code_Symbol.empty_data, Code_Symbol.empty_data))))
haftmann@27103
   265
  end;
haftmann@27000
   266
haftmann@37821
   267
fun add_target (target, seri) = put_target (target, Fundamental seri);
haftmann@28054
   268
fun extend_target (target, (super, modify)) =
haftmann@37821
   269
  put_target (target, Extension (super, modify));
haftmann@27000
   270
haftmann@28054
   271
fun map_target_data target f thy =
haftmann@27304
   272
  let
haftmann@28054
   273
    val _ = assert_target thy target;
haftmann@27000
   274
  in
haftmann@28054
   275
    thy
haftmann@56232
   276
    |> (Targets.map o apfst o Symtab.map_entry target o map_target o apsnd) f
haftmann@27000
   277
  end;
haftmann@27000
   278
haftmann@28054
   279
fun map_reserved target =
haftmann@53275
   280
  map_target_data target o apfst;
haftmann@53275
   281
fun map_identifiers target =
haftmann@53275
   282
  map_target_data target o apsnd o apfst;
haftmann@53274
   283
fun map_printings target =
haftmann@53274
   284
  map_target_data target o apsnd o apsnd;
haftmann@27000
   285
haftmann@34243
   286
fun set_default_code_width k = (Targets.map o apsnd) (K k);
haftmann@34071
   287
haftmann@27000
   288
haftmann@34021
   289
(** serializer usage **)
haftmann@34021
   290
haftmann@34021
   291
(* montage *)
haftmann@34021
   292
haftmann@37822
   293
fun the_fundamental thy =
haftmann@34021
   294
  let
haftmann@56232
   295
    val (targets, _) = Targets.get thy;
haftmann@37822
   296
    fun fundamental target = case Symtab.lookup targets target
haftmann@37821
   297
     of SOME data => (case the_description data
haftmann@37822
   298
         of Fundamental data => data
haftmann@37822
   299
          | Extension (super, _) => fundamental super)
haftmann@34021
   300
      | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@37822
   301
  in fundamental end;
haftmann@37822
   302
haftmann@37822
   303
fun the_literals thy = #literals o the_fundamental thy;
haftmann@34021
   304
haftmann@40059
   305
fun collapse_hierarchy thy =
haftmann@39153
   306
  let
haftmann@56232
   307
    val (targets, _) = Targets.get thy;
wenzelm@44450
   308
    fun collapse target =
haftmann@39153
   309
      let
haftmann@39153
   310
        val data = case Symtab.lookup targets target
haftmann@39153
   311
         of SOME data => data
haftmann@39153
   312
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@39153
   313
      in case the_description data
haftmann@39724
   314
       of Fundamental _ => (K I, data)
haftmann@39153
   315
        | Extension (super, modify) => let
haftmann@40059
   316
            val (modify', data') = collapse super
haftmann@39724
   317
          in (fn naming => modify' naming #> modify naming, merge_target false target (data', data)) end
haftmann@39153
   318
      end;
haftmann@40059
   319
  in collapse end;
haftmann@40059
   320
haftmann@40059
   321
local
haftmann@40059
   322
haftmann@40059
   323
fun activate_target thy target =
haftmann@40059
   324
  let
haftmann@56232
   325
    val (_, default_width) = Targets.get thy;
haftmann@40059
   326
    val (modify, data) = collapse_hierarchy thy target;
haftmann@56232
   327
  in (default_width, data, modify) end;
haftmann@39153
   328
haftmann@53274
   329
fun activate_const_syntax thy literals cs_data naming =
haftmann@53274
   330
  (Symtab.empty, naming)
haftmann@53274
   331
  |> fold_map (fn (c, data) => fn (tab, naming) =>
haftmann@53274
   332
      case Code_Thingol.lookup_const naming c
haftmann@53274
   333
       of SOME name => let
haftmann@53274
   334
              val (syn, naming') =
haftmann@53274
   335
                Code_Printer.activate_const_syntax thy literals c data naming
haftmann@53274
   336
            in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
haftmann@53274
   337
        | NONE => (NONE, (tab, naming))) cs_data
haftmann@34021
   338
  |>> map_filter I;
haftmann@34021
   339
haftmann@53274
   340
fun activate_syntax lookup_name things =
haftmann@53274
   341
  Symtab.empty
haftmann@53274
   342
  |> fold_map (fn (thing_identifier, data) => fn tab => case lookup_name thing_identifier
haftmann@53274
   343
       of SOME name => (SOME name, Symtab.update_new (name, data) tab)
haftmann@53274
   344
        | NONE => (NONE, tab)) things
haftmann@34021
   345
  |>> map_filter I;
haftmann@34021
   346
haftmann@53274
   347
fun activate_symbol_syntax thy literals naming printings =
haftmann@34021
   348
  let
haftmann@53274
   349
    val (names_const, (const_syntax, _)) =
haftmann@53274
   350
      activate_const_syntax thy literals (Code_Symbol.dest_constant_data printings) naming;
haftmann@53274
   351
    val (names_tyco, tyco_syntax) =
haftmann@53274
   352
      activate_syntax (Code_Thingol.lookup_tyco naming) (Code_Symbol.dest_type_constructor_data printings);
haftmann@53274
   353
    val (names_class, class_syntax) =
haftmann@53274
   354
      activate_syntax (Code_Thingol.lookup_class naming) (Code_Symbol.dest_type_class_data printings);
haftmann@53274
   355
    val names_inst = map_filter (Code_Thingol.lookup_instance naming o fst)
haftmann@53274
   356
      (Code_Symbol.dest_class_instance_data printings);
haftmann@39153
   357
  in
haftmann@53274
   358
    (names_const @ names_tyco @ names_class @ names_inst,
haftmann@53274
   359
      (const_syntax, tyco_syntax, class_syntax))
haftmann@39153
   360
  end;
haftmann@39153
   361
haftmann@56232
   362
fun project_program thy names_hidden names1 program2 =
haftmann@39153
   363
  let
wenzelm@43232
   364
    val ctxt = Proof_Context.init_global thy;
haftmann@34021
   365
    val names2 = subtract (op =) names_hidden names1;
wenzelm@47485
   366
    val program3 = Graph.restrict (not o member (op =) names_hidden) program2;
haftmann@39162
   367
    val names4 = Graph.all_succs program3 names2;
haftmann@56232
   368
    val unimplemented = Code_Thingol.unimplemented program3;
wenzelm@43230
   369
    val _ =
haftmann@56231
   370
      if null unimplemented then ()
wenzelm@43230
   371
      else error ("No code equations for " ^
haftmann@56231
   372
        commas (map (Proof_Context.extern_const ctxt) unimplemented));
wenzelm@47485
   373
    val program4 = Graph.restrict (member (op =) names4) program3;
haftmann@39162
   374
  in (names4, program4) end;
haftmann@39153
   375
haftmann@56232
   376
fun prepare_serializer thy (serializer : serializer) literals reserved identifiers
haftmann@53274
   377
    printings module_name args naming proto_program names =
haftmann@39153
   378
  let
haftmann@53274
   379
    val (names_hidden, (const_syntax, tyco_syntax, class_syntax)) =
haftmann@53274
   380
      activate_symbol_syntax thy literals naming printings;
haftmann@56232
   381
    val (names_all, program) = project_program thy names_hidden names proto_program;
haftmann@39724
   382
    fun select_include (name, (content, cs)) =
haftmann@39724
   383
      if null cs orelse exists (fn c => case Code_Thingol.lookup_const naming c
haftmann@39724
   384
       of SOME name => member (op =) names_all name
haftmann@39724
   385
        | NONE => false) cs
haftmann@39724
   386
      then SOME (name, content) else NONE;
haftmann@53274
   387
    val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
haftmann@34021
   388
  in
haftmann@53275
   389
    (serializer args (Proof_Context.init_global thy) {
haftmann@53275
   390
      symbol_of = Code_Thingol.symbol_of proto_program,
haftmann@53275
   391
      module_name = module_name,
haftmann@39152
   392
      reserved_syms = reserved,
haftmann@53275
   393
      identifiers = identifiers,
haftmann@39152
   394
      includes = includes,
haftmann@53274
   395
      const_syntax = Symtab.lookup const_syntax,
haftmann@39153
   396
      tyco_syntax = Symtab.lookup tyco_syntax,
haftmann@53274
   397
      class_syntax = Symtab.lookup class_syntax },
haftmann@41590
   398
      program)
haftmann@34021
   399
  end;
haftmann@34021
   400
haftmann@41590
   401
fun mount_serializer thy target some_width module_name args naming program names =
haftmann@34021
   402
  let
haftmann@56232
   403
    val (default_width, data, modify) = activate_target thy target;
haftmann@39147
   404
    val serializer = case the_description data
haftmann@39153
   405
     of Fundamental seri => #serializer seri;
haftmann@53275
   406
    val (prepared_serializer, prepared_program) =
haftmann@56232
   407
      prepare_serializer thy serializer (the_literals thy target)
haftmann@53275
   408
        (the_reserved data) (the_identifiers data) (the_printings data)
haftmann@53274
   409
        module_name args naming (modify naming program) names
haftmann@34071
   410
    val width = the_default default_width some_width;
haftmann@41592
   411
  in (fn program => prepared_serializer program width, prepared_program) end;
haftmann@41592
   412
haftmann@41592
   413
fun invoke_serializer thy target some_width module_name args naming program names =
haftmann@41592
   414
  let
haftmann@54550
   415
    val check = if module_name = "" then I else check_name true;
haftmann@41592
   416
    val (mounted_serializer, prepared_program) = mount_serializer thy
haftmann@54550
   417
      target some_width (check module_name) args naming program names;
haftmann@41592
   418
  in mounted_serializer prepared_program end;
haftmann@34021
   419
haftmann@53275
   420
fun assert_module_name "" = error "Empty module name not allowed here"
haftmann@39159
   421
  | assert_module_name module_name = module_name;
haftmann@39159
   422
haftmann@49441
   423
fun using_master_directory thy =
haftmann@49441
   424
  Option.map (Path.append (File.pwd ()) o Path.append (Thy_Load.master_directory thy));
haftmann@49386
   425
haftmann@34021
   426
in
haftmann@34021
   427
haftmann@49583
   428
val generatedN = "Generated_Code";
haftmann@49583
   429
haftmann@39724
   430
fun export_code_for thy some_path target some_width module_name args =
haftmann@49386
   431
  export (using_master_directory thy some_path)
haftmann@49386
   432
  ooo invoke_serializer thy target some_width module_name args;
haftmann@39144
   433
haftmann@39724
   434
fun produce_code_for thy target some_width module_name args =
haftmann@39336
   435
  let
haftmann@41592
   436
    val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
haftmann@39724
   437
  in fn naming => fn program => fn names =>
haftmann@39885
   438
    produce (serializer naming program names) |> apsnd (fn deresolve => map deresolve names)
haftmann@39724
   439
  end;
haftmann@39155
   440
haftmann@39724
   441
fun present_code_for thy target some_width module_name args =
haftmann@39724
   442
  let
haftmann@41592
   443
    val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
haftmann@39724
   444
  in fn naming => fn program => fn (names, selects) =>
haftmann@39885
   445
    present selects (serializer naming program names)
haftmann@39724
   446
  end;
haftmann@39144
   447
haftmann@39144
   448
fun check_code_for thy target strict args naming program names_cs =
haftmann@37824
   449
  let
haftmann@37824
   450
    val { env_var, make_destination, make_command } =
haftmann@37824
   451
      (#check o the_fundamental thy) target;
wenzelm@42810
   452
    fun ext_check p =
wenzelm@44450
   453
      let
haftmann@37824
   454
        val destination = make_destination p;
haftmann@41592
   455
        val _ = export (SOME destination) (invoke_serializer thy target (SOME 80)
haftmann@49583
   456
          generatedN args naming program names_cs);
haftmann@49583
   457
        val cmd = make_command generatedN;
wenzelm@44721
   458
      in
wenzelm@44721
   459
        if Isabelle_System.bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
haftmann@37824
   460
        then error ("Code check failed for " ^ target ^ ": " ^ cmd)
haftmann@37824
   461
        else ()
haftmann@37824
   462
      end;
wenzelm@44721
   463
  in
wenzelm@44721
   464
    if getenv env_var = ""
haftmann@37825
   465
    then if strict
haftmann@37825
   466
      then error (env_var ^ " not set; cannot check code for " ^ target)
haftmann@37825
   467
      else warning (env_var ^ " not set; skipped checking code for " ^ target)
wenzelm@42810
   468
    else Isabelle_System.with_tmp_dir "Code_Test" ext_check
haftmann@37824
   469
  end;
haftmann@37824
   470
haftmann@41612
   471
fun evaluation mounted_serializer prepared_program consts ((vs, ty), t) =
haftmann@41592
   472
  let
haftmann@41592
   473
    val _ = if Code_Thingol.contains_dict_var t then
haftmann@41592
   474
      error "Term to be evaluated contains free dictionaries" else ();
wenzelm@44206
   475
    val v' = singleton (Name.variant_list (map fst vs)) "a";
haftmann@41592
   476
    val vs' = (v', []) :: vs;
haftmann@41592
   477
    val ty' = Code_Thingol.fun_tyco `%% [ITyVar v', ty];
haftmann@41592
   478
    val value_name = "Value.value.value"
haftmann@41592
   479
    val program = prepared_program
haftmann@41592
   480
      |> Graph.new_node (value_name,
wenzelm@53298
   481
          Code_Thingol.Fun (@{const_name dummy_pattern}, (((vs', ty'), [(([IVar NONE], t), (NONE, true))]), NONE)))
haftmann@41612
   482
      |> fold (curry (perhaps o try o Graph.add_edge) value_name) consts;
haftmann@41592
   483
    val (program_code, deresolve) = produce (mounted_serializer program);
haftmann@41592
   484
    val value_name' = the (deresolve value_name);
haftmann@41592
   485
  in (program_code, value_name') end;
haftmann@41592
   486
haftmann@41612
   487
fun evaluator thy target naming program consts =
haftmann@41592
   488
  let
haftmann@41592
   489
    val (mounted_serializer, prepared_program) = mount_serializer thy
haftmann@49583
   490
      target NONE generatedN [] naming program consts;
haftmann@41612
   491
  in evaluation mounted_serializer prepared_program consts end;
haftmann@41592
   492
haftmann@34021
   493
end; (* local *)
haftmann@34021
   494
haftmann@34021
   495
haftmann@34021
   496
(* code generation *)
haftmann@34021
   497
haftmann@56231
   498
fun implemented_functions thy naming program =
haftmann@34021
   499
  let
haftmann@56232
   500
    val cs = Code_Thingol.unimplemented program;
haftmann@34021
   501
    val names = map_filter (Code_Thingol.lookup_const naming) cs;
haftmann@34021
   502
  in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
haftmann@34021
   503
haftmann@34021
   504
fun read_const_exprs thy cs =
haftmann@34021
   505
  let
haftmann@34021
   506
    val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
haftmann@36271
   507
    val (names2, (naming, program)) = Code_Thingol.consts_program thy true cs2;
haftmann@56231
   508
    val names3 = implemented_functions thy naming program;
haftmann@36271
   509
    val cs3 = map_filter (fn (c, name) =>
haftmann@36271
   510
      if member (op =) names3 name then SOME c else NONE) (cs2 ~~ names2);
haftmann@36271
   511
  in union (op =) cs3 cs1 end;
haftmann@34021
   512
haftmann@39144
   513
fun prep_destination "" = NONE
haftmann@53572
   514
  | prep_destination "-" = (legacy_feature "drop \"file\" argument entirely instead of \"-\""; NONE)
haftmann@39144
   515
  | prep_destination s = SOME (Path.explode s);
haftmann@39144
   516
haftmann@34021
   517
fun export_code thy cs seris =
haftmann@34021
   518
  let
haftmann@37824
   519
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@39144
   520
    val _ = map (fn (((target, module_name), some_path), args) =>
haftmann@39144
   521
      export_code_for thy some_path target NONE module_name args naming program names_cs) seris;
haftmann@34021
   522
  in () end;
haftmann@34021
   523
haftmann@39144
   524
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs)
haftmann@39144
   525
  ((map o apfst o apsnd) prep_destination seris);
haftmann@39144
   526
haftmann@39155
   527
fun produce_code thy cs target some_width some_module_name args =
haftmann@39144
   528
  let
haftmann@39144
   529
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@39155
   530
  in produce_code_for thy target some_width some_module_name args naming program names_cs end;
haftmann@39155
   531
haftmann@39155
   532
fun present_code thy cs names_stmt target some_width some_module_name args =
haftmann@39155
   533
  let
haftmann@39155
   534
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@39155
   535
  in present_code_for thy target some_width some_module_name args naming program (names_cs, names_stmt naming) end;
haftmann@34021
   536
haftmann@37824
   537
fun check_code thy cs seris =
haftmann@37824
   538
  let
haftmann@37824
   539
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@39144
   540
    val _ = map (fn ((target, strict), args) =>
haftmann@39144
   541
      check_code_for thy target strict args naming program names_cs) seris;
haftmann@37824
   542
  in () end;
haftmann@37824
   543
haftmann@37824
   544
fun check_code_cmd raw_cs seris thy = check_code thy (read_const_exprs thy raw_cs) seris;
haftmann@37824
   545
haftmann@39720
   546
local
haftmann@39720
   547
haftmann@39720
   548
val parse_const_terms = Scan.repeat1 Args.term
haftmann@39720
   549
  >> (fn ts => fn thy => map (Code.check_const thy) ts);
haftmann@39720
   550
haftmann@39720
   551
fun parse_names category parse internalize lookup =
haftmann@39720
   552
  Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
haftmann@39720
   553
  >> (fn xs => fn thy => fn naming => map_filter (lookup naming o internalize thy) xs);
wenzelm@44450
   554
haftmann@39720
   555
val parse_consts = parse_names "consts" Args.term
haftmann@53514
   556
  Code.check_const Code_Thingol.lookup_const;
haftmann@39720
   557
haftmann@39720
   558
val parse_types = parse_names "types" (Scan.lift Args.name)
haftmann@39720
   559
  Sign.intern_type Code_Thingol.lookup_tyco;
haftmann@39720
   560
haftmann@39720
   561
val parse_classes = parse_names "classes" (Scan.lift Args.name)
haftmann@39720
   562
  Sign.intern_class Code_Thingol.lookup_class;
haftmann@39720
   563
haftmann@39720
   564
val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@39720
   565
  (fn thy => fn (raw_tyco, raw_class) => (Sign.intern_class thy raw_class, Sign.intern_type thy raw_tyco))
haftmann@39720
   566
    Code_Thingol.lookup_instance;
haftmann@39720
   567
haftmann@39720
   568
in
haftmann@39720
   569
wenzelm@44450
   570
val antiq_setup =
wenzelm@44450
   571
  Thy_Output.antiquotation @{binding code_stmts}
wenzelm@44450
   572
    (parse_const_terms --
wenzelm@44450
   573
      Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances)
wenzelm@44450
   574
      -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
wenzelm@44450
   575
    (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target, some_width)) =>
wenzelm@44450
   576
      let val thy = Proof_Context.theory_of ctxt in
wenzelm@44450
   577
        present_code thy (mk_cs thy)
wenzelm@44450
   578
          (fn naming => maps (fn f => f thy naming) mk_stmtss)
wenzelm@44450
   579
          target some_width "Example" []
wenzelm@44450
   580
      end);
haftmann@39720
   581
haftmann@39720
   582
end;
haftmann@39720
   583
haftmann@34021
   584
haftmann@28054
   585
(** serializer configuration **)
haftmann@28054
   586
haftmann@53275
   587
(* reserved symbol names *)
haftmann@53275
   588
haftmann@53275
   589
fun add_reserved target sym thy =
haftmann@53275
   590
  let
haftmann@53275
   591
    val (_, data) = collapse_hierarchy thy target;
haftmann@53275
   592
    val _ = if member (op =) (the_reserved data) sym
haftmann@53275
   593
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@53275
   594
      else ();
haftmann@53275
   595
  in
haftmann@53275
   596
    thy
haftmann@53275
   597
    |> map_reserved target (insert (op =) sym)
haftmann@53275
   598
  end;
haftmann@53275
   599
haftmann@53275
   600
haftmann@53514
   601
(* checking of syntax *)
haftmann@53275
   602
haftmann@53274
   603
fun check_const_syntax thy c syn =
haftmann@53274
   604
  if Code_Printer.requires_args syn > Code.args_number thy c
haftmann@53274
   605
  then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@53274
   606
  else syn;
haftmann@53274
   607
haftmann@53274
   608
fun check_tyco_syntax thy tyco syn =
haftmann@53274
   609
  if fst syn <> Sign.arity_number thy tyco
haftmann@53274
   610
  then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@53274
   611
  else syn;
haftmann@53274
   612
haftmann@53275
   613
haftmann@53275
   614
(* custom symbol names *)
haftmann@53275
   615
wenzelm@53355
   616
fun arrange_name_decls x =
haftmann@53275
   617
  let
haftmann@53275
   618
    fun arrange is_module (sym, target_names) = map (fn (target, some_name) =>
haftmann@53275
   619
      (target, (sym, Option.map (check_name is_module) some_name))) target_names;
haftmann@53275
   620
  in
haftmann@53275
   621
    Code_Symbol.maps_attr' (arrange false) (arrange false) (arrange false)
wenzelm@53355
   622
      (arrange false) (arrange false) (arrange true) x
haftmann@53275
   623
  end;
haftmann@53275
   624
haftmann@53275
   625
fun cert_name_decls thy = cert_syms thy #> arrange_name_decls;
haftmann@53275
   626
haftmann@53275
   627
fun read_name_decls thy = read_syms thy #> arrange_name_decls;
haftmann@53275
   628
haftmann@53275
   629
fun set_identifier (target, sym_name) = map_identifiers target (Code_Symbol.set_data sym_name);
haftmann@53275
   630
haftmann@53275
   631
fun gen_set_identifiers prep_name_decl raw_name_decls thy =
haftmann@53275
   632
  fold set_identifier (prep_name_decl thy raw_name_decls) thy;
haftmann@53275
   633
haftmann@53275
   634
val set_identifiers = gen_set_identifiers cert_name_decls;
haftmann@53275
   635
val set_identifiers_cmd = gen_set_identifiers read_name_decls;
haftmann@53275
   636
wenzelm@55200
   637
fun add_module_alias_cmd target aliasses thy =
haftmann@53572
   638
  let
haftmann@53572
   639
    val _ = legacy_feature "prefer \"code_identifier\" over \"code_modulename\"";
haftmann@53572
   640
  in
haftmann@53572
   641
    fold (fn (sym, name) => set_identifier
wenzelm@55200
   642
      (target, Code_Symbol.Module (sym, if name = "" then NONE else SOME (check_name true name))))
wenzelm@55200
   643
      aliasses thy
haftmann@53572
   644
  end;
haftmann@53275
   645
haftmann@53275
   646
haftmann@53275
   647
(* custom printings *)
haftmann@53275
   648
haftmann@53274
   649
fun arrange_printings prep_const thy =
haftmann@24219
   650
  let
haftmann@53274
   651
    fun arrange check (sym, target_syns) =
haftmann@53274
   652
      map (fn (target, some_syn) => (target, (sym, Option.map (check thy sym) some_syn))) target_syns;
haftmann@53274
   653
  in
haftmann@53274
   654
    Code_Symbol.maps_attr'
haftmann@53274
   655
      (arrange check_const_syntax) (arrange check_tyco_syntax)
haftmann@53274
   656
        (arrange ((K o K) I)) (arrange ((K o K) I)) (arrange ((K o K) I))
haftmann@53274
   657
        (arrange (fn thy => fn _ => fn (raw_content, raw_cs) =>
haftmann@53274
   658
          (Code_Printer.str raw_content, map (prep_const thy) raw_cs)))
haftmann@53274
   659
  end;
haftmann@24219
   660
haftmann@53274
   661
fun cert_printings thy = cert_syms thy #> arrange_printings cert_const thy;
haftmann@24219
   662
haftmann@53274
   663
fun read_printings thy = read_syms thy #> arrange_printings Code.read_const thy;
haftmann@34071
   664
haftmann@53274
   665
fun set_printing (target, sym_syn) = map_printings target (Code_Symbol.set_data sym_syn);
haftmann@53274
   666
haftmann@53274
   667
fun gen_set_printings prep_print_decl raw_print_decls thy =
haftmann@53274
   668
  fold set_printing (prep_print_decl thy raw_print_decls) thy;
haftmann@53274
   669
haftmann@53274
   670
val set_printings = gen_set_printings cert_printings;
haftmann@53274
   671
val set_printings_cmd = gen_set_printings read_printings;
haftmann@53274
   672
haftmann@53274
   673
fun gen_add_syntax Symbol prep_x prep_syn target raw_x some_raw_syn thy =
haftmann@24992
   674
  let
haftmann@53572
   675
    val _ = legacy_feature "prefer \"code_printing\" for custom serialisations"
haftmann@53274
   676
    val x = prep_x thy raw_x;
haftmann@53274
   677
  in set_printing (target, Symbol (x, Option.map (prep_syn thy x) some_raw_syn)) thy end;
haftmann@28926
   678
haftmann@53274
   679
fun gen_add_const_syntax prep_const =
haftmann@53274
   680
  gen_add_syntax Code_Symbol.Constant prep_const check_const_syntax;
haftmann@24219
   681
haftmann@53274
   682
fun gen_add_tyco_syntax prep_tyco =
haftmann@53274
   683
  gen_add_syntax Code_Symbol.Type_Constructor prep_tyco check_tyco_syntax;
haftmann@53274
   684
haftmann@53274
   685
fun gen_add_class_syntax prep_class =
haftmann@53274
   686
  gen_add_syntax Code_Symbol.Type_Class prep_class ((K o K) I);
haftmann@53274
   687
haftmann@53274
   688
fun gen_add_instance_syntax prep_inst =
haftmann@53274
   689
  gen_add_syntax Code_Symbol.Class_Instance prep_inst ((K o K) I);
haftmann@53274
   690
haftmann@53274
   691
fun gen_add_include prep_const target (name, some_content) thy =
haftmann@53274
   692
  gen_add_syntax Code_Symbol.Module (K I)
haftmann@53274
   693
    (fn thy => fn _ => fn (raw_content, raw_cs) => (Code_Printer.str raw_content, map (prep_const thy) raw_cs))
haftmann@53274
   694
    target name some_content thy;
haftmann@53274
   695
haftmann@53274
   696
haftmann@27103
   697
(* concrete syntax *)
haftmann@27000
   698
haftmann@34021
   699
local
haftmann@34021
   700
haftmann@48447
   701
fun zip_list (x :: xs) f g =
haftmann@34141
   702
  f
haftmann@37854
   703
  :|-- (fn y =>
haftmann@34141
   704
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@37854
   705
    :|-- (fn xys => pair ((x, y) :: xys)));
haftmann@34141
   706
haftmann@37854
   707
fun process_multi_syntax parse_thing parse_syntax change =
haftmann@37854
   708
  (Parse.and_list1 parse_thing
wenzelm@47823
   709
  :|-- (fn things => Scan.repeat1 (@{keyword "("} |-- Parse.name --
haftmann@53205
   710
        (zip_list things (Scan.option parse_syntax) @{keyword "and"}) --| @{keyword ")"})))
haftmann@37854
   711
  >> (Toplevel.theory oo fold)
haftmann@37854
   712
    (fn (target, syns) => fold (fn (raw_x, syn) => change target raw_x syn) syns);
haftmann@24219
   713
haftmann@24219
   714
in
haftmann@24219
   715
haftmann@53274
   716
val add_reserved = add_reserved;
haftmann@53274
   717
val add_const_syntax = gen_add_const_syntax (K I);
haftmann@53274
   718
val add_tyco_syntax = gen_add_tyco_syntax cert_tyco;
haftmann@39149
   719
val add_class_syntax = gen_add_class_syntax cert_class;
haftmann@39149
   720
val add_instance_syntax = gen_add_instance_syntax cert_inst;
haftmann@53274
   721
val add_include = gen_add_include (K I);
haftmann@24219
   722
haftmann@53274
   723
val add_const_syntax_cmd = gen_add_const_syntax Code.read_const;
haftmann@53274
   724
val add_tyco_syntax_cmd = gen_add_tyco_syntax read_tyco;
haftmann@39149
   725
val add_class_syntax_cmd = gen_add_class_syntax read_class;
haftmann@39149
   726
val add_instance_syntax_cmd = gen_add_instance_syntax read_inst;
haftmann@53274
   727
val add_include_cmd = gen_add_include Code.read_const;
haftmann@24219
   728
haftmann@28054
   729
fun parse_args f args =
wenzelm@36969
   730
  case Scan.read Token.stopper f args
haftmann@28054
   731
   of SOME x => x
haftmann@28054
   732
    | NONE => error "Bad serializer arguments";
haftmann@28054
   733
haftmann@28054
   734
haftmann@27304
   735
(** Isar setup **)
haftmann@27103
   736
haftmann@53274
   737
fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
haftmann@53571
   738
  parse_keyword |-- Parse.!!! (parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"})
haftmann@53571
   739
    -- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target)));
haftmann@53274
   740
haftmann@53274
   741
fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
wenzelm@53938
   742
  parse_single_symbol_pragma @{keyword "constant"} Parse.term parse_const
haftmann@53274
   743
    >> Code_Symbol.Constant
haftmann@53514
   744
  || parse_single_symbol_pragma @{keyword "type_constructor"} Parse.type_const parse_tyco
haftmann@53274
   745
    >> Code_Symbol.Type_Constructor
haftmann@53514
   746
  || parse_single_symbol_pragma @{keyword "type_class"} Parse.class parse_class
haftmann@53274
   747
    >> Code_Symbol.Type_Class
haftmann@53274
   748
  || parse_single_symbol_pragma @{keyword "class_relation"} parse_classrel_ident parse_classrel
haftmann@53274
   749
    >> Code_Symbol.Class_Relation
haftmann@53274
   750
  || parse_single_symbol_pragma @{keyword "class_instance"} parse_inst_ident parse_inst
haftmann@53274
   751
    >> Code_Symbol.Class_Instance
haftmann@53274
   752
  || parse_single_symbol_pragma @{keyword "code_module"} Parse.name parse_module
haftmann@53274
   753
    >> Code_Symbol.Module;
haftmann@53274
   754
haftmann@53274
   755
fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@53274
   756
  Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
haftmann@53274
   757
    (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
haftmann@53274
   758
haftmann@53275
   759
val code_expr_argsP = Scan.optional (@{keyword "("} |-- Args.parse --| @{keyword ")"}) [];
haftmann@53275
   760
haftmann@53571
   761
fun code_expr_inP raw_cs =
haftmann@53571
   762
  Scan.repeat (@{keyword "in"} |-- Parse.!!! (Parse.name
haftmann@53571
   763
    -- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
haftmann@53571
   764
    -- Scan.optional (@{keyword "file"} |-- Parse.name) ""
haftmann@53571
   765
    -- code_expr_argsP))
haftmann@53571
   766
      >> (fn seri_args => export_code_cmd raw_cs seri_args);
haftmann@53571
   767
haftmann@53571
   768
fun code_expr_checkingP raw_cs =
haftmann@53571
   769
  (@{keyword "checking"} |-- Parse.!!!
haftmann@53571
   770
    (Scan.repeat (Parse.name -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true)
haftmann@53571
   771
    -- code_expr_argsP)))
haftmann@53571
   772
      >> (fn seri_args => check_code_cmd raw_cs seri_args);
haftmann@53571
   773
wenzelm@53938
   774
val code_exprP = Scan.repeat1 Parse.term
haftmann@53571
   775
  :|-- (fn raw_cs => (code_expr_checkingP raw_cs || code_expr_inP raw_cs));
haftmann@53275
   776
haftmann@53275
   777
val _ =
haftmann@53275
   778
  Outer_Syntax.command @{command_spec "code_reserved"}
haftmann@53275
   779
    "declare words as reserved for target language"
haftmann@53275
   780
    (Parse.name -- Scan.repeat1 Parse.name
haftmann@53275
   781
      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
haftmann@53275
   782
haftmann@53275
   783
val _ =
haftmann@53275
   784
  Outer_Syntax.command @{command_spec "code_identifier"} "declare mandatory names for code symbols"
haftmann@53275
   785
    (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
haftmann@53275
   786
      >> (Toplevel.theory o fold set_identifiers_cmd));
haftmann@53275
   787
haftmann@53275
   788
val _ =
haftmann@53275
   789
  Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
haftmann@53275
   790
    (Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
haftmann@53275
   791
      >> (fn (target, modlnames) => (Toplevel.theory o add_module_alias_cmd target) modlnames));
haftmann@53275
   792
haftmann@53274
   793
val _ =
haftmann@53274
   794
  Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
haftmann@53274
   795
    (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
haftmann@53274
   796
      Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
haftmann@53274
   797
      (Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
haftmann@53274
   798
      >> (Toplevel.theory o fold set_printings_cmd));
haftmann@53274
   799
haftmann@53274
   800
val _ =
haftmann@53274
   801
  Outer_Syntax.command @{command_spec "code_const"} "define code syntax for constant"
wenzelm@53938
   802
    (process_multi_syntax Parse.term Code_Printer.parse_const_syntax
haftmann@53274
   803
      add_const_syntax_cmd);
haftmann@53274
   804
haftmann@53274
   805
val _ =
haftmann@53274
   806
  Outer_Syntax.command @{command_spec "code_type"} "define code syntax for type constructor"
haftmann@53514
   807
    (process_multi_syntax Parse.type_const Code_Printer.parse_tyco_syntax
haftmann@53274
   808
      add_tyco_syntax_cmd);
haftmann@53274
   809
wenzelm@24867
   810
val _ =
wenzelm@47836
   811
  Outer_Syntax.command @{command_spec "code_class"} "define code syntax for class"
haftmann@53514
   812
    (process_multi_syntax Parse.class Parse.string
wenzelm@47836
   813
      add_class_syntax_cmd);
haftmann@24219
   814
wenzelm@24867
   815
val _ =
wenzelm@47836
   816
  Outer_Syntax.command @{command_spec "code_instance"} "define code syntax for instance"
haftmann@53514
   817
    (process_multi_syntax parse_inst_ident (Parse.minus >> K ())
wenzelm@47836
   818
      add_instance_syntax_cmd);
haftmann@24219
   819
wenzelm@24867
   820
val _ =
wenzelm@47836
   821
  Outer_Syntax.command @{command_spec "code_include"}
wenzelm@47836
   822
    "declare piece of code to be included in generated code"
wenzelm@47836
   823
    (Parse.name -- Parse.name -- (Parse.text :|--
wenzelm@47836
   824
      (fn "-" => Scan.succeed NONE
wenzelm@47836
   825
        | s => Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [] >> pair s >> SOME))
wenzelm@47836
   826
      >> (fn ((target, name), content_consts) =>
wenzelm@47836
   827
          (Toplevel.theory o add_include_cmd target) (name, content_consts)));
haftmann@24992
   828
haftmann@24992
   829
val _ =
wenzelm@47836
   830
  Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
wenzelm@47836
   831
    (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@30492
   832
haftmann@24219
   833
end; (*local*)
haftmann@24219
   834
haftmann@39870
   835
haftmann@39870
   836
(** external entrance point -- for codegen tool **)
haftmann@39870
   837
haftmann@39983
   838
fun codegen_tool thyname cmd_expr =
haftmann@39870
   839
  let
haftmann@39870
   840
    val thy = Thy_Info.get_theory thyname;
haftmann@39870
   841
    val parse = Scan.read Token.stopper (Parse.!!! code_exprP) o
haftmann@39870
   842
      (filter Token.is_proper o Outer_Syntax.scan Position.none);
haftmann@39870
   843
  in case parse cmd_expr
haftmann@39870
   844
   of SOME f => (writeln "Now generating code..."; f thy)
haftmann@39870
   845
    | NONE => error ("Bad directive " ^ quote cmd_expr)
haftmann@39870
   846
  end;
haftmann@39870
   847
wenzelm@44450
   848
wenzelm@44450
   849
(** theory setup **)
wenzelm@44450
   850
wenzelm@44450
   851
val setup = antiq_setup;
wenzelm@44450
   852
haftmann@24219
   853
end; (*struct*)