src/Pure/Isar/overloading.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 36354 bbd742107f56
child 38342 09d4a04d5c2e
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
haftmann@25519
     1
(*  Title:      Pure/Isar/overloading.ML
haftmann@25519
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@25519
     3
haftmann@25519
     4
Overloaded definitions without any discipline.
haftmann@25519
     5
*)
haftmann@25519
     6
haftmann@25519
     7
signature OVERLOADING =
haftmann@25519
     8
sig
haftmann@35126
     9
  val init: (string * (string * typ) * bool) list -> theory -> Proof.context
haftmann@25519
    10
  val conclude: local_theory -> local_theory
haftmann@25519
    11
  val declare: string * typ -> theory -> term * theory
haftmann@30519
    12
  val confirm: binding -> local_theory -> local_theory
haftmann@30519
    13
  val define: bool -> binding -> string * term -> theory -> thm * theory
haftmann@30519
    14
  val operation: Proof.context -> binding -> (string * bool) option
haftmann@25606
    15
  val pretty: Proof.context -> Pretty.T
haftmann@35859
    16
haftmann@26238
    17
  type improvable_syntax
haftmann@26238
    18
  val add_improvable_syntax: Proof.context -> Proof.context
haftmann@26238
    19
  val map_improvable_syntax: (improvable_syntax -> improvable_syntax)
haftmann@26238
    20
    -> Proof.context -> Proof.context
haftmann@26520
    21
  val set_primary_constraints: Proof.context -> Proof.context
haftmann@25519
    22
end;
haftmann@25519
    23
haftmann@25519
    24
structure Overloading: OVERLOADING =
haftmann@25519
    25
struct
haftmann@25519
    26
haftmann@26259
    27
(** generic check/uncheck combinators for improvable constants **)
haftmann@26238
    28
haftmann@26249
    29
type improvable_syntax = ((((string * typ) list * (string * typ) list) *
haftmann@26730
    30
  ((((string * typ -> (typ * typ) option) * (string * typ -> (typ * term) option)) * bool) *
haftmann@26249
    31
    (term * term) list)) * bool);
haftmann@25519
    32
wenzelm@33519
    33
structure ImprovableSyntax = Proof_Data
wenzelm@33519
    34
(
haftmann@26249
    35
  type T = {
haftmann@26520
    36
    primary_constraints: (string * typ) list,
haftmann@26520
    37
    secondary_constraints: (string * typ) list,
haftmann@26249
    38
    improve: string * typ -> (typ * typ) option,
haftmann@26249
    39
    subst: string * typ -> (typ * term) option,
haftmann@26730
    40
    consider_abbrevs: bool,
haftmann@26249
    41
    unchecks: (term * term) list,
haftmann@26249
    42
    passed: bool
haftmann@26249
    43
  };
haftmann@26238
    44
  fun init _ = {
haftmann@26520
    45
    primary_constraints = [],
haftmann@26520
    46
    secondary_constraints = [],
haftmann@26238
    47
    improve = K NONE,
haftmann@26238
    48
    subst = K NONE,
haftmann@26730
    49
    consider_abbrevs = false,
haftmann@26238
    50
    unchecks = [],
haftmann@26238
    51
    passed = true
haftmann@26238
    52
  };
haftmann@26238
    53
);
haftmann@25536
    54
haftmann@26520
    55
