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