src/Tools/code/code_thingol.ML
author haftmann
Wed Oct 22 14:15:45 2008 +0200 (2008-10-22)
changeset 28663 bd8438543bf2
parent 28423 9fc3befd8191
child 28688 1a9fabb515cd
permissions -rw-r--r--
code identifier namings are no longer imperative
haftmann@24219
     1
(*  Title:      Tools/code/code_thingol.ML
haftmann@24219
     2
    ID:         $Id$
haftmann@24219
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     4
haftmann@24219
     5
Intermediate language ("Thin-gol") representing executable code.
haftmann@24918
     6
Representation and translation.
haftmann@24219
     7
*)
haftmann@24219
     8
haftmann@24219
     9
infix 8 `%%;
haftmann@24219
    10
infixr 6 `->;
haftmann@24219
    11
infixr 6 `-->;
haftmann@24219
    12
infix 4 `$;
haftmann@24219
    13
infix 4 `$$;
haftmann@24219
    14
infixr 3 `|->;
haftmann@24219
    15
infixr 3 `|-->;
haftmann@24219
    16
haftmann@24219
    17
signature BASIC_CODE_THINGOL =
haftmann@24219
    18
sig
haftmann@24219
    19
  type vname = string;
haftmann@24219
    20
  datatype dict =
haftmann@24219
    21
      DictConst of string * dict list list
haftmann@24219
    22
    | DictVar of string list * (vname * (int * int));
haftmann@24219
    23
  datatype itype =
haftmann@24219
    24
      `%% of string * itype list
haftmann@24219
    25
    | ITyVar of vname;
haftmann@24591
    26
  type const = string * (dict list list * itype list (*types of arguments*))
haftmann@24219
    27
  datatype iterm =
haftmann@24591
    28
      IConst of const
haftmann@24219
    29
    | IVar of vname
haftmann@24219
    30
    | `$ of iterm * iterm
haftmann@24219
    31
    | `|-> of (vname * itype) * iterm
haftmann@24219
    32
    | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
haftmann@24219
    33
        (*((term, type), [(selector pattern, body term )]), primitive term)*)
haftmann@24219
    34
  val `-> : itype * itype -> itype;
haftmann@24219
    35
  val `--> : itype list * itype -> itype;
haftmann@24219
    36
  val `$$ : iterm * iterm list -> iterm;
haftmann@24219
    37
  val `|--> : (vname * itype) list * iterm -> iterm;
haftmann@24219
    38
  type typscheme = (vname * sort) list * itype;
haftmann@24219
    39
end;
haftmann@24219
    40
haftmann@24219
    41
signature CODE_THINGOL =
haftmann@24219
    42
sig
haftmann@28663
    43
  include BASIC_CODE_THINGOL
haftmann@28663
    44
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list
haftmann@28663
    45
  val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a
haftmann@28663
    46
  val unfold_fun: itype -> itype list * itype
haftmann@28663
    47
  val unfold_app: iterm -> iterm * iterm list
haftmann@28663
    48
  val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option
haftmann@28663
    49
  val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm
haftmann@28663
    50
  val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option
haftmann@28663
    51
  val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm
haftmann@24219
    52
  val unfold_const_app: iterm ->
haftmann@28663
    53
    ((string * (dict list list * itype list)) * iterm list) option
haftmann@24219
    54
  val collapse_let: ((vname * itype) * iterm) * iterm
haftmann@28663
    55
    -> (iterm * itype) * (iterm * iterm) list
haftmann@28663
    56
  val eta_expand: int -> (string * (dict list list * itype list)) * iterm list -> iterm
haftmann@28663
    57
  val contains_dictvar: iterm -> bool
haftmann@28663
    58
  val locally_monomorphic: iterm -> bool
haftmann@28663
    59
  val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
haftmann@28663
    60
  val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
haftmann@28663
    61
  val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
haftmann@28663
    62
haftmann@28663
    63
  type naming
haftmann@28663
    64
  val lookup_class: naming -> class -> string option
haftmann@28663
    65
  val lookup_classrel: naming -> class * class -> string option
haftmann@28663
    66
  val lookup_tyco: naming -> string -> string option
haftmann@28663
    67
  val lookup_instance: naming -> class * string -> string option
haftmann@28663
    68
  val lookup_const: naming -> string -> string option
haftmann@24219
    69
haftmann@24918
    70
  datatype stmt =
haftmann@27024
    71
      NoStmt
haftmann@28663
    72
    | Fun of string * (typscheme * ((iterm list * iterm) * (thm * bool)) list)
haftmann@28663
    73
    | Datatype of string * ((vname * sort) list * (string * itype list) list)
haftmann@28663
    74
    | Datatypecons of string * string
haftmann@28663
    75
    | Class of class * (vname * ((class * string) list * (string * itype) list))
haftmann@24219
    76
    | Classrel of class * class
haftmann@28663
    77
    | Classparam of string * class
haftmann@24219
    78
    | Classinst of (class * (string * (vname * sort) list))
haftmann@24219
    79
          * ((class * (string * (string * dict list list))) list
haftmann@28663
    80
        * ((string * const) * (thm * bool)) list)
haftmann@28663
    81
  type program = stmt Graph.T
haftmann@28663
    82
  val empty_funs: program -> string list
haftmann@28663
    83
  val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm
haftmann@28663
    84
  val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
haftmann@28663
    85
  val is_cons: program -> string -> bool
haftmann@28663
    86
  val contr_classparam_typs: program -> string -> itype option list
haftmann@24219
    87
haftmann@28663
    88
  val consts_program: theory -> string list -> string list * (naming * program)
haftmann@28663
    89
  val cached_program: theory -> naming * program
haftmann@27103
    90
  val eval_conv: theory
haftmann@28663
    91
    -> (term -> term * (naming -> program -> typscheme * iterm -> string list -> thm))
haftmann@28663
    92
    -> cterm -> thm
haftmann@27103
    93
  val eval_term: theory
