src/Tools/code/code_thingol.ML
author haftmann
Thu, 04 Oct 2007 19:41:51 +0200
changeset 24837 cacc5744be75
parent 24811 3bf788a0c49a
child 24918 22013215eece
permissions -rw-r--r--
clarified terminology
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_thingol.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Intermediate language ("Thin-gol") representing executable code.
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
infix 8 `%%;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
infixr 6 `->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
infixr 6 `-->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
infix 4 `$;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    12
infix 4 `$$;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    13
infixr 3 `|->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    14
infixr 3 `|-->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    15
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
signature BASIC_CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    18
  type vname = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
  datatype dict =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
      DictConst of string * dict list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
    | DictVar of string list * (vname * (int * int));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
  datatype itype =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
      `%% of string * itype list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
    | ITyVar of vname;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    25
  type const = string * (dict list list * itype list (*types of arguments*))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    26
  datatype iterm =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    27
      IConst of const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    28
    | IVar of vname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
    | `$ of iterm * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
    | `|-> of (vname * itype) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
    | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
        (*((term, type), [(selector pattern, body term )]), primitive term)*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
  val `-> : itype * itype -> itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  val `--> : itype list * itype -> itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
  val `$$ : iterm * iterm list -> iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
  val `|--> : (vname * itype) list * iterm -> iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
  type typscheme = (vname * sort) list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
signature CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
  include BASIC_CODE_THINGOL;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
  val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
  val unfold_fun: itype -> itype list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
  val unfold_app: iterm -> iterm * iterm list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
  val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
  val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
  val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
  val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
  val unfold_const_app: iterm ->
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
    ((string * (dict list list * itype list)) * iterm list) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
  val collapse_let: ((vname * itype) * iterm) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
    -> (iterm * itype) * (iterm * iterm) list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
  val eta_expand: (string * (dict list list * itype list)) * iterm list -> int -> iterm;
