src/Pure/codegen.ML
author berghofe
Wed, 27 Nov 2002 17:16:47 +0100
changeset 13728 8004e56204fd
parent 13073 cc9d7f403a4b
child 13731 e2d17090052b
permissions -rw-r--r--
Added some functions for processing PGIP (thanks to David Aspinall).
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
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    14
  datatype 'a mixfix =
11520
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
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    18
    | Quote of 'a;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    19
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    20
  type 'a codegen
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    21
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    22
  val add_codegen: string -> term codegen -> theory -> theory
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    23
  val add_tycodegen: string -> typ codegen -> theory -> theory
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
    24
  val add_attribute: string -> theory attribute -> theory -> theory
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    25
  val print_codegens: theory -> unit
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    26
  val generate_code: theory -> (string * string) list -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    27
  val generate_code_i: theory -> (string * term) list -> string
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    28
  val assoc_consts: (xstring * string option * term mixfix list) list -> theory -> theory
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    29
  val assoc_consts_i: (xstring * typ option * term mixfix list) list -> theory -> theory
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    30
  val assoc_types: (xstring * typ mixfix list) list -> theory -> theory
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    31
  val get_assoc_code: theory -> string -> typ -> term mixfix list option
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    32
  val get_assoc_type: theory -> string -> typ mixfix list option
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    33
  val invoke_codegen: theory -> string -> bool ->
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    34
    (exn option * string) Graph.T * term -> (exn option * string) Graph.T * Pretty.T
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    35
  val invoke_tycodegen: theory -> string -> bool ->
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    36
    (exn option * string) Graph.T * typ -> (exn option * string) Graph.T * Pretty.T
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    37
  val mk_const_id: Sign.sg -> string -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    38
  val mk_type_id: Sign.sg -> string -> string
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    39
  val rename_term: term -> term
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    40
  val get_defn: theory -> string -> typ -> ((term list * term) * int option) option
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    41
  val is_instance: theory -> typ -> typ -> bool
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    42
  val parens: Pretty.T -> Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    43
  val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    44
  val eta_expand: term -> term list -> int -> term
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    45
  val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    46
  val parsers: OuterSyntax.parser list
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    47
  val setup: (theory -> theory) list
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    48
end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    49
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    50
structure Codegen : CODEGEN =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    51
struct
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    52
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    53
val quiet_mode = ref true;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    54
fun message s = if !quiet_mode then () else writeln s;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    55
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    56
(**** Mixfix syntax ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    57
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    58
datatype 'a mixfix =
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    59
    Arg
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    60
  | Ignore
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    61
  | Pretty of Pretty.T
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    62
  | Quote of 'a;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    63
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    64
fun is_arg Arg = true
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    65
  | is_arg Ignore = true
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    66
  | is_arg _ = false;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    67
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    68
fun quotes_of [] = []
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    69
  | quotes_of (Quote q :: ms) = q :: quotes_of ms
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    70
  | quotes_of (_ :: ms) = quotes_of ms;
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    71
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    72
fun args_of [] xs = ([], xs)
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    73
  | args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs)
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    74
  | args_of (Ignore :: ms) (_ :: xs) = args_of ms xs
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    75
  | args_of (_ :: ms) xs = args_of ms xs;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    76
12490
d2a2c479b3cb made SML/XL happy;
wenzelm
parents: 12452
diff changeset
    77