haftmann@28663
    94
    -> (term -> term * (naming -> program -> typscheme * iterm -> string list -> 'a))
haftmann@28663
    95
    -> term -> 'a
haftmann@24219
    96
end;
haftmann@24219
    97
haftmann@28054
    98
structure Code_Thingol: CODE_THINGOL =
haftmann@24219
    99
struct
haftmann@24219
   100
haftmann@24219
   101
(** auxiliary **)
haftmann@24219
   102
haftmann@24219
   103
fun unfoldl dest x =
haftmann@24219
   104
  case dest x
haftmann@24219
   105
   of NONE => (x, [])
haftmann@24219
   106
    | SOME (x1, x2) =>
haftmann@24219
   107
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
haftmann@24219
   108
haftmann@24219
   109
fun unfoldr dest x =
haftmann@24219
   110
  case dest x
haftmann@24219
   111
   of NONE => ([], x)
haftmann@24219
   112
    | SOME (x1, x2) =>
haftmann@24219
   113
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
haftmann@24219
   114
haftmann@24219
   115
haftmann@28369
   116
(** language core - types, patterns, expressions **)
haftmann@24219
   117
haftmann@24219
   118
type vname = string;
haftmann@24219
   119
haftmann@24219
   120
datatype dict =
haftmann@24219
   121
    DictConst of string * dict list list
haftmann@24219
   122
  | DictVar of string list * (vname * (int * int));
haftmann@24219
   123
haftmann@24219
   124
datatype itype =
haftmann@24219
   125
    `%% of string * itype list
haftmann@24219
   126
  | ITyVar of vname;
haftmann@24219
   127
haftmann@24591
   128
type const = string * (dict list list * itype list (*types of arguments*))
haftmann@24591
   129
haftmann@24219
   130
datatype iterm =
haftmann@24591
   131
    IConst of const
haftmann@24219
   132
  | IVar of vname
haftmann@24219
   133
  | `$ of iterm * iterm
haftmann@24219
   134
  | `|-> of (vname * itype) * iterm
haftmann@24219
   135
  | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
haftmann@24219
   136
    (*see also signature*)
haftmann@24219
   137
haftmann@24219
   138
(*
haftmann@24219
   139
  variable naming conventions
haftmann@24219
   140
haftmann@24219
   141
  bare names:
haftmann@24219
   142
    variable names          v
haftmann@24219
   143
    class names             class
haftmann@24219
   144
    type constructor names  tyco
haftmann@24219
   145
    datatype names          dtco
haftmann@24837
   146
    const names (general)   c (const)
haftmann@24219
   147
    constructor names       co
haftmann@24837
   148
    class parameter names   classparam
haftmann@24219
   149
    arbitrary name          s
haftmann@24219
   150
haftmann@24837
   151
    v, c, co, classparam also annotated with types etc.
haftmann@24219
   152
haftmann@24219
   153
  constructs:
haftmann@24219
   154
    sort                    sort
haftmann@24219
   155
    type parameters         vs
haftmann@24219
   156
    type                    ty
haftmann@24219
   157
    type schemes            tysm
haftmann@24219
   158
    term                    t
haftmann@24219
   159
    (term as pattern)       p
haftmann@24219
   160
    instance (class, tyco)  inst
haftmann@24219
   161
 *)
haftmann@24219
   162
haftmann@24219
   163
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2];
haftmann@24219
   164
val op `--> = Library.foldr (op `->);
haftmann@24219
   165
val op `$$ = Library.foldl (op `$);
haftmann@24219
   166
val op `|--> = Library.foldr (op `|->);
haftmann@24219
   167
haftmann@24219
   168
val unfold_fun = unfoldr
haftmann@24219
   169
  (fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2)
haftmann@24219
   170
    | _ => NONE);
haftmann@24219
   171
haftmann@24219
   172
val unfold_app = unfoldl
haftmann@24219
   173
  (fn op `$ t => SOME t
haftmann@24219
   174
    | _ => NONE);
haftmann@24219
   175
haftmann@24219
   176
val split_abs =
haftmann@24219
   177
  (fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) =>
haftmann@24219
   178
        if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t)
haftmann@24219
   179
    | (v, ty) `|-> t => SOME (((v, NONE), ty), t)
haftmann@24219
   180
    | _ => NONE);
haftmann@24219
   181
haftmann@24219
   182
val unfold_abs = unfoldr split_abs;
haftmann@24219
   183
haftmann@24219
   184
val split_let = 
haftmann@24219
   185
  (fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
haftmann@24219
   186
    | _ => NONE);
haftmann@24219
   187
haftmann@24219
   188
val unfold_let = unfoldr split_let;
haftmann@24219
   189
haftmann@24219
   190
fun unfold_const_app t =
haftmann@24219
   191
 case unfold_app t
haftmann@24219
   192
  of (IConst c, ts) => SOME (c, ts)
haftmann@24219
   193
   | _ => NONE;
haftmann@24219
   194
haftmann@24219
   195
fun fold_aiterms f (t as IConst _) = f t
haftmann@24219
   196
  | fold_aiterms f (t as IVar _) = f t
haftmann@24219
   197
  | fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2
haftmann@24219
   198
  | fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t'
haftmann@24219
   199
  | fold_aiterms f (ICase (_, t)) = fold_aiterms f t;
haftmann@24219
   200
haftmann@24219
   201
fun fold_constnames f =
haftmann@24219
   202
  let
haftmann@24219
   203
    fun add (IConst (c, _)) = f c
haftmann@24219
   204
      | add _ = I;
haftmann@24219
   205
  in fold_aiterms add end;
haftmann@24219
   206
haftmann@24219
   207
fun fold_varnames f =
haftmann@24219
   208
  let
haftmann@24219
   209
    fun add (IVar v) = f v
haftmann@24219
   210
      | add ((v, _) `|-> _) = f v
haftmann@24219
   211
      | add _ = I;
haftmann@24219
   212
  in fold_aiterms add end;
haftmann@24219
   213
haftmann@24219
   214
fun fold_unbound_varnames f =
haftmann@24219
   215
  let
haftmann@24219
   216
    fun add _ (IConst _) = I
haftmann@24219
   217
      | add vs (IVar v) = if not (member (op =) vs v) then f v else I
haftmann@24219
   218
      | add vs (t1 `$ t2) = add vs t1 #> add vs t2
haftmann@24219
   219
      | add vs ((v, _) `|-> t) = add (insert (op =) v vs) t
haftmann@24219
   220
      | add vs (ICase (_, t)) = add vs t;
haftmann@24219
   221
  in add [] end;
haftmann@24219
   222
haftmann@24219
   223
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) =
haftmann@24219
   224
      let
haftmann@24219
   225
        fun exists_v t = fold_unbound_varnames (fn w => fn b =>
haftmann@24219
   226
          b orelse v = w) t false;
haftmann@24219
   227
      in if v = w andalso forall (fn (t1, t2) =>
haftmann@24219
   228
        exists_v t1 orelse not (exists_v t2)) ds
haftmann@24219
   229
        then ((se, ty), ds)
haftmann@24219
   230
        else ((se, ty), [(IVar v, be)])
haftmann@24219
   231
      end
haftmann@24219
   232
  | collapse_let (((v, ty), se), be) =
haftmann@24219
   233
      ((se, ty), [(IVar v, be)])
haftmann@24219
   234
haftmann@27711
   235
fun eta_expand k (c as (_, (_, tys)), ts) =
haftmann@24219
   236
  let
haftmann@24219
   237
    val j = length ts;
haftmann@24219
   238
    val l = k - j;
haftmann@24219
   239
    val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
haftmann@24219
   240
    val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys);
