src/Pure/Tools/codegen_thingol.ML
author haftmann
Tue, 06 Jun 2006 14:55:19 +0200
changeset 19785 52d71ee5c8a8
parent 19616 2545e8ab59a5
child 19816 a8c8ed1c85e0
permissions -rw-r--r--
bugfixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
     1
(*  Title:      Pure/Tools/codegen_thingol.ML
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     2
    ID:         $Id$
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     4
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     5
Intermediate language ("Thin-gol") for code extraction.
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     6
*)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     7
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
     8
infix 8 `%%;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
     9
infixr 6 `->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    10
infixr 6 `-->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    11
infix 4 `$;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    12
infix 4 `$$;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    13
infixr 3 `|->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    14
infixr 3 `|-->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    15
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    16
signature BASIC_CODEGEN_THINGOL =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    17
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    18
  type vname = string;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    19
  type sortcontext = ClassPackage.sortcontext;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    20
  datatype iclasslookup = Instance of string * iclasslookup list list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    21
                        | Lookup of class list * (string * int);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    22
  datatype itype =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    23
      `%% of string * itype list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    24
    | `-> of itype * itype
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    25
    | ITyVar of vname;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    26
  datatype iexpr =
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
    27
      IConst of string * (iclasslookup list list * itype)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    28
    | IVar of vname
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    29
    | `$ of iexpr * iexpr
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    30
    | `|-> of (vname * itype) * iexpr
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    31
    | INum of IntInf.int (*positive!*) * unit
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    32
    | IChar of string (*length one!*) * iexpr
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    33
    | IAbs of ((iexpr * itype) * iexpr) * iexpr
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    34
        (* (((binding expression (ve), binding type (vty)),
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    35
                body expression (be)), native expression (e0)) *)
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    36
    | ICase of ((iexpr * itype) * (iexpr * iexpr) list) * iexpr;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    37
        (* ((discrimendum expression (de), discrimendum type (dty)),
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    38
                [(selector expression (se), body expression (be))]),
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    39
                native expression (e0)) *)
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    40
end;
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    41
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    42
signature CODEGEN_THINGOL =
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    43
sig
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    44
  include BASIC_CODEGEN_THINGOL;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    45
  val `--> : itype list * itype -> itype;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    46
  val `$$ : iexpr * iexpr list -> iexpr;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    47
  val `|--> : (vname * itype) list * iexpr -> iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    48
  val pretty_itype: itype -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    49
  val pretty_iexpr: iexpr -> Pretty.T;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    50
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    51
  val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    52
  val unfold_fun: itype -> itype list * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    53
  val unfold_app: iexpr -> iexpr * iexpr list;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    54
  val unfold_abs: iexpr -> (iexpr * itype) list * iexpr;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    55
  val unfold_let: iexpr -> ((iexpr * itype) * iexpr) list * iexpr;
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
    56
  val unfold_const_app: iexpr ->
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
    57
    ((string * (iclasslookup list list * itype)) * iexpr list) option;
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
    58
  val add_constnames: iexpr -> string list -> string list;
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
    59
  val add_varnames: iexpr -> string list -> string list;
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
    60
  val is_pat: iexpr -> bool;
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
    61
  val map_pure: (iexpr -> 'a) -> iexpr -> 'a;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    62
  val resolve_tycos: (string -> string) -> itype * iexpr list -> itype * iexpr list;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    63
  val resolve_consts: (string -> string) -> iexpr -> iexpr;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    64
  val eta_expand: (string * (iclasslookup list list * itype)) * iexpr list -> int -> iexpr;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    65
  
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    66
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    67
  type funn = (iexpr list * iexpr) list * (sortcontext * itype);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    68
  type datatyp = sortcontext * (string * itype list) list;
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
    69
  datatype prim =
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
    70
      Pretty of Pretty.T
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
    71
    | Name;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    72
  datatype def =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    73
      Undef
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
    74
    | Prim of (string * prim list) list
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    75
    | Fun of funn
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    76
    | Typesyn of (vname * sort) list * itype
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
    77
    | Datatype of datatyp
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    78
    | Datatypecons of string
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    79
    | Class of class list * (vname * (string * (sortcontext * itype)) list)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    80
    | Classmember of class
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
    81
    | Classinst of ((class * (string * (vname * sort) list))
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    82
          * (class * (string * iclasslookup list list)) list)
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19215
diff changeset
    83
        * (string * ((string * funn) * iclasslookup list list)) list
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
    84
    | Classinstmember;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    85
  type module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    86
  type transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    87
  type 'dst transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    88
  val pretty_def: def -> Pretty.T;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    89
  val pretty_module: module -> Pretty.T; 
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
    90
  val pretty_deps: module -> Pretty.T;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    91
  val empty_module: module;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
    92
  val get_def: module -> string -> def;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
    93
  val add_prim: string -> (string * prim list) -> module -> module;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    94
  val ensure_prim: string -> string -> module -> module;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    95
  val merge_module: module * module -> module;
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
    96
  val diff_module: module * module -> (string * def) list;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
    97
  val project_module: string list -> module -> module;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
    98
  val purge_module: string list -> module -> module;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
    99
  val has_nsp: string -> string -> bool;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   100
  val succeed: 'a -> transact -> 'a transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   101
  val fail: string -> transact -> 'a transact_fin;
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   102
  val ensure_def: (string * (string -> transact -> def transact_fin)) list -> string
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   103
    -> string -> transact -> transact;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   104
  val start_transact: string option -> (transact -> 'a * transact) -> module -> 'a * module;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   105
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   106
  val debug: bool ref;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   107
  val debug_msg: ('a -> string) -> 'a -> 'a;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   108
  val soft_exc: bool ref;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   109
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   110
  val serialize:
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   111
    ((string -> string -> string) -> string -> (string * def) list -> 'a option)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   112
    -> ((string -> string) -> string list -> (string * string) * 'a list -> 'a option)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   113
    -> (string -> string option)
18919
haftmann
parents: 18918
diff changeset
   114
    -> (string * string -> string)
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   115
    -> string list list -> string -> module -> 'a option;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   116
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   117
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   118
structure CodegenThingol: CODEGEN_THINGOL =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   119
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   120
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   121
(** auxiliary **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   122
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   123
val debug = ref false;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   124
fun debug_msg f x = (if !debug then Output.debug (f x) else (); x);
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   125
val soft_exc = ref true;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   126
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   127
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   128
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   129
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   130
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   131
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   132
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   133
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   134
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   135
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   136
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   137
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   138
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   139
fun map_yield f [] = ([], [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   140
  | map_yield f (x::xs) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   141
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   142
        val (y, x') = f x
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   143
        val (ys, xs') = map_yield f xs
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   144
      in (y::ys, x'::xs') end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   145
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   146
fun get_prefix eq ([], ys) = ([], ([], ys))
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   147
  | get_prefix eq (xs, []) = ([], (xs, []))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   148
  | get_prefix eq (xs as x::xs', ys as y::ys') =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   149
      if eq (x, y) then
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   150
        let val (ps', xys'') = get_prefix eq (xs', ys')
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   151
        in (x::ps', xys'') end
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   152
      else ([], (xs, ys));
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   153
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   154
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   155
(** language core - types, pattern, expressions **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   156
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   157
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   158
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   159
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   160
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   161
type sortcontext = ClassPackage.sortcontext;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   162
datatype iclasslookup = Instance of string * iclasslookup list list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   163
                      | Lookup of class list * (string * int);
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   164
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   165
datatype itype =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   166
    `%% of string * itype list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   167
  | `-> of itype * itype
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   168
  | ITyVar of vname;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   170
datatype iexpr =
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   171
    IConst of string * (iclasslookup list list * itype)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   172
  | IVar of vname
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   173
  | `$ of iexpr * iexpr
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   174
  | `|-> of (vname * itype) * iexpr
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   175
  | INum of IntInf.int (*positive!*) * unit
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   176
  | IChar of string (*length one!*) * iexpr
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   177
  | IAbs of ((iexpr * itype) * iexpr) * iexpr
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   178
  | ICase of ((iexpr * itype) * (iexpr * iexpr) list) * iexpr;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   179
    (*see also signature*)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   180
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   181
(*
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   182
  variable naming conventions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   183
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   184
  bare names:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   185
    variable names          v
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   186
    class names             cls
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   187
    type constructor names  tyco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   188
    datatype names          dtco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   189
    const names (general)   c
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   190
    constructor names       co
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   191
    class member names      m
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   192
    arbitrary name          s
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   193
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   194
  constructs:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   195
    sort                    sort
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   196
    type                    ty
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   197
    expression              e
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   198
    pattern                 p, pat
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   199
    instance (cls, tyco)    inst
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   200
    variable (v, ty)        var
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   201
    class member (m, ty)    membr
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   202
    constructors (co, tys)  constr
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   203
 *)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   204
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   205
val op `--> = Library.foldr (op `->);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   206
val op `$$ = Library.foldl (op `$);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   207
val op `|--> = Library.foldr (op `|->);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   208
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   209
val pretty_sortcontext =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   210
  Pretty.list "(" ")" o Pretty.commas o map (fn (v, sort) => (Pretty.block o Pretty.breaks)
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   211
    [Pretty.str v, Pretty.str "::", Pretty.enum "&" "" "" (map Pretty.str sort)]);
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   212
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   213
fun pretty_itype (tyco `%% tys) =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   214
      Pretty.enum "" "(" ")" (Pretty.str tyco :: map pretty_itype tys)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   215
  | pretty_itype (ty1 `-> ty2) =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   216
      Pretty.enum "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2]
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   217
  | pretty_itype (ITyVar v) =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   218
      Pretty.str v;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   219
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   220
fun pretty_iexpr (IConst (c, _)) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   221
      Pretty.str c
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   222
  | pretty_iexpr (IVar v) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   223
      Pretty.str ("?" ^ v)
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   224
  | pretty_iexpr (e1 `$ e2) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   225
      (Pretty.enclose "(" ")" o Pretty.breaks)
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   226
        [pretty_iexpr e1, pretty_iexpr e2]
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   227
  | pretty_iexpr ((v, ty) `|-> e) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   228
      (Pretty.enclose "(" ")" o Pretty.breaks)
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   229
        [Pretty.str v, Pretty.str "::", pretty_itype ty, Pretty.str "|->", pretty_iexpr e]
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   230
  | pretty_iexpr (INum (n, _)) =
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   231
      (Pretty.str o IntInf.toString) n
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   232
  | pretty_iexpr (IChar (c, _)) =
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   233
      (Pretty.str o quote) c
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   234
  | pretty_iexpr (IAbs (((e1, _), e2), _)) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   235
      (Pretty.enclose "(" ")" o Pretty.breaks)
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   236
        [pretty_iexpr e1, Pretty.str "|->", pretty_iexpr e2]
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   237
  | pretty_iexpr (ICase (((e, _), cs), _)) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   238
      (Pretty.enclose "(" ")" o Pretty.breaks) [
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   239
        Pretty.str "case",
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   240
        pretty_iexpr e,
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   241
        Pretty.enclose "(" ")" (map (fn (p, e) =>
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   242
          (Pretty.block o Pretty.breaks) [
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   243
            pretty_iexpr p,
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   244
            Pretty.str "=>",
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   245
            pretty_iexpr e
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   246
          ]
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   247
        ) cs)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   248
      ];
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   249
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   250
val unfold_fun = unfoldr
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   251
  (fn op `-> t => SOME t
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   252
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   253
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   254
val unfold_app = unfoldl
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   255
  (fn op `$ e => SOME e
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   256
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   257
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   258
val unfold_abs = unfoldr
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   259
  (fn (v, ty) `|-> e => SOME ((IVar v, ty), e)
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   260
    | IAbs (((e1, ty), e2), _) => SOME ((e1, ty), e2)
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   261
    | _ => NONE)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   262
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   263
val unfold_let = unfoldr
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   264
  (fn ICase (((de, dty), [(se, be)]), _) => SOME (((se, dty), de), be)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   265
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   266
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   267
fun unfold_const_app e = 
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   268
 case unfold_app e
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   269
  of (IConst x, es) => SOME (x, es)
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   270
   | _ => NONE;
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   271
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   272
fun map_itype _ (ty as ITyVar _) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   273
      ty
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   274
  | map_itype f (tyco `%% tys) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   275
      tyco `%% map f tys
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   276
  | map_itype f (t1 `-> t2) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   277
      f t1 `-> f t2;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   278
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   279
fun eq_ityp ((sctxt1, ty1), (sctxt2, ty2)) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   280
  let
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   281
    exception NO_MATCH;
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   282
    fun eq_sctxt subs sctxt1 sctxt2 =
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   283
      map (fn (v : string, sort : string list) => case AList.lookup (op =) subs v
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   284
       of NONE => raise NO_MATCH
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   285
        | SOME (v' : string) => case AList.lookup (op =) sctxt2 v'
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   286
           of NONE => raise NO_MATCH
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   287
            | SOME sort' => if sort <> sort' then raise NO_MATCH else ()) sctxt1
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   288
    fun eq (ITyVar v1) (ITyVar v2) subs =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   289
          (case AList.lookup (op =) subs v1
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   290
           of NONE => subs |> AList.update (op =) (v1, v2)
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   291
            | SOME v1' =>
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   292
                if v1' <> v2
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   293
                then raise NO_MATCH
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   294
                else subs)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   295
      | eq (tyco1 `%% tys1) (tyco2 `%% tys2) subs =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   296
          if tyco1 <> tyco2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   297
          then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   298
          else subs |> fold2 eq tys1 tys2
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   299
      | eq (ty11 `-> ty12) (ty21 `-> ty22) subs =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   300
          subs |> eq ty11 ty21 |> eq ty12 ty22
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   301
      | eq _ _ _ = raise NO_MATCH;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   302
  in
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   303
    (eq ty1 ty2 []; true)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   304
    handle NO_MATCH => false
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   305
  end;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   306
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   307
fun instant_itype f =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   308
  let
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   309
    fun instant (ITyVar x) = f x
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   310
      | instant y = map_itype instant y;
19215
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   311
  in instant end;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   312
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   313
fun is_pat (e as IConst (_, ([], _))) = true
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   314
  | is_pat (e as IVar _) = true
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   315
  | is_pat (e as (e1 `$ e2)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   316
      is_pat e1 andalso is_pat e2
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   317
  | is_pat (e as INum _) = true
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   318
  | is_pat (e as IChar _) = true
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   319
  | is_pat e = false;
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   320
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   321
fun map_pure f (e as IConst _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   322
      f e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   323
  | map_pure f (e as IVar _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   324
      f e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   325
  | map_pure f (e as _ `$ _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   326
      f e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   327
  | map_pure f (e as _ `|-> _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   328
      f e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   329
  | map_pure _ (INum _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   330
      error ("sorry, no pure representation of numerals so far")
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   331
  | map_pure f (IChar (_, e0)) =
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   332
      f e0
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   333
  | map_pure f (IAbs (_, e0)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   334
      f e0
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   335
  | map_pure f (ICase (_, e0)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   336
      f e0;
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
   337
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   338
fun resolve_tycos _ = error "";
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   339
fun resolve_consts _ = error "";
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   340
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   341
fun add_constnames (IConst (c, _)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   342
      insert (op =) c
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   343
  | add_constnames (IVar _) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   344
      I
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   345
  | add_constnames (e1 `$ e2) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   346
      add_constnames e1 #> add_constnames e2
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   347
  | add_constnames (_ `|-> e) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   348
      add_constnames e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   349
  | add_constnames (INum _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   350
      I
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   351
  | add_constnames (IChar _) =
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   352
      I
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   353
  | add_constnames (IAbs (_, e)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   354
      add_constnames e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   355
  | add_constnames (ICase (_, e)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   356
      add_constnames e;
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   357
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   358
fun add_varnames (IConst _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   359
      I
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   360
  | add_varnames (IVar v) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   361
      insert (op =) v
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   362
  | add_varnames (e1 `$ e2) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   363
      add_varnames e1 #> add_varnames e2
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   364
  | add_varnames ((v, _) `|-> e) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   365
      insert (op =) v #> add_varnames e
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   366
  | add_varnames (INum _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   367
      I
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   368
  | add_varnames (IChar _) =
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   369
      I
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   370
  | add_varnames (IAbs (((ve, _), be), _)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   371
      add_varnames ve #> add_varnames be
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   372
  | add_varnames (ICase (((de, _), bses), _)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   373
      add_varnames de #> fold (fn (be, se) => add_varnames be #> add_varnames se) bses;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   374
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   375
fun invent seed used =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   376
  let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   377
    val x = Term.variant used seed
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   378
  in (x, x :: used) end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   379
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   380
fun eta_expand (c as (_, (_, ty)), es) k =
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   381
  let
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   382
    val j = length es;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   383
    val l = k - j;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   384
    val tys = (curry Library.take l o curry Library.drop j o fst o unfold_fun) ty;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   385
    val vs = [] |> fold add_varnames es |> fold_map invent (replicate l "x") |> fst;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   386
  in vs ~~ tys `|--> IConst c `$$ es @ map IVar vs end;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   387
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   388
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   389
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   390
(** language module system - definitions, modules, transactions **)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   391
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   392
(* type definitions *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   393
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   394
type funn = (iexpr list * iexpr) list * (sortcontext * itype);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   395
type datatyp = sortcontext * (string * itype list) list;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   396
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   397
datatype prim =
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   398
    Pretty of Pretty.T
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   399
  | Name;
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   400
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   401
datatype def =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   402
    Undef
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   403
  | Prim of (string * prim list) list
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   404
  | Fun of funn
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   405
  | Typesyn of (vname * sort) list * itype
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   406
  | Datatype of datatyp
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   407
  | Datatypecons of string
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   408
  | Class of class list * (vname * (string * (sortcontext * itype)) list)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   409
  | Classmember of class
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   410
  | Classinst of ((class * (string * (vname * sort) list))
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   411
        * (class * (string * iclasslookup list list)) list)
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19215
diff changeset
   412
      * (string * ((string * funn) * iclasslookup list list)) list
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   413
  | Classinstmember;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   414
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   415
datatype node = Def of def | Module of node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   416
type module = node Graph.T;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   417
type transact = Graph.key option * module;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   418
datatype 'dst transact_res = Succeed of 'dst | Fail of string list * exn option;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   419
type 'dst transact_fin = 'dst transact_res * module;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   420
exception FAIL of string list * exn option;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   421
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   422
val eq_def = (op =) : def * def -> bool;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   423
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   424
(* simple diagnosis *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   425
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   426
fun pretty_def Undef =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   427
      Pretty.str "<UNDEF>"
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   428
  | pretty_def (Prim prims) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   429
      Pretty.str ("<PRIM " ^ (commas o map fst) prims ^ ">")
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   430
  | pretty_def (Fun (eqs, (sortctxt, ty))) =
18812
a4554848b59e renamed Pretty.gen_list to Pretty.enum;
wenzelm
parents: 18756
diff changeset
   431
      Pretty.enum " |" "" "" (
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   432
        map (fn (ps, body) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   433
          Pretty.block [
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   434
            Pretty.enum "," "[" "]" (map pretty_iexpr ps),
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   435
            Pretty.str " |->",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   436
            Pretty.brk 1,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   437
            pretty_iexpr body,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   438
            Pretty.str "::",
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   439
            pretty_sortcontext sortctxt,
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   440
            Pretty.str "/",
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   441
            pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   442
          ]) eqs
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   443
        )
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   444
  | pretty_def (Typesyn (vs, ty)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   445
      Pretty.block [
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   446
        pretty_sortcontext vs,
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   447
        Pretty.str " |=> ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   448
        pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   449
      ]
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   450
  | pretty_def (Datatype (vs, cs)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   451
      Pretty.block [
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   452
        pretty_sortcontext vs,
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   453
        Pretty.str " |=> ",
18852
f1e2602ca7ba replaced gen_list by enum
haftmann
parents: 18850
diff changeset
   454
        Pretty.enum " |" "" ""
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   455
          (map (fn (c, tys) => (Pretty.block o Pretty.breaks)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   456
            (Pretty.str c :: map pretty_itype tys)) cs)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   457
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   458
  | pretty_def (Datatypecons dtname) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   459
      Pretty.str ("cons " ^ dtname)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   460
  | pretty_def (Class (supcls, (v, mems))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   461
      Pretty.block [
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   462
        Pretty.str ("class var " ^ v ^ "extending "),
18812
a4554848b59e renamed Pretty.gen_list to Pretty.enum;
wenzelm
parents: 18756
diff changeset
   463
        Pretty.enum "," "[" "]" (map Pretty.str supcls),
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   464
        Pretty.str " with ",
18852
f1e2602ca7ba replaced gen_list by enum
haftmann
parents: 18850
diff changeset
   465
        Pretty.enum "," "[" "]"
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   466
          (map (fn (m, (_, ty)) => Pretty.block
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   467
            [Pretty.str (m ^ "::"), pretty_itype ty]) mems)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   468
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   469
  | pretty_def (Classmember clsname) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   470
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   471
        Pretty.str "class member belonging to ",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   472
        Pretty.str clsname
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   473
      ]
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   474
  | pretty_def (Classinst (((clsname, (tyco, arity)), _), _)) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   475
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   476
        Pretty.str "class instance (",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   477
        Pretty.str clsname,
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   478
        Pretty.str ", (",
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   479
        Pretty.str tyco,
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   480
        Pretty.str ", ",
18852
f1e2602ca7ba replaced gen_list by enum
haftmann
parents: 18850
diff changeset
   481
        Pretty.enum "," "[" "]" (map (Pretty.enum "," "{" "}" o
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   482
          map Pretty.str o snd) arity),
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   483
        Pretty.str "))"
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   484
      ]
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   485
  | pretty_def Classinstmember =
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   486
      Pretty.str "class instance member";
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   487
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   488
fun pretty_module modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   489
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   490
    fun pretty (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   491
          Pretty.block (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   492
            Pretty.str ("module " ^ name ^ " {")
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   493
            :: Pretty.brk 1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   494
            :: Pretty.chunks (map pretty (AList.make (Graph.get_node modl)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
   495
                 (Graph.strong_conn modl |> flat |> rev)))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   496
            :: Pretty.str "}" :: nil
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   497
          )
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   498
      | pretty (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   499
          Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def]
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   500
  in pretty ("//", Module modl) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   501
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   502
fun pretty_deps modl =
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   503
  let
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   504
    fun one_node key =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   505
      let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   506
        val preds_ = Graph.imm_preds modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   507
        val succs_ = Graph.imm_succs modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   508
        val mutbs = gen_inter (op =) (preds_, succs_);
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
   509
        val preds = subtract (op =) mutbs preds_;
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
   510
        val succs = subtract (op =) mutbs succs_;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   511
      in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   512
        (Pretty.block o Pretty.fbreaks) (
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   513
          Pretty.str key
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   514
          :: map (fn s => Pretty.str ("<-> " ^ s)) mutbs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   515
          @ map (fn s => Pretty.str ("<-- " ^ s)) preds
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   516
          @ map (fn s => Pretty.str ("--> " ^ s)) succs
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   517
          @ (the_list oo Option.mapPartial)
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   518
            ((fn Module modl' => SOME (pretty_deps modl')
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   519
               | _ => NONE) o Graph.get_node modl) (SOME key)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   520
        )
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   521
      end
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   522
  in
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   523
    modl
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   524
    |> Graph.strong_conn
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
   525
    |> flat
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   526
    |> rev
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   527
    |> map one_node
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   528
    |> Pretty.chunks
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   529
  end;
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   530
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   531
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   532
(* name handling *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   533
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   534
fun dest_name name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   535
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   536
    val name' = NameSpace.unpack name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   537
    val (name'', name_base) = split_last name'
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   538
    val (modl, shallow) = split_last name''
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   539
  in (modl, NameSpace.pack [shallow, name_base]) end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   540
  handle Empty => error ("not a qualified name: " ^ quote name);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   541
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   542
fun has_nsp name shallow =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   543
  NameSpace.is_qualified name
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   544
  andalso let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   545
    val name' = NameSpace.unpack name
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   546
    val (name'', _) = split_last name'
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   547
    val (_, shallow') = split_last name''
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   548
  in shallow' = shallow end;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   549
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   550
fun dest_modl (Module m) = m;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   551
fun dest_def (Def d) = d;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   552
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   553
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   554
(* modules *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   555
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   556
val empty_module = Graph.empty; (*read: "depends on"*)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   557
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   558
fun get_def modl name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   559
  case dest_name name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   560
   of (modlname, base) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   561
        let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   562
          fun get (Module node) [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   563
                (dest_def o Graph.get_node node) base
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   564
            | get (Module node) (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   565
                get (Graph.get_node node m) ms
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   566
        in get (Module modl) modlname end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   567
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   568
fun add_def (name, def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   569
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   570
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   571
    fun add [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   572
          Graph.new_node (base, Def def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   573
      | add (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   574
          Graph.default_node (m, Module empty_module)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   575
          #> Graph.map_node m (Module o add ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   576
  in add modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   577
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   578
fun add_dep (name1, name2) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   579
  if name1 = name2 then modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   580
  else
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   581
    let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   582
      val m1 = dest_name name1 |> apsnd single |> (op @);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   583
      val m2 = dest_name name2 |> apsnd single |> (op @);
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   584
      val (ms, (r1, r2)) = get_prefix (op =) (m1, m2);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   585
      val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   586
      val add_edge =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   587
        if null r1 andalso null r2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   588
        then Graph.add_edge
19785
52d71ee5c8a8 bugfixes
haftmann
parents: 19616
diff changeset
   589
        else fn edge => fn gr => (Graph.add_edge_acyclic edge gr
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   590
          handle Graph.CYCLES _ => error ("adding dependency "
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   591
            ^ quote name1 ^ " -> " ^ quote name2 ^ " would result in module dependency cycle"))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   592
      fun add [] node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   593
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   594
            |> add_edge (s1, s2)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   595
        | add (m::ms) node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   596
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   597
            |> Graph.map_node m (Module o add ms o dest_modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   598
    in add ms modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   599
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   600
fun map_def name f =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   601
  let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   602
    val (modl, base) = dest_name name;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   603
    fun mapp [] =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   604
          Graph.map_node base (Def o f o dest_def)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   605
      | mapp (m::ms) =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   606
          Graph.map_node m (Module o mapp ms o dest_modl)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   607
  in mapp modl end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   608
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   609
fun map_defs f =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   610
  let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   611
    fun mapp (Def def) =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   612
          (Def o f) def
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   613
      | mapp (Module modl) =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   614
          (Module o Graph.map_nodes mapp) modl
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   615
  in dest_modl o mapp o Module end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   616
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   617
fun fold_defs f =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   618
  let
19616
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   619
    fun fol prfix (name, (Def def, _)) =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   620
          f (NameSpace.pack (prfix @ [name]), def)
19616
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   621
      | fol prfix (name, (Module modl, _)) =
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   622
          Graph.fold (fol (prfix @ [name])) modl
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   623
  in Graph.fold (fol []) end;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   624
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   625
fun add_deps f modl =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   626
  modl
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   627
  |> fold add_dep ([] |> fold_defs (append o f) modl);
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   628
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   629
fun add_def_incr (name, Undef) module =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   630
      (case try (get_def module) name
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   631
       of NONE => (error "attempted to add Undef to module")
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   632
        | SOME Undef => (error "attempted to add Undef to module")
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   633
        | SOME def' => map_def name (K def') module)
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   634
  | add_def_incr (name, def) module =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   635
      (case try (get_def module) name
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   636
       of NONE => add_def (name, def) module
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   637
        | SOME Undef => map_def name (K def) module
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   638
        | SOME def' => if eq_def (def, def')
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   639
            then module
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   640
            else error ("tried to overwrite definition " ^ name));
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   641
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   642
fun add_prim name (target, primdef as _::_) =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   643
  let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   644
    val (modl, base) = dest_name name;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   645
    fun add [] module =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   646
          (case try (Graph.get_node module) base
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   647
           of NONE =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   648
                module
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   649
                |> Graph.new_node (base, (Def o Prim) [(target, primdef)])
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   650
            | SOME (Def (Prim prim)) =>
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   651
                if AList.defined (op =) prim target
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   652
                then error ("already primitive definition (" ^ target
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   653
                  ^ ") present for " ^ name)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   654
                else
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   655
                  module
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   656
                  |> Graph.map_node base ((K o Def o Prim) (AList.update (op =)
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   657
                       (target, primdef) prim))
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   658
            | _ => error ("already non-primitive definition present for " ^ name))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   659
      | add (m::ms) module =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   660
          module
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   661
          |> Graph.default_node (m, Module empty_module)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   662
          |> Graph.map_node m (Module o add ms o dest_modl)
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   663
  in add modl end;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   664
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   665
fun ensure_prim name target =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   666
  let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   667
    val (modl, base) = dest_name name;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   668
    fun ensure [] module =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   669
          (case try (Graph.get_node module) base
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   670
           of NONE =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   671
                module
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   672
                |> Graph.new_node (base, (Def o Prim) [(target, [])])
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   673
            | SOME (Def (Prim prim)) =>
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   674
                module
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   675
                |> Graph.map_node base ((K o Def o Prim) (AList.default (op =)
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   676
                     (target, []) prim))
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   677
            | _ => module)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   678
      | ensure (m::ms) module =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   679
          module
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   680
          |> Graph.default_node (m, Module empty_module)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   681
          |> Graph.map_node m (Module o ensure ms o dest_modl)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   682
  in ensure modl end;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   683
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   684
fun merge_module modl12 =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   685
  let
19025
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   686
    fun join_module _ (Module m1, Module m2) =
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   687
          Module (merge_module (m1, m2))
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   688
      | join_module name (Def d1, Def d2) =
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   689
          if eq_def (d1, d2) then Def d1 else Def d1 (*raise Graph.DUP name*)
19025
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   690
      | join_module name _ = raise Graph.DUP name
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   691
  in Graph.join join_module modl12 end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   692
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   693
fun diff_module modl12 =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   694
  let
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   695
    fun diff_entry prefix modl2 (name, Def def1) = 
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   696
          let
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   697
            val e2 = try (Graph.get_node modl2) name
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   698
          in if is_some e2 andalso eq_def (def1, (dest_def o the) e2)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   699
            then I
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   700
            else cons (NameSpace.pack (prefix @ [name]), def1)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   701
          end
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   702
      | diff_entry prefix modl2 (name, Module modl1) =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   703
          diff_modl (prefix @ [name]) (modl1,
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   704
            (the_default empty_module o Option.map dest_modl o try (Graph.get_node modl2)) name)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   705
    and diff_modl prefix (modl1, modl2) =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   706
      fold (diff_entry prefix modl2)
19466
wenzelm
parents: 19341
diff changeset
   707
        ((AList.make (Graph.get_node modl1) o flat o Graph.strong_conn) modl1)
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   708
  in diff_modl [] modl12 [] end;
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   709
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   710
local 
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   711
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   712
fun project_trans f names modl =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   713
  let
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   714
    datatype pathnode = PN of (string list * (string * pathnode) list);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   715
    fun mk_ipath ([], base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   716
          PN (base :: defs, modls)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   717
      | mk_ipath (n::ns, base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   718
          modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   719
          |> AList.default (op =) (n, PN ([], []))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   720
          |> AList.map_entry (op =) n (mk_ipath (ns, base))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   721
          |> (pair defs #> PN);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   722
    fun select (PN (defs, modls)) (Module module) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   723
      module
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   724
      |> f (Graph.all_succs module (defs @ map fst modls))
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   725
      |> fold (fn (name, modls) => Graph.map_node name (select modls)) modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   726
      |> Module;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   727
  in
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   728
    Module modl
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   729
    |> select (fold (mk_ipath o dest_name)
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   730
         (filter NameSpace.is_qualified names) (PN ([], [])))
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   731
    |> dest_modl
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   732
  end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   733
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   734
in
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   735
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   736
val project_module = project_trans Graph.subgraph;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   737
val purge_module = project_trans Graph.del_nodes;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   738
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   739
end; (*local*)
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   740
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   741
fun imports_of modl name =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   742
  let
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   743
    (*fun submodules prfx modl =
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   744
      cons prfx
19616
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   745
      #> Graph.fold
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   746
          (fn (m, (Module modl, _)) => submodules (prfx @ [m]) modl
2545e8ab59a5 replaced Graph.fold_nodes by general Graph.fold;
wenzelm
parents: 19607
diff changeset
   747
            | (_, (Def _, _)) => I) modl;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   748
    fun get_modl name =
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   749
      fold (fn n => fn modl => (dest_modl oo Graph.get_node) modl n) name modl*)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   750
    fun imports prfx [] modl =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   751
          []
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   752
      | imports prfx (m::ms) modl =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   753
          map (cons m) (imports (prfx @ [m]) ms ((dest_modl oo Graph.get_node) modl m))
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   754
          @ map single (Graph.imm_succs modl m)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   755
  in
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   756
    modl
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   757
    |> imports [] name 
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   758
    (*|> cons name
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   759
    |> map (fn name => submodules name (get_modl name) [])
19466
wenzelm
parents: 19341
diff changeset
   760
    |> flat
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   761
    |> remove (op =) name*)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   762
    |> map NameSpace.pack
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   763
  end;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   764
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   765
fun check_samemodule names =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   766
  fold (fn name =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   767
    let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   768
      val modn = (fst o dest_name) name
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   769
    in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   770
     fn NONE => SOME modn
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   771
      | SOME mod' => if modn = mod' then SOME modn
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   772
          else error "inconsistent name prefix for simultanous names"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   773
    end
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   774
  ) names NONE;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   775
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   776
fun check_funeqs eqs =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   777
  (fold (fn (pats, _) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   778
    let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   779
      val l = length pats
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   780
    in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   781
     fn NONE => SOME l
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   782
      | SOME l' => if l = l' then SOME l
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   783
          else error "function definition with different number of arguments"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   784
    end
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   785
  ) eqs NONE; eqs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   786
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   787
fun check_prep_def modl Undef =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   788
      Undef
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   789
  | check_prep_def modl (d as Prim _) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   790
      d
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   791
  | check_prep_def modl (Fun (eqs, d)) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   792
      Fun (check_funeqs eqs, d)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   793
  | check_prep_def modl (d as Typesyn _) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   794
      d
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   795
  | check_prep_def modl (d as Datatype _) =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   796
      d
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   797
  | check_prep_def modl (Datatypecons dtco) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   798
      error "attempted to add bare datatype constructor"
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   799
  | check_prep_def modl (d as Class _) =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   800
      d
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   801
  | check_prep_def modl (Classmember _) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   802
      error "attempted to add bare class member"
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   803
  | check_prep_def modl (Classinst ((d as ((class, (tyco, arity)), _), memdefs))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   804
      let
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   805
        val Class (_, (v, membrs)) = get_def modl class;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   806
        val _ = if length memdefs > length memdefs
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   807
          then error "too many member definitions given"
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   808
          else ();
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   809
        fun instant (w, ty) v =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   810
          if v = w then ty else ITyVar v;
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   811
        fun mk_memdef (m, (sortctxt, ty)) =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   812
          case AList.lookup (op =) memdefs m
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   813
           of NONE => error ("missing definition for member " ^ quote m)
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19215
diff changeset
   814
            | SOME ((m', (eqs, (sortctxt', ty'))), lss) =>
19215
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   815
                let
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   816
                  val sortctxt'' = sortctxt |> fold (fn v_sort => AList.update (op =) v_sort) arity;
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   817
                  val ty'' = instant_itype (instant (v, tyco `%% map (ITyVar o fst) arity)) ty;
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   818
                in if eq_ityp ((sortctxt'', ty''), (sortctxt', ty'))
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19215
diff changeset
   819
                then (m, ((m', (check_funeqs eqs, (sortctxt', ty'))), lss))
19215
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   820
                else
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   821
                  error ("inconsistent type for member definition " ^ quote m ^ " [" ^ v ^ "]: "
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   822
                    ^ (Pretty.output o Pretty.block o Pretty.breaks) [
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   823
                      pretty_sortcontext sortctxt'',
19215
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   824
                      Pretty.str "|=>",
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   825
                      pretty_itype ty''
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   826
                    ] ^ " vs. " ^ (Pretty.output o Pretty.block o Pretty.breaks) [
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   827
                      pretty_sortcontext sortctxt',
19215
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   828
                      Pretty.str "|=>",
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   829
                      pretty_itype ty'
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   830
                    ]
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   831
                  )
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   832
                end
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   833
      in Classinst (d, map mk_memdef membrs) end
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   834
  | check_prep_def modl Classinstmember =
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   835
      error "attempted to add bare class instance member";
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   836
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   837
fun postprocess_def (name, Datatype (_, constrs)) =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   838
      (check_samemodule (name :: map fst constrs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   839
      fold (fn (co, _) =>
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   840
        add_def_incr (co, Datatypecons name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   841
        #> add_dep (co, name)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   842
        #> add_dep (name, co)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   843
      ) constrs
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   844
      )
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   845
  | postprocess_def (name, Class (_, (_, membrs))) =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   846
      (check_samemodule (name :: map fst membrs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   847
      fold (fn (m, _) =>
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   848
        add_def_incr (m, Classmember name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   849
        #> add_dep (m, name)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   850
        #> add_dep (name, m)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   851
      ) membrs
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   852
      )
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   853
  | postprocess_def (name, Classinst (_, memdefs)) =
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19215
diff changeset
   854
      (check_samemodule (name :: map (fst o fst o snd) memdefs);
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19215
diff changeset
   855
      fold (fn (_, ((m', _), _)) =>
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   856
        add_def_incr (m', Classinstmember)
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   857
      ) memdefs
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
   858
      )
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   859
  | postprocess_def _ =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   860
      I;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   861
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   862
fun succeed some (_, modl) = (Succeed some, modl);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   863
fun fail msg (_, modl) = (Fail ([msg], NONE), modl);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   864
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   865
fun check_fail _ (Succeed dst, trns) = (dst, trns)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   866
  | check_fail msg (Fail (msgs, e), _) = raise FAIL (msg::msgs, e);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   867
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   868
fun select_generator _ src [] modl =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   869
      (SOME src, modl) |> fail ("no code generator available")
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   870
  | select_generator mk_msg src gens modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   871
      let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   872
        fun handle_fail msgs f =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   873
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   874
            in
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   875
              if ! soft_exc
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   876
              then
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   877
                (SOME src, modl) |> f
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   878
                handle FAIL exc => (Fail exc, modl)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   879
                     | e => (Fail (msgs, SOME e), modl)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   880
              else
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   881
                (SOME src, modl) |> f
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   882
                handle FAIL exc => (Fail exc, modl)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   883
            end;
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   884
        fun select msgs [(gname, gen)] =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   885
              handle_fail (msgs @ [mk_msg gname]) (gen src)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   886
          | select msgs ((gname, gen)::gens) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   887
              let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   888
                val msgs' = msgs @ [mk_msg gname]
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   889
              in case handle_fail msgs' (gen src)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   890
               of (Fail (_, NONE), _) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   891
                   select msgs' gens
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   892
               | result => result
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   893
          end;
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   894
      in select [] gens end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   895
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   896
fun ensure_def defgens msg name (dep, modl) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   897
  let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   898
    val msg' = case dep
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   899
     of NONE => msg
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   900
      | SOME dep => msg ^ ", with dependency " ^ quote dep;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   901
    fun add_dp NONE = I
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   902
      | add_dp (SOME dep) =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   903
          debug_msg (fn _ => "adding dependency " ^ quote dep ^ " -> " ^ quote name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   904
          #> add_dep (dep, name);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   905
    fun prep_def def modl =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   906
      (check_prep_def modl def, modl);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   907
  in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   908
    modl
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   909
    |> (if can (get_def modl) name
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   910
        then
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   911
          debug_msg (fn _ => "asserting node " ^ quote name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   912
          #> add_dp dep
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   913
        else
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   914
          debug_msg (fn _ => "allocating node " ^ quote name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   915
          #> add_def (name, Undef)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   916
          #> add_dp dep
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   917
          #> debug_msg (fn _ => "creating node " ^ quote name)
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   918
          #> select_generator (fn gname => "trying code generator "
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   919
               ^ gname ^ " for definition of " ^ quote name) name defgens
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   920
          #> debug_msg (fn _ => "checking creation of node " ^ quote name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   921
          #> check_fail msg'
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   922
          #-> (fn def => prep_def def)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   923
          #-> (fn def =>
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   924
             debug_msg (fn _ => "addition of " ^ name ^ " := " ^ (Pretty.output o pretty_def) def)
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   925
          #> debug_msg (fn _ => "adding")
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
   926
          #> add_def_incr (name, def)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   927
          #> debug_msg (fn _ => "postprocessing")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   928
          #> postprocess_def (name, def)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   929
          #> debug_msg (fn _ => "adding done")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   930
       ))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   931
    |> pair dep
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   932
  end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   933
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   934
fun start_transact init f modl =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   935
  let
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   936
    fun handle_fail f x =
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   937
      (f x
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   938
      handle FAIL (msgs, NONE) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   939
        (error o cat_lines) ("code generation failed, while:" :: msgs))
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   940
      handle FAIL (msgs, SOME e) =>
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   941
        ((Output.error_msg o cat_lines) ("code generation failed, while:" :: msgs); raise e);
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   942
  in
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   943
    (init, modl)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   944
    |> handle_fail f
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   945
    |-> (fn x => fn (_, module) => (x, module))
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   946
  end;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   947
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   948
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   949
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   950
(** generic serialization **)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   951
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   952
(* resolving *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   953
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   954
structure NameMangler = NameManglerFun (
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   955
  type ctxt = (string * string -> string) * (string -> string option);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   956
  type src = string * string;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   957
  val ord = prod_ord string_ord string_ord;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   958
  fun mk (postprocess, validate) ((shallow, name), 0) =
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   959
        let
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   960
          val name' = postprocess (shallow, name);
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   961
        in case validate name'
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   962
         of NONE => name'
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   963
          | _ => mk (postprocess, validate) ((shallow, name), 1)
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   964
        end
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   965
    | mk (postprocess, validate) (("", name), i) =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   966
        postprocess ("", name ^ replicate_string i "'")
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   967
        |> perhaps validate
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   968
    | mk (postprocess, validate) ((shallow, name), 1) =
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   969
        postprocess (shallow, shallow ^ "_" ^ name)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   970
        |> perhaps validate
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   971
    | mk (postprocess, validate) ((shallow, name), i) =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   972
        postprocess (shallow, name ^ replicate_string i "'")
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   973
        |> perhaps validate;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   974
  fun is_valid _ _ = true;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   975
  fun maybe_unique _ _ = NONE;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   976
  fun re_mangle _ dst = error ("no such definition name: " ^ quote dst);
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   977
);
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   978
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   979
fun mk_deresolver module nsp_conn postprocess validate =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   980
  let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   981
    datatype tabnode = N of string * tabnode Symtab.table option;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   982
    fun mk module manglers tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   983
      let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   984
        fun mk_name name =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   985
          case NameSpace.unpack name
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   986
           of [n] => ("", n)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   987
            | [s, n] => (s, n);
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   988
        fun in_conn (shallow, conn) =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   989
          member (op = : string * string -> bool) conn shallow;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   990
        fun add_name name =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   991
          let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   992
            val n as (shallow, _) = mk_name name;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   993
          in
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   994
            AList.map_entry_yield in_conn shallow (
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   995
              NameMangler.declare (postprocess, validate) n
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   996
              #-> (fn n' => pair (name, n'))
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   997
            ) #> apfst the
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   998
          end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   999
        val (renamings, manglers') =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1000
          fold_map add_name (Graph.keys module) manglers;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1001
        fun extend_tab (n, n') =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1002
          if (length o NameSpace.unpack) n = 1
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1003
          then
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1004
            Symtab.update_new
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1005
              (n, N (n', SOME (mk ((dest_modl o Graph.get_node module) n) manglers' Symtab.empty)))
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1006
          else
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1007
            Symtab.update_new (n, N (n', NONE));
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1008
      in fold extend_tab renamings tab end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1009
    fun get_path_name [] tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1010
          ([], SOME tab)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1011
      | get_path_name [p] tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1012
          let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1013
            val SOME (N (p', tab')) = Symtab.lookup tab p
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1014
          in ([p'], tab') end
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1015
      | get_path_name [p1, p2] tab =
18919
haftmann
parents: 18918
diff changeset
  1016
          (case Symtab.lookup tab p1
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1017
           of SOME (N (p', SOME tab')) => 
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1018
                let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1019
                  val (ps', tab'') = get_path_name [p2] tab'
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1020
                in (p' :: ps', tab'') end
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1021
            | NONE =>
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1022
                let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1023
                  val SOME (N (p', NONE)) = Symtab.lookup tab (NameSpace.pack [p1, p2])
18919
haftmann
parents: 18918
diff changeset
  1024
                in ([p'], NONE) end)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1025
      | get_path_name (p::ps) tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1026
          let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1027
            val SOME (N (p', SOME tab')) = Symtab.lookup tab p
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1028
            val (ps', tab'') = get_path_name ps tab'
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1029
          in (p' :: ps', tab'') end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1030
    fun deresolv tab prefix name =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1031
      let
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1032
        val (common, (_, rem)) = get_prefix (op =) (prefix, NameSpace.unpack name);
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1033
        val (_, SOME tab') = get_path_name common tab;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1034
        val (name', _) = get_path_name rem tab';
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1035
      in NameSpace.pack name' end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1036
  in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1037
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1038
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1039
(* serialization *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1040
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1041
fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module =
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1042
  let
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1043
    val resolver = mk_deresolver module nsp_conn postprocess validate;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1044
    fun sresolver s = (resolver o NameSpace.unpack) s
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1045
    fun mk_name prfx name =
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1046
      let
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1047
        val name_qual = NameSpace.pack (prfx @ [name])
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1048
      in (name_qual, resolver prfx name_qual) end;
18756
5eb3df798405 more general serializer
haftmann
parents: 18702
diff changeset
  1049
    fun mk_contents prfx module =
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
  1050
      map_filter (seri prfx)
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1051
        ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)
18756
5eb3df798405 more general serializer
haftmann
parents: 18702
diff changeset
  1052
    and seri prfx ([(name, Module modl)]) =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1053
          seri_module (resolver []) (map (resolver []) (imports_of module (prfx @ [name])))
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1054
            (mk_name prfx name, mk_contents (prfx @ [name]) modl)
18756
5eb3df798405 more general serializer
haftmann
parents: 18702
diff changeset
  1055
      | seri prfx ds =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1056
          seri_defs sresolver (NameSpace.pack prfx)
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
  1057
            (map (fn (name, Def def) => (fst (mk_name prfx name), def)) ds)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1058
  in
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1059
    seri_module (resolver []) (imports_of module [])
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
  1060
      (*map (resolver []) (Graph.strong_conn module |> flat |> rev)*)
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1061
      (("", name_root), (mk_contents [] module))
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1062
  end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1063
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1064
end; (* struct *)
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
  1065
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
  1066
structure BasicCodegenThingol: BASIC_CODEGEN_THINGOL = CodegenThingol;