src/Tools/code/code_thingol.ML
author haftmann
Sun Mar 22 11:56:32 2009 +0100 (2009-03-22)
changeset 30648 17365ef082f3
parent 30364 577edc39b501
child 30932 35f255987e18
permissions -rw-r--r--
clarified relationship of modules Code_Name and Code_Printer
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@30648
   245
      val base = (Code_Name.purify_base 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
datatype naming = Naming of {
haftmann@28663
   265
  class: class Symtab.table * Name.context,
haftmann@30648
   266
  classrel: string Symreltab.table * Name.context,
haftmann@28663
   267
  tyco: string Symtab.table * Name.context,
haftmann@30648
   268
  instance: string Symreltab.table * Name.context,
haftmann@28663
   269
  const: string Symtab.table * Name.context
haftmann@28663
   270
}
haftmann@28663
   271
haftmann@28663
   272
fun dest_Naming (Naming naming) = naming;
haftmann@28663
   273
haftmann@28663
   274
val empty_naming = Naming {
haftmann@28663
   275
  class = (Symtab.empty, Name.context),
haftmann@30648
   276
  classrel = (Symreltab.empty, Name.context),
haftmann@28663
   277
  tyco = (Symtab.empty, Name.context),
haftmann@30648
   278
  instance = (Symreltab.empty, Name.context),
haftmann@28663
   279
  const = (Symtab.empty, Name.context)
haftmann@28663
   280
};
haftmann@28663
   281
haftmann@28663
   282
local
haftmann@28663
   283
  fun mk_naming (class, classrel, tyco, instance, const) =
haftmann@28663
   284
    Naming { class = class, classrel = classrel,
haftmann@28663
   285
      tyco = tyco, instance = instance, const = const };
haftmann@28663
   286
  fun map_naming f (Naming { class, classrel, tyco, instance, const }) =
haftmann@28663
   287
    mk_naming (f (class, classrel, tyco, instance, const));
haftmann@28663
   288
in
haftmann@28663
   289
  fun map_class f = map_naming
haftmann@28663
   290
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   291
      (f class, classrel, tyco, inst, const));
haftmann@28663
   292
  fun map_classrel f = map_naming
haftmann@28663
   293
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   294
      (class, f classrel, tyco, inst, const));
haftmann@28663
   295
  fun map_tyco f = map_naming
haftmann@28663
   296
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   297
      (class, classrel, f tyco, inst, const));
haftmann@28663
   298
  fun map_instance f = map_naming
haftmann@28663
   299
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   300
      (class, classrel, tyco, f inst, const));
haftmann@28663
   301
  fun map_const f = map_naming
haftmann@28663
   302
    (fn (class, classrel, tyco, inst, const) =>
haftmann@28663
   303
      (class, classrel, tyco, inst, f const));
haftmann@28663
   304
end; (*local*)
haftmann@28663
   305
haftmann@28663
   306
fun add_variant update (thing, name) (tab, used) =
haftmann@28663
   307
  let
haftmann@28663
   308
    val (name', used') = yield_singleton Name.variants name used;
haftmann@28663
   309
    val tab' = update (thing, name') tab;
haftmann@28663
   310
  in (tab', used') end;
haftmann@28663
   311
haftmann@28663
   312
fun declare thy mapp lookup update namify thing =
haftmann@28663
   313
  mapp (add_variant update (thing, namify thy thing))
haftmann@28663
   314
  #> `(fn naming => the (lookup naming thing));
haftmann@28663
   315
haftmann@28688
   316
haftmann@28688
   317
(* lookup and declare *)
haftmann@28688
   318
haftmann@28688
   319
local
haftmann@28688
   320
haftmann@28688
   321
val suffix_class = "class";
haftmann@28688
   322
val suffix_classrel = "classrel"
haftmann@28688
   323
val suffix_tyco = "tyco";
haftmann@28688
   324
val suffix_instance = "inst";
haftmann@28688
   325
val suffix_const = "const";
haftmann@28688
   326
haftmann@28688
   327
fun add_suffix nsp NONE = NONE
wenzelm@30364
   328
  | add_suffix nsp (SOME name) = SOME (Long_Name.append name nsp);
haftmann@28688
   329
haftmann@28688
   330
in
haftmann@28688
   331
haftmann@28663
   332
val lookup_class = add_suffix suffix_class
haftmann@28663
   333
  oo Symtab.lookup o fst o #class o dest_Naming;
haftmann@28663
   334
val lookup_classrel = add_suffix suffix_classrel
haftmann@30648
   335
  oo Symreltab.lookup o fst o #classrel o dest_Naming;
haftmann@28663
   336