haftmann@24219
   241
  in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
haftmann@24219
   242
haftmann@24662
   243
fun contains_dictvar t =
haftmann@24662
   244
  let
haftmann@24662
   245
    fun contains (DictConst (_, dss)) = (fold o fold) contains dss
haftmann@24662
   246
      | contains (DictVar _) = K true;
haftmann@24662
   247
  in
haftmann@24662
   248
    fold_aiterms
haftmann@24662
   249
      (fn IConst (_, (dss, _)) => (fold o fold) contains dss | _ => I) t false
haftmann@24662
   250
  end;
haftmann@24662
   251
  
haftmann@25621
   252
fun locally_monomorphic (IConst _) = false
haftmann@25621
   253
  | locally_monomorphic (IVar _) = true
haftmann@25621
   254
  | locally_monomorphic (t `$ _) = locally_monomorphic t
haftmann@25621
   255
  | locally_monomorphic (_ `|-> t) = locally_monomorphic t
haftmann@25621
   256
  | locally_monomorphic (ICase ((_, ds), _)) = exists (locally_monomorphic o snd) ds;
haftmann@25621
   257
haftmann@25621
   258
haftmann@28663
   259
(** naming policies **)
haftmann@28663
   260
haftmann@28663
   261
(* identifier categories *)
haftmann@28663
   262
haftmann@28663
   263
val suffix_class = "class";
haftmann@28663
   264
val suffix_classrel = "classrel"
haftmann@28663
   265
val suffix_tyco = "tyco";
haftmann@28663
   266
val suffix_instance = "inst";
haftmann@28663
   267
val suffix_const = "const";
haftmann@28663
   268
haftmann@28663
   269
fun add_suffix nsp NONE = NONE
haftmann@28663
   270
  | add_suffix nsp (SOME name) = SOME (NameSpace.append name nsp);
haftmann@28663
   271
haftmann@28663
   272
haftmann@28663
   273
(* policies *)
haftmann@28663
   274
haftmann@28663
   275
local
haftmann@28663
   276
  fun thyname_of thy f x = the (AList.lookup (op =) (f x) Markup.theory_nameN);
haftmann@28663
   277
  fun thyname_of_class thy =
haftmann@28663
   278
    thyname_of thy (ProofContext.query_class (ProofContext.init thy));
haftmann@28663
   279
  fun thyname_of_tyco thy =
haftmann@28663
   280
    thyname_of thy (Type.the_tags (Sign.tsig_of thy));
haftmann@28663
   281
  fun thyname_of_instance thy inst = case AxClass.arity_property thy inst Markup.theory_nameN
haftmann@28663
   282
   of [] => error ("no such instance: " ^ quote (snd inst ^ " :: " ^ fst inst))
haftmann@28663
   283
    | thyname :: _ => thyname;
haftmann@28663
   284
  fun thyname_of_const thy c = case Code.get_datatype_of_constr thy c
haftmann@28663
   285
     of SOME dtco => thyname_of_tyco thy dtco
haftmann@28663
   286
      | NONE => (case AxClass.class_of_param thy c
haftmann@28663
   287
         of SOME class => thyname_of_class thy class
haftmann@28663
   288
          | NONE => (case AxClass.inst_of_param thy c
haftmann@28663
   289
             of SOME (c, tyco) => thyname_of_instance thy
haftmann@28663
   290
                  ((the o AxClass.class_of_param thy) c, tyco)
haftmann@28663
   291
              | NONE => thyname_of thy (Consts.the_tags (Sign.consts_of thy)) c));
haftmann@28663
   292
  fun namify thy get_basename get_thyname name =
haftmann@28663
   293
    let
haftmann@28663
   294
      val prefix = get_thyname thy name;
haftmann@28663
   295
      val base = (Code_Name.purify_base true o get_basename) name;
haftmann@28663
   296
    in NameSpace.append prefix base end;
haftmann@28663
   297
in
haftmann@28663
   298
haftmann@28663
   299
fun namify_class thy = namify thy NameSpace.base thyname_of_class;
haftmann@28663
   300
fun namify_classrel thy = namify thy (fn (class1, class2) => 
haftmann@28663
   301
  NameSpace.base class2 ^ "_" ^ NameSpace.base class1) (fn thy => thyname_of_class thy o fst);
haftmann@28663
   302
  (*order fits nicely with composed projections*)
haftmann@28663
   303
fun namify_tyco thy = namify thy NameSpace.base thyname_of_tyco;
haftmann@28663
   304
fun namify_instance thy = namify thy (fn (class, tyco) => 
haftmann@28663
   305
  NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance;
haftmann@28663
   306
fun namify_const thy = namify thy NameSpace.base thyname_of_const;
haftmann@28663
   307
haftmann@28663
   308
end; (* local *)
haftmann@28663
   309
haftmann@28663
   310
haftmann@28663
   311
(* naming data with lookup and declare *)
haftmann@28663
   312
haftmann@28663
   313
structure StringPairTab = Code_Name.StringPairTab;
haftmann@28663
   314
haftmann@28663
   315
datatype naming = Naming of {
haftmann@28663
   316
  class: class Symtab.table * Name.context,
haftmann@28663
   317
  classrel: string StringPairTab.table * Name.context,
haftmann@28663
   318
  tyco: string Symtab.table * Name.context,
haftmann@28663
   319
  instance: string StringPairTab.table * Name.context,
haftmann@28663
   320
  const: string Symtab.table * Name.context
haftmann@28663
   321
}
haftmann@28663
   322
haftmann@28663
   323
fun dest_Naming (Naming naming) = naming;
haftmann@28663
   324
haftmann@28663
   325
val empty_naming = Naming {
haftmann@28663
   326
  class = (Symtab.empty, Name.context),
haftmann@28663
   327
  classrel = (StringPairTab.empty, Name.context),
haftmann@28663
   328
  tyco = (Symtab.empty, Name.context),
haftmann@28663
   329
  instance = (StringPairTab.empty, Name.context),
haftmann@28663
   330
  const = (Symtab.empty, Name.context)
haftmann@28663
   331
};
haftmann@28663
   332
haftmann@28663
   333
local
haftmann@28663
   334
  fun mk_naming (class, classrel, tyco, instance, const) =
haftmann@28663
   335
    Naming { class = class, classrel = classrel,
haftmann@28663
   336
      tyco = tyco, instance = instance, const = const };
haftmann@28663
   337
  fun map_naming f (Naming { class, classrel, tyco, instance, const }) =
haftmann@28663
   338
    mk_naming (f (class, classrel, tyco, instance, const));
haftmann@28663
   339
in
haftmann@28663
   340
  fun map_class f = map_naming
haftmann@28663
   341
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   342
      (f class, classrel, tyco, inst, const));
