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