val lookup_tyco = add_suffix suffix_tyco
haftmann@28663
   337
  oo Symtab.lookup o fst o #tyco o dest_Naming;
haftmann@28663
   338
val lookup_instance = add_suffix suffix_instance
haftmann@30648
   339
  oo Symreltab.lookup o fst o #instance o dest_Naming;
haftmann@28663
   340
val lookup_const = add_suffix suffix_const
haftmann@28663
   341
  oo Symtab.lookup o fst o #const o dest_Naming;
haftmann@28663
   342
haftmann@28663
   343
fun declare_class thy = declare thy map_class
haftmann@28663
   344
  lookup_class Symtab.update_new namify_class;
haftmann@28663
   345
fun declare_classrel thy = declare thy map_classrel
haftmann@30648
   346
  lookup_classrel Symreltab.update_new namify_classrel;
haftmann@28663
   347
fun declare_tyco thy = declare thy map_tyco
haftmann@28663
   348
  lookup_tyco Symtab.update_new namify_tyco;
haftmann@28663
   349
fun declare_instance thy = declare thy map_instance
haftmann@30648
   350
  lookup_instance Symreltab.update_new namify_instance;
haftmann@28663
   351
fun declare_const thy = declare thy map_const
haftmann@28663
   352
  lookup_const Symtab.update_new namify_const;
haftmann@28663
   353
haftmann@28688
   354
val unfold_fun = unfoldr
haftmann@28688
   355
  (fn "Pure.fun.tyco" `%% [ty1, ty2] => SOME (ty1, ty2)
haftmann@28688
   356
    | _ => NONE); (*depends on suffix_tyco and namify_tyco!*)
haftmann@28688
   357
haftmann@28688
   358
end; (* local *)
haftmann@28688
   359
haftmann@24219
   360
haftmann@27103
   361
(** statements, abstract programs **)
haftmann@24219
   362
haftmann@24219
   363
type typscheme = (vname * sort) list * itype;
haftmann@24918
   364
datatype stmt =
haftmann@27024
   365
    NoStmt
haftmann@28663
   366
  | Fun of string * (typscheme * ((iterm list * iterm) * (thm * bool)) list)
haftmann@28663
   367
  | Datatype of string * ((vname * sort) list * (string * itype list) list)
haftmann@28663
   368
  | Datatypecons of string * string
haftmann@28663
   369
  | Class of class * (vname * ((class * string) list * (string * itype) list))
haftmann@24219
   370
  | Classrel of class * class
haftmann@28663
   371
  | Classparam of string * class
haftmann@24219
   372
  | Classinst of (class * (string * (vname * sort) list))
haftmann@24219
   373
        * ((class * (string * (string * dict list list))) list
haftmann@28350
   374
      * ((string * const) * (thm * bool)) list);
haftmann@24219
   375
haftmann@27103
   376
type program = stmt Graph.T;
haftmann@24219
   377
haftmann@27103
   378
fun empty_funs program =
haftmann@28663
   379
  Graph.fold (fn (name, (Fun (c, (_, [])), _)) => cons c
haftmann@27103
   380
               | _ => I) program [];
haftmann@24219
   381
haftmann@27711
   382
fun map_terms_bottom_up f (t as IConst _) = f t
haftmann@27711
   383
  | map_terms_bottom_up f (t as IVar _) = f t
haftmann@27711
   384
  | map_terms_bottom_up f (t1 `$ t2) = f
haftmann@27711
   385
      (map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2)
haftmann@27711
   386
  | map_terms_bottom_up f ((v, ty) `|-> t) = f
haftmann@27711
   387
      ((v, ty) `|-> map_terms_bottom_up f t)
haftmann@27711
   388
  | map_terms_bottom_up f (ICase (((t, ty), ps), t0)) = f
haftmann@27711
   389
      (ICase (((map_terms_bottom_up f t, ty), (map o pairself)
haftmann@27711
   390
        (map_terms_bottom_up f) ps), map_terms_bottom_up f t0));
haftmann@27711
   391
haftmann@27711
   392
fun map_terms_stmt f NoStmt = NoStmt
haftmann@28663
   393
  | map_terms_stmt f (Fun (c, (tysm, eqs))) = Fun (c, (tysm, (map o apfst)
haftmann@28663
   394
      (fn (ts, t) => (map f ts, f t)) eqs))
haftmann@27711
   395
  | map_terms_stmt f (stmt as Datatype _) = stmt
haftmann@27711
   396
  | map_terms_stmt f (stmt as Datatypecons _) = stmt
haftmann@27711
   397
  | map_terms_stmt f (stmt as Class _) = stmt
