src/Pure/Isar/code.ML
author haftmann
Thu, 14 May 2009 15:09:48 +0200
changeset 31156 90fed3d4430f
parent 31152 e79d1ff2abc5
child 31599 97b4d289c646
permissions -rw-r--r--
merged module code_unit.ML into code.ML
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.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
Abstract executable content of theory.  Management of data dependent on
25485
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
     5
executable content.  Cache assumes non-concurrent processing of a single theory.
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 =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
sig
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    10
  (*constructor sets*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    11
  val constrset_of_consts: theory -> (string * typ) list
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    12
    -> string * ((string * sort) list * (string * typ list) list)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    13
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    14
  (*typ instantiations*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    15
  val typscheme: theory -> string * typ -> (string * sort) list * typ
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    16
  val inst_thm: theory -> sort Vartab.table -> thm -> thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    17
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    18
  (*constants*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    19
  val string_of_typ: theory -> typ -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    20
  val string_of_const: theory -> string -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    21
  val no_args: theory -> string -> int
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    22
  val check_const: theory -> term -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    23
  val read_bare_const: theory -> string -> string * typ
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    24
  val read_const: theory -> string -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    25
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    26
  (*constant aliasses*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    27
  val add_const_alias: thm -> theory -> theory
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    28
  val triv_classes: theory -> class list
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    29
  val resubst_alias: theory -> string -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    30
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    31
  (*code equations*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    32
  val mk_eqn: theory -> (string -> bool) -> thm * bool -> thm * bool
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    33
  val mk_eqn_liberal: theory -> (string -> bool) -> thm -> (thm * bool) option
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    34
  val assert_eqn: theory -> thm * bool -> thm * bool
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    35
  val assert_eqns_const: theory -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    36
    -> (thm * bool) list -> (thm * bool) list
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    37
  val const_typ_eqn: thm -> string * typ
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    38
  val const_eqn: theory -> thm -> string
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    39
  val typscheme_eqn: theory -> thm -> (string * sort) list * typ
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    40
  val expand_eta: theory -> int -> thm -> thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    41
  val rewrite_eqn: simpset -> thm -> thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    42
  val rewrite_head: thm list -> thm -> thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    43
  val norm_args: theory -> thm list -> thm list 
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    44
  val norm_varnames: theory -> thm list -> thm list
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    45
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    46
  (*case certificates*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    47
  val case_cert: thm -> string * (int * string list)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    48
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    49
  (*infrastructure*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    50
  val add_attribute: string * attribute parser -> theory -> theory
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    51
  val purge_data: theory -> theory
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    52
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    53
  (*executable content*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    54
  val add_datatype: (string * typ) list -> theory -> theory
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    55
  val add_datatype_cmd: string list -> theory -> theory
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    56
  val type_interpretation:
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    57
    (string * ((string * sort) list * (string * typ list) list)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    58
      -> theory -> theory) -> theory -> theory
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
    59
  val add_eqn: thm -> theory -> theory
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
    60
  val add_nbe_eqn: thm -> theory -> theory
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
    61
  val add_default_eqn: thm -> theory -> theory
28703
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28695
diff changeset
    62
  val add_default_eqn_attribute: attribute
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28695
diff changeset
    63
  val add_default_eqn_attrib: Attrib.src
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
    64
  val del_eqn: thm -> theory -> theory
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
    65
  val del_eqns: string -> theory -> theory
28971
300ec36a19af renamed type Lazy.T to lazy;
wenzelm
parents: 28708
diff changeset
    66
  val add_eqnl: string * (thm * bool) list lazy -> theory -> theory
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
    67
  val add_case: thm -> theory -> theory
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
    68
  val add_undefined: string -> theory -> theory
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    70
  (*data retrieval*)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
  val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    72
  val get_datatype_of_constr: theory -> string -> string option
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    73
  val default_typscheme: theory -> string -> (string * sort) list * typ
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
    74
  val these_eqns: theory -> string -> (thm * bool) list
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
    75
  val get_case_scheme: theory -> string -> (int * (int * string list)) option
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
    76
  val is_undefined: theory -> string -> bool
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
  val print_codesetup: theory -> unit
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
signature CODE_DATA_ARGS =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
  type T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
  val empty: T
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
    84
  val purge: theory -> string list -> T -> T
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
signature CODE_DATA =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
  type T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
  val get: theory -> T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
  val change: theory -> (T -> T) -> T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
  val change_yield: theory -> (T -> 'a * T) -> 'a * T
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
signature PRIVATE_CODE =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
  include CODE
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
    98
  val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T)
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
    99
    -> serial
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
  val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
    -> theory -> 'a
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
  val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
    -> theory -> ('a -> 'a) -> 'a
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
  val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
    -> theory -> ('a -> 'b * 'a) -> 'b * 'a
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
structure Code : PRIVATE_CODE =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   111
(* auxiliary *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   112
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   113
fun string_of_typ thy = setmp show_sorts true (Syntax.string_of_typ_global thy);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   114
fun string_of_const thy c = case AxClass.inst_of_param thy c
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   115
 of SOME (c, tyco) => Sign.extern_const thy c ^ " " ^ enclose "[" "]" (Sign.extern_type thy tyco)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   116
  | NONE => Sign.extern_const thy c;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   117
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   118
fun no_args thy = length o fst o strip_type o Sign.the_const_type thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   119
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   120
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   121
(* utilities *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   122
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   123
fun typscheme thy (c, ty) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   124
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   125
    val ty' = Logic.unvarifyT ty;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   126
    fun dest (TFree (v, sort)) = (v, sort)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   127
      | dest ty = error ("Illegal type parameter in type scheme: " ^ Syntax.string_of_typ_global thy ty);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   128
    val vs = map dest (Sign.const_typargs thy (c, ty'));
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   129
  in (vs, Type.strip_sorts ty') end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   130
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   131
fun inst_thm thy tvars' thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   132
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   133
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   134
    val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   135
    fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   136
     of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   137
          (tvar, (v, inter_sort (sort, sort'))))
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   138
      | NONE => NONE;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   139
    val insts = map_filter mk_inst tvars;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   140
  in Thm.instantiate (insts, []) thm end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   141
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   142
fun expand_eta thy k thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   143
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   144
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   145
    val (head, args) = strip_comb lhs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   146
    val l = if k = ~1
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   147
      then (length o fst o strip_abs) rhs
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   148
      else Int.max (0, k - length args);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   149
    val used = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   150
    fun get_name _ 0 = pair []
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   151
      | get_name (Abs (v, ty, t)) k =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   152
          Name.variants [v]
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   153
          ##>> get_name t (k - 1)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   154
          #>> (fn ([v'], vs') => (v', ty) :: vs')
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   155
      | get_name t k = 
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   156
          let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   157
            val (tys, _) = (strip_type o fastype_of) t
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   158
          in case tys
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   159
           of [] => raise TERM ("expand_eta", [t])
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   160
            | ty :: _ =>
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   161
                Name.variants [""]
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   162
                #-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   163
                #>> (fn vs' => (v, ty) :: vs'))
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   164
          end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   165
    val (vs, _) = get_name rhs l used;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   166
    fun expand (v, ty) thm = Drule.fun_cong_rule thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   167
      (Thm.cterm_of thy (Var ((v, 0), ty)));
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   168
  in
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   169
    thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   170
    |> fold expand vs
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   171
    |> Conv.fconv_rule Drule.beta_eta_conversion
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   172
  end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   173
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   174
fun eqn_conv conv =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   175
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   176
    fun lhs_conv ct = if can Thm.dest_comb ct
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   177
      then (Conv.combination_conv lhs_conv conv) ct
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   178
      else Conv.all_conv ct;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   179
  in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   180
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   181
fun head_conv conv =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   182
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   183
    fun lhs_conv ct = if can Thm.dest_comb ct
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   184
      then (Conv.fun_conv lhs_conv) ct
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   185
      else conv ct;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   186
  in Conv.fun_conv (Conv.arg_conv lhs_conv) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   187
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   188
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   189
val rewrite_head = Conv.fconv_rule o head_conv o MetaSimplifier.rewrite false;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   190
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   191
fun norm_args thy thms =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   192
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   193
    val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   194
    val k = fold (curry Int.max o num_args_of o Thm.prop_of) thms 0;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   195
  in
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   196
    thms
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   197
    |> map (expand_eta thy k)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   198
    |> map (Conv.fconv_rule Drule.beta_eta_conversion)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   199
  end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   200
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   201
fun canonical_tvars thy thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   202
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   203
    val ctyp = Thm.ctyp_of thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   204
    val purify_tvar = unprefix "'" #> Name.desymbolize false #> prefix "'";
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   205
    fun tvars_subst_for thm = (fold_types o fold_atyps)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   206
      (fn TVar (v_i as (v, _), sort) => let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   207
            val v' = purify_tvar v
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   208
          in if v = v' then I
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   209
          else insert (op =) (v_i, (v', sort)) end
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   210
        | _ => I) (prop_of thm) [];
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   211
    fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   212
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   213
        val ty = TVar (v_i, sort)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   214
      in
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   215
        (maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   216
      end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   217
    val maxidx = Thm.maxidx_of thm + 1;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   218
    val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   219
  in Thm.instantiate (inst, []) thm end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   220
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   221
fun canonical_vars thy thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   222
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   223
    val cterm = Thm.cterm_of thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   224
    val purify_var = Name.desymbolize false;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   225
    fun vars_subst_for thm = fold_aterms
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   226
      (fn Var (v_i as (v, _), ty) => let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   227
            val v' = purify_var v
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   228
          in if v = v' then I
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   229
          else insert (op =) (v_i, (v', ty)) end
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   230
        | _ => I) (prop_of thm) [];
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   231
    fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   232
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   233
        val t = Var (v_i, ty)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   234
      in
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   235
        (maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   236
      end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   237
    val maxidx = Thm.maxidx_of thm + 1;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   238
    val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   239
  in Thm.instantiate ([], inst) thm end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   240
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   241
fun canonical_absvars thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   242
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   243
    val t = Thm.plain_prop_of thm;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   244
    val purify_var = Name.desymbolize false;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   245
    val t' = Term.map_abs_vars purify_var t;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   246
  in Thm.rename_boundvars t t' thm end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   247
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   248
fun norm_varnames thy thms =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   249
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   250
    fun burrow_thms f [] = []
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   251
      | burrow_thms f thms =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   252
          thms
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   253
          |> Conjunction.intr_balanced
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   254
          |> f
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   255
          |> Conjunction.elim_balanced (length thms)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   256
  in
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   257
    thms
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   258
    |> map (canonical_vars thy)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   259
    |> map canonical_absvars
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   260
    |> map Drule.zero_var_indexes
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   261
    |> burrow_thms (canonical_tvars thy)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   262
    |> Drule.zero_var_indexes_list
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   263
  end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   264
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   265
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   266
(* const aliasses *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   267
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   268
structure ConstAlias = TheoryDataFun
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   269
(
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   270
  type T = ((string * string) * thm) list * class list;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   271
  val empty = ([], []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   272
  val copy = I;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   273
  val extend = I;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   274
  fun merge _ ((alias1, classes1), (alias2, classes2)) : T =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   275
    (Library.merge (eq_snd Thm.eq_thm_prop) (alias1, alias2),
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   276
      Library.merge (op =) (classes1, classes2));
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   277
);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   278
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   279
fun add_const_alias thm thy =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   280
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   281
    val lhs_rhs = case try Logic.dest_equals (Thm.prop_of thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   282
     of SOME lhs_rhs => lhs_rhs
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   283
      | _ => error ("Not an equation: " ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   284
    val c_c' = case try (pairself (AxClass.unoverload_const thy o dest_Const)) lhs_rhs
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   285
     of SOME c_c' => c_c'
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   286
      | _ => error ("Not an equation with two constants: " ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   287
    val some_class = the_list (AxClass.class_of_param thy (snd c_c'));
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   288
  in thy |>
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   289
    ConstAlias.map (fn (alias, classes) =>
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   290
      ((c_c', thm) :: alias, fold (insert (op =)) some_class classes))
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   291
  end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   292
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   293
fun resubst_alias thy =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   294
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   295
    val alias = fst (ConstAlias.get thy);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   296
    val subst_inst_param = Option.map fst o AxClass.inst_of_param thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   297
    fun subst_alias c =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   298
      get_first (fn ((c', c''), _) => if c = c'' then SOME c' else NONE) alias;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   299
  in
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   300
    perhaps subst_inst_param
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   301
    #> perhaps subst_alias
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   302
  end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   303
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   304
val triv_classes = snd o ConstAlias.get;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   305
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   306
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   307
(* reading constants as terms *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   308
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   309
fun check_bare_const thy t = case try dest_Const t
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   310
 of SOME c_ty => c_ty
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   311
  | NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   312
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   313
fun check_const thy = AxClass.unoverload_const thy o check_bare_const thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   314
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   315
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   316
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   317
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   318
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   319
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   320
(* constructor sets *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   321
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   322
fun constrset_of_consts thy cs =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   323
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   324
    val _ = map (fn (c, _) => if (is_some o AxClass.class_of_param thy) c
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   325
      then error ("Is a class parameter: " ^ string_of_const thy c) else ()) cs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   326
    fun no_constr (c, ty) = error ("Not a datatype constructor: " ^ string_of_const thy c
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   327
      ^ " :: " ^ string_of_typ thy ty);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   328
    fun last_typ c_ty ty =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   329
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   330
        val frees = OldTerm.typ_tfrees ty;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   331
        val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) ty
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   332
          handle TYPE _ => no_constr c_ty
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   333
        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr c_ty else ();
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   334
        val _ = if length frees <> length vs then no_constr c_ty else ();
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   335
      in (tyco, vs) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   336
    fun ty_sorts (c, ty) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   337
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   338
        val ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   339
        val (tyco, _) = last_typ (c, ty) ty_decl;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   340
        val (_, vs) = last_typ (c, ty) ty;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   341
      in ((tyco, map snd vs), (c, (map fst vs, ty))) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   342
    fun add ((tyco', sorts'), c) ((tyco, sorts), cs) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   343
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   344
        val _ = if tyco' <> tyco
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   345
          then error "Different type constructors in constructor set"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   346
          else ();
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   347
        val sorts'' = map2 (curry (Sorts.inter_sort (Sign.classes_of thy))) sorts' sorts
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   348
      in ((tyco, sorts), c :: cs) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   349
    fun inst vs' (c, (vs, ty)) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   350
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   351
        val the_v = the o AList.lookup (op =) (vs ~~ vs');
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   352
        val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   353
      in (c, (fst o strip_type) ty') end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   354
    val c' :: cs' = map ty_sorts cs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   355
    val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   356
    val vs = Name.names Name.context Name.aT sorts;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   357
    val cs''' = map (inst vs) cs'';
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   358
  in (tyco, (vs, rev cs''')) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   359
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   360
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   361
(* code equations *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   362
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   363
exception BAD_THM of string;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   364
fun bad_thm msg = raise BAD_THM msg;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   365
fun error_thm f thm = f thm handle BAD_THM msg => error msg;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   366
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   367
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   368
fun is_linear thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   369
  let val (_, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   370
  in not (has_duplicates (op =) ((fold o fold_aterms)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   371
    (fn Var (v, _) => cons v | _ => I) args [])) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   372
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   373
fun gen_assert_eqn thy is_constr_head is_constr_pat (thm, proper) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   374
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   375
    val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   376
      handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   377
           | THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   378
    fun vars_of t = fold_aterms (fn Var (v, _) => insert (op =) v
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   379
      | Free _ => bad_thm ("Illegal free variable in equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   380
          ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   381
      | _ => I) t [];
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   382
    fun tvars_of t = fold_term_types (fn _ =>
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   383
      fold_atyps (fn TVar (v, _) => insert (op =) v
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   384
        | TFree _ => bad_thm 
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   385
      ("Illegal free type variable in equation\n" ^ Display.string_of_thm thm))) t [];
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   386
    val lhs_vs = vars_of lhs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   387
    val rhs_vs = vars_of rhs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   388
    val lhs_tvs = tvars_of lhs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   389
    val rhs_tvs = tvars_of rhs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   390
    val _ = if null (subtract (op =) lhs_vs rhs_vs)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   391
      then ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   392
      else bad_thm ("Free variables on right hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   393
        ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   394
    val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   395
      then ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   396
      else bad_thm ("Free type variables on right hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   397
        ^ Display.string_of_thm thm)    val (head, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   398
    val (c, ty) = case head
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   399
     of Const (c_ty as (_, ty)) => (AxClass.unoverload_const thy c_ty, ty)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   400
      | _ => bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   401
    fun check _ (Abs _) = bad_thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   402
          ("Abstraction on left hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   403
            ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   404
      | check 0 (Var _) = ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   405
      | check _ (Var _) = bad_thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   406
          ("Variable with application on left hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   407
            ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   408
      | check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   409
      | check n (Const (c_ty as (c, ty))) = if n = (length o fst o strip_type) ty
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   410
          then if not proper orelse is_constr_pat (AxClass.unoverload_const thy c_ty)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   411
            then ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   412
            else bad_thm (quote c ^ " is not a constructor, on left hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   413
              ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   414
          else bad_thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   415
            ("Partially applied constant " ^ quote c ^ " on left hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   416
               ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   417
    val _ = map (check 0) args;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   418
    val _ = if not proper orelse is_linear thm then ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   419
      else bad_thm ("Duplicate variables on left hand side of equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   420
        ^ Display.string_of_thm thm);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   421
    val _ = if (is_none o AxClass.class_of_param thy) c
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   422
      then ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   423
      else bad_thm ("Polymorphic constant as head in equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   424
        ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   425
    val _ = if not (is_constr_head c)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   426
      then ()
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   427
      else bad_thm ("Constructor as head in equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   428
        ^ Display.string_of_thm thm)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   429
    val ty_decl = Sign.the_const_type thy c;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   430
    val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   431
      then () else bad_thm ("Type\n" ^ string_of_typ thy ty
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   432
           ^ "\nof equation\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   433
           ^ Display.string_of_thm thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   434
           ^ "\nis incompatible with declared function type\n"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   435
           ^ string_of_typ thy ty_decl)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   436
  in (thm, proper) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   437
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   438
fun assert_eqn thy is_constr = error_thm (gen_assert_eqn thy is_constr is_constr);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   439
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   440
val const_typ_eqn = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   441
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   442
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   443
(*those following are permissive wrt. to overloaded constants!*)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   444
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   445
fun mk_eqn thy is_constr_head = error_thm (gen_assert_eqn thy is_constr_head (K true)) o
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   446
  apfst (LocalDefs.meta_rewrite_rule (ProofContext.init thy));
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   447
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   448
fun mk_eqn_liberal thy is_constr_head = Option.map (fn (thm, _) => (thm, is_linear thm))
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   449
  o try_thm (gen_assert_eqn thy is_constr_head (K true))
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   450
  o rpair false o LocalDefs.meta_rewrite_rule (ProofContext.init thy);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   451
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   452
fun const_typ_eqn_unoverload thy thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   453
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   454
    val (c, ty) = const_typ_eqn thm;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   455
    val c' = AxClass.unoverload_const thy (c, ty);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   456
  in (c', ty) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   457
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   458
fun typscheme_eqn thy = typscheme thy o const_typ_eqn_unoverload thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   459
fun const_eqn thy = fst o const_typ_eqn_unoverload thy;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   460
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   461
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   462
(* case cerificates *)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   463
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   464
fun case_certificate thm =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   465
  let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   466
    val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   467
      o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.prop_of) thm;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   468
    val _ = case head of Free _ => true
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   469
      | Var _ => true
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   470
      | _ => raise TERM ("case_cert", []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   471
    val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   472
    val (Const (case_const, _), raw_params) = strip_comb case_expr;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   473
    val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   474
    val _ = if n = ~1 then raise TERM ("case_cert", []) else ();
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   475
    val params = map (fst o dest_Var) (nth_drop n raw_params);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   476
    fun dest_case t =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   477
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   478
        val (head' $ t_co, rhs) = Logic.dest_equals t;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   479
        val _ = if head' = head then () else raise TERM ("case_cert", []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   480
        val (Const (co, _), args) = strip_comb t_co;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   481
        val (Var (param, _), args') = strip_comb rhs;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   482
        val _ = if args' = args then () else raise TERM ("case_cert", []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   483
      in (param, co) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   484
    fun analyze_cases cases =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   485
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   486
        val co_list = fold (AList.update (op =) o dest_case) cases [];
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   487
      in map (the o AList.lookup (op =) co_list) params end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   488
    fun analyze_let t =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   489
      let
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   490
        val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   491
        val _ = if head' = head then () else raise TERM ("case_cert", []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   492
        val _ = if arg' = arg then () else raise TERM ("case_cert", []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   493
        val _ = if [param'] = params then () else raise TERM ("case_cert", []);
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   494
      in [] end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   495
    fun analyze (cases as [let_case]) =
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   496
          (analyze_cases cases handle Bind => analyze_let let_case)
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   497
      | analyze cases = analyze_cases cases;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   498
  in (case_const, (n, analyze cases)) end;
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   499
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   500
fun case_cert thm = case_certificate thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   501
  handle Bind => error "bad case certificate"
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   502
       | TERM _ => error "bad case certificate";
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   503
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   504
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   505
(** code attributes **)
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   506
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   507
structure CodeAttr = TheoryDataFun (
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30357
diff changeset
   508
  type T = (string * attribute parser) list;
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   509
  val empty = [];
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   510
  val copy = I;
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   511
  val extend = I;
26970
bc28e7bcb765 explicit type schemes for functions
haftmann
parents: 26947
diff changeset
   512
  fun merge _ = AList.merge (op = : string * string -> bool) (K true);
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   513
);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   514
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   515
fun add_attribute (attr as (name, _)) =
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   516
  let
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28525
diff changeset
   517
    fun add_parser ("", parser) attrs = attrs |> rev |> AList.update (op =) ("", parser) |> rev
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   518
      | add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28525
diff changeset
   519
  in CodeAttr.map (fn attrs => if not (name = "") andalso AList.defined (op =) attrs name
4e74209f113e `code func` now just `code`
haftmann
parents: 28525
diff changeset
   520
    then error ("Code attribute " ^ name ^ " already declared") else add_parser attr attrs)
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   521
  end;
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   522
30722
623d4831c8cf simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents: 30528
diff changeset
   523
val _ = Context.>> (Context.map_theory
623d4831c8cf simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents: 30528
diff changeset
   524
  (Attrib.setup (Binding.name "code")
623d4831c8cf simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents: 30528
diff changeset
   525
    (Scan.peek (fn context =>
623d4831c8cf simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents: 30528
diff changeset
   526
      List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))))
623d4831c8cf simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents: 30528
diff changeset
   527
    "declare theorems for code generation"));
623d4831c8cf simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents: 30528
diff changeset
   528
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   529
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   530
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   531
(** logical and syntactical specification of executable code **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   532
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   533
(* code equations *)
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   534
28971
300ec36a19af renamed type Lazy.T to lazy;
wenzelm
parents: 28708
diff changeset
   535
type eqns = bool * (thm * bool) list lazy;
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   536
  (*default flag, theorems with proper flag (perhaps lazy)*)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   537
28673
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   538
fun pretty_lthms ctxt r = case Lazy.peek r
28672
0baf1d9c6780 adapted Susp.peek;
wenzelm
parents: 28562
diff changeset
   539
 of SOME thms => map (ProofContext.pretty_thm ctxt o fst) (Exn.release thms)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   540
  | NONE => [Pretty.str "[...]"];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   541
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   542
fun certificate thy f r =
28673
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   543
  case Lazy.peek r
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   544
   of SOME thms => (Lazy.value o f thy) (Exn.release thms)
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   545
    | NONE => let
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   546
        val thy_ref = Theory.check_thy thy;
28673
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   547
      in Lazy.lazy (fn () => (f (Theory.deref thy_ref) o Lazy.force) r) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   548
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   549
fun add_drop_redundant thy (thm, proper) thms =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   550
  let
31152
e79d1ff2abc5 strip sorts while checking pattern subsumption
haftmann
parents: 31125
diff changeset
   551
    val args_of = snd o strip_comb o map_types Type.strip_sorts
e79d1ff2abc5 strip sorts while checking pattern subsumption
haftmann
parents: 31125
diff changeset
   552
      o fst o Logic.dest_equals o Thm.plain_prop_of;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   553
    val args = args_of thm;
28403
da9ae7774513 more precise redundancy check
haftmann
parents: 28368
diff changeset
   554
    val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1);
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28143
diff changeset
   555
    fun matches_args args' = length args <= length args' andalso
28403
da9ae7774513 more precise redundancy check
haftmann
parents: 28368
diff changeset
   556
      Pattern.matchess thy (args, (map incr_idx o curry Library.take (length args)) args');
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   557
    fun drop (thm', proper') = if (proper orelse not proper')
28359
bd4750bcb4e6 clarifed redundancy policy
haftmann
parents: 28353
diff changeset
   558
      andalso matches_args (args_of thm') then 
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   559
        (warning ("Code generator: dropping redundant code equation\n" ^ Display.string_of_thm thm'); true)
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28143
diff changeset
   560
      else false;
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   561
  in (thm, proper) :: filter_out drop thms end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   562
28673
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   563
fun add_thm thy _ thm (false, thms) = (false, Lazy.map_force (add_drop_redundant thy thm) thms)
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   564
  | add_thm thy true thm (true, thms) = (true, Lazy.map_force (fn thms => thms @ [thm]) thms)
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   565
  | add_thm thy false thm (true, thms) = (false, Lazy.value [thm]);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   566
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   567
fun add_lthms lthms _ = (false, lthms);
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   568
28673
d746a8c12c43 renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents: 28672
diff changeset
   569
fun del_thm thm = (apsnd o Lazy.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true));
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   570
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   571
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   572
(* specification data *)
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   573
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   574
datatype spec = Spec of {
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   575
  concluded_history: bool,
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   576
  eqns: ((bool * eqns) * (serial * eqns) list) Symtab.table
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   577
    (*with explicit history*),
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   578
  dtyps: ((serial * ((string * sort) list * (string * typ list) list)) list) Symtab.table
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   579
    (*with explicit history*),
30076
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   580
  cases: (int * (int * string list)) Symtab.table * unit Symtab.table
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   581
};
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   582
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   583
fun mk_spec ((concluded_history, eqns), (dtyps, cases)) =
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   584
  Spec { concluded_history = concluded_history, eqns = eqns, dtyps = dtyps, cases = cases };
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   585
val empty_spec =
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   586
  mk_spec ((false, Symtab.empty), (Symtab.empty, (Symtab.empty, Symtab.empty)));
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   587
fun map_spec f (Spec { concluded_history = concluded_history, eqns = eqns,
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   588
  dtyps = dtyps, cases = cases }) =
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   589
  mk_spec (f ((concluded_history, eqns), (dtyps, cases)));
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   590
fun merge_spec (Spec { concluded_history = _, eqns = eqns1, dtyps = dtyps1, cases = (cases1, undefs1) },
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   591
  Spec { concluded_history = _, eqns = eqns2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   592
  let
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   593
    fun merge_eqns ((_, history1), (_, history2)) =
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   594
      let
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   595
        val raw_history = AList.merge (op = : serial * serial -> bool)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   596
          (K true) (history1, history2)
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   597
        val filtered_history = filter_out (fst o snd) raw_history
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   598
        val history = if null filtered_history
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   599
          then raw_history else filtered_history;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   600
      in ((false, (snd o hd) history), history) end;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   601
    val eqns = Symtab.join (K merge_eqns) (eqns1, eqns2);
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   602
    val dtyps = Symtab.join (K (AList.merge (op =) (K true))) (dtyps1, dtyps2);
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   603
    val cases = (Symtab.merge (K true) (cases1, cases2),
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   604
      Symtab.merge (K true) (undefs1, undefs2));
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   605
  in mk_spec ((false, eqns), (dtyps, cases)) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   606
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   607
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   608
(* code setup data *)
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   609
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   610
fun the_spec (Spec x) = x;
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
   611
val the_eqns = #eqns o the_spec;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   612
val the_dtyps = #dtyps o the_spec;
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   613
val the_cases = #cases o the_spec;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   614
val map_concluded_history = map_spec o apfst o apfst;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   615
val map_eqns = map_spec o apfst o apsnd;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   616
val map_dtyps = map_spec o apsnd o apfst;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   617
val map_cases = map_spec o apsnd o apsnd;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   618
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   619
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   620
(* data slots dependent on executable content *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   621
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   622
(*private copy avoids potential conflict of table exceptions*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   623
structure Datatab = TableFun(type key = int val ord = int_ord);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   624
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   625
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   626
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   627
type kind = {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   628
  empty: Object.T,
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   629
  purge: theory -> string list -> Object.T -> Object.T
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   630
};
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   631
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   632
val kinds = ref (Datatab.empty: kind Datatab.table);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   633
val kind_keys = ref ([]: serial list);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   634
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   635
fun invoke f k = case Datatab.lookup (! kinds) k
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   636
 of SOME kind => f kind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   637
  | NONE => sys_error "Invalid code data identifier";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   638
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   639
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   640
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   641
fun declare_data empty purge =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   642
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   643
    val k = serial ();
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   644
    val kind = {empty = empty, purge = purge};
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   645
    val _ = change kinds (Datatab.update (k, kind));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   646
    val _ = change kind_keys (cons k);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   647
  in k end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   648
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   649
fun invoke_init k = invoke (fn kind => #empty kind) k;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   650
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   651
fun invoke_purge_all thy cs =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   652
  fold (fn k => Datatab.map_entry k
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   653
    (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   654
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   655
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   656
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   657
25312
eb9067371342 clarified merge
haftmann
parents: 24969
diff changeset
   658
(** theory store **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   659
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   660
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   661
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   662
type data = Object.T Datatab.table;
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   663
val empty_data = Datatab.empty : data;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   664
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   665
structure Code_Data = TheoryDataFun
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   666
(
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   667
  type T = spec * data ref;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   668
  val empty = (empty_spec, ref empty_data);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   669
  fun copy (spec, data) = (spec, ref (! data));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
  val extend = copy;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   671
  fun merge pp ((spec1, data1), (spec2, data2)) =
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   672
    (merge_spec (spec1, spec2), ref empty_data);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   673
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   674
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   675
fun thy_data f thy = f ((snd o Code_Data.get) thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   676
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   677
fun get_ensure_init kind data_ref =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   678
  case Datatab.lookup (! data_ref) kind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   679
   of SOME x => x
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   680
    | NONE => let val y = invoke_init kind
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   681
        in (change data_ref (Datatab.update (kind, y)); y) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   682
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   683
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   684
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   685
(* access to executable content *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   686
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   687
val the_exec = fst o Code_Data.get;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   688
27983
00e005cdceb0 corrected cache handling for class operations
haftmann
parents: 27675
diff changeset
   689
fun complete_class_params thy cs =
00e005cdceb0 corrected cache handling for class operations
haftmann
parents: 27675
diff changeset
   690
  fold (fn c => case AxClass.inst_of_param thy c
00e005cdceb0 corrected cache handling for class operations
haftmann
parents: 27675
diff changeset
   691
   of NONE => insert (op =) c
00e005cdceb0 corrected cache handling for class operations
haftmann
parents: 27675
diff changeset
   692
    | SOME (c', _) => insert (op =) c' #> insert (op =) c) cs [];
00e005cdceb0 corrected cache handling for class operations
haftmann
parents: 27675
diff changeset
   693
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   694
fun map_exec_purge touched f thy =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   695
  Code_Data.map (fn (exec, data) => (f exec, ref (case touched
27983
00e005cdceb0 corrected cache handling for class operations
haftmann
parents: 27675
diff changeset
   696
   of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data)
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   697
    | NONE => empty_data))) thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   698
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   699
val purge_data = (Code_Data.map o apsnd) (K (ref empty_data));
27675
cb0021d56e11 added explicit purge_data
haftmann
parents: 27609
diff changeset
   700
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   701
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   702
(* tackling equation history *)
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   703
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   704
fun get_eqns thy c =
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   705
  Symtab.lookup ((the_eqns o the_exec) thy) c
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   706
  |> Option.map (Lazy.force o snd o snd o fst)
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   707
  |> these;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   708
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   709
fun continue_history thy = if (#concluded_history o the_spec o the_exec) thy
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   710
  then thy
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   711
    |> (Code_Data.map o apfst o map_concluded_history) (K false)
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   712
    |> SOME
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   713
  else NONE;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   714
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   715
fun conclude_history thy = if (#concluded_history o the_spec o the_exec) thy
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   716
  then NONE
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   717
  else thy
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   718
    |> (Code_Data.map o apfst)
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   719
        ((map_eqns o Symtab.map) (fn ((changed, current), history) =>
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   720
          ((false, current),
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   721
            if changed then (serial (), current) :: history else history))
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   722
        #> map_concluded_history (K true))
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   723
    |> SOME;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   724
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   725
val _ = Context.>> (Context.map_theory (Code_Data.init
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   726
  #> Theory.at_begin continue_history
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   727
  #> Theory.at_end conclude_history));
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   728
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   729
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   730
(* access to data dependent on abstract executable content *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   731
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   732
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   733
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   734
fun change_data (kind, mk, dest) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   735
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   736
    fun chnge data_ref f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   737
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   738
        val data = get_ensure_init kind data_ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   739
        val data' = f (dest data);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   740
      in (change data_ref (Datatab.update (kind, mk data')); data') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   741
  in thy_data chnge end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   742
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   743
fun change_yield_data (kind, mk, dest) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   744
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   745
    fun chnge data_ref f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   746
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   747
        val data = get_ensure_init kind data_ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   748
        val (x, data') = f (dest data);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   749
      in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   750
  in thy_data chnge end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   751
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   752
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   753
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   754
fun print_codesetup thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   755
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   756
    val ctxt = ProofContext.init thy;
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   757
    val exec = the_exec thy;
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
   758
    fun pretty_eqn (s, (_, lthms)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   759
      (Pretty.block o Pretty.fbreaks) (
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   760
        Pretty.str s :: pretty_lthms ctxt lthms
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   761
      );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   762
    fun pretty_dtyp (s, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   763
          Pretty.str s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   764
      | pretty_dtyp (s, cos) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   765
          (Pretty.block o Pretty.breaks) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   766
            Pretty.str s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   767
            :: Pretty.str "="
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   768
            :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (string_of_const thy c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   769
                 | (c, tys) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   770
                     (Pretty.block o Pretty.breaks)
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   771
                        (Pretty.str (string_of_const thy c)
26947
133905a0c493 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   772
                          :: Pretty.str "of"
133905a0c493 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   773
                          :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   774
          );
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
   775
    val eqns = the_eqns exec
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   776
      |> Symtab.dest
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   777
      |> (map o apfst) (string_of_const thy)
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   778
      |> (map o apsnd) (snd o fst)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   779
      |> sort (string_ord o pairself fst);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   780
    val dtyps = the_dtyps exec
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   781
      |> Symtab.dest
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   782
      |> map (fn (dtco, (_, (vs, cos)) :: _) =>
26947
133905a0c493 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   783
          (Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   784
      |> sort (string_ord o pairself fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   785
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   786
    (Pretty.writeln o Pretty.chunks) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   787
      Pretty.block (
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   788
        Pretty.str "code equations:"
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   789
        :: Pretty.fbrk
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
   790
        :: (Pretty.fbreaks o map pretty_eqn) eqns
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   791
      ),
25968
66cfe1d00be0 print postprocessor equations
haftmann
parents: 25597
diff changeset
   792
      Pretty.block (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   793
        Pretty.str "datatypes:"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   794
        :: Pretty.fbrk
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   795
        :: (Pretty.fbreaks o map pretty_dtyp) dtyps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   796
      )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   797
    ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   798
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   799
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   800
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   801
(** theorem transformation and certification **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   802
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28403
diff changeset
   803
fun common_typ_eqns thy [] = []
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28403
diff changeset
   804
  | common_typ_eqns thy [thm] = [thm]
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28403
diff changeset
   805
  | common_typ_eqns thy (thms as thm :: _) = (*FIXME is too general*)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   806
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   807
        fun incr_thm thm max =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   808
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   809
            val thm' = incr_indexes max thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   810
            val max' = Thm.maxidx_of thm' + 1;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   811
          in (thm', max') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   812
        val (thms', maxidx) = fold_map incr_thm thms 0;
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   813
        val ty1 :: tys = map (snd o const_typ_eqn) thms';
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   814
        fun unify ty env = Sign.typ_unify thy (ty1, ty) env
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   815
          handle Type.TUNIFY =>
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   816
            error ("Type unificaton failed, while unifying code equations\n"
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   817
            ^ (cat_lines o map Display.string_of_thm) thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   818
            ^ "\nwith types\n"
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   819
            ^ (cat_lines o map (string_of_typ thy)) (ty1 :: tys));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   820
        val (env, _) = fold unify tys (Vartab.empty, maxidx)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   821
        val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   822
          cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   823
      in map (Thm.instantiate (instT, [])) thms' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   824
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   825
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   826
(** interfaces and attributes **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   827
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   828
fun get_datatype thy tyco =
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   829
  case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco)
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   830
   of (_, spec) :: _ => spec
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   831
    | [] => Sign.arity_number thy tyco
24848
5dbbd33c3236 replaced literal 'a by Name.aT;
wenzelm
parents: 24844
diff changeset
   832
        |> Name.invents Name.context Name.aT
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   833
        |> map (rpair [])
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   834
        |> rpair [];
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   835
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   836
fun get_datatype_of_constr thy c =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   837
  case (snd o strip_type o Sign.the_const_type thy) c
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   838
   of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   839
       then SOME tyco else NONE
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   840
    | _ => NONE;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   841
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   842
fun is_constr thy = is_some o get_datatype_of_constr thy;
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   843
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   844
val assert_eqn = fn thy => assert_eqn thy (is_constr thy);
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28703
diff changeset
   845
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   846
fun assert_eqns_const thy c eqns =
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28703
diff changeset
   847
  let
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   848
    fun cert (eqn as (thm, _)) = if c = const_eqn thy thm
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   849
      then eqn else error ("Wrong head of code equation,\nexpected constant "
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   850
        ^ string_of_const thy c ^ "\n" ^ Display.string_of_thm thm)
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   851
  in map (cert o assert_eqn thy) eqns end;
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28703
diff changeset
   852
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   853
fun change_eqns delete c f = (map_exec_purge (SOME [c]) o map_eqns
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   854
  o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, Lazy.value [])), [])))
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   855
    o apfst) (fn (_, eqns) => (true, f eqns));
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   856
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   857
fun gen_add_eqn default (eqn as (thm, _)) thy =
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   858
  let val c = const_eqn thy thm
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   859
  in change_eqns false c (add_thm thy default eqn) thy end;
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   860
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   861
fun add_eqn thm thy =
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   862
  gen_add_eqn false (mk_eqn thy (is_constr thy) (thm, true)) thy;
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   863
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   864
fun add_default_eqn thm thy =
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   865
  case mk_eqn_liberal thy (is_constr thy) thm
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   866
   of SOME eqn => gen_add_eqn true eqn thy
24624
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24585
diff changeset
   867
    | NONE => thy;
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24585
diff changeset
   868
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   869
fun add_nbe_eqn thm thy =
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   870
  gen_add_eqn false (mk_eqn thy (is_constr thy) (thm, false)) thy;
26021
25d06476727e explicit del_funcs
haftmann
parents: 25968
diff changeset
   871
28368
8437fb395294 clarified function transformator interface
haftmann
parents: 28359
diff changeset
   872
fun add_eqnl (c, lthms) thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   873
  let
31090
3be41b271023 clarified matter of "proper" flag in code equations
haftmann
parents: 31088
diff changeset
   874
    val lthms' = certificate thy (fn thy => assert_eqns_const thy c) lthms;
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   875
  in change_eqns false c (add_lthms lthms') thy end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   876
28703
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28695
diff changeset
   877
val add_default_eqn_attribute = Thm.declaration_attribute
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28695
diff changeset
   878
  (fn thm => Context.mapping (add_default_eqn thm) I);
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28695
diff changeset
   879
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   880
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   881
fun del_eqn thm thy = case mk_eqn_liberal thy (is_constr thy) thm
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   882
 of SOME (thm, _) => change_eqns true (const_eqn thy thm) (del_thm thm) thy
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   883
  | NONE => thy;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   884
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   885
fun del_eqns c = change_eqns true c (K (false, Lazy.value []));
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   886
30076
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   887
fun get_case_scheme thy = Symtab.lookup ((fst o the_cases o the_exec) thy);
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   888
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   889
val is_undefined = Symtab.defined o snd o the_cases o the_exec;
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   890
25485
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   891
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
25462
dad0291cb76a rudimentary instantiation target
haftmann
parents: 25312
diff changeset
   892
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   893
fun add_datatype raw_cs thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   894
  let
25597
34860182b250 moved instance parameter management from class.ML to axclass.ML
haftmann
parents: 25501
diff changeset
   895
    val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   896
    val (tyco, vs_cos) = constrset_of_consts thy cs;
30076
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   897
    val old_cs = (map fst o snd o get_datatype thy) tyco;
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   898
    fun drop_outdated_cases cases = fold Symtab.delete_safe
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   899
      (Symtab.fold (fn (c, (_, (_, cos))) =>
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   900
        if exists (member (op =) old_cs) cos
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   901
          then insert (op =) c else I) cases []) cases;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   902
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   903
    thy
31091
8316d22f10f5 corrected deletetion of code equations for constructors
haftmann
parents: 31090
diff changeset
   904
    |> fold (del_eqns o fst) cs
28703
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28695
diff changeset
   905
    |> map_exec_purge NONE
28695
f7a06d7284c8 explicit history for equations; tuned
haftmann
parents: 28673
diff changeset
   906
        ((map_dtyps o Symtab.map_default (tyco, [])) (cons (serial (), vs_cos))
30076
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   907
        #> (map_cases o apfst) drop_outdated_cases)
25485
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   908
    |> TypeInterpretation.data (tyco, serial ())
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   909
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   910
25485
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   911
fun type_interpretation f =  TypeInterpretation.interpretation
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   912
  (fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   913
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   914
fun add_datatype_cmd raw_cs thy =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   915
  let
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   916
    val cs = map (read_bare_const thy) raw_cs;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   917
  in add_datatype cs thy end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   918
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   919
fun add_case thm thy =
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   920
  let
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   921
    val (c, (k, case_pats)) = case_cert thm;
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   922
    val _ = case filter_out (is_constr thy) case_pats
30076
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   923
     of [] => ()
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   924
      | cs => error ("Non-constructor(s) in case certificate: " ^ commas (map quote cs));
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   925
    val entry = (1 + Int.max (1, length case_pats), (k, case_pats))
f3043dafef5f improved treatment of case certificates
haftmann
parents: 30060
diff changeset
   926
  in (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update (c, entry)) thy end;
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   927
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   928
fun add_undefined c thy =
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   929
  (map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy;
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24837
diff changeset
   930
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
   931
val _ = Context.>> (Context.map_theory
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   932
  (let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   933
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   934
    fun add_simple_attribute (name, f) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   935
      add_attribute (name, Scan.succeed (mk_attribute f));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   936
    fun add_del_attribute (name, (add, del)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   937
      add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   938
        || Scan.succeed (mk_attribute add))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   939
  in
25462
dad0291cb76a rudimentary instantiation target
haftmann
parents: 25312
diff changeset
   940
    TypeInterpretation.init
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28525
diff changeset
   941
    #> add_del_attribute ("", (add_eqn, del_eqn))
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   942
    #> add_simple_attribute ("nbe", add_nbe_eqn)
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
   943
  end));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   944
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28403
diff changeset
   945
fun these_eqns thy c =
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   946
  get_eqns thy c
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   947
  |> (map o apfst) (Thm.transfer thy)
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31091
diff changeset
   948
  |> burrow_fst (common_typ_eqns thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   949
28525
42297ae4df47 clarified preprocessor policies
haftmann
parents: 28423
diff changeset
   950
fun default_typscheme thy c =
42297ae4df47 clarified preprocessor policies
haftmann
parents: 28423
diff changeset
   951
  let
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   952
    fun the_const_typscheme c = (curry (typscheme thy) c o snd o dest_Const
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   953
      o TermSubst.zero_var_indexes o curry Const "" o Sign.the_const_type thy) c;
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   954
    fun strip_sorts (vs, ty) = (map (fn (v, _) => (v, [])) vs, ty);
28423
9fc3befd8191 clarified codegen interfaces
haftmann
parents: 28403
diff changeset
   955
  in case AxClass.class_of_param thy c
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   956
   of SOME class => ([(Name.aT, [class])], snd (the_const_typscheme c))
31088
36a011423fcc clarified terminilogy concerning nbe equations
haftmann
parents: 30928
diff changeset
   957
    | NONE => if is_constr thy c
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   958
        then strip_sorts (the_const_typscheme c)
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   959
        else case get_eqns thy c
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31152
diff changeset
   960
         of (thm, _) :: _ => (typscheme_eqn thy o Drule.zero_var_indexes) thm
30023
55954f726043 permissive check for pattern discipline in case schemes
haftmann
parents: 29970
diff changeset
   961
          | [] => strip_sorts (the_const_typscheme c) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   962
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   963
end; (*struct*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   964
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   965
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   966
(** type-safe interfaces for data depedent on executable content **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   967
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   968
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   969
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   970
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   971
type T = Data.T;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   972
exception Data of T;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   973
fun dest (Data x) = x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   974
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   975
val kind = Code.declare_data (Data Data.empty)
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27582
diff changeset
   976
  (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   977
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   978
val data_op = (kind, Data, dest);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   979
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   980
val get = Code.get_data data_op;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   981
val change = Code.change_data data_op;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   982
fun change_yield thy = Code.change_yield_data data_op thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   983
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   984
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   985
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 28054
diff changeset
   986
structure Code : CODE = struct open Code; end;