24662
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
    56
  val contains_dictvar: iterm -> bool;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
  val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
  val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
  val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
  datatype def =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
      Bot
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    63
    | Fun of typscheme * ((iterm list * iterm) * thm) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
    | Datatype of (vname * sort) list * (string * itype list) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
    | Datatypecons of string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
    66
    | Class of vname * ((class * string) list * (string * itype) list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
    | Classrel of class * class
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
    68
    | Classparam of class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
    | Classinst of (class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
          * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    71
        * ((string * const) * thm) list);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
  type code = def Graph.T;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
  type transact;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
  val empty_code: code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
  val merge_code: code * code -> code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
  val project_code: bool (*delete empty funs*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
    -> string list (*hidden*) -> string list option (*selected*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
    -> code -> code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
  val empty_funs: code -> string list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
  val is_cons: code -> string -> bool;
24283
haftmann
parents: 24252
diff changeset
    81
  val add_eval_def: string (*bind name*) * (iterm * itype) -> code -> code;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
24252
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
    83
  val ensure_def: (string -> string) -> (transact -> def * transact) -> string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
    84
    -> transact -> transact;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
  val start_transact: (transact -> 'a * transact) -> code -> 'a * code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
structure CodeThingol: CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
(** auxiliary **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
fun unfoldl dest x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
  case dest x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
   of NONE => (x, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
    | SOME (x1, x2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
fun unfoldr dest x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
  case dest x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
   of NONE => ([], x)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
    | SOME (x1, x2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
(** language core - types, pattern, expressions **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
(* language representation *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
type vname = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
datatype dict =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
    DictConst of string * dict list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
  | DictVar of string list * (vname * (int * int));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
datatype itype =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
    `%% of string * itype list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
  | ITyVar of vname;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   120
type const = string * (dict list list * itype list (*types of arguments*))
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   121
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
datatype iterm =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   123
    IConst of const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
  | IVar of vname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
  | `$ of iterm * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
  | `|-> of (vname * itype) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
  | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
    (*see also signature*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
(*
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
  variable naming conventions
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
  bare names:
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
    variable names          v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
    class names             class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
    type constructor names  tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
    datatype names          dtco
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   138
    const names (general)   c (const)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
    constructor names       co
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   140
    class parameter names   classparam
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
    arbitrary name          s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   143
    v, c, co, classparam also annotated with types etc.
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
  constructs:
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
    sort                    sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
    type parameters         vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
    type                    ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
    type schemes            tysm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
    term                    t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
    (term as pattern)       p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
    instance (class, tyco)  inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
 *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
val op `--> = Library.foldr (op `->);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
val op `$$ = Library.foldl (op `$);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
val op `|--> = Library.foldr (op `|->);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
val unfold_fun = unfoldr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
  (fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
val unfold_app = unfoldl
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
  (fn op `$ t => SOME t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
val split_abs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
  (fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
        if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
    | (v, ty) `|-> t => SOME (((v, NONE), ty), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
val unfold_abs = unfoldr split_abs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
val split_let = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
  (fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
val unfold_let = unfoldr split_let;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
fun unfold_const_app t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
 case unfold_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
  of (IConst c, ts) => SOME (c, ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
   | _ => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
fun fold_aiterms f (t as IConst _) = f t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
  | fold_aiterms f (t as IVar _) = f t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
  | fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
  | fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
  | fold_aiterms f (ICase (_, t)) = fold_aiterms f t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
fun fold_constnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
    fun add (IConst (c, _)) = f c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
      | add _ = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
  in fold_aiterms add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
fun fold_varnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
    fun add (IVar v) = f v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
      | add ((v, _) `|-> _) = f v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
      | add _ = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
  in fold_aiterms add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
fun fold_unbound_varnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
    fun add _ (IConst _) = I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
      | add vs (IVar v) = if not (member (op =) vs v) then f v else I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
      | add vs (t1 `$ t2) = add vs t1 #> add vs t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
      | add vs ((v, _) `|-> t) = add (insert (op =) v vs) t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
      | add vs (ICase (_, t)) = add vs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
  in add [] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
        fun exists_v t = fold_unbound_varnames (fn w => fn b =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
          b orelse v = w) t false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
      in if v = w andalso forall (fn (t1, t2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
        exists_v t1 orelse not (exists_v t2)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
        then ((se, ty), ds)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
        else ((se, ty), [(IVar v, be)])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
  | collapse_let (((v, ty), se), be) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
      ((se, ty), [(IVar v, be)])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
fun eta_expand (c as (_, (_, tys)), ts) k =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
    val j = length ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
    val l = k - j;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
    val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
    val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
  in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
24662
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   235
fun contains_dictvar t =
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   236
  let
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   237
    fun contains (DictConst (_, dss)) = (fold o fold) contains dss
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   238
      | contains (DictVar _) = K true;
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   239
  in
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   240
    fold_aiterms
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   241
      (fn IConst (_, (dss, _)) => (fold o fold) contains dss | _ => I) t false
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   242
  end;
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   243
  
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   244
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
(** definitions, transactions **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
type typscheme = (vname * sort) list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
datatype def =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   249
    Bot
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   250
  | Fun of typscheme * ((iterm list * iterm) * thm) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
  | Datatype of (vname * sort) list * (string * itype list) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
  | Datatypecons of string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   253
  | Class of vname * ((class * string) list * (string * itype) list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
  | Classrel of class * class
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   255
  | Classparam of class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
  | Classinst of (class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   258
      * ((string * const) * thm) list);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
type code = def Graph.T;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   263
(* abstract code *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
val empty_code = Graph.empty : code; (*read: "depends on"*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
fun ensure_bot name = Graph.default_node (name, Bot);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
fun add_def_incr (name, Bot) code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
      (case the_default Bot (try (Graph.get_node code) name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
       of Bot => error "Attempted to add Bot to code"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
        | _ => code)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
  | add_def_incr (name, def) code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
      (case try (Graph.get_node code) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
       of NONE => Graph.new_node (name, def) code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
        | SOME Bot => Graph.map_node name (K def) code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
        | SOME _ => error ("Tried to overwrite definition " ^ quote name));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   279
fun add_dep (NONE, _) = I
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   280
  | add_dep (SOME name1, name2) =
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   281
      if name1 = name2 then I else Graph.add_edge (name1, name2);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
val merge_code : code * code -> code = Graph.merge (K true);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
fun project_code delete_empty_funs hidden raw_selected code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
    fun is_empty_fun name = case Graph.get_node code name
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24283
diff changeset
   288
     of Fun (_, []) => true
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
      | _ => false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
    val names = subtract (op =) hidden (Graph.keys code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
    val deleted = Graph.all_preds code (filter is_empty_fun names);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
    val selected = case raw_selected
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
     of NONE => names |> subtract (op =) deleted 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
      | SOME sel => sel
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
          |> delete_empty_funs ? subtract (op =) deleted
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
          |> subtract (op =) hidden
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
          |> Graph.all_succs code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
          |> delete_empty_funs ? subtract (op =) deleted
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
          |> subtract (op =) hidden;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
    code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
    |> Graph.subgraph (member (op =) selected)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
fun empty_funs code =
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24283
diff changeset
   306
  Graph.fold (fn (name, (Fun (_, []), _)) => cons name
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
               | _ => I) code [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
fun is_cons code name = case Graph.get_node code name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
 of Datatypecons _ => true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
  | _ => false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
(* transaction protocol *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
24283
haftmann
parents: 24252
diff changeset
   316
type transact = Graph.key option * code;
haftmann
parents: 24252
diff changeset
   317
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   318
fun ensure_def labelled_name defgen name (dep, code) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
    fun add_def false =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
          ensure_bot name
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   322
          #> add_dep (dep, name)
24252
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   323
          #> curry defgen (SOME name)
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   324
          ##> snd
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   325
          #-> (fn def => add_def_incr (name, def))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
      | add_def true =
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   327
          add_dep (dep, name);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
    code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
    |> add_def (can (Graph.get_node code) name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
    |> pair dep
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
fun start_transact f code =
24252
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   335
  (NONE, code)
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   336
  |> f
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   337
  |-> (fn x => fn (_, code) => (x, code));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
24283
haftmann
parents: 24252
diff changeset
   339
fun add_eval_def (name, (t, ty)) code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
  code
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   341
  |> Graph.new_node (name, Fun (([], ty), [(([], t), Drule.dummy_thm)]))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
  |> fold (curry Graph.add_edge name) (Graph.keys code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
end; (*struct*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
structure BasicCodeThingol: BASIC_CODE_THINGOL = CodeThingol;