haftmann@27711
   398
  | map_terms_stmt f (stmt as Classrel _) = stmt
haftmann@27711
   399
  | map_terms_stmt f (stmt as Classparam _) = stmt
haftmann@27711
   400
  | map_terms_stmt f (Classinst (arity, (superarities, classparms))) =
haftmann@27711
   401
      Classinst (arity, (superarities, (map o apfst o apsnd) (fn const =>
haftmann@27711
   402
        case f (IConst const) of IConst const' => const') classparms));
haftmann@27711
   403
haftmann@27103
   404
fun is_cons program name = case Graph.get_node program name
haftmann@24219
   405
 of Datatypecons _ => true
haftmann@24219
   406
  | _ => false;
haftmann@24219
   407
haftmann@27103
   408
fun contr_classparam_typs program name = case Graph.get_node program name
haftmann@28663
   409
 of Classparam (_, class) => let
haftmann@28663
   410
        val Class (_, (_, (_, params))) = Graph.get_node program class;
haftmann@25621
   411
        val SOME ty = AList.lookup (op =) params name;
haftmann@25621
   412
        val (tys, res_ty) = unfold_fun ty;
haftmann@25621
   413
        fun no_tyvar (_ `%% tys) = forall no_tyvar tys
haftmann@25621
   414
          | no_tyvar (ITyVar _) = false;
haftmann@25621
   415
      in if no_tyvar res_ty
haftmann@25621
   416
        then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys
haftmann@25621
   417
        else []
haftmann@25621
   418
      end
haftmann@25621
   419
  | _ => [];
haftmann@25621
   420
haftmann@24219
   421
haftmann@27103
   422
(** translation kernel **)
haftmann@24219
   423
haftmann@28724
   424
(* generic mechanisms *)
haftmann@28724
   425
haftmann@28663
   426
fun ensure_stmt lookup declare generate thing (dep, (naming, program)) =
haftmann@24219
   427
  let
haftmann@28706
   428
    fun add_dep name = case dep of NONE => I
haftmann@28706
   429
      | SOME dep => Graph.add_edge (dep, name);
haftmann@28706
   430
    val (name, naming') = case lookup naming thing
haftmann@28706
   431
     of SOME name => (name, naming)
haftmann@28706
   432
      | NONE => declare thing naming;
haftmann@28706
   433
  in case try (Graph.get_node program) name
haftmann@28706
   434
   of SOME stmt => program
haftmann@28663
   435
        |> add_dep name
haftmann@28706
   436
        |> pair naming'
haftmann@28663
   437
        |> pair dep
haftmann@28663
   438
        |> pair name
haftmann@28706
   439
    | NONE => program
haftmann@28706
   440
        |> Graph.default_node (name, NoStmt)
haftmann@28706
   441
        |> add_dep name
haftmann@28706
   442
        |> pair naming'
haftmann@28706
   443
        |> curry generate (SOME name)
haftmann@28706
   444
        ||> snd
haftmann@28706
   445
        |-> (fn stmt => (apsnd o Graph.map_node name) (K stmt))
haftmann@28706
   446
        |> pair dep
haftmann@28706
   447
        |> pair name
haftmann@24219
   448
  end;
haftmann@24219
   449
haftmann@26972
   450
fun not_wellsorted thy thm ty sort e =
haftmann@26972
   451
  let
haftmann@26972
   452
    val err_class = Sorts.class_error (Syntax.pp_global thy) e;
haftmann@26972
   453
    val err_thm = case thm
haftmann@30023
   454
     of SOME thm => "\n(in code equation " ^ Display.string_of_thm thm ^ ")" | NONE => "";
haftmann@26972
   455
    val err_typ = "Type " ^ Syntax.string_of_typ_global thy ty ^ " not of sort "
haftmann@26972
   456
      ^ Syntax.string_of_sort_global thy sort;
haftmann@26972
   457
  in error ("Wellsortedness error" ^ err_thm ^ ":\n" ^ err_typ ^ "\n" ^ err_class) end;
haftmann@26972
   458
haftmann@28724
   459
haftmann@28724
   460
(* translation *)
haftmann@28724
   461
haftmann@26972
   462
fun ensure_class thy (algbr as (_, algebra)) funcgr class =
haftmann@24918
   463
  let
haftmann@28924
   464
    val superclasses = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
wenzelm@24969
   465
    val cs = #params (AxClass.get_info thy class);
haftmann@24918
   466
    val stmt_class =
haftmann@24918
   467
      fold_map (fn superclass => ensure_class thy algbr funcgr superclass
haftmann@24918
   468
        ##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses
haftmann@24918
   469
      ##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c
haftmann@28688
   470
        ##>> translate_typ thy algbr funcgr ty) cs
haftmann@28663
   471
      #>> (fn info => Class (class, (unprefix "'" Name.aT, info)))
haftmann@28663
   472
  in ensure_stmt lookup_class (declare_class thy) stmt_class class end
haftmann@24918
   473
and ensure_classrel thy algbr funcgr (subclass, superclass) =
haftmann@24918
   474
  let
haftmann@24918
   475
    val stmt_classrel =
haftmann@24918
   476
      ensure_class thy algbr funcgr subclass
haftmann@24918
   477
      ##>> ensure_class thy algbr funcgr superclass
haftmann@24918
   478
      #>> Classrel;
haftmann@28663
   479
  in ensure_stmt lookup_classrel (declare_classrel thy) stmt_classrel (subclass, superclass) end
haftmann@28688
   480
and ensure_tyco thy algbr funcgr tyco =
haftmann@28688
   481
  let
haftmann@28688
   482
    val stmt_datatype =
haftmann@24918
   483
      let
haftmann@28688
   484
        val (vs, cos) = Code.get_datatype thy tyco;
haftmann@28688
   485
      in
haftmann@28688
   486
        fold_map (translate_tyvar_sort thy algbr funcgr) vs
haftmann@28688
   487
        ##>> fold_map (fn (c, tys) =>
haftmann@28688
   488
          ensure_const thy algbr funcgr c
haftmann@28688
   489
          ##>> fold_map (translate_typ thy algbr funcgr) tys) cos
haftmann@28688
   490
        #>> (fn info => Datatype (tyco, info))
haftmann@28688
   491
      end;
haftmann@28688
   492
  in ensure_stmt lookup_tyco (declare_tyco thy) stmt_datatype tyco end
haftmann@28688
   493
and translate_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) =
haftmann@24918
   494
  fold_map (ensure_class thy algbr funcgr) (proj_sort sort)
haftmann@24918
   495
  #>> (fn sort => (unprefix "'" v, sort))
haftmann@30009
   496
and translate_typ thy algbr funcgr (TFree (v, _)) =
haftmann@30009
   497
      pair (ITyVar (unprefix "'" v))
haftmann@28688
   498
  | translate_typ thy algbr funcgr (Type (tyco, tys)) =
haftmann@24918
   499
      ensure_tyco thy algbr funcgr tyco
haftmann@28688
   500
      ##>> fold_map (translate_typ thy algbr funcgr) tys
haftmann@24918
   501
      #>> (fn (tyco, tys) => tyco `%% tys)
