src/Pure/Isar/code_unit.ML
author wenzelm
Tue, 25 Sep 2007 13:28:37 +0200
changeset 24707 dfeb98f84e93
parent 24624 b8383b1bbae3
child 24844 98c006a30218
permissions -rw-r--r--
Syntax.parse/check/read;
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
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
     5
Basic units 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
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
  val string_of_typ: theory -> typ -> string
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    11
  val string_of_const: theory -> string -> string
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    12
  val no_args: theory -> string -> int
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    13
  val read_bare_const: theory -> string -> string * typ
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    14
  val read_const: theory -> string -> string
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    15
  val read_const_exprs: theory -> (string list -> string list)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    16
    -> string list -> bool * string list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    18
  val constrset_of_consts: theory -> (string * typ) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
    -> string * ((string * sort) list * (string * typ list) list)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
  val typ_sort_inst: Sorts.algebra -> typ * sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
    -> sort Vartab.table -> sort Vartab.table
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  val assert_rew: thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
  val mk_rew: thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
  val mk_func: thm -> thm
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    26
  val head_func: thm -> string * typ
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
  val bad_thm: string -> 'a
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    28
  val error_thm: (thm -> thm) -> thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
  val warning_thm: (thm -> thm) -> thm -> thm option
24624
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24423
diff changeset
    30
  val try_thm: (thm -> thm) -> thm -> thm option
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
  val inst_thm: sort Vartab.table -> thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
  val expand_eta: int -> thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  val rewrite_func: thm list -> thm -> thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
  val norm_args: thm list -> thm list 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
  val norm_varnames: (string -> string) -> (string -> string) -> thm list -> thm list 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
