src/Pure/codegen.ML
author wenzelm
Wed, 28 Nov 2001 00:46:26 +0100
changeset 12311 ce5f9e61c037
parent 12123 739eba13e2cd
child 12452 68493b92e7a6
permissions -rw-r--r--
theory data: removed obsolete finish method;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/codegen.ML
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
11539
0f17da240450 tuned headers;
wenzelm
parents: 11520
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
0f17da240450 tuned headers;
wenzelm
parents: 11520
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
     5
11539
0f17da240450 tuned headers;
wenzelm
parents: 11520
diff changeset
     6
Generic code generator.
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
     7
*)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
     8
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
     9
signature CODEGEN =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    10
sig
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    11
  val quiet_mode : bool ref
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    12
  val message : string -> unit
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    13
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    14
  datatype mixfix =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    15
      Arg
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    16
    | Ignore
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    17
    | Pretty of Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    18
    | Term of term;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    19
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    20
  val add_codegen: string ->
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    21
    (theory -> (exn option * string) Graph.T -> string -> bool -> term ->
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    22
    ((exn option * string) Graph.T * Pretty.T) option) -> theory -> theory
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    23
  val print_codegens: theory -> unit
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    24
  val generate_code: theory -> (string * string) list -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    25
  val generate_code_i: theory -> (string * term) list -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    26
  val assoc_consts: (xstring * string option * mixfix list) list -> theory -> theory
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    27
  val assoc_consts_i: (xstring * typ option * mixfix list) list -> theory -> theory
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    28
  val assoc_types: (xstring * string) list -> theory -> theory
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    29
  val get_assoc_code: theory -> string -> typ -> mixfix list option
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    30
  val get_assoc_types: theory -> (string * string) list
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    31
  val invoke_codegen: theory -> (exn option * string) Graph.T ->
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    32
    string -> bool -> term -> (exn option * string) Graph.T * Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    33
  val mk_const_id: Sign.sg -> string -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    34
  val mk_type_id: Sign.sg -> string -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    35
  val rename_term: term -> term
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    36
  val get_defn: theory -> string -> typ -> ((term list * term) * int option) option
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    37
  val is_instance: theory -> typ -> typ -> bool
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    38
  val parens: Pretty.T -> Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    39
  val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    40
  val eta_expand: term -> term list -> int -> term
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    41
  val parsers: OuterSyntax.parser list
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    42
  val setup: (theory -> theory) list
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    43
end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    44
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    45
structure Codegen : CODEGEN =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    46
struct
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    47
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    48
val quiet_mode = ref true;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    49
fun message s = if !quiet_mode then () else writeln s;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    50
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    51
(**** Mixfix syntax ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    52
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    53
datatype mixfix =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    54
    Arg
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    55
  | Ignore
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    56
  | Pretty of Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    57
  | Term of term;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    58
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    59
fun is_arg Arg = true
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    60
  | is_arg Ignore = true
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    61
  | is_arg _ = false;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    62
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    63
fun terms_of [] = []
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    64
  | terms_of (Term t :: ms) = t :: terms_of ms
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    65
  | terms_of (_ :: ms) = terms_of ms;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    66
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    67
val num_args = length o filter is_arg;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    68
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    69
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    70
(**** theory data ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    71
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    72
(* data kind 'Pure/codegen' *)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    73
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    74
structure CodegenArgs =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    75
struct
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    76
  val name = "Pure/codegen";
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    77
  type T =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    78
    {codegens : (string * (theory -> (exn option * string) Graph.T -> string ->
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    79
       bool -> term -> ((exn option * string) Graph.T * Pretty.T) option)) list,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    80
     consts : ((string * typ) * mixfix list) list,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    81
     types : (string * string) list};
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    82
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    83
  val empty = {codegens = [], consts = [], types = []};
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    84
  val copy = I;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    85
  val prep_ext = I;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    86
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    87
  fun merge ({codegens = codegens1, consts = consts1, types = types1},
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    88
             {codegens = codegens2, consts = consts2, types = types2}) =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    89
    {codegens = rev (merge_alists (rev codegens1) (rev codegens2)),
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    90
     consts   = merge_alists consts1 consts2,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    91
     types    = merge_alists types1 types2};
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    92
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    93
  fun print sg (cs:T) = Pretty.writeln
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    94
    (Pretty.strs ("code generators:" :: map fst (#codegens cs)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    95
end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    96
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    97
structure CodegenData = TheoryDataFun(CodegenArgs);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    98
val print_codegens = CodegenData.print;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    99
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   100
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   101
(**** add new code generator to theory ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   102
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   103
fun add_codegen name f thy =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   104
  let val {codegens, consts, types} = CodegenData.get thy
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   105
  in (case assoc (codegens, name) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   106
      None => CodegenData.put {codegens = (name, f)::codegens,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   107
        consts = consts, types = types} thy
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   108
    | Some _ => error ("Code generator " ^ name ^ " already declared"))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   109
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   110
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   111
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   112
(**** associate constants with target language code ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   113
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   114
fun gen_assoc_consts prep_type xs thy = foldl (fn (thy, (s, tyopt, syn)) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   115
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   116
    val sg = sign_of thy;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   117
    val {codegens, consts, types} = CodegenData.get thy;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   118
    val cname = Sign.intern_const sg s;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   119
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   120
    (case Sign.const_type sg cname of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   121
       Some T =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   122
         let val T' = (case tyopt of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   123
                None => T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   124
              | Some ty =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   125
                  let val U = prep_type sg ty
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   126
                  in if Type.typ_instance (Sign.tsig_of sg, U, T) then U
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   127
                    else error ("Illegal type constraint for constant " ^ cname)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   128
                  end)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   129
         in (case assoc (consts, (cname, T')) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   130
             None => CodegenData.put {codegens = codegens,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   131
               consts = ((cname, T'), syn) :: consts, types = types} thy
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   132
           | Some _ => error ("Constant " ^ cname ^ " already associated with code"))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   133
         end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   134
     | _ => error ("Not a constant: " ^ s))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   135
  end) (thy, xs);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   136
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   137
val assoc_consts_i = gen_assoc_consts (K I);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   138
val assoc_consts = gen_assoc_consts (fn sg => typ_of o read_ctyp sg);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   139
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   140
(**** associate types with target language types ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   141
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   142
fun assoc_types xs thy = foldl (fn (thy, (s, syn)) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   143
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   144
    val {codegens, consts, types} = CodegenData.get thy;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   145
    val tc = Sign.intern_tycon (sign_of thy) s
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   146
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   147
    (case assoc (types, tc) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   148
       None => CodegenData.put {codegens = codegens, consts = consts,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   149
         types = (tc, syn) :: types} thy
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   150
     | Some _ => error ("Type " ^ tc ^ " already associated with code"))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   151
  end) (thy, xs);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   152
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   153
fun get_assoc_types thy = #types (CodegenData.get thy);
11546
wenzelm
parents: 11539
diff changeset
   154
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   155
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   156
(**** retrieve definition of constant ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   157
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   158
fun is_instance thy T1 T2 =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   159
  Type.typ_instance (Sign.tsig_of (sign_of thy), T1, Type.varifyT T2);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   160
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   161
fun get_assoc_code thy s T = apsome snd (find_first (fn ((s', T'), _) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   162
  s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   163
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   164
fun get_defn thy s T =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   165
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   166
    val axms = flat (map (Symtab.dest o #axioms o Theory.rep_theory)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   167
      (thy :: Theory.ancestors_of thy));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   168
    val defs = mapfilter (fn (_, t) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   169
      (let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   170
         val (lhs, rhs) = Logic.dest_equals t;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   171
         val (c, args) = strip_comb lhs;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   172
         val (s', T') = dest_Const c
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   173
       in if s=s' then Some (T', (args, rhs)) else None end) handle TERM _ =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   174
         None) axms;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   175
    val i = find_index (is_instance thy T o fst) defs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   176
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   177
    if i>=0 then Some (snd (nth_elem (i, defs)),
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   178
      if length defs = 1 then None else Some i)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   179
    else None
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   180
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   181
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   182
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   183
(**** make valid ML identifiers ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   184
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   185
fun gen_mk_id kind rename sg s =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   186
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   187
    val (xs as x::_) = explode (rename (space_implode "_"
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   188
      (NameSpace.unpack (Sign.cond_extern sg kind s))));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   189
    fun check_str [] = ""
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   190
      | check_str (x::xs) =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   191
          (if Symbol.is_letter x orelse Symbol.is_digit x orelse x="_" then x
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   192
           else "_" ^ string_of_int (ord x)) ^ check_str xs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   193
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   194
    (if not (Symbol.is_letter x) then "id" else "") ^ check_str xs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   195
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   196
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   197
val mk_const_id = gen_mk_id Sign.constK I;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   198
val mk_type_id = gen_mk_id Sign.typeK
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   199
  (fn s => if s mem ThmDatabase.ml_reserved then s ^ "_type" else s);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   200
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   201
fun rename_term t =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   202
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   203
    val names = add_term_names (t, map (fst o fst o dest_Var) (term_vars t));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   204
    val clash = names inter ThmDatabase.ml_reserved;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   205
    val ps = clash ~~ variantlist (clash, names);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   206
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   207
    fun rename (Var ((a, i), T)) = Var ((if_none (assoc (ps, a)) a, i), T)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   208
      | rename (Free (a, T)) = Free (if_none (assoc (ps, a)) a, T)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   209
      | rename (Abs (s, T, t)) = Abs (s, T, rename t)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   210
      | rename (t $ u) = rename t $ rename u
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   211
      | rename t = t;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   212
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   213
    rename t
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   214
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   215
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   216
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   217
(**** invoke suitable code generator for term t ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   218
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   219
fun invoke_codegen thy gr dep brack t = (case get_first
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   220
   (fn (_, f) => f thy gr dep brack t) (#codegens (CodegenData.get thy)) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   221
      None => error ("Unable to generate code for term:\n" ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   222
        Sign.string_of_term (sign_of thy) t ^ "\nrequired by:\n" ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   223
        commas (Graph.all_succs gr [dep]))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   224
    | Some x => x)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   225
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   226
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   227
(**** code generator for mixfix expressions ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   228
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   229
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"];
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   230
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   231
fun pretty_fn [] p = [p]
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   232
  | pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") ::
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   233
      Pretty.brk 1 :: pretty_fn xs p;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   234
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   235
fun pretty_mixfix [] [] _ = []
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   236
  | pretty_mixfix (Arg :: ms) (p :: ps) qs = p :: pretty_mixfix ms ps qs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   237
  | pretty_mixfix (Ignore :: ms) (p :: ps) qs = pretty_mixfix ms ps qs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   238
  | pretty_mixfix (Pretty p :: ms) ps qs = p :: pretty_mixfix ms ps qs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   239
  | pretty_mixfix (Term _ :: ms) ps (q :: qs) = q :: pretty_mixfix ms ps qs;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   240
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   241
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   242
(**** default code generator ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   243
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   244
fun eta_expand t ts i =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   245
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   246
    val (Ts, _) = strip_type (fastype_of t);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   247
    val j = i - length ts
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   248
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   249
    foldr (fn (T, t) => Abs ("x", T, t))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   250
      (take (j, Ts), list_comb (t, ts @ map Bound (j-1 downto 0)))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   251
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   252
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   253
fun mk_app _ p [] = p
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   254
  | mk_app brack p ps = if brack then
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   255
       Pretty.block (Pretty.str "(" ::
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   256
         separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"])
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   257
     else Pretty.block (separate (Pretty.brk 1) (p :: ps));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   258
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   259
fun new_names t xs = variantlist (xs,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   260
  map (fst o fst o dest_Var) (term_vars t) union
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   261
  add_term_names (t, ThmDatabase.ml_reserved));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   262
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   263
fun new_name t x = hd (new_names t [x]);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   264
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   265
fun default_codegen thy gr dep brack t =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   266
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   267
    val (u, ts) = strip_comb t;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   268
    fun mapcode brack' gr ts = foldl_map
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   269
      (fn (gr, t) => invoke_codegen thy gr dep brack' t) (gr, ts)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   270
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   271
  in (case u of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   272
      Var ((s, i), _) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   273
        let val (gr', ps) = mapcode true gr ts
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   274
        in Some (gr', mk_app brack (Pretty.str (s ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   275
           (if i=0 then "" else string_of_int i))) ps)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   276
        end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   277
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   278
    | Free (s, _) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   279
        let val (gr', ps) = mapcode true gr ts
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   280
        in Some (gr', mk_app brack (Pretty.str s) ps) end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   281
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   282
    | Const (s, T) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   283
      (case get_assoc_code thy s T of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   284
         Some ms =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   285
           let val i = num_args ms
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   286
           in if length ts < i then
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   287
               default_codegen thy gr dep brack (eta_expand u ts i)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   288
             else
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   289
               let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   290
                 val ts1 = take (i, ts);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   291
                 val ts2 = drop (i, ts);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   292
                 val (gr1, ps1) = mapcode false gr ts1;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   293
                 val (gr2, ps2) = mapcode true gr1 ts2;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   294
                 val (gr3, ps3) = mapcode false gr2 (terms_of ms);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   295
               in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   296
                 Some (gr3, mk_app brack (Pretty.block (pretty_mixfix ms ps1 ps3)) ps2)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   297
               end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   298
           end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   299
       | None => (case get_defn thy s T of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   300
           None => None
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   301
         | Some ((args, rhs), k) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   302
             let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   303
               val id = mk_const_id (sign_of thy) s ^ (case k of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   304
                 None => "" | Some i => "_def" ^ string_of_int i);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   305
               val (gr', ps) = mapcode true gr ts;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   306
             in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   307
               Some (Graph.add_edge (id, dep) gr' handle Graph.UNDEF _ =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   308
                 let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   309
                   val _ = message ("expanding definition of " ^ s);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   310
                   val (Ts, _) = strip_type T;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   311
                   val (args', rhs') =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   312
                     if not (null args) orelse null Ts then (args, rhs) else
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   313
                       let val v = Free (new_name rhs "x", hd Ts)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   314
                       in ([v], betapply (rhs, v)) end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   315
                   val (gr1, p) = invoke_codegen thy (Graph.add_edge (id, dep)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   316
                     (Graph.new_node (id, (None, "")) gr')) id false rhs';
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   317
                   val (gr2, xs) = mapcode false gr1 args';
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   318
                 in Graph.map_node id (K (None, Pretty.string_of (Pretty.block
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   319
                   (Pretty.str (if null args' then "val " else "fun ") ::
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   320
                    separate (Pretty.brk 1) (Pretty.str id :: xs) @
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   321
                    [Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr2
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   322
                 end, mk_app brack (Pretty.str id) ps)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   323
             end))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   324
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   325
    | Abs _ =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   326
      let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   327
        val (bs, Ts) = ListPair.unzip (strip_abs_vars u);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   328
        val t = strip_abs_body u
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   329
        val bs' = new_names t bs;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   330
        val (gr1, ps) = mapcode true gr ts;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   331
        val (gr2, p) = invoke_codegen thy gr1 dep false
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   332
          (subst_bounds (map Free (rev (bs' ~~ Ts)), t));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   333
      in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   334
        Some (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   335
          [Pretty.str ")"])) ps)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   336
      end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   337
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   338
    | _ => None)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   339
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   340
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   341
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   342
fun output_code gr xs = implode (map (snd o Graph.get_node gr)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   343
  (rev (Graph.all_preds gr xs)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   344
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   345
fun gen_generate_code prep_term thy = Pretty.setmp_margin 80 (fn xs =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   346
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   347
    val sg = sign_of thy;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   348
    val gr = Graph.new_node ("<Top>", (None, "")) Graph.empty;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   349
    val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   350
      (invoke_codegen thy gr "<Top>" false t)) (gr, map (apsnd (prep_term sg)) xs)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   351
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   352
    "structure Generated =\nstruct\n\n" ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   353
    output_code gr' ["<Top>"] ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   354
    space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   355
      [Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   356
    "\n\nend;\n\nopen Generated;\n"
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   357
  end);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   358
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   359
val generate_code_i = gen_generate_code (K I);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   360
val generate_code = gen_generate_code
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   361
  (fn sg => term_of o read_cterm sg o rpair TypeInfer.logicT);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   362
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   363
fun parse_mixfix rd s =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   364
  (case Scan.finite Symbol.stopper (Scan.repeat
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   365
     (   $$ "_" >> K Arg
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   366
      || $$ "?" >> K Ignore
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   367
      || $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   368
      || $$ "{" |-- $$ "*" |-- Scan.repeat1
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   369
           (   $$ "'" |-- Scan.one Symbol.not_eof
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   370
            || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   371
         $$ "*" --| $$ "}" >> (Term o rd o implode)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   372
      || Scan.repeat1
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   373
           (   $$ "'" |-- Scan.one Symbol.not_eof
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   374
            || Scan.unless ($$ "_" || $$ "?" || $$ "/" || $$ "{" |-- $$ "*")
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   375
                 (Scan.one Symbol.not_eof)) >> (Pretty o Pretty.str o implode)))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   376
       (Symbol.explode s) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   377
     (p, []) => p
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   378
   | _ => error ("Malformed annotation: " ^ quote s));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   379
11546
wenzelm
parents: 11539
diff changeset
   380
structure P = OuterParse and K = OuterSyntax.Keyword;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   381
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   382
val assoc_typeP =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   383
  OuterSyntax.command "types_code"
11546
wenzelm
parents: 11539
diff changeset
   384
  "associate types with target language types" K.thy_decl
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   385
    (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")") >>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   386
     (Toplevel.theory o assoc_types));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   387
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   388
val assoc_constP =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   389
  OuterSyntax.command "consts_code"
11546
wenzelm
parents: 11539
diff changeset
   390
  "associate constants with target language code" K.thy_decl
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   391
    (Scan.repeat1
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   392
       (P.xname -- (Scan.option (P.$$$ "::" |-- P.string)) --|
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   393
        P.$$$ "(" -- P.string --| P.$$$ ")") >>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   394
     (fn xs => Toplevel.theory (fn thy => assoc_consts
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   395
       (map (fn ((name, optype), mfx) => (name, optype, parse_mixfix
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   396
         (term_of o read_cterm (sign_of thy) o rpair TypeInfer.logicT) mfx))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   397
           xs) thy)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   398
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   399
val generate_codeP =
11546
wenzelm
parents: 11539
diff changeset
   400
  OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   401
    (Scan.option (P.$$$ "(" |-- P.string --| P.$$$ ")") --
11546
wenzelm
parents: 11539
diff changeset
   402
     Scan.repeat1 (P.name --| P.$$$ "=" -- P.string) >>
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   403
     (fn (opt_fname, xs) => Toplevel.theory (fn thy =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   404
        ((case opt_fname of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   405
            None => use_text Context.ml_output false
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   406
          | Some fname => File.write (Path.unpack fname))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   407
              (generate_code thy xs); thy))));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   408
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   409
val parsers = [assoc_typeP, assoc_constP, generate_codeP];
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   410
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   411
val setup = [CodegenData.init, add_codegen "default" default_codegen];
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   412
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   413
end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   414
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   415
OuterSyntax.add_parsers Codegen.parsers;