haftmann@28688
   502
and translate_dicts thy (algbr as (proj_sort, algebra)) funcgr thm (ty, sort) =
haftmann@24918
   503
  let
wenzelm@26939
   504
    val pp = Syntax.pp_global thy;
haftmann@24918
   505
    datatype typarg =
haftmann@24918
   506
        Global of (class * string) * typarg list list
haftmann@24918
   507
      | Local of (class * class) list * (string * (int * sort));
haftmann@24918
   508
    fun class_relation (Global ((_, tyco), yss), _) class =
haftmann@24918
   509
          Global ((class, tyco), yss)
haftmann@24918
   510
      | class_relation (Local (classrels, v), subclass) superclass =
haftmann@24918
   511
          Local ((subclass, superclass) :: classrels, v);
haftmann@24918
   512
    fun type_constructor tyco yss class =
haftmann@29962
   513
      Global ((class, tyco), (map o map) fst yss);
haftmann@24918
   514
    fun type_variable (TFree (v, sort)) =
haftmann@24918
   515
      let
haftmann@24918
   516
        val sort' = proj_sort sort;
haftmann@24918
   517
      in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
haftmann@24918
   518
    val typargs = Sorts.of_sort_derivation pp algebra
haftmann@24918
   519
      {class_relation = class_relation, type_constructor = type_constructor,
haftmann@26972
   520
       type_variable = type_variable} (ty, proj_sort sort)
haftmann@26972
   521
      handle Sorts.CLASS_ERROR e => not_wellsorted thy thm ty sort e;
haftmann@24918
   522
    fun mk_dict (Global (inst, yss)) =
haftmann@24918
   523
          ensure_inst thy algbr funcgr inst
haftmann@24918
   524
          ##>> (fold_map o fold_map) mk_dict yss
haftmann@24918
   525
          #>> (fn (inst, dss) => DictConst (inst, dss))
haftmann@24918
   526
      | mk_dict (Local (classrels, (v, (k, sort)))) =
haftmann@24918
   527
          fold_map (ensure_classrel thy algbr funcgr) classrels