haftmann@28663
   343
  fun map_classrel f = map_naming
haftmann@28663
   344
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   345
      (class, f classrel, tyco, inst, const));
haftmann@28663
   346
  fun map_tyco f = map_naming
haftmann@28663
   347
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   348
      (class, classrel, f tyco, inst, const));
haftmann@28663
   349
  fun map_instance f = map_naming
haftmann@28663
   350
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   351
      (class, classrel, tyco, f inst, const));
haftmann@28663
   352
  fun map_const f = map_naming
haftmann@28663
   353
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   354
      (class, classrel, tyco, inst, f const));
haftmann@28663
   355
end; (*local*)
haftmann@28663
   356
haftmann@28663
   357
fun add_variant update (thing, name) (tab, used) =
haftmann@28663
   358
  let
haftmann@28663
   359
    val (name', used') = yield_singleton Name.variants name used;
haftmann@28663
   360
    val tab' = update (thing, name') tab;
haftmann@28663
   361
  in (tab', used') end;
haftmann@28663
   362
haftmann@28663
   363
fun declare thy mapp lookup update namify thing =
haftmann@28663
   364
  mapp (add_variant update (thing, namify thy thing))
haftmann@28663
   365
  #> `(fn naming => the (lookup naming thing));
haftmann@28663
   366
haftmann@28663
   367
val lookup_class = add_suffix suffix_class
haftmann@28663
   368
  oo Symtab.lookup o fst o #class o dest_Naming;
haftmann@28663
   369
val lookup_classrel = add_suffix suffix_classrel
haftmann@28663
   370
  oo StringPairTab.lookup o fst o #classrel o dest_Naming;
haftmann@28663
   371
val lookup_tyco = add_suffix suffix_tyco
haftmann@28663
   372
  oo Symtab.lookup o fst o #tyco o dest_Naming;
haftmann@28663
   373
val lookup_instance = add_suffix suffix_instance
haftmann@28663
   374
  oo StringPairTab.lookup o fst o #instance o dest_Naming;
haftmann@28663
   375
val lookup_const = add_suffix suffix_const
haftmann@28663
   376
  oo Symtab.lookup o fst o #const o dest_Naming;
haftmann@28663
   377
haftmann@28663
   378
fun declare_class thy = declare thy map_class
haftmann@28663
   379
  lookup_class Symtab.update_new namify_class;
haftmann@28663
   380
fun declare_classrel thy = declare thy map_classrel
haftmann@28663
   381
  lookup_classrel StringPairTab.update_new namify_classrel;
haftmann@28663
   382
fun declare_tyco thy = declare thy map_tyco
haftmann@28663
   383
  lookup_tyco Symtab.update_new namify_tyco;
haftmann@28663
   384
fun declare_instance thy = declare thy map_instance
haftmann@28663
   385
  lookup_instance StringPairTab.update_new namify_instance;
haftmann@28663
   386
fun declare_const thy = declare thy map_const
haftmann@28663
   387
  lookup_const Symtab.update_new namify_const;
haftmann@28663
   388
haftmann@24219
   389
haftmann@27103
   390
(** statements, abstract programs **)
haftmann@24219
   391
haftmann@24219
   392
type typscheme = (vname * sort) list * itype;
haftmann@24918
   393
datatype stmt =
haftmann@27024
   394
    NoStmt
haftmann@28663
   395
  | Fun of string * (typscheme * ((iterm list * iterm) * (thm * bool)) list)
haftmann@28663
   396
  | Datatype of string * ((vname * sort) list * (string * itype list) list)
haftmann@28663
   397
  | Datatypecons of string * string
haftmann@28663
   398
  | Class of class * (vname * ((class * string) list * (string * itype) list))
haftmann@24219
   399
  | Classrel of class * class
haftmann@28663
   400
  | Classparam of string * class
haftmann@24219
   401
  | Classinst of (class * (string * (vname * sort) list))
haftmann@24219
   402
        * ((class * (string * (string * dict list list))) list
haftmann@28350
   403
      * ((string * const) * (thm * bool)) list);
haftmann@24219
   404
haftmann@27103
   405
type program = stmt Graph.T;
haftmann@24219
   406
haftmann@27103
   407
fun empty_funs program =
haftmann@28663
   408
  Graph.fold (fn (name, (Fun (c, (_, [])), _)) => cons c
haftmann@27103
   409
               | _ => I) program [];
haftmann@24219
   410
haftmann@27711
   411
fun map_terms_bottom_up f (t as IConst _) = f t
haftmann@27711
   412
  | map_terms_bottom_up f (t as IVar _) = f t
haftmann@27711
   413
  | map_terms_bottom_up f (t1 `$ t2) = f
haftmann@27711
   414
      (map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2)
haftmann@27711
   415
  | map_terms_bottom_up f ((v, ty) `|-> t) = f
