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