haftmann@24918
   528
          #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
haftmann@27422
   529
  in fold_map mk_dict typargs end
haftmann@28688
   530
and translate_eq thy algbr funcgr (thm, linear) =
haftmann@24918
   531
  let
haftmann@24918
   532
    val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
haftmann@24918
   533
      o Logic.unvarify o prop_of) thm;
haftmann@24918
   534
  in
haftmann@28688
   535
    fold_map (translate_term thy algbr funcgr (SOME thm)) args
haftmann@28688
   536
    ##>> translate_term thy algbr funcgr (SOME thm) rhs
haftmann@28350
   537
    #>> rpair (thm, linear)
haftmann@24918
   538
  end
haftmann@26972
   539
and ensure_inst thy (algbr as (_, algebra)) funcgr (class, tyco) =
haftmann@24918
   540
  let
haftmann@28924
   541
    val superclasses = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
wenzelm@24969
   542
    val classparams = these (try (#params o AxClass.get_info thy) class);
haftmann@24918
   543
    val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
haftmann@24918
   544
    val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
haftmann@24918
   545
    val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
haftmann@24918
   546
      Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
haftmann@24918
   547
    val arity_typ = Type (tyco, map TFree vs);
haftmann@24918
   548
    val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
haftmann@28688
   549
    fun translate_superarity superclass =
haftmann@24918
   550
      ensure_class thy algbr funcgr superclass
haftmann@24918
   551
      ##>> ensure_classrel thy algbr funcgr (class, superclass)
haftmann@28688
   552
      ##>> translate_dicts thy algbr funcgr NONE (arity_typ, [superclass])
haftmann@24918
   553
      #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
haftmann@24918
   554
            (superclass, (classrel, (inst, dss))));
haftmann@28688
   555
    fun translate_classparam_inst (c, ty) =
haftmann@24918
   556
      let
haftmann@24918
   557
        val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
haftmann@25597
   558
        val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy c_inst);
haftmann@24918
   559
        val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
haftmann@24918
   560
          o Logic.dest_equals o Thm.prop_of) thm;
haftmann@24918
   561
      in
haftmann@24918
   562
        ensure_const thy algbr funcgr c
haftmann@28688
   563
        ##>> translate_const thy algbr funcgr (SOME thm) c_ty
haftmann@28350
   564
        #>> (fn (c, IConst c_inst) => ((c, c_inst), (thm, true)))
haftmann@24918
   565
      end;
haftmann@24918
   566
    val stmt_inst =
haftmann@24918
   567
      ensure_class thy algbr funcgr class
haftmann@24918
   568
      ##>> ensure_tyco thy algbr funcgr tyco
haftmann@28688
   569
      ##>> fold_map (translate_tyvar_sort thy algbr funcgr) vs
haftmann@28688
   570
      ##>> fold_map translate_superarity superclasses
haftmann@28688
   571
      ##>> fold_map translate_classparam_inst classparams
haftmann@24918
   572
      #>> (fn ((((class, tyco), arity), superarities), classparams) =>
haftmann@24918
   573
             Classinst ((class, (tyco, arity)), (superarities, classparams)));
haftmann@28663
   574
  in ensure_stmt lookup_instance (declare_instance thy) stmt_inst (class, tyco) end
haftmann@26972
   575
and ensure_const thy algbr funcgr c =
haftmann@24918
   576
  let
haftmann@24918
   577
    fun stmt_datatypecons tyco =
haftmann@24918
   578
      ensure_tyco thy algbr funcgr tyco
haftmann@28663
   579
      #>> (fn tyco => Datatypecons (c, tyco));
haftmann@24918
   580
    fun stmt_classparam class =
haftmann@24918
   581
      ensure_class thy algbr funcgr class
haftmann@28663
   582
      #>> (fn class => Classparam (c, class));
haftmann@30023
   583
    fun stmt_fun ((vs, ty), raw_thms) =
haftmann@24918
   584
      let
wenzelm@29272
   585
        val thms = if null (Term.add_tfreesT ty []) orelse (null o fst o strip_type) ty
haftmann@24918
   586
          then raw_thms
haftmann@28423
   587
          else (map o apfst) (Code_Unit.expand_eta thy 1) raw_thms;
haftmann@24918
   588
      in
haftmann@28688
   589
        fold_map (translate_tyvar_sort thy algbr funcgr) vs
haftmann@28688
   590
        ##>> translate_typ thy algbr funcgr ty
haftmann@28688
   591
        ##>> fold_map (translate_eq thy algbr funcgr) thms
haftmann@28663
   592
        #>> (fn info => Fun (c, info))
haftmann@24918
   593
      end;
