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