src/Tools/code/code_thingol.ML
author wenzelm
Thu, 22 Nov 2007 14:51:34 +0100
changeset 25456 6f79698f294d
parent 25042 a33b78d63114
child 25485 33840a854e63
permissions -rw-r--r--
tuned;
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.
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
     6
Representation and translation.
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
infix 8 `%%;
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
infixr 6 `-->;
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
infix 4 `$$;
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
infixr 3 `|-->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
signature BASIC_CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    18
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
  type vname = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
  datatype dict =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
      DictConst of string * dict list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
    | DictVar of string list * (vname * (int * int));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  datatype itype =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
      `%% of string * itype list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
    | ITyVar of vname;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    26
  type const = string * (dict list list * itype list (*types of arguments*))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
  datatype iterm =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    28
      IConst of const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
    | IVar of vname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
    | `$ of iterm * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
    | `|-> of (vname * itype) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
    | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
        (*((term, type), [(selector pattern, body term )]), primitive term)*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  val `-> : itype * itype -> itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
  val `--> : itype list * itype -> itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
  val `$$ : iterm * iterm list -> iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
  val `|--> : (vname * itype) list * iterm -> iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
  type typscheme = (vname * sort) list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
signature CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  include BASIC_CODE_THINGOL;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
  val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
  val unfold_fun: itype -> itype list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
  val unfold_app: iterm -> iterm * iterm list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
  val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
  val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
  val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
  val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
  val unfold_const_app: iterm ->
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
    ((string * (dict list list * itype list)) * iterm list) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
  val collapse_let: ((vname * itype) * iterm) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
    -> (iterm * itype) * (iterm * iterm) list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
  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
    57
  val contains_dictvar: iterm -> bool;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
  val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
  val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
  val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    62
  datatype stmt =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
      Bot
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    64
    | Fun of typscheme * ((iterm list * iterm) * thm) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
    | Datatype of (vname * sort) list * (string * itype list) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
    | Datatypecons of string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
    67
    | Class of vname * ((class * string) list * (string * itype) list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
    | Classrel of class * class
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
    69
    | Classparam of class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
    | Classinst of (class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
          * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    72
        * ((string * const) * thm) list);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    73
  type code = stmt Graph.T;
24219
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;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    82
  type transact;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    83
  val ensure_const: theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    84
    -> CodeFuncgr.T -> string -> transact -> string * transact;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    85
  val ensure_value: theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    86
    -> CodeFuncgr.T -> term -> transact -> string * transact;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    87
  val add_value_stmt: iterm * itype -> code -> code;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    88
  val transact: (transact -> 'a * transact) -> code -> 'a * code;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
structure CodeThingol: CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
(** auxiliary **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
fun unfoldl dest x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
  case dest x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
   of NONE => (x, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
    | SOME (x1, x2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
fun unfoldr dest x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
  case dest x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
   of NONE => ([], x)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
    | SOME (x1, x2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
(** language core - types, pattern, expressions **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
(* language representation *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
type vname = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
datatype dict =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
    DictConst of string * dict list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
  | DictVar of string list * (vname * (int * int));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
datatype itype =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
    `%% of string * itype list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
  | ITyVar of vname;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   123