fun map_improvable_syntax f = ImprovableSyntax.map (fn { primary_constraints,
haftmann@26730
    56
  secondary_constraints, improve, subst, consider_abbrevs, unchecks, passed } => let
haftmann@26730
    57
    val (((primary_constraints', secondary_constraints'),
haftmann@26730
    58
      (((improve', subst'), consider_abbrevs'), unchecks')), passed')
haftmann@26730
    59
        = f (((primary_constraints, secondary_constraints),
haftmann@26730
    60
            (((improve, subst), consider_abbrevs), unchecks)), passed)
haftmann@26520
    61
  in { primary_constraints = primary_constraints', secondary_constraints = secondary_constraints',
haftmann@26730
    62
    improve = improve', subst = subst', consider_abbrevs = consider_abbrevs',
haftmann@26730
    63
    unchecks = unchecks', passed = passed'
haftmann@26249
    64
  } end);
haftmann@26238
    65
haftmann@26249
    66
val mark_passed = (map_improvable_syntax o apsnd) (K true);
haftmann@26238
    67
haftmann@26238
    68
fun improve_term_check ts ctxt =
haftmann@25519
    69
  let
wenzelm@36354
    70
    val { secondary_constraints, improve, subst, consider_abbrevs, passed, ... } =
wenzelm@36354
    71
      ImprovableSyntax.get ctxt;
haftmann@26238
    72
    val tsig = (Sign.tsig_of o ProofContext.theory_of) ctxt;
wenzelm@27285
    73
    val is_abbrev = consider_abbrevs andalso ProofContext.abbrev_mode ctxt;
haftmann@26730
    74
    val passed_or_abbrev = passed orelse is_abbrev;
haftmann@26238
    75
    fun accumulate_improvements (Const (c, ty)) = (case improve (c, ty)
haftmann@26597
    76
         of SOME ty_ty' => Type.typ_match tsig ty_ty'
haftmann@26238
    77
          | _ => I)
haftmann@26238
    78
      | accumulate_improvements _ = I;
haftmann@26238
    79
    val improvements = (fold o fold_aterms) accumulate_improvements ts Vartab.empty;
wenzelm@32035
    80
    val ts' = (map o map_types) (Envir.subst_type improvements) ts;
haftmann@26238
    81
    fun apply_subst t = Envir.expand_term (fn Const (c, ty) => (case subst (c, ty)
haftmann@26259
    82
         of SOME (ty', t') =>
haftmann@26238
    83
              if Type.typ_instance tsig (ty, ty')
haftmann@26238
    84
              then SOME (ty', apply_subst t') else NONE
haftmann@26238
    85
          | NONE => NONE)
haftmann@26259
    86
        | _ => NONE) t;
haftmann@26730
    87
    val ts'' = if is_abbrev then ts' else map apply_subst ts';
haftmann@26730
    88
  in if eq_list (op aconv) (ts, ts'') andalso passed_or_abbrev then NONE else
haftmann@26730
    89
    if passed_or_abbrev then SOME (ts'', ctxt)
haftmann@26238
    90
    else SOME (ts'', ctxt
haftmann@26520
    91
      |> fold (ProofContext.add_const_constraint o apsnd SOME) secondary_constraints
haftmann@26238
    92
      |> mark_passed)
haftmann@26238
    93
  end;
haftmann@25519
    94
haftmann@31698
    95
fun rewrite_liberal thy unchecks t =
haftmann@31698
    96
  case try (Pattern.rewrite_term thy unchecks []) t
haftmann@31698
    97
   of NONE => NONE
haftmann@31698
    98
    | SOME t' => if t aconv t' then NONE else SOME t';
haftmann@31698
    99
haftmann@26238
   100
fun improve_term_uncheck ts ctxt =
haftmann@25519
   101
  let
haftmann@26238
   102
    val thy = ProofContext.theory_of ctxt;
haftmann@26238
   103
    val unchecks = (#unchecks o ImprovableSyntax.get) ctxt;
haftmann@31698
   104
    val ts' = map (rewrite_liberal thy unchecks) ts;
haftmann@31698
   105
  in if exists is_some ts' then SOME (map2 the_default ts ts', ctxt) else NONE end;
haftmann@26238
   106
haftmann@26520
   107
fun set_primary_constraints ctxt =
haftmann@26259
   108
  let
haftmann@26520
   109
    val { primary_constraints, ... } = ImprovableSyntax.get ctxt;
haftmann@26520
   110
  in fold (ProofContext.add_const_constraint o apsnd SOME) primary_constraints ctxt end;
haftmann@26259
   111
haftmann@26259
   112
val add_improvable_syntax =
haftmann@26259
   113
  Context.proof_map
haftmann@26238
   114
    (Syntax.add_term_check 0 "improvement" improve_term_check
haftmann@26238
   115
    #> Syntax.add_term_uncheck 0 "improvement" improve_term_uncheck)
haftmann@26520
   116
  #> set_primary_constraints;
haftmann@26259
   117
haftmann@26259
   118
haftmann@26259
   119
(** overloading target **)
haftmann@26259
   120
haftmann@26259
   121
(* bookkeeping *)
haftmann@26259
   122
wenzelm@33519
   123
structure OverloadingData = Proof_Data
haftmann@26259
   124
(
haftmann@26259
   125
  type T = ((string * typ) * (string * bool)) list;
haftmann@26259
   126
  fun init _ = [];
haftmann@26259
   127
);
haftmann@26259
   128
wenzelm@33671
   129
val get_overloading = OverloadingData.get o Local_Theory.target_of;
wenzelm@33671
   130
val map_overloading = Local_Theory.target o OverloadingData.map;
haftmann@26259
   131
haftmann@30519
   132
fun operation lthy b = get_overloading lthy
haftmann@30519
   133
  |> get_first (fn ((c, _), (v, checked)) =>
haftmann@30519
   134
      if Binding.name_of b = v then SOME (c, checked) else NONE);
haftmann@26259
   135
haftmann@32343
   136
haftmann@32343
   137
(* target *)
haftmann@26259
   138
haftmann@32343
   139
fun synchronize_syntax ctxt =
haftmann@32343
   140
  let
haftmann@32343
   141
    val overloading = OverloadingData.get ctxt;
haftmann@32343
   142
    fun subst (c, ty) = case AList.lookup (op =) overloading (c, ty)
haftmann@32343
   143
     of SOME (v, _) => SOME (ty, Free (v, ty))
haftmann@32343
   144
      | NONE => NONE;
haftmann@32343
   145
    val unchecks =
haftmann@32343
   146
      map (fn (c_ty as (_, ty), (v, _)) => (Free (v, ty), Const c_ty)) overloading;
haftmann@32343
   147
  in 
haftmann@32343
   148
    ctxt
haftmann@32343
   149
    |> map_improvable_syntax (K ((([], []), (((K NONE, subst), false), unchecks)), false))
haftmann@32343
   150
  end
haftmann@26259
   151
haftmann@32343
   152
fun init raw_overloading thy =
haftmann@32343
   153
  let
haftmann@32343
   154
    val _ = if null raw_overloading then error "At least one parameter must be given" else ();
haftmann@32343
   155
    val overloading = map (fn (v, c_ty, checked) => (c_ty, (v, checked))) raw_overloading;
haftmann@32343
   156
  in
haftmann@32343
   157
    thy
haftmann@32379
   158
    |> Theory.checkpoint
wenzelm@36610
   159
    |> ProofContext.init_global
haftmann@32343
   160
    |> OverloadingData.put overloading
haftmann@32343
   161
    |> fold (fn ((_, ty), (v, _)) => Variable.declare_names (Free (v, ty))) overloading
haftmann@32343
   162
    |> add_improvable_syntax
haftmann@32343
   163
    |> synchronize_syntax
haftmann@32343
   164
  end;
haftmann@26259
   165
haftmann@26259
   166
fun declare c_ty = pair (Const c_ty);
haftmann@26259
   167
wenzelm@36106
   168
fun define checked b (c, t) =
wenzelm@36106
   169
  Thm.add_def (not checked) true (b, Logic.mk_equals (Const (c, Term.fastype_of t), t))
wenzelm@36106
   170
  #>> snd;
haftmann@25519
   171
haftmann@32343
   172
fun confirm b = map_overloading (filter_out (fn (_, (c', _)) => c' = Binding.name_of b))
wenzelm@33671
   173
  #> Local_Theory.target synchronize_syntax
haftmann@25519
   174
haftmann@25519
   175
fun conclude lthy =
haftmann@25519
   176
  let
haftmann@25519
   177
    val overloading = get_overloading lthy;
haftmann@25519
   178
    val _ = if null overloading then () else
haftmann@26259
   179
      error ("Missing definition(s) for parameter(s) " ^ commas (map (quote
haftmann@25519
   180
        o Syntax.string_of_term lthy o Const o fst) overloading));
haftmann@25519
   181
  in
haftmann@25519
   182
    lthy
haftmann@25519
   183
  end;
haftmann@25519
   184
haftmann@25606
   185
fun pretty lthy =
haftmann@25606
   186
  let
haftmann@25606
   187
    val thy = ProofContext.theory_of lthy;
haftmann@25606
   188
    val overloading = get_overloading lthy;
haftmann@25606
   189
    fun pr_operation ((c, ty), (v, _)) =
haftmann@25861
   190
      (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
wenzelm@26939
   191
        Pretty.str (Sign.extern_const thy c), Pretty.str "::", Syntax.pretty_typ lthy ty];
haftmann@25606
   192
  in
haftmann@25606
   193
    (Pretty.block o Pretty.fbreaks)
haftmann@25606
   194
      (Pretty.str "overloading" :: map pr_operation overloading)
haftmann@25606
   195
  end;
haftmann@25606
   196
haftmann@25519
   197
end;