src/Pure/Isar/code_unit.ML
author haftmann
Thu, 25 Sep 2008 09:28:03 +0200
changeset 28346 b8390cd56b8f
parent 28310 e7adede08de5
child 28368 8437fb395294
permissions -rw-r--r--
discontinued special treatment of op = vs. eq_class.eq
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Isar/code_unit.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
     5
Basic notions of code generation.  Auxiliary.
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
signature CODE_UNIT =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
sig
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    10
  (*generic non-sense*)
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    11
  val bad_thm: string -> 'a
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    12
  val error_thm: (thm -> thm) -> thm -> thm
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    13
  val warning_thm: (thm -> thm) -> thm -> thm option
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    14
  val try_thm: (thm -> thm) -> thm -> thm option
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    15
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    16
  (*typ instantiations*)
26970
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    17
  val typscheme: theory -> string * typ -> (string * sort) list * typ
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    18
  val inst_thm: sort Vartab.table -> thm -> thm
25540
e209975d5606 added constrain_thm
haftmann
parents: 25485
diff changeset
    19
  val constrain_thm: sort -> thm -> thm
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    20
26747
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
    21
  (*constant aliasses*)
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    22
  val add_const_alias: thm -> theory -> theory
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
    23
  val triv_classes: theory -> class list
26747
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
    24
  val resubst_alias: theory -> string -> string
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
    25
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
    26
  (*constants*)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
  val string_of_typ: theory -> typ -> string
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    28
  val string_of_const: theory -> string -> string
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    29
  val no_args: theory -> string -> int
26112
ac2ce7242eae added further interface for reading constants
haftmann
parents: 25597
diff changeset
    30
  val check_const: theory -> term -> string
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
  val read_bare_const: theory -> string -> string * typ
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    32
  val read_const: theory -> string -> string
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    34
  (*constructor sets*)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    35
  val constrset_of_consts: theory -> (string * typ) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
    -> string * ((string * sort) list * (string * typ list) list)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    38
  (*defining equations*)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
  val assert_rew: thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
  val mk_rew: thm -> thm
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
    41
  val mk_func: bool -> thm -> thm
26970
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    42
  val head_func: thm -> string * ((string * sort) list * typ)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  val expand_eta: int -> thm -> thm
27582
367aff8d7ffd dropped junk
haftmann
parents: 27558
diff changeset
    44
  val rewrite_func: simpset -> thm -> thm