haftmann@27711
   416
      ((v, ty) `|-> map_terms_bottom_up f t)
haftmann@27711
   417
  | map_terms_bottom_up f (ICase (((t, ty), ps), t0)) = f
haftmann@27711
   418
      (ICase (((map_terms_bottom_up f t, ty), (map o pairself)
haftmann@27711
   419
        (map_terms_bottom_up f) ps), map_terms_bottom_up f t0));
haftmann@27711
   420
haftmann@27711
   421
fun map_terms_stmt f NoStmt = NoStmt
haftmann@28663
   422
  | map_terms_stmt f (Fun (c, (tysm, eqs))) = Fun (c, (tysm, (map o apfst)
haftmann@28663
   423
      (fn (ts, t) => (map f ts, f t)) eqs))
haftmann@27711
   424
  | map_terms_stmt f (stmt as Datatype _) = stmt
haftmann@27711
   425
  | map_terms_stmt f (stmt as Datatypecons _) = stmt
haftmann@27711
   426
  | map_terms_stmt f (stmt as Class _) = stmt
haftmann@27711
   427
  | map_terms_stmt f (stmt as Classrel _) = stmt
haftmann@27711
   428
  | map_terms_stmt f (stmt as Classparam _) = stmt
haftmann@27711
   429
  | map_terms_stmt f (Classinst (arity, (superarities, classparms))) =
haftmann@27711
   430
      Classinst (arity, (superarities, (map o apfst o apsnd) (fn const =>
haftmann@27711
   431
        case f (IConst const) of IConst const' => const') classparms));
haftmann@27711
   432
haftmann@27103
   433
fun is_cons program name = case Graph.get_node program name
haftmann@24219
   434
 of Datatypecons _ => true
haftmann@24219
   435
  | _ => false;
haftmann@24219
   436
haftmann@27103
   437
fun contr_classparam_typs program name = case Graph.get_node program name
haftmann@28663
   438
 of Classparam (_, class) => let
haftmann@28663
   439
        val Class (_, (_, (_, params))) = Graph.get_node program class;
haftmann@25621
   440
        val SOME ty = AList.lookup (op =) params name;
haftmann@25621
   441
        val (tys, res_ty) = unfold_fun ty;
haftmann@25621
   442
        fun no_tyvar (_ `%% tys) = forall no_tyvar tys
haftmann@25621
   443
          | no_tyvar (ITyVar _) = false;
haftmann@25621
   444
      in if no_tyvar res_ty
haftmann@25621
   445
        then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys
haftmann@25621
   446
        else []
haftmann@25621
   447
      end
haftmann@25621
   448
  | _ => [];
haftmann@25621
   449
haftmann@24219
   450
haftmann@27103
   451
(** translation kernel **)
haftmann@24219
   452
haftmann@28663
   453
fun ensure_stmt lookup declare generate thing (dep, (naming, program)) =
haftmann@24219
   454
  let
haftmann@28663
   455
    fun add_dep name = case dep
haftmann@28663
   456
     of NONE => I | SOME dep => Graph.add_edge (dep, name);
haftmann@28663
   457
  in case lookup naming thing
haftmann@28663
   458
   of SOME name => program
haftmann@28663
   459
        |> add_dep name
haftmann@28663
   460
        |> pair naming
haftmann@28663
   461
        |> pair dep
haftmann@28663
   462
        |> pair name
haftmann@28663
   463
    | NONE => let
haftmann@28663
   464
          val (name, naming') = declare thing naming;
haftmann@28663
   465
        in
haftmann@28663
   466
          program
haftmann@28663
   467
          |> Graph.default_node (name, NoStmt)
haftmann@28663
   468
          |> add_dep name
haftmann@28663
   469
          |> pair naming'
haftmann@28663
   470
          |> curry generate (SOME name)
haftmann@28663
   471
          ||> snd
haftmann@28663
   472
          |-> (fn stmt => (apsnd o Graph.map_node name) (K stmt))
haftmann@28663
   473
          |> pair dep
haftmann@28663
   474
          |> pair name
haftmann@28663
   475
        end
haftmann@24219
   476
  end;
haftmann@24219
   477
haftmann@26972
   478
fun not_wellsorted thy thm ty sort e =
haftmann@26972
   479
  let
haftmann@26972
   480
    val err_class = Sorts.class_error (Syntax.pp_global thy) e;
haftmann@26972
   481
    val err_thm = case thm
haftmann@26972
   482
     of SOME thm => "\n(in defining equation " ^ Display.string_of_thm thm ^ ")" | NONE => "";
haftmann@26972
   483
    val err_typ = "Type " ^ Syntax.string_of_typ_global thy ty ^ " not of sort "
haftmann@26972
   484
      ^ Syntax.string_of_sort_global thy sort;
haftmann@26972
   485
  in error ("Wellsortedness error" ^ err_thm ^ ":\n" ^ err_typ ^ "\n" ^ err_class) end;
haftmann@26972
   486
haftmann@26972
   487
fun ensure_class thy (algbr as (_, algebra)) funcgr class =
haftmann@24918
   488
  let
haftmann@24918
   489
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
wenzelm@24969
   490
    val cs = #params (AxClass.get_info thy class);
haftmann@24918
   491
    val stmt_class =
haftmann@24918
   492
      fold_map (fn superclass => ensure_class thy algbr funcgr superclass
haftmann@24918
   493
        ##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses
haftmann@24918
   494
      ##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c
haftmann@24918
   495
        ##>> exprgen_typ thy algbr funcgr ty) cs
haftmann@28663
   496
      #>> (fn info => Class (class, (unprefix "'" Name.aT, info)))
haftmann@28663
   497
  in ensure_stmt lookup_class (declare_class thy) stmt_class class end
haftmann@24918
   498
and ensure_classrel thy algbr funcgr (subclass, superclass) =
haftmann@24918
   499
  let
haftmann@24918
   500
    val stmt_classrel =
haftmann@24918
   501
      ensure_class thy algbr funcgr subclass
haftmann@24918
   502
      ##>> ensure_class thy algbr funcgr superclass
haftmann@24918
   503
      #>> Classrel;
haftmann@28663
   504
  in ensure_stmt lookup_classrel (declare_classrel thy) stmt_classrel (subclass, superclass) end
haftmann@24918
   505
and ensure_tyco thy algbr funcgr "fun" =
haftmann@24918
   506
      pair "fun"
haftmann@24918
   507
  | ensure_tyco thy algbr funcgr tyco =
haftmann@24918
   508
      let
haftmann@24918
   509
        val stmt_datatype =
haftmann@24918
   510
          let
haftmann@24918
   511
            val (vs, cos) = Code.get_datatype thy tyco;
haftmann@24918
   512
          in
haftmann@24918
   513
            fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
haftmann@24918
   514
            ##>> fold_map (fn (c, tys) =>
haftmann@24918
   515
              ensure_const thy algbr funcgr c
haftmann@24918
   516
              ##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos
haftmann@28663
   517
            #>> (fn info => Datatype (tyco, info))
haftmann@24918
   518
          end;
haftmann@28663
   519
      in ensure_stmt lookup_tyco (declare_tyco thy) stmt_datatype tyco end
haftmann@26972
   520
and exprgen_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) =
haftmann@24918
   521
  fold_map (ensure_class thy algbr funcgr) (proj_sort sort)
haftmann@24918
   522
  #>> (fn sort => (unprefix "'" v, sort))
haftmann@27422
   523
and exprgen_typ thy algbr funcgr (TFree v_sort) =
haftmann@27422
   524
      exprgen_tyvar_sort thy algbr funcgr v_sort
haftmann@24918
   525
      #>> (fn (v, sort) => ITyVar v)
haftmann@24918
   526
  | exprgen_typ thy algbr funcgr (Type (tyco, tys)) =
haftmann@24918
   527
      ensure_tyco thy algbr funcgr tyco
haftmann@24918
   528
      ##>> fold_map (exprgen_typ thy algbr funcgr) tys
haftmann@24918
   529
      #>> (fn (tyco, tys) => tyco `%% tys)
haftmann@26972
   530
and exprgen_dicts thy (algbr as (proj_sort, algebra)) funcgr thm (ty, sort) =
haftmann@24918
   531
  let
wenzelm@26939
   532
    val pp = Syntax.pp_global thy;
haftmann@24918
   533
    datatype typarg =
haftmann@24918
   534
        Global of (class * string) * typarg list list
haftmann@24918
   535
      | Local of (class * class) list * (string * (int * sort));
haftmann@24918
   536
    fun class_relation (Global ((_, tyco), yss), _) class =
haftmann@24918
   537
          Global ((class, tyco), yss)
haftmann@24918
   538
      | class_relation (Local (classrels, v), subclass) superclass =
haftmann@24918
   539
          Local ((subclass, superclass) :: classrels, v);
haftmann@24918
   540
    fun type_constructor tyco yss class =
haftmann@24918
   541
      Global ((class, tyco), (map o map) fst yss);
haftmann@24918
   542
    fun type_variable (TFree (v, sort)) =
haftmann@24918
   543
      let
haftmann@24918
   544
        val sort' = proj_sort sort;
haftmann@24918
   545
      in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
haftmann@24918
   546
    val typargs = Sorts.of_sort_derivation pp algebra
haftmann@24918
   547
      {class_relation = class_relation, type_constructor = type_constructor,
haftmann@26972
   548
       type_variable = type_variable} (ty, proj_sort sort)
haftmann@26972
   549
      handle Sorts.CLASS_ERROR e => not_wellsorted thy thm ty sort e;
haftmann@24918
   550
    fun mk_dict (Global (inst, yss)) =
haftmann@24918
   551
          ensure_inst thy algbr funcgr inst
haftmann@24918
   552
          ##>> (fold_map o fold_map) mk_dict yss
haftmann@24918
   553
          #>> (fn (inst, dss) => DictConst (inst, dss))
haftmann@24918
   554
      | mk_dict (Local (classrels, (v, (k, sort)))) =
haftmann@24918
   555
          fold_map (ensure_classrel thy algbr funcgr) classrels
haftmann@24918
   556
          #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
haftmann@27422
   557
  in fold_map mk_dict typargs end
haftmann@28350
   558
and exprgen_eq thy algbr funcgr (thm, linear) =
haftmann@24918
   559
  let
haftmann@24918
   560
    val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
haftmann@24918
   561
      o Logic.unvarify o prop_of) thm;
