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