haftmann@28663
   594
    val stmt_const = case Code.get_datatype_of_constr thy c
haftmann@24918
   595
     of SOME tyco => stmt_datatypecons tyco
haftmann@24918
   596
      | NONE => (case AxClass.class_of_param thy c
haftmann@24918
   597
         of SOME class => stmt_classparam class
haftmann@30202
   598
          | NONE => stmt_fun (Code_Wellsorted.typ funcgr c, Code_Wellsorted.eqns funcgr c))
haftmann@28663
   599
  in ensure_stmt lookup_const (declare_const thy) stmt_const c end
haftmann@28688
   600
and translate_term thy algbr funcgr thm (Const (c, ty)) =
haftmann@28688
   601
      translate_app thy algbr funcgr thm ((c, ty), [])
haftmann@28688
   602
  | translate_term thy algbr funcgr thm (Free (v, _)) =
haftmann@24918
   603
      pair (IVar v)
haftmann@28688
   604
  | translate_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) =
haftmann@24918
   605
      let
haftmann@24918
   606
        val (v, t) = Syntax.variant_abs abs;
haftmann@24918
   607
      in
haftmann@28688
   608
        translate_typ thy algbr funcgr ty
haftmann@28688
   609
        ##>> translate_term thy algbr funcgr thm t
haftmann@24918
   610
        #>> (fn (ty, t) => (v, ty) `|-> t)
haftmann@24918
   611
      end
haftmann@28688
   612
  | translate_term thy algbr funcgr thm (t as _ $ _) =
haftmann@24918
   613
      case strip_comb t
haftmann@24918
   614
       of (Const (c, ty), ts) =>
haftmann@28688
   615
            translate_app thy algbr funcgr thm ((c, ty), ts)
