src/Pure/Tools/codegen_func.ML
author haftmann
Thu, 26 Apr 2007 13:33:12 +0200
changeset 22804 d3c23b90c6c6
parent 22737 d87ccbcc2702
child 22900 f8a7c10e1bd0
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/codegen_func.ML
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     2
    ID:         $Id$
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     4
22185
haftmann
parents: 22049
diff changeset
     5
Basic handling of defining equations ("func"s) for code generator framework.
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     6
*)
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     7
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     8
signature CODEGEN_FUNC =
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
     9
sig
22185
haftmann
parents: 22049
diff changeset
    10
  val assert_rew: thm -> thm
22737
haftmann
parents: 22705
diff changeset
    11
  val mk_rew: thm -> thm
haftmann
parents: 22705
diff changeset
    12
  val mk_func: thm -> thm
haftmann
parents: 22705
diff changeset
    13
  val head_func: thm -> CodegenConsts.const * typ
haftmann
parents: 22705
diff changeset
    14
  val bad_thm: string -> 'a
haftmann
parents: 22705
diff changeset
    15
  val error_thm: (thm -> thm) -> thm -> thm
haftmann
parents: 22705
diff changeset
    16
  val warning_thm: (thm -> thm) -> thm -> thm option
22185
haftmann
parents: 22049
diff changeset
    17
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
    18
  val inst_thm: sort Vartab.table -> thm -> thm
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    19
  val expand_eta: int -> thm -> thm
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    20
  val rewrite_func: thm list -> thm -> thm
22211
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
    21
  val norm_args: thm list -> thm list 
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
    22
  val norm_varnames: (string -> string) -> (string -> string) -> thm list -> thm list 
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
    23
end;
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
    24
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
    25
structure CodegenFunc : CODEGEN_FUNC =
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
    26
struct
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
    27
22737
haftmann
parents: 22705
diff changeset
    28
haftmann
parents: 22705
diff changeset
    29
(* auxiliary *)
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    30
22737
haftmann
parents: 22705
diff changeset
    31
exception BAD_THM of string;
haftmann
parents: 22705
diff changeset
    32
fun bad_thm msg = raise BAD_THM msg;
haftmann
parents: 22705
diff changeset
    33
fun error_thm f thm = f thm handle BAD_THM msg => error msg;
haftmann
parents: 22705
diff changeset
    34
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg
haftmann
parents: 22705
diff changeset
    35
  => (warning msg; NONE);
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    36
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    37
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    38
(* making rewrite theorems *)
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    39
22185
haftmann
parents: 22049
diff changeset
    40
fun assert_rew thm =
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    41
  let
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    42
    val thy = Thm.theory_of_thm thm;
22737
haftmann
parents: 22705
diff changeset
    43
    val (lhs, rhs) = (Logic.dest_equals o Thm.prop_of) thm
haftmann
parents: 22705
diff changeset
    44
      handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm);
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    45
    fun vars_of t = fold_aterms
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    46
     (fn Var (v, _) => insert (op =) v
22737
haftmann
parents: 22705
diff changeset
    47
       | Free _ => bad_thm ("Illegal free variable in rewrite theorem\n"
haftmann
parents: 22705
diff changeset
    48
           ^ Display.string_of_thm thm)
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    49
       | _ => I) t [];
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    50
    fun tvars_of t = fold_term_types
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    51
     (fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v
22737
haftmann
parents: 22705
diff changeset
    52
                          | TFree _ => bad_thm 
haftmann
parents: 22705
diff changeset
    53
      ("Illegal free type variable in rewrite theorem\n" ^ Display.string_of_thm thm))) t [];
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    54
    val lhs_vs = vars_of lhs;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    55
    val rhs_vs = vars_of rhs;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    56
    val lhs_tvs = tvars_of lhs;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    57
    val rhs_tvs = tvars_of lhs;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    58
    val _ = if null (subtract (op =) lhs_vs rhs_vs)
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    59
      then ()
22737
haftmann
parents: 22705
diff changeset
    60
      else bad_thm ("Free variables on right hand side of rewrite theorem\n"
haftmann
parents: 22705
diff changeset
    61
        ^ Display.string_of_thm thm);
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    62
    val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    63
      then ()
22737
haftmann
parents: 22705
diff changeset
    64
      else bad_thm ("Free type variables on right hand side of rewrite theorem\n"
haftmann
parents: 22705
diff changeset
    65
        ^ Display.string_of_thm thm)
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    66
  in thm end;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    67
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    68
fun mk_rew thm =
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
    69
  let
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    70
    val thy = Thm.theory_of_thm thm;
22737
haftmann
parents: 22705
diff changeset
    71
    val ctxt = ProofContext.init thy;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    72
  in
22737
haftmann
parents: 22705
diff changeset
    73
    thm