haftmann@24918
   562
  in
haftmann@26972
   563
    fold_map (exprgen_term thy algbr funcgr (SOME thm)) args
haftmann@26972
   564
    ##>> exprgen_term thy algbr funcgr (SOME thm) rhs
haftmann@28350
   565
    #>> rpair (thm, linear)
haftmann@24918
   566
  end
haftmann@26972
   567
and ensure_inst thy (algbr as (_, algebra)) funcgr (class, tyco) =
haftmann@24918
   568
  let
haftmann@24918
   569
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
wenzelm@24969
   570
    val classparams = these (try (#params o AxClass.get_info thy) class);
haftmann@24918
   571
    val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
haftmann@24918
   572
    val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
haftmann@24918
   573
    val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
haftmann@24918
   574
      Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
haftmann@24918
   575
    val arity_typ = Type (tyco, map TFree vs);
haftmann@24918
   576
    val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
haftmann@24918
   577
    fun exprgen_superarity superclass =
haftmann@24918
   578
      ensure_class thy algbr funcgr superclass
haftmann@24918
   579
      ##>> ensure_classrel thy algbr funcgr (class, superclass)
haftmann@26972
   580
      ##>> exprgen_dicts thy algbr funcgr NONE (arity_typ, [superclass])
haftmann@24918
   581
      #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
haftmann@24918
   582
            (superclass, (classrel, (inst, dss))));
haftmann@24918
   583
    fun exprgen_classparam_inst (c, ty) =
haftmann@24918
   584
      let
haftmann@24918
   585
        val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
haftmann@25597
   586
        val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy c_inst);
haftmann@24918
   587
        val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
haftmann@24918
   588
          o Logic.dest_equals o Thm.prop_of) thm;
haftmann@24918
   589
      in
haftmann@24918
   590
        ensure_const thy algbr funcgr c
haftmann@26972
   591
        ##>> exprgen_const thy algbr funcgr (SOME thm) c_ty
haftmann@28350
   592
        #>> (fn (c, IConst c_inst) => ((c, c_inst), (thm, true)))
haftmann@24918
   593
      end;
haftmann@24918
   594
    val stmt_inst =
haftmann@24918
   595
      ensure_class thy algbr funcgr class
haftmann@24918
   596
      ##>> ensure_tyco thy algbr funcgr tyco
haftmann@24918
   597
      ##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
haftmann@24918
   598
      ##>> fold_map exprgen_superarity superclasses
haftmann@24918
   599
      ##>> fold_map exprgen_classparam_inst classparams
haftmann@24918
   600
      #>> (fn ((((class, tyco), arity), superarities), classparams) =>
haftmann@24918
   601
             Classinst ((class, (tyco, arity)), (superarities, classparams)));
haftmann@28663
   602
  in ensure_stmt lookup_instance (declare_instance thy) stmt_inst (class, tyco) end
haftmann@26972
   603
and ensure_const thy algbr funcgr c =
haftmann@24918
   604
  let
haftmann@24918
   605
    fun stmt_datatypecons tyco =
haftmann@24918
   606
      ensure_tyco thy algbr funcgr tyco
haftmann@28663
   607
      #>> (fn tyco => Datatypecons (c, tyco));
haftmann@24918
   608
    fun stmt_classparam class =
haftmann@24918
   609
      ensure_class thy algbr funcgr class
haftmann@28663
   610
      #>> (fn class => Classparam (c, class));
haftmann@28663
   611
    fun stmt_fun ((vs, raw_ty), raw_thms) =
haftmann@24918
   612
      let
haftmann@26972
   613
        val ty = Logic.unvarifyT raw_ty;
haftmann@24918
   614
        val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