haftmann@24918
   616
        | (t', ts) =>
haftmann@28688
   617
            translate_term thy algbr funcgr thm t'
haftmann@28688
   618
            ##>> fold_map (translate_term thy algbr funcgr thm) ts
haftmann@24918
   619
            #>> (fn (t, ts) => t `$$ ts)
haftmann@28688
   620
and translate_const thy algbr funcgr thm (c, ty) =
haftmann@26972
   621
  let
haftmann@26972
   622
    val tys = Sign.const_typargs thy (c, ty);
haftmann@30202
   623
    val sorts = (map snd o fst o Code_Wellsorted.typ funcgr) c;
haftmann@26972
   624
    val tys_args = (fst o Term.strip_type) ty;
haftmann@26972
   625
  in
haftmann@26972
   626
    ensure_const thy algbr funcgr c
haftmann@28688
   627
    ##>> fold_map (translate_dicts thy algbr funcgr thm) (tys ~~ sorts)
haftmann@28688
   628
    ##>> fold_map (translate_typ thy algbr funcgr) tys_args
haftmann@26972
   629
    #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
haftmann@26972
   630
  end
haftmann@29973
   631
and translate_app_const thy algbr funcgr thm (c_ty, ts) =
haftmann@28688
   632
  translate_const thy algbr funcgr thm c_ty
haftmann@28688
   633
  ##>> fold_map (translate_term thy algbr funcgr thm) ts
haftmann@24918
   634
  #>> (fn (t, ts) => t `$$ ts)
haftmann@29952
   635
and translate_case thy algbr funcgr thm (num_args, (t_pos, case_pats)) (c_ty, ts) =
haftmann@24918
   636
  let
haftmann@29952
   637
    val (tys, _) = (chop num_args o fst o strip_type o snd) c_ty;
haftmann@29952
   638
    val t = nth ts t_pos;
haftmann@29952
   639
    val ty = nth tys t_pos;
haftmann@29952
   640
    val ts_clause = nth_drop t_pos ts;
haftmann@29952
   641
    fun mk_clause (co, num_co_args) t =
haftmann@24918
   642
      let
haftmann@29952
   643
        val (vs, body) = Term.strip_abs_eta num_co_args t;
haftmann@29952
   644
        val not_undefined = case body
haftmann@29952
   645
         of (Const (c, _)) => not (Code.is_undefined thy c)
haftmann@29952
   646
          | _ => true;
haftmann@29952
   647
        val pat = list_comb (Const (co, map snd vs ---> ty), map Free vs);
haftmann@29952
   648
      in (not_undefined, (pat, body)) end;
haftmann@29952
   649
    val clauses = if null case_pats then let val ([v_ty], body) =
haftmann@29952
   650
        Term.strip_abs_eta 1 (the_single ts_clause)
haftmann@29952
   651
      in [(true, (Free v_ty, body))] end
haftmann@29952
   652
      else map (uncurry mk_clause)
haftmann@29952
   653
        (AList.make (Code_Unit.no_args thy) case_pats ~~ ts_clause);
haftmann@29952
   654
    fun retermify ty (_, (IVar x, body)) =
haftmann@29952
   655
          (x, ty) `|-> body
haftmann@29952
   656
      | retermify _ (_, (pat, body)) =
haftmann@24918
   657
          let
haftmann@29952
   658
            val (IConst (_, (_, tys)), ts) = unfold_app pat;
haftmann@29952
   659
            val vs = map2 (fn IVar x => fn ty => (x, ty)) ts tys;
haftmann@29952
   660
          in vs `|--> body end;
haftmann@29952
   661
    fun mk_icase const t ty clauses =
haftmann@29952
   662
      let
haftmann@29952
   663
        val (ts1, ts2) = chop t_pos (map (retermify ty) clauses);
haftmann@29952
   664
      in
haftmann@29952
   665
        ICase (((t, ty), map_filter (fn (b, d) => if b then SOME d else NONE) clauses),
haftmann@29952
   666
          const `$$ (ts1 @ t :: ts2))
haftmann@29952
   667
      end;
haftmann@24918
   668
  in
haftmann@29952
   669
    translate_const thy algbr funcgr thm c_ty
haftmann@29952
   670
    ##>> translate_term thy algbr funcgr thm t
haftmann@29952
   671
    ##>> translate_typ thy algbr funcgr ty
haftmann@29952
   672
    ##>> fold_map (fn (b, (pat, body)) => translate_term thy algbr funcgr thm pat
haftmann@29952
   673
      ##>> translate_term thy algbr funcgr thm body
haftmann@29952
   674
      #>> pair b) clauses
haftmann@29952
   675
    #>> (fn (((const, t), ty), ds) => mk_icase const t ty ds)
haftmann@24918
   676
  end
haftmann@29973
   677
and translate_app_case thy algbr funcgr thm (case_scheme as (num_args, _)) ((c, ty), ts) =
haftmann@29973
   678
  if length ts < num_args then
haftmann@29973
   679
    let
haftmann@29973
   680
      val k = length ts;
haftmann@29973
   681
      val tys = (curry Library.take (num_args - k) o curry Library.drop k o fst o strip_type) ty;
haftmann@29973
   682
      val ctxt = (fold o fold_aterms) Term.declare_term_frees ts Name.context;
haftmann@29973
   683
      val vs = Name.names ctxt "a" tys;
haftmann@29973
   684
    in
haftmann@29973
   685
      fold_map (translate_typ thy algbr funcgr) tys
haftmann@29973
   686
      ##>> translate_case thy algbr funcgr thm case_scheme ((c, ty), ts @ map Free vs)
haftmann@29973
   687
      #>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
haftmann@29973
   688
    end
haftmann@29973
   689
  else if length ts > num_args then
haftmann@29973
   690
    translate_case thy algbr funcgr thm case_scheme ((c, ty), Library.take (num_args, ts))
haftmann@29973
   691
    ##>> fold_map (translate_term thy algbr funcgr thm) (Library.drop (num_args, ts))
haftmann@29973
   692
    #>> (fn (t, ts) => t `$$ ts)
haftmann@29973
   693
  else
haftmann@29973
   694
    translate_case thy algbr funcgr thm case_scheme ((c, ty), ts)
haftmann@29973
   695
and translate_app thy algbr funcgr thm (c_ty_ts as ((c, _), _)) =
haftmann@29973
   696
  case Code.get_case_scheme thy c
haftmann@30023
   697
   of SOME case_scheme => translate_app_case thy algbr funcgr thm case_scheme c_ty_ts
haftmann@29973
   698
    | NONE => translate_app_const thy algbr funcgr thm c_ty_ts;
haftmann@24918
   699
haftmann@25969
   700
haftmann@27103
   701
(* store *)
haftmann@27103
   702
haftmann@27103
   703
structure Program = CodeDataFun
haftmann@27103
   704
(
haftmann@28663
   705
  type T = naming * program;
haftmann@28663
   706
  val empty = (empty_naming, Graph.empty);
haftmann@28706
   707
  fun purge thy cs (naming, program) =
haftmann@27609
   708
    let
haftmann@28706
   709
      val names_delete = cs
haftmann@28706
   710
        |> map_filter (lookup_const naming)
haftmann@28706
   711
        |> filter (can (Graph.get_node program))
haftmann@28706
   712
        |> Graph.all_preds program;
haftmann@28706
   713
      val program' = Graph.del_nodes names_delete program;
haftmann@28706
   714
    in (naming, program') end;
haftmann@27103
   715
);
haftmann@27103
   716
haftmann@27103
   717
val cached_program = Program.get;
haftmann@25969
   718
haftmann@28924
   719
fun invoke_generation thy (algebra, funcgr) f name =
haftmann@28663
   720
  Program.change_yield thy (fn naming_program => (NONE, naming_program)
haftmann@28924
   721
    |> f thy algebra funcgr name
haftmann@28663
   722
    |-> (fn name => fn (_, naming_program) => (name, naming_program)));
haftmann@27103
   723
haftmann@27103
   724
haftmann@27103
   725
(* program generation *)
haftmann@27103
   726
haftmann@27103
   727
fun consts_program thy cs =
haftmann@27103
   728
  let
haftmann@28663
   729
    fun project_consts cs (naming, program) =
haftmann@27103
   730
      let
haftmann@27103
   731
        val cs_all = Graph.all_succs program cs;
haftmann@28663
   732
      in (cs, (naming, Graph.subgraph (member (op =) cs_all) program)) end;
haftmann@27103
   733
    fun generate_consts thy algebra funcgr =
haftmann@27103
   734
      fold_map (ensure_const thy algebra funcgr);
haftmann@27103
   735
  in
haftmann@30202
   736
    invoke_generation thy (Code_Wellsorted.make thy cs) generate_consts cs
haftmann@27103
   737
    |-> project_consts
haftmann@27103
   738
  end;
haftmann@27103
   739
haftmann@27103
   740
haftmann@27103
   741
(* value evaluation *)
haftmann@25969
   742
haftmann@24918
   743
fun ensure_value thy algbr funcgr t = 
haftmann@24918
   744
  let
haftmann@24918
   745
    val ty = fastype_of t;
haftmann@24918
   746
    val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
haftmann@24918
   747
      o dest_TFree))) t [];