haftmann
parents: 22705
diff changeset
    74
    |> LocalDefs.meta_rewrite_rule ctxt
haftmann
parents: 22705
diff changeset
    75
    |> assert_rew
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    76
  end;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    77
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    78
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
    79
(* making defining equations *)
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
    80
22737
haftmann
parents: 22705
diff changeset
    81
fun assert_func thm =
haftmann
parents: 22705
diff changeset
    82
  let
haftmann
parents: 22705
diff changeset
    83
    val thy = Thm.theory_of_thm thm;
22804
haftmann
parents: 22737
diff changeset
    84
    val (head, args) = (strip_comb o fst o Logic.dest_equals
22737
haftmann
parents: 22705
diff changeset
    85
      o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm;
22804
haftmann
parents: 22737
diff changeset
    86
    val _ = case head of Const _ => () | _ =>
haftmann
parents: 22737
diff changeset
    87
      bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm);
22737
haftmann
parents: 22705
diff changeset
    88
    val _ =
haftmann
parents: 22705
diff changeset
    89
      if has_duplicates (op =)
haftmann
parents: 22705
diff changeset
    90
        ((fold o fold_aterms) (fn Var (v, _) => cons v
haftmann
parents: 22705
diff changeset
    91
          | _ => I
haftmann
parents: 22705
diff changeset
    92
        ) args [])
haftmann
parents: 22705
diff changeset
    93
      then bad_thm ("Duplicated variables on left hand side of equation\n"
haftmann
parents: 22705
diff changeset
    94
        ^ Display.string_of_thm thm)
haftmann
parents: 22705
diff changeset
    95
      else ()
haftmann
parents: 22705
diff changeset
    96
    fun check _ (Abs _) = bad_thm
haftmann
parents: 22705
diff changeset
    97
          ("Abstraction on left hand side of equation\n"
haftmann
parents: 22705
diff changeset
    98
            ^ Display.string_of_thm thm)
haftmann
parents: 22705
diff changeset
    99
      | check 0 (Var _) = ()
haftmann
parents: 22705
diff changeset
   100
      | check _ (Var _) = bad_thm
haftmann
parents: 22705
diff changeset
   101
          ("Variable with application on left hand side of defining equation\n"
haftmann
parents: 22705
diff changeset
   102
            ^ Display.string_of_thm thm)
haftmann
parents: 22705
diff changeset
   103
      | check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
haftmann
parents: 22705
diff changeset
   104
      | check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty
haftmann
parents: 22705
diff changeset
   105
          then bad_thm
haftmann
parents: 22705
diff changeset
   106
            ("Partially applied constant on left hand side of equation"
haftmann
parents: 22705
diff changeset
   107
               ^ Display.string_of_thm thm)
haftmann
parents: 22705
diff changeset
   108
          else ();
haftmann
parents: 22705
diff changeset
   109
    val _ = map (check 0) args;
haftmann
parents: 22705
diff changeset
   110
  in thm end;
22484
25dfebd7b4c8 improved treatment of defining equations stemming from specification tools
haftmann
parents: 22475
diff changeset
   111
22737
haftmann
parents: 22705
diff changeset
   112
val mk_func = assert_func o Drule.fconv_rule Drule.beta_eta_conversion o mk_rew;
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   113
22737
haftmann
parents: 22705
diff changeset
   114
fun head_func thm =
haftmann
parents: 22705
diff changeset
   115
  let
haftmann
parents: 22705
diff changeset
   116
    val thy = Thm.theory_of_thm thm;