haftmann@24918
   615
          then raw_thms
haftmann@28423
   616
          else (map o apfst) (Code_Unit.expand_eta thy 1) raw_thms;
haftmann@24918
   617
      in
haftmann@28663
   618
        fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
haftmann@28663
   619
        ##>> exprgen_typ thy algbr funcgr ty
haftmann@28663
   620
        ##>> fold_map (exprgen_eq thy algbr funcgr) thms
haftmann@28663
   621
        #>> (fn info => Fun (c, info))
haftmann@24918
   622
      end;
haftmann@28663
   623
    val stmt_const = case Code.get_datatype_of_constr thy c
haftmann@24918
   624
     of SOME tyco => stmt_datatypecons tyco
haftmann@24918
   625
      | NONE => (case AxClass.class_of_param thy c
haftmann@24918
   626
         of SOME class => stmt_classparam class
haftmann@28663
   627
          | NONE => stmt_fun (Code_Funcgr.typ funcgr c, Code_Funcgr.eqns funcgr c))
haftmann@28663
   628
  in ensure_stmt lookup_const (declare_const thy) stmt_const c end
haftmann@26972
   629
and exprgen_term thy algbr funcgr thm (Const (c, ty)) =
haftmann@26972
   630
      exprgen_app thy algbr funcgr thm ((c, ty), [])
haftmann@26972
   631
  | exprgen_term thy algbr funcgr thm (Free (v, _)) =
haftmann@24918
   632
      pair (IVar v)
haftmann@26972
   633
  | exprgen_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) =
haftmann@24918
   634
      let
haftmann@24918
   635
        val (v, t) = Syntax.variant_abs abs;
haftmann@24918
   636
      in
haftmann@24918
   637
        exprgen_typ thy algbr funcgr ty
haftmann@26972
   638
        ##>> exprgen_term thy algbr funcgr thm t
haftmann@24918
   639
        #>> (fn (ty, t) => (v, ty) `|-> t)
haftmann@24918
   640
      end
haftmann@26972
   641
  | exprgen_term thy algbr funcgr thm (t as _ $ _) =
haftmann@24918
   642
      case strip_comb t
haftmann@24918
   643
       of (Const (c, ty), ts) =>
haftmann@26972
   644
            exprgen_app thy algbr funcgr thm ((c, ty), ts)
haftmann@24918
   645
        | (t', ts) =>
haftmann@26972
   646
            exprgen_term thy algbr funcgr thm t'
haftmann@26972
   647
            ##>> fold_map (exprgen_term thy algbr funcgr thm) ts
haftmann@24918
   648
            #>> (fn (t, ts) => t `$$ ts)
haftmann@26972
   649
and exprgen_const thy algbr funcgr thm (c, ty) =
haftmann@26972
   650
  let
haftmann@26972
   651
    val tys = Sign.const_typargs thy (c, ty);
haftmann@28054
   652
    val sorts = (map snd o fst o Code_Funcgr.typ funcgr) c;
haftmann@26972
   653
    val tys_args = (fst o Term.strip_type) ty;
haftmann@26972
   654
  in
haftmann@26972
   655
    ensure_const thy algbr funcgr c
haftmann@26972
   656
    ##>> fold_map (exprgen_dicts thy algbr funcgr thm) (tys ~~ sorts)
haftmann@26972
   657
    ##>> fold_map (exprgen_typ thy algbr funcgr) tys_args
haftmann@26972
   658
    #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
haftmann@26972
   659
  end
haftmann@26972
   660
and exprgen_app_default thy algbr funcgr thm (c_ty, ts) =
haftmann@26972
   661
  exprgen_const thy algbr funcgr thm c_ty
haftmann@26972
   662
  ##>> fold_map (exprgen_term thy algbr funcgr thm) ts
haftmann@24918
   663
  #>> (fn (t, ts) => t `$$ ts)
haftmann@26972
   664
and exprgen_case thy algbr funcgr thm n cases (app as ((c, ty), ts)) =
haftmann@24918
   665
  let
haftmann@24918
   666
    val (tys, _) =
haftmann@24918
   667
      (chop (1 + (if null cases then 1 else length cases)) o fst o strip_type) ty;
haftmann@24918
   668
    val dt = nth ts n;
haftmann@24918
   669
    val dty = nth tys n;
haftmann@24918
   670
    fun is_undefined (Const (c, _)) = Code.is_undefined thy c
haftmann@24918
   671
      | is_undefined _ = false;
haftmann@24918
   672
    fun mk_case (co, n) t =
haftmann@24918
   673
      let
haftmann@24918
   674
        val (vs, body) = Term.strip_abs_eta n t;
haftmann@24918
   675
        val selector = list_comb (Const (co, map snd vs ---> dty), map Free vs);
haftmann@24918
   676
      in if is_undefined body then NONE else SOME (selector, body) end;
haftmann@24918
   677
    fun mk_ds [] =
haftmann@24918
   678
          let
haftmann@24918
   679
            val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts))
haftmann@24918
   680
          in [(Free v_ty, body)] end
haftmann@24918
   681
      | mk_ds cases = map_filter (uncurry mk_case)
haftmann@28054
   682
          (AList.make (Code_Unit.no_args thy) cases ~~ nth_drop n ts);
haftmann@24918
   683
  in
haftmann@26972
   684
    exprgen_term thy algbr funcgr thm dt
haftmann@24918
   685
    ##>> exprgen_typ thy algbr funcgr dty
haftmann@26972
   686
    ##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr thm pat
haftmann@26972
   687
          ##>> exprgen_term thy algbr funcgr thm body) (mk_ds cases)
haftmann@26972
   688
    ##>> exprgen_app_default thy algbr funcgr thm app
haftmann@24918
   689
    #>> (fn (((dt, dty), ds), t0) => ICase (((dt, dty), ds), t0))
haftmann@24918
   690
  end
haftmann@26972
   691
and exprgen_app thy algbr funcgr thm ((c, ty), ts) = case Code.get_case_data thy c
haftmann@24918
   692
 of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in
haftmann@24918
   693
      if length ts < i then
haftmann@24918
   694
        let
haftmann@24918
   695
          val k = length ts;
haftmann@24918
   696
          val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
haftmann@24918
   697
          val ctxt = (fold o fold_aterms)