type const = string * (dict list list * itype list (*types of arguments*))
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   124
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
datatype iterm =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   126
    IConst of const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
  | IVar of vname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
  | `$ of iterm * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
  | `|-> of (vname * itype) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
  | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
    (*see also signature*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
(*
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
  variable naming conventions
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
  bare names:
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
    variable names          v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
    class names             class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
    type constructor names  tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
    datatype names          dtco
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   141
    const names (general)   c (const)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
    constructor names       co
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   143
    class parameter names   classparam
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
    arbitrary name          s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   146
    v, c, co, classparam also annotated with types etc.
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
  constructs:
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
    sort                    sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
    type parameters         vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
    type                    ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
    type schemes            tysm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
    term                    t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
    (term as pattern)       p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
    instance (class, tyco)  inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
 *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
val op `--> = Library.foldr (op `->);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
val op `$$ = Library.foldl (op `$);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
val op `|--> = Library.foldr (op `|->);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
val unfold_fun = unfoldr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
  (fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
val unfold_app = unfoldl
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
  (fn op `$ t => SOME t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
val split_abs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
  (fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
        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
   174
    | (v, ty) `|-> t => SOME (((v, NONE), ty), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
val unfold_abs = unfoldr split_abs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
val split_let = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
  (fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
val unfold_let = unfoldr split_let;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
fun unfold_const_app t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
 case unfold_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
  of (IConst c, ts) => SOME (c, ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
   | _ => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
fun fold_aiterms f (t as IConst _) = f t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
  | fold_aiterms f (t as IVar _) = f t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
  | fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
  | fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
  | fold_aiterms f (ICase (_, t)) = fold_aiterms f t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
fun fold_constnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
    fun add (IConst (c, _)) = f c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
      | add _ = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
  in fold_aiterms add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
fun fold_varnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
    fun add (IVar v) = f v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
      | add ((v, _) `|-> _) = f v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
      | add _ = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
  in fold_aiterms add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
fun fold_unbound_varnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
    fun add _ (IConst _) = I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
      | 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
   213
      | add vs (t1 `$ t2) = add vs t1 #> add vs t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
      | add vs ((v, _) `|-> t) = add (insert (op =) v vs) t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
      | add vs (ICase (_, t)) = add vs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
  in add [] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
        fun exists_v t = fold_unbound_varnames (fn w => fn b =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
          b orelse v = w) t false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
      in if v = w andalso forall (fn (t1, t2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
        exists_v t1 orelse not (exists_v t2)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
        then ((se, ty), ds)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
        else ((se, ty), [(IVar v, be)])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
  | collapse_let (((v, ty), se), be) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
      ((se, ty), [(IVar v, be)])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
fun eta_expand (c as (_, (_, tys)), ts) k =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
    val j = length ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
    val l = k - j;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
    val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
    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
   236
  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
   237
24662
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   238
fun contains_dictvar t =
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   239
  let
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   240
    fun contains (DictConst (_, dss)) = (fold o fold) contains dss
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   241
      | contains (DictVar _) = K true;
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   242
  in
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   243
    fold_aiterms
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   244
      (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
   245
  end;
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   246
  
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
(** definitions, transactions **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   249
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
type typscheme = (vname * sort) list * itype;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   251
datatype stmt =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
    Bot
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   253
  | Fun of typscheme * ((iterm list * iterm) * thm) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
  | Datatype of (vname * sort) list * (string * itype list) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
  | Datatypecons of string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   256
  | Class of vname * ((class * string) list * (string * itype) list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
  | Classrel of class * class
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   258
  | Classparam of class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
  | Classinst of (class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   261
      * ((string * const) * thm) list);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   263
type code = stmt Graph.T;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
(* abstract code *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
val empty_code = Graph.empty : code; (*read: "depends on"*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
fun ensure_bot name = Graph.default_node (name, Bot);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
fun add_def_incr (name, Bot) code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
      (case the_default Bot (try (Graph.get_node code) name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
       of Bot => error "Attempted to add Bot to code"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
        | _ => code)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
  | add_def_incr (name, def) code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
      (case try (Graph.get_node code) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
       of NONE => Graph.new_node (name, def) code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   279
        | SOME Bot => Graph.map_node name (K def) code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
        | SOME _ => error ("Tried to overwrite definition " ^ quote name));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   282
fun add_dep (NONE, _) = I
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   283
  | add_dep (SOME name1, name2) =
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   284
      if name1 = name2 then I else Graph.add_edge (name1, name2);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
val merge_code : code * code -> code = Graph.merge (K true);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
fun project_code delete_empty_funs hidden raw_selected code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
    fun is_empty_fun name = case Graph.get_node code name
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24283
diff changeset
   291
     of Fun (_, []) => true
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
      | _ => false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
    val names = subtract (op =) hidden (Graph.keys code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
    val deleted = Graph.all_preds code (filter is_empty_fun names);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
    val selected = case raw_selected
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
     of NONE => names |> subtract (op =) deleted 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
      | SOME sel => sel
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
          |> Graph.all_succs code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
          |> delete_empty_funs ? subtract (op =) deleted
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
          |> subtract (op =) hidden;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
    code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
    |> Graph.subgraph (member (op =) selected)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
fun empty_funs code =
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24283
diff changeset
   309
  Graph.fold (fn (name, (Fun (_, []), _)) => cons name
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
               | _ => I) code [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
fun is_cons code name = case Graph.get_node code name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
 of Datatypecons _ => true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
  | _ => false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
(* transaction protocol *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
24283
haftmann
parents: 24252
diff changeset
   319
type transact = Graph.key option * code;
haftmann
parents: 24252
diff changeset
   320
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   321
fun ensure_stmt stmtgen name (dep, code) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
    fun add_def false =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
          ensure_bot name
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   325
          #> add_dep (dep, name)
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   326
          #> curry stmtgen (SOME name)
24252
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   327
          ##> snd
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   328
          #-> (fn def => add_def_incr (name, def))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
      | add_def true =
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   330
          add_dep (dep, name);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
    code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
    |> add_def (can (Graph.get_node code) name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
    |> pair dep
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   335
    |> pair name
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   338
fun transact f code =
24252
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   339
  (NONE, code)
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   340
  |> f
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   341
  |-> (fn x => fn (_, code) => (x, code));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   343
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   344
(* translation kernel *)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   345
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   346
fun ensure_class thy (algbr as ((_, algebra), _)) funcgr class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   347
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   348
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24918
diff changeset
   349
    val cs = #params (AxClass.get_info thy class);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   350
    val class' = CodeName.class thy class;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   351
    val stmt_class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   352
      fold_map (fn superclass => ensure_class thy algbr funcgr superclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   353
        ##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   354
      ##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   355
        ##>> exprgen_typ thy algbr funcgr ty) cs
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24918
diff changeset
   356
      #>> (fn info => Class (unprefix "'" Name.aT, info))
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   357
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   358
    ensure_stmt stmt_class class'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   359
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   360
and ensure_classrel thy algbr funcgr (subclass, superclass) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   361
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   362
    val classrel' = CodeName.classrel thy (subclass, superclass);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   363
    val stmt_classrel =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   364
      ensure_class thy algbr funcgr subclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   365
      ##>> ensure_class thy algbr funcgr superclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   366
      #>> Classrel;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   367
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   368
    ensure_stmt stmt_classrel classrel'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   369
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   370
and ensure_tyco thy algbr funcgr "fun" =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   371
      pair "fun"
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   372
  | ensure_tyco thy algbr funcgr tyco =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   373
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   374
        val stmt_datatype =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   375
          let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   376
            val (vs, cos) = Code.get_datatype thy tyco;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   377
          in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   378
            fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   379
            ##>> fold_map (fn (c, tys) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   380
              ensure_const thy algbr funcgr c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   381
              ##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   382
            #>> Datatype
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   383
          end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   384
        val tyco' = CodeName.tyco thy tyco;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   385
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   386
        ensure_stmt stmt_datatype tyco'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   387
      end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   388
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   389
  fold_map (ensure_class thy algbr funcgr) (proj_sort sort)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   390
  #>> (fn sort => (unprefix "'" v, sort))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   391
and exprgen_typ thy algbr funcgr (TFree vs) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   392
      exprgen_tyvar_sort thy algbr funcgr vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   393
      #>> (fn (v, sort) => ITyVar v)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   394
  | exprgen_typ thy algbr funcgr (Type (tyco, tys)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   395
      ensure_tyco thy algbr funcgr tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   396
      ##>> fold_map (exprgen_typ thy algbr funcgr) tys
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   397
      #>> (fn (tyco, tys) => tyco `%% tys)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   398
and exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   399
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   400
    val pp = Sign.pp thy;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   401
    datatype typarg =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   402
        Global of (class * string) * typarg list list
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   403
      | Local of (class * class) list * (string * (int * sort));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   404
    fun class_relation (Global ((_, tyco), yss), _) class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   405
          Global ((class, tyco), yss)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   406
      | class_relation (Local (classrels, v), subclass) superclass =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   407
          Local ((subclass, superclass) :: classrels, v);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   408
    fun type_constructor tyco yss class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   409
      Global ((class, tyco), (map o map) fst yss);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   410
    fun type_variable (TFree (v, sort)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   411
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   412
        val sort' = proj_sort sort;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   413
      in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   414
    val typargs = Sorts.of_sort_derivation pp algebra
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   415
      {class_relation = class_relation, type_constructor = type_constructor,
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   416
       type_variable = type_variable}
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   417
      (ty_ctxt, proj_sort sort_decl);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   418
    fun mk_dict (Global (inst, yss)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   419
          ensure_inst thy algbr funcgr inst
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   420
          ##>> (fold_map o fold_map) mk_dict yss
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   421
          #>> (fn (inst, dss) => DictConst (inst, dss))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   422
      | mk_dict (Local (classrels, (v, (k, sort)))) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   423
          fold_map (ensure_classrel thy algbr funcgr) classrels
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   424
          #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   425
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   426
    fold_map mk_dict typargs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   427
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   428
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   429
  let
25042
a33b78d63114 renamed Consts.the_declaration to Consts.the_type;
wenzelm
parents: 24969
diff changeset
   430
    val ty_decl = Consts.the_type consts c;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   431
    val (tys, tys_decl) = pairself (curry (Consts.typargs consts) c) (ty_ctxt, ty_decl);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   432
    val sorts = map (snd o dest_TVar) tys_decl;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   433
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   434
    fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   435
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   436
and exprgen_eq thy algbr funcgr thm =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   437
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   438
    val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   439
      o Logic.unvarify o prop_of) thm;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   440
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   441
    fold_map (exprgen_term thy algbr funcgr) args
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   442
    ##>> exprgen_term thy algbr funcgr rhs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   443
    #>> rpair thm
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   444
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   445
and ensure_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   446
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   447
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24918
diff changeset
   448
    val classparams = these (try (#params o AxClass.get_info thy) class);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   449
    val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   450
    val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   451
    val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   452
      Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   453
    val arity_typ = Type (tyco, map TFree vs);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   454
    val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   455
    fun exprgen_superarity superclass =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   456
      ensure_class thy algbr funcgr superclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   457
      ##>> ensure_classrel thy algbr funcgr (class, superclass)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   458
      ##>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   459
      #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   460
            (superclass, (classrel, (inst, dss))));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   461
    fun exprgen_classparam_inst (c, ty) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   462
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   463
        val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   464
        val thm = Class.unoverload thy (Thm.cterm_of thy c_inst);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   465
        val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   466
          o Logic.dest_equals o Thm.prop_of) thm;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   467
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   468
        ensure_const thy algbr funcgr c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   469
        ##>> exprgen_const thy algbr funcgr c_ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   470
        #>> (fn (c, IConst c_inst) => ((c, c_inst), thm))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   471
      end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   472
    val stmt_inst =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   473
      ensure_class thy algbr funcgr class
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   474
      ##>> ensure_tyco thy algbr funcgr tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   475
      ##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   476
      ##>> fold_map exprgen_superarity superclasses
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   477
      ##>> fold_map exprgen_classparam_inst classparams
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   478
      #>> (fn ((((class, tyco), arity), superarities), classparams) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   479
             Classinst ((class, (tyco, arity)), (superarities, classparams)));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   480
    val inst = CodeName.instance thy (class, tyco);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   481
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   482
    ensure_stmt stmt_inst inst
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   483
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   484
and ensure_const thy (algbr as (_, consts)) funcgr c =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   485
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   486
    val c' = CodeName.const thy c;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   487
    fun stmt_datatypecons tyco =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   488
      ensure_tyco thy algbr funcgr tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   489
      #>> K (Datatypecons c');
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   490
    fun stmt_classparam class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   491
      ensure_class thy algbr funcgr class
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   492
      #>> K (Classparam c');
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   493
    fun stmt_fun trns =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   494
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   495
        val raw_thms = CodeFuncgr.funcs funcgr c;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   496
        val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   497
        val vs = (map dest_TFree o Consts.typargs consts) (c, ty);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   498
        val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   499
          then raw_thms
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   500
          else map (CodeUnit.expand_eta 1) raw_thms;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   501
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   502
        trns
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   503
        |> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   504
        ||>> exprgen_typ thy algbr funcgr ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   505
        ||>> fold_map (exprgen_eq thy algbr funcgr) thms
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   506
        |>> (fn ((vs, ty), eqs) => Fun ((vs, ty), eqs))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   507
      end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   508
    val stmtgen = case Code.get_datatype_of_constr thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   509
     of SOME tyco => stmt_datatypecons tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   510
      | NONE => (case AxClass.class_of_param thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   511
         of SOME class => stmt_classparam class
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   512
          | NONE => stmt_fun)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   513
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   514
    ensure_stmt stmtgen c'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   515
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   516
and exprgen_term thy algbr funcgr (Const (c, ty)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   517
      exprgen_app thy algbr funcgr ((c, ty), [])
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   518
  | exprgen_term thy algbr funcgr (Free (v, _)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   519
      pair (IVar v)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   520
  | exprgen_term thy algbr funcgr (Abs (abs as (_, ty, _))) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   521
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   522
        val (v, t) = Syntax.variant_abs abs;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   523
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   524
        exprgen_typ thy algbr funcgr ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   525
        ##>> exprgen_term thy algbr funcgr t
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   526
        #>> (fn (ty, t) => (v, ty) `|-> t)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   527
      end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   528
  | exprgen_term thy algbr funcgr (t as _ $ _) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   529
      case strip_comb t
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   530
       of (Const (c, ty), ts) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   531
            exprgen_app thy algbr funcgr ((c, ty), ts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   532
        | (t', ts) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   533
            exprgen_term thy algbr funcgr t'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   534
            ##>> fold_map (exprgen_term thy algbr funcgr) ts
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   535
            #>> (fn (t, ts) => t `$$ ts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   536
and exprgen_const thy algbr funcgr (c, ty) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   537
  ensure_const thy algbr funcgr c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   538
  ##>> exprgen_dict_parms thy algbr funcgr (c, ty)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   539
  ##>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   540
  #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   541
and exprgen_app_default thy algbr funcgr (c_ty, ts) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   542
  exprgen_const thy algbr funcgr c_ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   543
  ##>> fold_map (exprgen_term thy algbr funcgr) ts
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   544
  #>> (fn (t, ts) => t `$$ ts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   545
and exprgen_case thy algbr funcgr n cases (app as ((c, ty), ts)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   546
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   547
    val (tys, _) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   548
      (chop (1 + (if null cases then 1 else length cases)) o fst o strip_type) ty;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   549
    val dt = nth ts n;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   550
    val dty = nth tys n;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   551
    fun is_undefined (Const (c, _)) = Code.is_undefined thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   552
      | is_undefined _ = false;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   553
    fun mk_case (co, n) t =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   554
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   555
        val (vs, body) = Term.strip_abs_eta n t;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   556
        val selector = list_comb (Const (co, map snd vs ---> dty), map Free vs);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   557
      in if is_undefined body then NONE else SOME (selector, body) end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   558
    fun mk_ds [] =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   559
          let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   560
            val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   561
          in [(Free v_ty, body)] end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   562
      | mk_ds cases = map_filter (uncurry mk_case)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   563
          (AList.make (CodeUnit.no_args thy) cases ~~ nth_drop n ts);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   564
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   565
    exprgen_term thy algbr funcgr dt
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   566
    ##>> exprgen_typ thy algbr funcgr dty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   567
    ##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr pat
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   568
          ##>> exprgen_term thy algbr funcgr body) (mk_ds cases)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   569
    ##>> exprgen_app_default thy algbr funcgr app
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   570
    #>> (fn (((dt, dty), ds), t0) => ICase (((dt, dty), ds), t0))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   571
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   572
and exprgen_app thy algbr funcgr ((c, ty), ts) = case Code.get_case_data thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   573
 of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   574
      if length ts < i then
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   575
        let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   576
          val k = length ts;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   577
          val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   578
          val ctxt = (fold o fold_aterms)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   579
            (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   580
          val vs = Name.names ctxt "a" tys;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   581
        in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   582
          fold_map (exprgen_typ thy algbr funcgr) tys
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   583
          ##>> exprgen_case thy algbr funcgr n cases ((c, ty), ts @ map Free vs)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   584
          #>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   585
        end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   586
      else if length ts > i then
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   587
        exprgen_case thy algbr funcgr n cases ((c, ty), Library.take (i, ts))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   588
        ##>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   589
        #>> (fn (t, ts) => t `$$ ts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   590
      else
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   591
        exprgen_case thy algbr funcgr n cases ((c, ty), ts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   592
      end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   593
  | NONE => exprgen_app_default thy algbr funcgr ((c, ty), ts);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   594
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   595
fun ensure_value thy algbr funcgr t = 
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   596
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   597
    val ty = fastype_of t;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   598
    val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   599
      o dest_TFree))) t [];
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   600
    val stmt_value =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   601
      fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   602
      ##>> exprgen_typ thy algbr funcgr ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   603
      ##>> exprgen_term thy algbr funcgr t
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   604
      #>> (fn ((vs, ty), t) => Fun ((vs, ty), [(([], t), Drule.dummy_thm)]));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   605
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   606
    ensure_stmt stmt_value CodeName.value_name
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   607
  end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   608
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   609
fun add_value_stmt (t, ty) code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   610
  code
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   611
  |> Graph.new_node (CodeName.value_name, Fun (([], ty), [(([], t), Drule.dummy_thm)]))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   612
  |> fold (curry Graph.add_edge CodeName.value_name) (Graph.keys code);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   613
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   614
end; (*struct*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   615
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   616
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   617
structure BasicCodeThingol: BASIC_CODE_THINGOL = CodeThingol;