fun num_args x = length (filter is_arg x);
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    78
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    79
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    80
(**** theory data ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    81
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    82
(* data kind 'Pure/codegen' *)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    83
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    84
type 'a codegen = theory -> (exn option * string) Graph.T ->
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    85
  string -> bool -> 'a -> ((exn option * string) Graph.T * Pretty.T) option;
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    86
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    87
structure CodegenArgs =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    88
struct
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    89
  val name = "Pure/codegen";
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    90
  type T =
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    91
    {codegens : (string * term codegen) list,
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    92
     tycodegens : (string * typ codegen) list,
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
    93
     consts : ((string * typ) * term mixfix list) list,
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
    94
     types : (string * typ mixfix list) list,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
    95
     attrs: (string * theory attribute) list};
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    96
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
    97
  val empty =
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
    98
    {codegens = [], tycodegens = [], consts = [], types = [], attrs = []};
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
    99
  val copy = I;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   100
  val prep_ext = I;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   101
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   102
  fun merge
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   103
    ({codegens = codegens1, tycodegens = tycodegens1,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   104
      consts = consts1, types = types1, attrs = attrs1},
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   105
     {codegens = codegens2, tycodegens = tycodegens2,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   106
      consts = consts2, types = types2, attrs = attrs2}) =
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   107
    {codegens = rev (merge_alists (rev codegens1) (rev codegens2)),
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   108
     tycodegens = rev (merge_alists (rev tycodegens1) (rev tycodegens2)),
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   109
     consts = merge_alists consts1 consts2,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   110
     types = merge_alists types1 types2,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   111
     attrs = merge_alists attrs1 attrs2};
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   112
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   113
  fun print sg ({codegens, tycodegens, ...} : T) =
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   114
    Pretty.writeln (Pretty.chunks
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   115
      [Pretty.strs ("term code generators:" :: map fst codegens),
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   116
       Pretty.strs ("type code generators:" :: map fst tycodegens)]);
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   117
end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   118
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   119
structure CodegenData = TheoryDataFun(CodegenArgs);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   120
val print_codegens = CodegenData.print;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   121
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   122
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   123
(**** add new code generators to theory ****)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   124
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   125
fun add_codegen name f thy =
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   126
  let val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   127
  in (case assoc (codegens, name) of
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   128
      None => CodegenData.put {codegens = (name, f) :: codegens,
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   129
        tycodegens = tycodegens, consts = consts, types = types,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   130
        attrs = attrs} thy
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   131
    | Some _ => error ("Code generator " ^ name ^ " already declared"))
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   132
  end;
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   133
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   134
fun add_tycodegen name f thy =
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   135
  let val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   136
  in (case assoc (tycodegens, name) of
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   137
      None => CodegenData.put {tycodegens = (name, f) :: tycodegens,
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   138
        codegens = codegens, consts = consts, types = types,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   139
        attrs = attrs} thy
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   140
    | Some _ => error ("Code generator " ^ name ^ " already declared"))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   141
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   142
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   143
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   144
(**** code attribute ****)
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   145
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   146
fun add_attribute name att thy =
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   147
  let val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   148
  in (case assoc (attrs, name) of
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   149
      None => CodegenData.put {tycodegens = tycodegens,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   150
        codegens = codegens, consts = consts, types = types,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   151
        attrs = (name, att) :: attrs} thy
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   152
    | Some _ => error ("Code attribute " ^ name ^ " already declared"))
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   153
  end;
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   154
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   155
val code_attr =
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   156
  Attrib.syntax (Scan.depend (fn thy => Scan.optional Args.name "" >>
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   157
    (fn name => (thy, case assoc (#attrs (CodegenData.get thy), name) of
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   158
          None => error ("Unknown code attribute: " ^ quote name)
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   159
        | Some att => att)))); 
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   160
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   161
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   162
(**** associate constants with target language code ****)
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 gen_assoc_consts prep_type xs thy = foldl (fn (thy, (s, tyopt, syn)) =>
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 sg = sign_of thy;
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   167
    val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   168
    val cname = Sign.intern_const sg s;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   169
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   170
    (case Sign.const_type sg cname of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   171
       Some T =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   172
         let val T' = (case tyopt of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   173
                None => T
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   174
              | Some ty =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   175
                  let val U = prep_type sg ty
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   176
                  in if Type.typ_instance (Sign.tsig_of sg, U, T) then U
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   177
                    else error ("Illegal type constraint for constant " ^ cname)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   178
                  end)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   179
         in (case assoc (consts, (cname, T')) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   180
             None => CodegenData.put {codegens = codegens,
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   181
               tycodegens = tycodegens,
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   182
               consts = ((cname, T'), syn) :: consts,
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   183
               types = types, attrs = attrs} thy
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   184
           | Some _ => error ("Constant " ^ cname ^ " already associated with code"))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   185
         end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   186
     | _ => error ("Not a constant: " ^ s))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   187
  end) (thy, xs);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   188
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   189
val assoc_consts_i = gen_assoc_consts (K I);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   190
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
   191
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   192
(**** associate types with target language types ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   193
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   194
fun assoc_types xs thy = foldl (fn (thy, (s, syn)) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   195
  let
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   196
    val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   197
    val tc = Sign.intern_tycon (sign_of thy) s
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   198
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   199
    (case assoc (types, tc) of
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   200
       None => CodegenData.put {codegens = codegens,
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   201
         tycodegens = tycodegens, consts = consts,
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   202
         types = (tc, syn) :: types, attrs = attrs} thy
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   203
     | Some _ => error ("Type " ^ tc ^ " already associated with code"))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   204
  end) (thy, xs);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   205
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   206
fun get_assoc_type thy s = assoc (#types (CodegenData.get thy), s);
11546
wenzelm
parents: 11539
diff changeset
   207
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   208
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   209
(**** retrieve definition of constant ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   210
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   211
fun is_instance thy T1 T2 =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   212
  Type.typ_instance (Sign.tsig_of (sign_of thy), T1, Type.varifyT T2);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   213
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   214
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
   215
  s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   216
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   217
fun get_defn thy s T =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   218
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   219
    val axms = flat (map (Symtab.dest o #axioms o Theory.rep_theory)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   220
      (thy :: Theory.ancestors_of thy));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   221
    val defs = mapfilter (fn (_, t) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   222
      (let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   223
         val (lhs, rhs) = Logic.dest_equals t;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   224
         val (c, args) = strip_comb lhs;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   225
         val (s', T') = dest_Const c
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   226
       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
   227
         None) axms;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   228
    val i = find_index (is_instance thy T o fst) defs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   229
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   230
    if i>=0 then Some (snd (nth_elem (i, defs)),
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   231
      if length defs = 1 then None else Some i)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   232
    else None
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   233
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   234
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   235
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   236
(**** make valid ML identifiers ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   237
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   238
fun gen_mk_id kind rename sg s =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   239
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   240
    val (xs as x::_) = explode (rename (space_implode "_"
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   241
      (NameSpace.unpack (Sign.cond_extern sg kind s))));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   242
    fun check_str [] = ""
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   243
      | check_str (" " :: xs) = "_" ^ check_str xs
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   244
      | check_str (x :: xs) =
12824
cdf586d56b8a Tuned name mangling function.
berghofe
parents: 12580
diff changeset
   245
          (if Symbol.is_letdig x then x
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   246
           else "_" ^ string_of_int (ord x)) ^ check_str xs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   247
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   248
    (if not (Symbol.is_letter x) then "id" else "") ^ check_str xs
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   249
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   250
13073
cc9d7f403a4b mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents: 13003
diff changeset
   251
val mk_const_id = gen_mk_id Sign.constK
cc9d7f403a4b mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents: 13003
diff changeset
   252
  (fn s => if s mem ThmDatabase.ml_reserved then s ^ "_const" else s);
cc9d7f403a4b mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents: 13003
diff changeset
   253
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   254
val mk_type_id = gen_mk_id Sign.typeK
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   255
  (fn s => if s mem ThmDatabase.ml_reserved then s ^ "_type" else s);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   256
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   257
fun rename_term t =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   258
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   259
    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
   260
    val clash = names inter ThmDatabase.ml_reserved;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   261
    val ps = clash ~~ variantlist (clash, names);
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 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
   264
      | rename (Free (a, T)) = Free (if_none (assoc (ps, a)) a, T)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   265
      | rename (Abs (s, T, t)) = Abs (s, T, rename t)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   266
      | rename (t $ u) = rename t $ rename u
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   267
      | rename t = t;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   268
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   269
    rename t
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   270
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   271
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   272
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   273
(**** invoke suitable code generator for term / type ****)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   274
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   275
fun invoke_codegen thy dep brack (gr, t) = (case get_first
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   276
   (fn (_, f) => f thy gr dep brack t) (#codegens (CodegenData.get thy)) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   277
      None => error ("Unable to generate code for term:\n" ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   278
        Sign.string_of_term (sign_of thy) t ^ "\nrequired by:\n" ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   279
        commas (Graph.all_succs gr [dep]))
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   280
    | Some x => x);
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   281
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   282
fun invoke_tycodegen thy dep brack (gr, T) = (case get_first
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   283
   (fn (_, f) => f thy gr dep brack T) (#tycodegens (CodegenData.get thy)) of
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   284
      None => error ("Unable to generate code for type:\n" ^
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   285
        Sign.string_of_typ (sign_of thy) T ^ "\nrequired by:\n" ^
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   286
        commas (Graph.all_succs gr [dep]))
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   287
    | Some x => x);
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   288
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   289
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   290
(**** code generator for mixfix expressions ****)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   291
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   292
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"];
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   293
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   294
fun pretty_fn [] p = [p]
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   295
  | pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") ::
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   296
      Pretty.brk 1 :: pretty_fn xs p;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   297
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   298
fun pretty_mixfix [] [] _ = []
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   299
  | pretty_mixfix (Arg :: ms) (p :: ps) qs = p :: pretty_mixfix ms ps qs
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   300
  | pretty_mixfix (Ignore :: ms) ps qs = pretty_mixfix ms ps qs
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   301
  | pretty_mixfix (Pretty p :: ms) ps qs = p :: pretty_mixfix ms ps qs
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   302
  | pretty_mixfix (Quote _ :: ms) ps (q :: qs) = q :: pretty_mixfix ms ps qs;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   303
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   304
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   305
(**** default code generators ****)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   306
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   307
fun eta_expand t ts i =
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 (Ts, _) = strip_type (fastype_of t);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   310
    val j = i - length ts
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   311
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   312
    foldr (fn (T, t) => Abs ("x", T, t))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   313
      (take (j, Ts), list_comb (t, ts @ map Bound (j-1 downto 0)))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   314
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   315
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   316
fun mk_app _ p [] = p
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   317
  | mk_app brack p ps = if brack then
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   318
       Pretty.block (Pretty.str "(" ::
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   319
         separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"])
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   320
     else Pretty.block (separate (Pretty.brk 1) (p :: ps));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   321
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   322
fun new_names t xs = variantlist (xs,
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   323
  map (fst o fst o dest_Var) (term_vars t) union
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   324
  add_term_names (t, ThmDatabase.ml_reserved));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   325
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   326
fun new_name t x = hd (new_names t [x]);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   327
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   328
fun default_codegen thy gr dep brack t =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   329
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   330
    val (u, ts) = strip_comb t;
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   331
    fun codegens brack = foldl_map (invoke_codegen thy dep brack)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   332
  in (case u of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   333
      Var ((s, i), _) =>
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   334
        let val (gr', ps) = codegens true (gr, ts)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   335
        in Some (gr', mk_app brack (Pretty.str (s ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   336
           (if i=0 then "" else string_of_int i))) ps)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   337
        end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   338
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   339
    | Free (s, _) =>
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   340
        let val (gr', ps) = codegens true (gr, ts)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   341
        in Some (gr', mk_app brack (Pretty.str s) ps) end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   342
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   343
    | Const (s, T) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   344
      (case get_assoc_code thy s T of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   345
         Some ms =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   346
           let val i = num_args ms
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   347
           in if length ts < i then
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   348
               default_codegen thy gr dep brack (eta_expand u ts i)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   349
             else
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   350
               let
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   351
                 val (ts1, ts2) = args_of ms ts;
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   352
                 val (gr1, ps1) = codegens false (gr, ts1);
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   353
                 val (gr2, ps2) = codegens true (gr1, ts2);
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   354
                 val (gr3, ps3) = codegens false (gr2, quotes_of ms);
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   355
               in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   356
                 Some (gr3, mk_app brack (Pretty.block (pretty_mixfix ms ps1 ps3)) ps2)
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
           end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   359
       | None => (case get_defn thy s T of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   360
           None => None
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   361
         | Some ((args, rhs), k) =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   362
             let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   363
               val id = mk_const_id (sign_of thy) s ^ (case k of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   364
                 None => "" | Some i => "_def" ^ string_of_int i);
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   365
               val (gr', ps) = codegens true (gr, ts);
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   366
             in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   367
               Some (Graph.add_edge (id, dep) gr' handle Graph.UNDEF _ =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   368
                 let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   369
                   val _ = message ("expanding definition of " ^ s);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   370
                   val (Ts, _) = strip_type T;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   371
                   val (args', rhs') =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   372
                     if not (null args) orelse null Ts then (args, rhs) else
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   373
                       let val v = Free (new_name rhs "x", hd Ts)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   374
                       in ([v], betapply (rhs, v)) end;
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   375
                   val (gr1, p) = invoke_codegen thy id false
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   376
                     (Graph.add_edge (id, dep)
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   377
                        (Graph.new_node (id, (None, "")) gr'), rhs');
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   378
                   val (gr2, xs) = codegens false (gr1, args');
12580
7fdc00bb2a9e Code generator now adds type constraints to val declarations (to make
berghofe
parents: 12555
diff changeset
   379
                   val (gr3, ty) = invoke_tycodegen thy id false (gr2, T);
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   380
                 in Graph.map_node id (K (None, Pretty.string_of (Pretty.block
12580
7fdc00bb2a9e Code generator now adds type constraints to val declarations (to make
berghofe
parents: 12555
diff changeset
   381
                   (separate (Pretty.brk 1) (if null args' then
7fdc00bb2a9e Code generator now adds type constraints to val declarations (to make
berghofe
parents: 12555
diff changeset
   382
                       [Pretty.str ("val " ^ id ^ " :"), ty]
7fdc00bb2a9e Code generator now adds type constraints to val declarations (to make
berghofe
parents: 12555
diff changeset
   383
                     else Pretty.str ("fun " ^ id) :: xs) @
7fdc00bb2a9e Code generator now adds type constraints to val declarations (to make
berghofe
parents: 12555
diff changeset
   384
                    [Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr3
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   385
                 end, mk_app brack (Pretty.str id) ps)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   386
             end))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   387
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   388
    | Abs _ =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   389
      let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   390
        val (bs, Ts) = ListPair.unzip (strip_abs_vars u);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   391
        val t = strip_abs_body u
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   392
        val bs' = new_names t bs;
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   393
        val (gr1, ps) = codegens true (gr, ts);
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   394
        val (gr2, p) = invoke_codegen thy dep false
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   395
          (gr1, subst_bounds (map Free (rev (bs' ~~ Ts)), t));
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   396
      in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   397
        Some (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   398
          [Pretty.str ")"])) ps)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   399
      end
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   400
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   401
    | _ => None)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   402
  end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   403
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   404
fun default_tycodegen thy gr dep brack (TVar ((s, i), _)) =
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   405
      Some (gr, Pretty.str (s ^ (if i = 0 then "" else string_of_int i)))
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   406
  | default_tycodegen thy gr dep brack (TFree (s, _)) = Some (gr, Pretty.str s)
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   407
  | default_tycodegen thy gr dep brack (Type (s, Ts)) =
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   408
      (case assoc (#types (CodegenData.get thy), s) of
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   409
         None => None
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   410
       | Some ms =>
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   411
           let
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   412
             val (gr', ps) = foldl_map
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   413
               (invoke_tycodegen thy dep false) (gr, fst (args_of ms Ts));
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   414
             val (gr'', qs) = foldl_map
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   415
               (invoke_tycodegen thy dep false) (gr', quotes_of ms)
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   416
           in Some (gr'', Pretty.block (pretty_mixfix ms ps qs)) end);
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   417
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   418
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   419
fun output_code gr xs = implode (map (snd o Graph.get_node gr)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   420
  (rev (Graph.all_preds gr xs)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   421
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   422
fun gen_generate_code prep_term thy = Pretty.setmp_margin 80 (fn xs =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   423
  let
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   424
    val sg = sign_of thy;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   425
    val gr = Graph.new_node ("<Top>", (None, "")) Graph.empty;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   426
    val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s)
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   427
      (invoke_codegen thy "<Top>" false (gr, t)))
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   428
        (gr, map (apsnd (prep_term sg)) xs)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   429
  in
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   430
    "structure Generated =\nstruct\n\n" ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   431
    output_code gr' ["<Top>"] ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   432
    space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   433
      [Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   434
    "\n\nend;\n\nopen Generated;\n"
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   435
  end);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   436
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   437
val generate_code_i = gen_generate_code (K I);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   438
val generate_code = gen_generate_code
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   439
  (fn sg => term_of o read_cterm sg o rpair TypeInfer.logicT);
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   440
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   441
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   442
(**** Interface ****)
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   443
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   444
fun parse_mixfix rd s =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   445
  (case Scan.finite Symbol.stopper (Scan.repeat
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   446
     (   $$ "_" >> K Arg
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   447
      || $$ "?" >> K Ignore
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   448
      || $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   449
      || $$ "{" |-- $$ "*" |-- Scan.repeat1
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   450
           (   $$ "'" |-- Scan.one Symbol.not_eof
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   451
            || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   452
         $$ "*" --| $$ "}" >> (Quote o rd o implode)
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   453
      || Scan.repeat1
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   454
           (   $$ "'" |-- Scan.one Symbol.not_eof
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   455
            || Scan.unless ($$ "_" || $$ "?" || $$ "/" || $$ "{" |-- $$ "*")
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   456
                 (Scan.one Symbol.not_eof)) >> (Pretty o Pretty.str o implode)))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   457
       (Symbol.explode s) of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   458
     (p, []) => p
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   459
   | _ => error ("Malformed annotation: " ^ quote s));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   460
11546
wenzelm
parents: 11539
diff changeset
   461
structure P = OuterParse and K = OuterSyntax.Keyword;
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   462
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   463
val assoc_typeP =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   464
  OuterSyntax.command "types_code"
11546
wenzelm
parents: 11539
diff changeset
   465
  "associate types with target language types" K.thy_decl
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   466
    (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")") >>
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   467
     (fn xs => Toplevel.theory (fn thy => assoc_types
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   468
       (map (fn (name, mfx) => (name, parse_mixfix
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   469
         (typ_of o read_ctyp (sign_of thy)) mfx)) xs) thy)));
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   470
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   471
val assoc_constP =
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   472
  OuterSyntax.command "consts_code"
11546
wenzelm
parents: 11539
diff changeset
   473
  "associate constants with target language code" K.thy_decl
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   474
    (Scan.repeat1
13003
3d5807d45439 clarified outer syntax;
wenzelm
parents: 12824
diff changeset
   475
       (P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --|
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   476
        P.$$$ "(" -- P.string --| P.$$$ ")") >>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   477
     (fn xs => Toplevel.theory (fn thy => assoc_consts
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   478
       (map (fn ((name, optype), mfx) => (name, optype, parse_mixfix
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   479
         (term_of o read_cterm (sign_of thy) o rpair TypeInfer.logicT) mfx))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   480
           xs) thy)));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   481
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   482
val generate_codeP =
11546
wenzelm
parents: 11539
diff changeset
   483
  OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl
13003
3d5807d45439 clarified outer syntax;
wenzelm
parents: 12824
diff changeset
   484
    (Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
3d5807d45439 clarified outer syntax;
wenzelm
parents: 12824
diff changeset
   485
     Scan.repeat1 (P.name --| P.$$$ "=" -- P.term) >>
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   486
     (fn (opt_fname, xs) => Toplevel.theory (fn thy =>
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   487
        ((case opt_fname of
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   488
            None => use_text Context.ml_output false
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   489
          | Some fname => File.write (Path.unpack fname))
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   490
              (generate_code thy xs); thy))));
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   491
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   492
val parsers = [assoc_typeP, assoc_constP, generate_codeP];
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   493
12452
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   494
val setup =
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   495
  [CodegenData.init,
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   496
   add_codegen "default" default_codegen,
68493b92e7a6 - Added code generator interface for types
berghofe
parents: 12311
diff changeset
   497
   add_tycodegen "default" default_tycodegen,
12555
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   498
   assoc_types [("fun", parse_mixfix (K dummyT) "(_ ->/ _)")],
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   499
   Attrib.add_attributes [("code",
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   500
     (code_attr, K Attrib.undef_local_attribute),
e6d7f040fdc7 "code" attribute is now managed by basic code generator module.
berghofe
parents: 12490
diff changeset
   501
     "declare theorems for code generation")]];
11520
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   502
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   503
end;
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   504
ae738c1ee155 Initial version of generic code generator.
berghofe
parents:
diff changeset
   505
OuterSyntax.add_parsers Codegen.parsers;