haftmann@24918
   698
            (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
haftmann@24918
   699
          val vs = Name.names ctxt "a" tys;
haftmann@24918
   700
        in
haftmann@24918
   701
          fold_map (exprgen_typ thy algbr funcgr) tys
haftmann@26972
   702
          ##>> exprgen_case thy algbr funcgr thm n cases ((c, ty), ts @ map Free vs)
haftmann@24918
   703
          #>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
haftmann@24918
   704
        end
haftmann@24918
   705
      else if length ts > i then
haftmann@26972
   706
        exprgen_case thy algbr funcgr thm n cases ((c, ty), Library.take (i, ts))
haftmann@26972
   707
        ##>> fold_map (exprgen_term thy algbr funcgr thm) (Library.drop (i, ts))
haftmann@24918
   708
        #>> (fn (t, ts) => t `$$ ts)
haftmann@24918
   709
      else
haftmann@26972
   710
        exprgen_case thy algbr funcgr thm n cases ((c, ty), ts)
haftmann@24918
   711
      end
haftmann@26972
   712
  | NONE => exprgen_app_default thy algbr funcgr thm ((c, ty), ts);
haftmann@24918
   713
haftmann@25969
   714
haftmann@27103
   715
(** generated programs **)
haftmann@27103
   716
haftmann@27103
   717
(* store *)
haftmann@27103
   718
haftmann@27103
   719
structure Program = CodeDataFun
haftmann@27103
   720
(
haftmann@28663
   721
  type T = naming * program;
haftmann@28663
   722
  val empty = (empty_naming, Graph.empty);
haftmann@28663
   723
  fun purge thy cs (naming, program) = empty (*FIXME: problem: un-declaration of names
haftmann@27609
   724
    let
haftmann@27609
   725
      val cs_exisiting =
haftmann@28054
   726
        map_filter (Code_Name.const_rev thy) (Graph.keys program);
haftmann@27609
   727
      val dels = (Graph.all_preds program
haftmann@28054
   728
          o map (Code_Name.const thy)
haftmann@27609
   729
          o filter (member (op =) cs_exisiting)
haftmann@27609
   730
        ) cs;
haftmann@28663
   731
    in Graph.del_nodes dels program end;*)
haftmann@27103
   732
);
haftmann@27103
   733
haftmann@27103
   734
val cached_program = Program.get;
haftmann@25969
   735
haftmann@28663
   736
fun generate thy funcgr f name =
haftmann@28663
   737
  Program.change_yield thy (fn naming_program => (NONE, naming_program)
haftmann@28663
   738
    |> f thy (Code.operational_algebra thy) funcgr name
haftmann@28663
   739
    |-> (fn name => fn (_, naming_program) => (name, naming_program)));
haftmann@27103
   740
haftmann@27103
   741
haftmann@27103
   742
(* program generation *)
haftmann@27103
   743
haftmann@27103
   744
fun consts_program thy cs =
haftmann@27103
   745
  let
haftmann@28663
   746
    fun project_consts cs (naming, program) =
haftmann@27103
   747
      let
haftmann@27103
   748
        val cs_all = Graph.all_succs program cs;
haftmann@28663
   749
      in (cs, (naming, Graph.subgraph (member (op =) cs_all) program)) end;
haftmann@27103
   750
    fun generate_consts thy algebra funcgr =
haftmann@27103
   751
      fold_map (ensure_const thy algebra funcgr);
haftmann@27103
   752
  in
haftmann@28054
   753
    generate thy (Code_Funcgr.make thy cs) generate_consts cs
haftmann@27103
   754
    |-> project_consts
haftmann@27103
   755
  end;
haftmann@27103
   756
haftmann@27103
   757
haftmann@27103
   758
(* value evaluation *)
haftmann@25969
   759
haftmann@24918
   760
fun ensure_value thy algbr funcgr t = 
haftmann@24918
   761
  let
haftmann@24918
   762
    val ty = fastype_of t;
haftmann@24918
   763
    val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
haftmann@24918
   764
      o dest_TFree))) t [];
haftmann@24918
   765
    val stmt_value =
haftmann@24918
   766
      fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
haftmann@24918
   767
      ##>> exprgen_typ thy algbr funcgr ty
haftmann@26972
   768
      ##>> exprgen_term thy algbr funcgr NONE t
haftmann@28663
   769
      #>> (fn ((vs, ty), t) => Fun
haftmann@28663
   770
        (Term.dummy_patternN, ((vs, ty), [(([], t), (Drule.dummy_thm, true))])));
haftmann@28663
   771
    fun term_value (dep, (naming, program1)) =
haftmann@25969
   772
      let
haftmann@28663
   773
        val Fun (_, ((vs, ty), [(([], t), _)])) =
haftmann@28663
   774
          Graph.get_node program1 Term.dummy_patternN;
haftmann@28663
   775
        val deps = Graph.imm_succs program1 Term.dummy_patternN;
haftmann@28663
   776
        val program2 = Graph.del_nodes [Term.dummy_patternN] program1;
haftmann@27103
   777
        val deps_all = Graph.all_succs program2 deps;
haftmann@27103
   778
        val program3 = Graph.subgraph (member (op =) deps_all) program2;
haftmann@28663
   779
      in (((naming, program3), (((vs, ty), t), deps)), (dep, (naming, program2))) end;
haftmann@26011
   780
  in
haftmann@28663
   781
    ensure_stmt ((K o K) NONE) pair stmt_value Term.dummy_patternN
haftmann@26011
   782
    #> snd
haftmann@26011
   783
    #> term_value
haftmann@26011
   784
  end;
haftmann@24219
   785
haftmann@27103
   786
fun eval eval_kind thy evaluator =
haftmann@27103
   787
  let
haftmann@27103
   788
    fun evaluator'' evaluator''' funcgr t =
haftmann@27103
   789
      let
haftmann@28663
   790
        val (((naming, program), (vs_ty_t, deps)), _) = generate thy funcgr ensure_value t;
haftmann@28663
   791
      in evaluator''' naming program vs_ty_t deps end;
haftmann@27103
   792
    fun evaluator' t =
haftmann@27103
   793
      let
haftmann@27103
   794
        val (t', evaluator''') = evaluator t;
haftmann@27103
   795
      in (t', evaluator'' evaluator''') end;
haftmann@27103
   796
  in eval_kind thy evaluator' end
haftmann@27103
   797
haftmann@28054
   798
fun eval_conv thy = eval Code_Funcgr.eval_conv thy;
haftmann@28054
   799
fun eval_term thy = eval Code_Funcgr.eval_term thy;
haftmann@27103
   800
haftmann@24219
   801
end; (*struct*)
haftmann@24219
   802
haftmann@24219
   803
haftmann@28054
   804
structure Basic_Code_Thingol: BASIC_CODE_THINGOL = Code_Thingol;