haftmann
parents: 22705
diff changeset
   117
    val (Const (c_ty as (_, ty))) = (fst o strip_comb o fst o Logic.dest_equals
haftmann
parents: 22705
diff changeset
   118
      o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm;
haftmann
parents: 22705
diff changeset
   119
    val const = CodegenConsts.const_of_cexpr thy c_ty;
haftmann
parents: 22705
diff changeset
   120
  in (const, ty) end;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   121
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   122
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   123
(* utilities *)
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   124
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   125
fun inst_thm tvars' thm =
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   126
  let
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   127
    val thy = Thm.theory_of_thm thm;
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   128
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   129
    fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   130
     of SOME sort => SOME (pairself (Thm.ctyp_of thy o TVar) (tvar, (v, sort)))
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   131
      | NONE => NONE;
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   132
    val insts = map_filter mk_inst tvars;
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   133
  in Thm.instantiate (insts, []) thm end;
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   134
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   135
fun expand_eta k thm =
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   136
  let
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   137
    val thy = Thm.theory_of_thm thm;
22705
6199df39688d Thm.plain_prop_of;
wenzelm
parents: 22554
diff changeset
   138
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm;
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   139
    val (head, args) = strip_comb lhs;
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   140
    val l = if k = ~1
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   141
      then (length o fst o strip_abs) rhs
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   142
      else Int.max (0, k - length args);
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   143
    val used = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   144
    fun get_name _ 0 used = ([], used)
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   145
      | get_name (Abs (v, ty, t)) k used =
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   146
          used
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   147
          |> Name.variants [v]
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   148
          ||>> get_name t (k - 1)
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   149
          |>> (fn ([v'], vs') => (v', ty) :: vs')
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   150
      | get_name t k used = 
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   151
          let
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   152
            val (tys, _) = (strip_type o fastype_of) t
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   153
          in case tys
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   154
           of [] => raise TERM ("expand_eta", [t])
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   155
            | ty :: _ =>
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   156
                used
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   157
                |> Name.variants [""]
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   158
                |-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1)
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   159
                #>> (fn vs' => (v, ty) :: vs'))
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   160
          end;
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   161
    val (vs, _) = get_name rhs l used;
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   162
    val vs_refl = map (fn (v, ty) => Thm.reflexive (Thm.cterm_of thy (Var ((v, 0), ty)))) vs;
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   163
  in
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   164
    fold (fn refl => fn thm => Thm.combination thm refl) vs_refl thm
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   165
  end;
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   166
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   167
fun rewrite_func rewrites thm =
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   168
  let
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   169
    val rewrite = MetaSimplifier.rewrite false rewrites;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   170
    val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   171
    val Const ("==", _) = Thm.term_of ct_eq;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   172
    val (ct_f, ct_args) = Drule.strip_comb ct_lhs;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   173
    val rhs' = rewrite ct_rhs;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   174
    val args' = map rewrite ct_args;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   175
    val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1)
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   176
      args' (Thm.reflexive ct_f));
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   177
  in Thm.transitive (Thm.transitive lhs' thm) rhs' end;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22023
diff changeset
   178
22211
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   179
fun norm_args thms =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   180
  let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   181
    val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals;
22705
6199df39688d Thm.plain_prop_of;
wenzelm
parents: 22554
diff changeset
   182
    val k = fold (curry Int.max o num_args_of o Thm.plain_prop_of) thms 0;
22211
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   183
  in
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   184
    thms
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   185
    |> map (expand_eta k)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   186
    |> map (Drule.fconv_rule Drule.beta_eta_conversion)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   187
  end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   188
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   189
fun canonical_tvars purify_tvar thm =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   190
  let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   191
    val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm);
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   192
    fun tvars_subst_for thm = (fold_types o fold_atyps)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   193
      (fn TVar (v_i as (v, _), sort) => let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   194
            val v' = purify_tvar v
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   195
          in if v = v' then I
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   196
          else insert (op =) (v_i, (v', sort)) end
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   197
        | _ => I) (prop_of thm) [];
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   198
    fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   199
      let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   200
        val ty = TVar (v_i, sort)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   201
      in
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   202
        (maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   203
      end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   204
    val maxidx = Thm.maxidx_of thm + 1;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   205
    val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   206
  in Thm.instantiate (inst, []) thm end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   207
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   208
fun canonical_vars purify_var thm =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   209
  let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   210
    val cterm = Thm.cterm_of (Thm.theory_of_thm thm);
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   211
    fun vars_subst_for thm = fold_aterms
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   212
      (fn Var (v_i as (v, _), ty) => let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   213
            val v' = purify_var v
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   214
          in if v = v' then I
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   215
          else insert (op =) (v_i, (v', ty)) end
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   216
        | _ => I) (prop_of thm) [];
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   217
    fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   218
      let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   219
        val t = Var (v_i, ty)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   220
      in
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   221
        (maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   222
      end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   223
    val maxidx = Thm.maxidx_of thm + 1;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   224
    val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   225
  in Thm.instantiate ([], inst) thm end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   226
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   227
fun canonical_absvars purify_var thm =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   228
  let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   229
    val t = Thm.prop_of thm;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   230
    val t' = Term.map_abs_vars purify_var t;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   231
  in Thm.rename_boundvars t t' thm end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   232
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   233
fun norm_varnames purify_tvar purify_var thms =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   234
  let
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   235
    fun burrow_thms f [] = []
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   236
      | burrow_thms f thms =
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   237
          thms
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   238
          |> Conjunction.intr_list
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   239
          |> f
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   240
          |> Conjunction.elim_list;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   241
  in
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   242
    thms
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   243
    |> burrow_thms (canonical_tvars purify_tvar)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   244
    |> map (canonical_vars purify_var)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   245
    |> map (canonical_absvars purify_var)
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   246
    |> map Drule.zero_var_indexes
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   247
  end;
e2b5f3d24a17 additional auxiliary here
haftmann
parents: 22197
diff changeset
   248
22023
487b79b95a20 added codegen_func.ML
haftmann
parents:
diff changeset
   249
end;