structure CodeUnit: CODE_UNIT =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
(* auxiliary *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
exception BAD_THM of string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
fun bad_thm msg = raise BAD_THM msg;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
fun error_thm f thm = f thm handle BAD_THM msg => error msg;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
  => (warning ("code generator: " ^ msg); NONE);
24624
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24423
diff changeset
    50
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
fun string_of_typ thy = setmp show_sorts true (Sign.string_of_typ thy);
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    53
fun string_of_const thy c = case Class.param_const thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    54
 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
    55
  | NONE => Sign.extern_const thy c;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    57
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
    58
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
(* reading constants as terms and wildcards pattern *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
fun read_bare_const thy raw_t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
  let
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24624
diff changeset
    63
    val t = Syntax.read_term_global thy raw_t;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
  in case try dest_Const t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
   of SOME c_ty => c_ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
    | NONE => error ("Not a constant: " ^ Sign.string_of_term thy t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    69
fun read_const thy = Class.unoverload_const thy o read_bare_const thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
fun consts_of thy some_thyname =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
    val this_thy = Option.map theory some_thyname |> the_default thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
    val cs = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
      ((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
    78
    fun belongs_here thyname c =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
          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
    80
  in case some_thyname
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    81
   of NONE => cs
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
    82
    | SOME thyname => filter (belongs_here thyname) cs
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
fun read_const_expr thy "*" = ([], consts_of thy NONE)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
  | read_const_expr thy s = if String.isSuffix ".*" s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
      then ([], consts_of thy (SOME (unsuffix ".*" s)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
      else ([read_const thy s], []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
fun read_const_exprs thy select exprs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
  case (pairself flat o split_list o map (read_const_expr thy)) exprs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
   of (consts, []) => (false, consts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
    | (consts, consts') => (true, consts @ select consts');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   100
(* constructor sets *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   102
fun constrset_of_consts thy cs =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   104
    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
   105
      ^ " :: " ^ string_of_typ thy ty);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   106
    fun last_typ c_ty ty =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   107
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   108
        val frees = typ_tfrees ty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   109
        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
   110
          handle TYPE _ => no_constr c_ty
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   111
        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
   112
        val _ = if length frees <> length vs then no_constr c_ty else ();
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   113
      in (tyco, vs) end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   114
    fun ty_sorts (c, ty) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   115
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   116
        val ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   117
        val (tyco, vs_decl) = last_typ (c, ty) ty_decl;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   118
        val (_, vs) = last_typ (c, ty) ty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   119
      in ((tyco, map snd vs), (c, (map fst vs, ty_decl))) end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   120
    fun add ((tyco', sorts'), c) ((tyco, sorts), cs) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   121
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   122
        val _ = if tyco' <> tyco
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   123
          then error "Different type constructors in constructor set"
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   124
          else ();
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   125
        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
   126
      in ((tyco, sorts), c :: cs) end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   127
    fun inst vs' (c, (vs, ty)) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   128
      let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   129
        val the_v = the o AList.lookup (op =) (vs ~~ vs');
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   130
        val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   131
      in (c, (fst o strip_type) ty') end;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   132
    val c' :: cs' = map ty_sorts cs;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   133
    val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   134
    val vs = Name.names Name.context "'a" sorts;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   135
    val cs''' = map (inst vs) cs'';
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   136
  in (tyco, (vs, cs''')) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
(* dictionary values *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
fun typ_sort_inst algebra =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   143
    val inters = Sorts.inter_sort algebra;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
    fun match _ [] = I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
      | match (TVar (v, S)) S' = Vartab.map_default (v, []) (fn S'' => inters (S, inters (S', S'')))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
      | match (Type (a, Ts)) S =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
          fold2 match Ts (Sorts.mg_domain algebra a S)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
  in uncurry match end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
(* making rewrite theorems *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
fun assert_rew thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
      handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
          | THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
    fun vars_of t = fold_aterms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
     (fn Var (v, _) => insert (op =) v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
       | Free _ => bad_thm ("Illegal free variable in rewrite theorem\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
           ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
       | _ => I) t [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
    fun tvars_of t = fold_term_types
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
     (fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
                          | TFree _ => bad_thm 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
      ("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
   167
    val lhs_vs = vars_of lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
    val rhs_vs = vars_of rhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
    val lhs_tvs = tvars_of lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
    val rhs_tvs = tvars_of lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
    val _ = if null (subtract (op =) lhs_vs rhs_vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
      then ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
      else bad_thm ("Free variables on right hand side of rewrite theorem\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
        ^ Display.string_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
    val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
      then ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
      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
   178
        ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
  in thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
fun mk_rew thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
    val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
    val ctxt = ProofContext.init thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
    thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
    |> LocalDefs.meta_rewrite_rule ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
    |> assert_rew
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
(* making defining equations *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
fun assert_func thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
    val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
    val (head, args) = (strip_comb o fst o Logic.dest_equals
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
      o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
    val _ = case head of Const _ => () | _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
      bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
    val _ =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
      if has_duplicates (op =)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
        ((fold o fold_aterms) (fn Var (v, _) => cons v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
          | _ => I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
        ) args [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
      then bad_thm ("Duplicated variables on left hand side of equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
        ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
      else ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
    fun check _ (Abs _) = bad_thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
          ("Abstraction on left hand side of equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
            ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
      | check 0 (Var _) = ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
      | check _ (Var _) = bad_thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
          ("Variable with application on left hand side of defining equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
            ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
      | check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
      | check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
          then bad_thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
            ("Partially applied constant on left hand side of equation\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
               ^ Display.string_of_thm thm)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
          else ();
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
    val _ = map (check 0) args;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
  in thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
val mk_func = assert_func o mk_rew;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
fun head_func thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
    val thy = Thm.theory_of_thm thm;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   230
    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
   231
      o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24219
diff changeset
   232
  in (c, ty) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
(* utilities *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
fun inst_thm tvars' thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   238
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   239
    val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   240
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   241
    fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   242
     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
   243
      | NONE => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   244
    val insts = map_filter mk_inst tvars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
  in Thm.instantiate (insts, []) thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
fun expand_eta k thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   249
    val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
    val (head, args) = strip_comb lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
    val l = if k = ~1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
      then (length o fst o strip_abs) rhs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
      else Int.max (0, k - length args);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
    val used = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
    fun get_name _ 0 used = ([], used)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
      | get_name (Abs (v, ty, t)) k used =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   258
          used
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
          |> Name.variants [v]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
          ||>> get_name t (k - 1)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
          |>> (fn ([v'], vs') => (v', ty) :: vs')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
      | get_name t k used = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   263
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
            val (tys, _) = (strip_type o fastype_of) t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
          in case tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
           of [] => raise TERM ("expand_eta", [t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
            | ty :: _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
                used
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
                |> Name.variants [""]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
                |-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
                #>> (fn vs' => (v, ty) :: vs'))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
    val (vs, _) = get_name rhs l used;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
    val vs_refl = map (fn (v, ty) => Thm.reflexive (Thm.cterm_of thy (Var ((v, 0), ty)))) vs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
    thm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
    |> fold (fn refl => fn thm => Thm.combination thm refl) vs_refl
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
    |> Conv.fconv_rule Drule.beta_eta_conversion
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   279
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
fun rewrite_func rewrites thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
    val rewrite = MetaSimplifier.rewrite false rewrites;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
    val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
    val Const ("==", _) = Thm.term_of ct_eq;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
    val (ct_f, ct_args) = Drule.strip_comb ct_lhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
    val rhs' = rewrite ct_rhs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
    val args' = map rewrite ct_args;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
    val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
      args' (Thm.reflexive ct_f));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
  in Thm.transitive (Thm.transitive lhs' thm) rhs' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
fun norm_args thms =
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 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
   296
    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
   297
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
    thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
    |> map (expand_eta k)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
    |> map (Conv.fconv_rule Drule.beta_eta_conversion)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
fun canonical_tvars purify_tvar thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
    val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
    fun tvars_subst_for thm = (fold_types o fold_atyps)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
      (fn TVar (v_i as (v, _), sort) => let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
            val v' = purify_tvar v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
          in if v = v' then I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
          else insert (op =) (v_i, (v', sort)) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
        | _ => I) (prop_of thm) [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
    fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
        val ty = TVar (v_i, sort)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
        (maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
    val maxidx = Thm.maxidx_of thm + 1;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
    val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
  in Thm.instantiate (inst, []) thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
fun canonical_vars purify_var thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
    val cterm = Thm.cterm_of (Thm.theory_of_thm thm);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
    fun vars_subst_for thm = fold_aterms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
      (fn Var (v_i as (v, _), ty) => let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
            val v' = purify_var v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
          in if v = v' then I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
          else insert (op =) (v_i, (v', ty)) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
        | _ => I) (prop_of thm) [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
    fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
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 t = Var (v_i, ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
        (maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
    val maxidx = Thm.maxidx_of thm + 1;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
    val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
  in Thm.instantiate ([], inst) thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
fun canonical_absvars purify_var thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
    val t = Thm.plain_prop_of thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
    val t' = Term.map_abs_vars purify_var t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
  in Thm.rename_boundvars t t' thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
fun norm_varnames purify_tvar purify_var thms =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   349
    fun burrow_thms f [] = []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
      | burrow_thms f thms =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
          thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   352
          |> Conjunction.intr_balanced
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
          |> f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   354
          |> Conjunction.elim_balanced (length thms)
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
    thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
    |> burrow_thms (canonical_tvars purify_tvar)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
    |> map (canonical_vars purify_var)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   359
    |> map (canonical_absvars purify_var)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   360
    |> map Drule.zero_var_indexes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   363
end;