367aff8d7ffd dropped junk
haftmann
parents: 27558
diff changeset
    45
  val rewrite_head: thm list -> thm -> thm
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
  val norm_args: thm list -> thm list 
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    47
  val norm_varnames: (string -> string) -> (string -> string) -> thm list -> thm list
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    48
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    49
  (*case certificates*)
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
    50
  val case_cert: thm -> string * (int * string list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 28015
diff changeset
    53
structure Code_Unit: CODE_UNIT =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
(* auxiliary *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
exception BAD_THM of string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
fun bad_thm msg = raise BAD_THM msg;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
fun error_thm f thm = f thm handle BAD_THM msg => error msg;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
  => (warning ("code generator: " ^ msg); NONE);
24624
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24423
diff changeset
    64
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26747
diff changeset
    66
fun string_of_typ thy = setmp show_sorts true (Syntax.string_of_typ_global thy);
25597
34860182b250 moved instance parameter management from class.ML to axclass.ML
haftmann
parents: 25540
diff changeset
    67
fun string_of_const thy c = case AxClass.inst_of_param thy c
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    68
 of SOME (c, tyco) => Sign.extern_const thy c ^ " " ^ enclose "[" "]" (Sign.extern_type thy tyco)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    69
  | NONE => Sign.extern_const thy c;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    71
fun no_args thy = length o fst o strip_type o Sign.the_const_type thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    73
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    74
(* utilities *)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    75
26970
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    76
fun typscheme thy (c, ty) =
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    77
  let
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    78
    fun dest (TVar ((v, 0), sort)) = (v, sort)
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    79
      | dest ty = error ("Illegal type parameter in type scheme: " ^ Syntax.string_of_typ_global thy ty);
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    80
    val vs = map dest (Sign.const_typargs thy (c, ty));
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    81
  in (vs, ty) end;
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
    82
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    83
fun inst_thm tvars' thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    84
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    85
    val thy = Thm.theory_of_thm thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    86
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
28310
e7adede08de5 corrected sort intersection
haftmann
parents: 28054
diff changeset
    87
    val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
e7adede08de5 corrected sort intersection
haftmann
parents: 28054
diff changeset
    88
    fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v
e7adede08de5 corrected sort intersection
haftmann
parents: 28054
diff changeset
    89
     of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar)
e7adede08de5 corrected sort intersection
haftmann
parents: 28054
diff changeset
    90
          (tvar, (v, inter_sort (sort, sort'))))
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    91
      | NONE => NONE;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    92
    val insts = map_filter mk_inst tvars;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    93
  in Thm.instantiate (insts, []) thm end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    94
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    95
fun constrain_thm sort thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    96
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    97
    val thy = Thm.theory_of_thm thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    98
    val constrain = curry (Sorts.inter_sort (Sign.classes_of thy)) sort
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
    99
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   100
    fun mk_inst (tvar as (v, sort)) = pairself (Thm.ctyp_of thy o TVar o pair v)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   101
      (sort, constrain sort)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   102
    val insts = map mk_inst tvars;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   103
  in Thm.instantiate (insts, []) thm end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   104
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   105
fun expand_eta k thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   106
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   107
    val thy = Thm.theory_of_thm thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   108
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   109
    val (head, args) = strip_comb lhs;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   110
    val l = if k = ~1
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   111
      then (length o fst o strip_abs) rhs
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   112
      else Int.max (0, k - length args);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   113
    val used = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   114
    fun get_name _ 0 = pair []
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   115
      | get_name (Abs (v, ty, t)) k =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   116
          Name.variants [v]
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   117
          ##>> get_name t (k - 1)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   118
          #>> (fn ([v'], vs') => (v', ty) :: vs')
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   119
      | get_name t k = 
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   120
          let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   121
            val (tys, _) = (strip_type o fastype_of) t
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   122
          in case tys
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   123
           of [] => raise TERM ("expand_eta", [t])
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   124
            | ty :: _ =>
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   125
                Name.variants [""]
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   126
                #-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   127
                #>> (fn vs' => (v, ty) :: vs'))
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   128
          end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   129
    val (vs, _) = get_name rhs l used;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   130
    fun expand (v, ty) thm = Drule.fun_cong_rule thm
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   131
      (Thm.cterm_of thy (Var ((v, 0), ty)));
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   132
  in
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   133
    thm
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   134
    |> fold expand vs
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   135
    |> Conv.fconv_rule Drule.beta_eta_conversion
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   136
  end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   137
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   138
fun func_conv conv =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   139
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   140
    fun lhs_conv ct = if can Thm.dest_comb ct
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   141
      then (Conv.combination_conv lhs_conv conv) ct
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   142
      else Conv.all_conv ct;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   143
  in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   144
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   145
fun head_conv conv =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   146
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   147
    fun lhs_conv ct = if can Thm.dest_comb ct
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   148
      then (Conv.fun_conv lhs_conv) ct
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   149
      else conv ct;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   150
  in Conv.fun_conv (Conv.arg_conv lhs_conv) end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   151
27582
367aff8d7ffd dropped junk
haftmann
parents: 27558
diff changeset
   152
val rewrite_func = Conv.fconv_rule o func_conv o Simplifier.rewrite;
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   153
val rewrite_head = Conv.fconv_rule o head_conv o MetaSimplifier.rewrite false;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   154
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   155
fun norm_args thms =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   156
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   157
    val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals;
28310
e7adede08de5 corrected sort intersection
haftmann
parents: 28054
diff changeset
   158
    val k = fold (curry Int.max o num_args_of o Thm.prop_of) thms 0;
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   159
  in
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   160
    thms
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   161
    |> map (expand_eta k)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   162
    |> map (Conv.fconv_rule Drule.beta_eta_conversion)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   163
  end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   164
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   165
fun canonical_tvars purify_tvar thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   166
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   167
    val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   168
    fun tvars_subst_for thm = (fold_types o fold_atyps)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   169
      (fn TVar (v_i as (v, _), sort) => let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   170
            val v' = purify_tvar v
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   171
          in if v = v' then I
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   172
          else insert (op =) (v_i, (v', sort)) end
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   173
        | _ => I) (prop_of thm) [];
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   174
    fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   175
      let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   176
        val ty = TVar (v_i, sort)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   177
      in
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   178
        (maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   179
      end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   180
    val maxidx = Thm.maxidx_of thm + 1;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   181
    val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   182
  in Thm.instantiate (inst, []) thm end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   183
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   184
fun canonical_vars purify_var thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   185
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   186
    val cterm = Thm.cterm_of (Thm.theory_of_thm thm);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   187
    fun vars_subst_for thm = fold_aterms
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   188
      (fn Var (v_i as (v, _), ty) => let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   189
            val v' = purify_var v
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   190
          in if v = v' then I
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   191
          else insert (op =) (v_i, (v', ty)) end
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   192
        | _ => I) (prop_of thm) [];
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   193
    fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   194
      let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   195
        val t = Var (v_i, ty)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   196
      in
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   197
        (maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   198
      end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   199
    val maxidx = Thm.maxidx_of thm + 1;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   200
    val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   201
  in Thm.instantiate ([], inst) thm end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   202
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   203
fun canonical_absvars purify_var thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   204
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   205
    val t = Thm.plain_prop_of thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   206
    val t' = Term.map_abs_vars purify_var t;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   207
  in Thm.rename_boundvars t t' thm end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   208
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   209
fun norm_varnames purify_tvar purify_var thms =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   210
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   211
    fun burrow_thms f [] = []
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   212
      | burrow_thms f thms =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   213
          thms
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   214
          |> Conjunction.intr_balanced
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   215
          |> f
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   216
          |> Conjunction.elim_balanced (length thms)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   217
  in
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   218
    thms
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   219
    |> burrow_thms (canonical_tvars purify_tvar)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   220
    |> map (canonical_vars purify_var)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   221
    |> map (canonical_absvars purify_var)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   222
    |> map Drule.zero_var_indexes
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   223
  end;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   224
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   225
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   226
(* const aliasses *)
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   227
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   228
structure ConstAlias = TheoryDataFun
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   229
(
26747
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   230
  type T = ((string * string) * thm) list * class list;
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   231
  val empty = ([], []);
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   232
  val copy = I;
26618
wenzelm
parents: 26610
diff changeset
   233
  val extend = I;
26747
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   234
  fun merge _ ((alias1, classes1), (alias2, classes2)) =
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   235
    (Library.merge (eq_snd Thm.eq_thm_prop) (alias1, alias2),
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   236
      Library.merge (op =) (classes1, classes2));
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   237
);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   238
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   239
fun add_const_alias thm =
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   240
  let
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   241
    val t = Thm.prop_of thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   242
    val thy = Thm.theory_of_thm thm;
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   243
    val lhs_rhs = case try Logic.dest_equals t
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   244
     of SOME lhs_rhs => lhs_rhs
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   245
      | _ => error ("Not an equation: " ^ Display.string_of_thm thm);
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   246
    val c_c' = case try (pairself (AxClass.unoverload_const thy o dest_Const)) lhs_rhs
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   247
     of SOME c_c' => c_c'
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   248
      | _ => error ("Not an equation with two constants: " ^ Display.string_of_thm thm);
26747
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   249
    val some_class = the_list (AxClass.class_of_param thy (snd c_c'));
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   250
  in
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   251
    ConstAlias.map (fn (alias, classes) =>
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   252
      ((c_c', thm) :: alias, fold (insert (op =)) some_class classes))
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   253
  end;
26354
46c7d00dd4b4 rearranged
haftmann
parents: 26330
diff changeset
   254
26747
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   255
fun resubst_alias thy =
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   256
  let
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   257
    val alias = fst (ConstAlias.get thy);
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   258
    val subst_inst_param = Option.map fst o AxClass.inst_of_param thy;
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   259
    fun subst_alias c =
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   260
      get_first (fn ((c', c''), _) => if c = c'' then SOME c' else NONE) alias;
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   261
  in
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   262
    perhaps subst_inst_param
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   263
    #> perhaps subst_alias
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   264
  end;
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   265
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   266
val triv_classes = snd o ConstAlias.get;
f32fa5f5bdd1 moved 'trivial classes' to foundation of code generator
haftmann
parents: 26618
diff changeset
   267
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   268
26610
df8c1ffdb8cc export subst_alias;
wenzelm
parents: 26519
diff changeset
   269
(* reading constants as terms *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
26112
ac2ce7242eae added further interface for reading constants
haftmann
parents: 25597
diff changeset
   271
fun check_bare_const thy t = case try dest_Const t
ac2ce7242eae added further interface for reading constants
haftmann
parents: 25597
diff changeset
   272
 of SOME c_ty => c_ty
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26747
diff changeset
   273
  | NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
26112
ac2ce7242eae added further interface for reading constants
haftmann
parents: 25597
diff changeset
   274
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   275
fun check_const thy = AxClass.unoverload_const thy o check_bare_const thy;
26112
ac2ce7242eae added further interface for reading constants
haftmann
parents: 25597
diff changeset
   276
ac2ce7242eae added further interface for reading constants
haftmann
parents: 25597
diff changeset
   277
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   279
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   282
(* constructor sets *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   284
fun constrset_of_consts thy cs =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   286
    fun no_constr (c, ty) = error ("Not a datatype constructor: " ^ string_of_const thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   287
      ^ " :: " ^ string_of_typ thy ty);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   288
    fun last_typ c_ty ty =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   289
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   290
        val frees = typ_tfrees ty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   291
        val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) ty
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   292
          handle TYPE _ => no_constr c_ty
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   293
        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr c_ty else ();
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   294
        val _ = if length frees <> length vs then no_constr c_ty else ();
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   295
      in (tyco, vs) end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   296
    fun ty_sorts (c, ty) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   297
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   298
        val ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c;
26970
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
   299
        val (tyco, _) = last_typ (c, ty) ty_decl;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   300
        val (_, vs) = last_typ (c, ty) ty;
28015
11635f41abc1 proper handling of type variabl names
haftmann
parents: 27610
diff changeset
   301
      in ((tyco, map snd vs), (c, (map fst vs, ty))) end;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   302
    fun add ((tyco', sorts'), c) ((tyco, sorts), cs) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   303
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   304
        val _ = if tyco' <> tyco
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   305
          then error "Different type constructors in constructor set"
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   306
          else ();
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   307
        val sorts'' = map2 (curry (Sorts.inter_sort (Sign.classes_of thy))) sorts' sorts
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   308
      in ((tyco, sorts), c :: cs) end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   309
    fun inst vs' (c, (vs, ty)) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   310
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   311
        val the_v = the o AList.lookup (op =) (vs ~~ vs');
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   312
        val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   313
      in (c, (fst o strip_type) ty') end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   314
    val c' :: cs' = map ty_sorts cs;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   315
    val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
24848
5dbbd33c3236 replaced literal 'a by Name.aT;
wenzelm
parents: 24844
diff changeset
   316
    val vs = Name.names Name.context Name.aT sorts;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   317
    val cs''' = map (inst vs) cs'';
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   318
  in (tyco, (vs, cs''')) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
26239
e105d24d15c1 some steps towards a refined treatment of equality
haftmann
parents: 26112
diff changeset
   321
(* rewrite theorems *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
fun assert_rew thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
      handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
          | THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
    fun vars_of t = fold_aterms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
     (fn Var (v, _) => insert (op =) v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
       | Free _ => bad_thm ("Illegal free variable in rewrite theorem\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
           ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
       | _ => I) t [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
    fun tvars_of t = fold_term_types
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
     (fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
                          | TFree _ => bad_thm 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
      ("Illegal free type variable in rewrite theorem\n" ^ Display.string_of_thm thm))) t [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
    val lhs_vs = vars_of lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
    val rhs_vs = vars_of rhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
    val lhs_tvs = tvars_of lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
    val rhs_tvs = tvars_of lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
    val _ = if null (subtract (op =) lhs_vs rhs_vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
      then ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
      else bad_thm ("Free variables on right hand side of rewrite theorem\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
        ^ Display.string_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
    val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
      then ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
      else bad_thm ("Free type variables on right hand side of rewrite theorem\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
        ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   349
  in thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
fun mk_rew thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   352
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
    val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   354
    val ctxt = ProofContext.init thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   355
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   356
    thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
    |> LocalDefs.meta_rewrite_rule ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
    |> assert_rew
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   359
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   360
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
26239
e105d24d15c1 some steps towards a refined treatment of equality
haftmann
parents: 26112
diff changeset
   362
(* defining equations *)
e105d24d15c1 some steps towards a refined treatment of equality
haftmann
parents: 26112
diff changeset
   363
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   364
fun assert_func linear thm =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   365
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   366
    val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   367
    val (head, args) = (strip_comb o fst o Logic.dest_equals
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   368
      o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   369
    val _ = case head of Const _ => () | _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   370
      bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   371
    val _ =
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   372
      if linear andalso has_duplicates (op =)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   373
        ((fold o fold_aterms) (fn Var (v, _) => cons v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   374
          | _ => I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   375
        ) args [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   376
      then bad_thm ("Duplicated variables on left hand side of equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   377
        ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   378
      else ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   379
    fun check _ (Abs _) = bad_thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   380
          ("Abstraction on left hand side of equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   381
            ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   382
      | check 0 (Var _) = ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   383
      | check _ (Var _) = bad_thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   384
          ("Variable with application on left hand side of defining equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   385
            ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   386
      | check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   387
      | check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   388
          then bad_thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   389
            ("Partially applied constant on left hand side of equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   390
               ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   391
          else ();
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   392
    val _ = map (check 0) args;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   393
  in thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   394
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   395
fun mk_func linear = assert_func linear o mk_rew;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   396
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   397
fun head_func thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
    val thy = Thm.theory_of_thm thm;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   400
    val Const (c, ty) = (fst o strip_comb o fst o Logic.dest_equals
27610
haftmann
parents: 27582
diff changeset
   401
      o Thm.plain_prop_of) thm;
26970
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26939
diff changeset
   402
  in (c, typscheme thy (c, ty)) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   404
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   405
(* case cerificates *)
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   406
24917
haftmann
parents: 24848
diff changeset
   407
fun case_certificate thm =
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   408
  let
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   409
    val thy = Thm.theory_of_thm thm;
24917
haftmann
parents: 24848
diff changeset
   410
    val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals
haftmann
parents: 24848
diff changeset
   411
      o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.prop_of) thm;
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   412
    val _ = case head of Free _ => true
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   413
      | Var _ => true
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   414
      | _ => raise TERM ("case_cert", []);
24917
haftmann
parents: 24848
diff changeset
   415
    val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr;
haftmann
parents: 24848
diff changeset
   416
    val (Const (case_const, _), raw_params) = strip_comb case_expr;
haftmann
parents: 24848
diff changeset
   417
    val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params;
haftmann
parents: 24848
diff changeset
   418
    val _ = if n = ~1 then raise TERM ("case_cert", []) else ();
haftmann
parents: 24848
diff changeset
   419
    val params = map (fst o dest_Var) (nth_drop n raw_params);
haftmann
parents: 24848
diff changeset
   420
    fun dest_case t =
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   421
      let
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   422
        val (head' $ t_co, rhs) = Logic.dest_equals t;
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   423
        val _ = if head' = head then () else raise TERM ("case_cert", []);
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   424
        val (Const (co, _), args) = strip_comb t_co;
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   425
        val (Var (param, _), args') = strip_comb rhs;
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   426
        val _ = if args' = args then () else raise TERM ("case_cert", []);
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   427
      in (param, co) end;
24917
haftmann
parents: 24848
diff changeset
   428
    fun analyze_cases cases =
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   429
      let
24917
haftmann
parents: 24848
diff changeset
   430
        val co_list = fold (AList.update (op =) o dest_case) cases [];
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   431
      in map (the o AList.lookup (op =) co_list) params end;
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   432
    fun analyze_let t =
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   433
      let
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   434
        val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t;
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   435
        val _ = if head' = head then () else raise TERM ("case_cert", []);
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   436
        val _ = if arg' = arg then () else raise TERM ("case_cert", []);
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   437
        val _ = if [param'] = params then () else raise TERM ("case_cert", []);
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   438
      in [] end;
24917
haftmann
parents: 24848
diff changeset
   439
    fun analyze (cases as [let_case]) =
haftmann
parents: 24848
diff changeset
   440
          (analyze_cases cases handle Bind => analyze_let let_case)
haftmann
parents: 24848
diff changeset
   441
      | analyze cases = analyze_cases cases;
haftmann
parents: 24848
diff changeset
   442
  in (case_const, (n, analyze cases)) end;
haftmann
parents: 24848
diff changeset
   443
haftmann
parents: 24848
diff changeset
   444
fun case_cert thm = case_certificate thm
haftmann
parents: 24848
diff changeset
   445
  handle Bind => error "bad case certificate"
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28310
diff changeset
   446
       | TERM _ => error "bad case certificate";
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24707
diff changeset
   447
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   448
end;