haftmann@24918
   748
    val stmt_value =
haftmann@28688
   749
      fold_map (translate_tyvar_sort thy algbr funcgr) vs
haftmann@28688
   750
      ##>> translate_typ thy algbr funcgr ty
haftmann@28688
   751
      ##>> translate_term thy algbr funcgr NONE t
haftmann@28663
   752
      #>> (fn ((vs, ty), t) => Fun
haftmann@28663
   753
        (Term.dummy_patternN, ((vs, ty), [(([], t), (Drule.dummy_thm, true))])));
haftmann@28663
   754
    fun term_value (dep, (naming, program1)) =
haftmann@25969
   755
      let
haftmann@28663
   756
        val Fun (_, ((vs, ty), [(([], t), _)])) =
haftmann@28663
   757
          Graph.get_node program1 Term.dummy_patternN;
haftmann@28663
   758
        val deps = Graph.imm_succs program1 Term.dummy_patternN;
haftmann@28663
   759
        val program2 = Graph.del_nodes [Term.dummy_patternN] program1;
haftmann@27103
   760
        val deps_all = Graph.all_succs program2 deps;
haftmann@27103
   761
        val program3 = Graph.subgraph (member (op =) deps_all) program2;
haftmann@28663
   762
      in (((naming, program3), (((vs, ty), t), deps)), (dep, (naming, program2))) end;
haftmann@26011
   763
  in
haftmann@28663
   764
    ensure_stmt ((K o K) NONE) pair stmt_value Term.dummy_patternN
haftmann@26011
   765
    #> snd
haftmann@26011
   766
    #> term_value
haftmann@26011
   767
  end;
haftmann@24219
   768
haftmann@28724
   769
fun eval thy evaluator t =
haftmann@27103
   770
  let
haftmann@28724
   771
    val (t', evaluator'') = evaluator t;
haftmann@28924
   772
    fun evaluator' algebra funcgr =
haftmann@27103
   773
      let
haftmann@28924
   774
        val (((naming, program), (vs_ty_t, deps)), _) =
haftmann@28924
   775
          invoke_generation thy (algebra, funcgr) ensure_value t';
haftmann@28724
   776
      in evaluator'' naming program vs_ty_t deps end;
haftmann@28724
   777
  in (t', evaluator') end
haftmann@27103
   778
haftmann@30202
   779
fun eval_conv thy = Code_Wellsorted.eval_conv thy o eval thy;
haftmann@30202
   780
fun eval_term thy = Code_Wellsorted.eval_term thy o eval thy;
haftmann@27103
   781
haftmann@24219
   782
end; (*struct*)
haftmann@24219
   783
haftmann@24219
   784
haftmann@28054
   785
structure Basic_Code_Thingol: BASIC_CODE_THINGOL = Code_Thingol;