src/Pure/Isar/overloading.ML
author wenzelm
Tue, 03 Apr 2012 10:59:20 +0200
changeset 48170 392c4cd97e5c
parent 48163 4bab649dedf0
child 48173 323b7d74b2a8
permissions -rw-r--r--
more uniform theory_abbrev with const_declaration;
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@26238
     9
  type improvable_syntax
haftmann@39624
    10
  val activate_improvable_syntax: Proof.context -> Proof.context
haftmann@26238
    11
  val map_improvable_syntax: (improvable_syntax -> improvable_syntax)
haftmann@26238
    12
    -> Proof.context -> Proof.context
haftmann@26520
    13
  val set_primary_constraints: Proof.context -> Proof.context
haftmann@38578
    14
haftmann@38578
    15
  val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
haftmann@38578
    16
  val overloading_cmd: (string * string * bool) list -> theory -> local_theory
haftmann@25519
    17
end;
haftmann@25519
    18
haftmann@25519
    19
structure Overloading: OVERLOADING =
haftmann@25519
    20
struct
haftmann@25519
    21
wenzelm@43285
    22
(* generic check/uncheck combinators for improvable constants *)
haftmann@25519
    23
haftmann@26249
    24
type improvable_syntax = ((((string * typ) list * (string * typ) list) *
haftmann@26730
    25
  ((((string * typ -> (typ * typ) option) * (string * typ -> (typ * term) option)) * bool) *
haftmann@26249
    26
    (term * term) list)) * bool);
haftmann@25536
    27
wenzelm@43285
    28
structure Improvable_Syntax = Proof_Data
wenzelm@33519
    29
(
haftmann@26249
    30
  type T = {
haftmann@26520
    31
    primary_constraints: (string * typ) list,
haftmann@26520
    32
    secondary_constraints: (string * typ) list,
haftmann@26249
    33
    improve: string * typ -> (typ * typ) option,
haftmann@26249
    34
    subst: string * typ -> (typ * term) option,
haftmann@26730
    35
    consider_abbrevs: bool,
haftmann@26249
    36
    unchecks: (term * term) list,
haftmann@26249
    37
    passed: bool
haftmann@26249
    38
  };
haftmann@26238
    39
  fun init _ = {
haftmann@26520
    40
    primary_constraints = [],
haftmann@26520
    41
    secondary_constraints = [],
haftmann@26238
    42
    improve = K NONE,
haftmann@26238
    43
    subst = K NONE,
haftmann@26730
    44
    consider_abbrevs = false,
haftmann@26238
    45
    unchecks = [],
haftmann@26238
    46
    passed = true
haftmann@26238
    47
  };
haftmann@26238
    48
);
haftmann@26238
    49
wenzelm@43285
    50
fun map_improvable_syntax f = Improvable_Syntax.map (fn {primary_constraints,
wenzelm@43285
    51
    secondary_constraints, improve, subst, consider_abbrevs, unchecks, passed} =>
wenzelm@41032
    52
  let
haftmann@26730
    53
    val (((primary_constraints', secondary_constraints'),
haftmann@26730
    54
      (((improve', subst'), consider_abbrevs'), unchecks')), passed')
haftmann@26730
    55
        = f (((primary_constraints, secondary_constraints),
haftmann@26730
    56
            (((improve, subst), consider_abbrevs), unchecks)), passed)
wenzelm@43285
    57
  in
wenzelm@43285
    58
   {primary_constraints = primary_constraints', secondary_constraints = secondary_constraints',
haftmann@26730
    59
    improve = improve', subst = subst', consider_abbrevs = consider_abbrevs',
wenzelm@43285
    60
    unchecks = unchecks', passed = passed'}
wenzelm@41032
    61
  end);
haftmann@26238
    62
haftmann@26249
    63
val mark_passed = (map_improvable_syntax o apsnd) (K true);
haftmann@26238
    64
haftmann@26238
    65
fun improve_term_check ts ctxt =
haftmann@25519
    66
  let
wenzelm@43263
    67
    val thy = Proof_Context.theory_of ctxt;
wenzelm@43263
    68
wenzelm@43285
    69
    val {secondary_constraints, improve, subst, consider_abbrevs, passed, ...} =
wenzelm@43285
    70
      Improvable_Syntax.get ctxt;
wenzelm@43231
    71
    val is_abbrev = consider_abbrevs andalso Proof_Context.abbrev_mode ctxt;
haftmann@26730
    72
    val passed_or_abbrev = passed orelse is_abbrev;
wenzelm@43263
    73
    fun accumulate_improvements (Const (c, ty)) =
wenzelm@43263
    74
          (case improve (c, ty) of
wenzelm@43263
    75
            SOME ty_ty' => Sign.typ_match thy ty_ty'
haftmann@26238
    76
          | _ => I)
haftmann@26238
    77
      | accumulate_improvements _ = I;
haftmann@26238
    78
    val improvements = (fold o fold_aterms) accumulate_improvements ts Vartab.empty;
wenzelm@32046
    79
    val ts' = (map o map_types) (Envir.subst_type improvements) ts;
wenzelm@43263
    80
    fun apply_subst t =
wenzelm@43263
    81
      Envir.expand_term
wenzelm@43263
    82
        (fn Const (c, ty) =>
wenzelm@43263
    83
          (case subst (c, ty) of
wenzelm@43263
    84
            SOME (ty', t') =>
wenzelm@43263
    85
              if Sign.typ_instance thy (ty, ty')
haftmann@26238
    86
              then SOME (ty', apply_subst t') else NONE
haftmann@26238
    87
          | NONE => NONE)
haftmann@26259
    88
        | _ => NONE) t;
haftmann@26730
    89
    val ts'' = if is_abbrev then ts' else map apply_subst ts';
wenzelm@41032
    90
  in
wenzelm@43285
    91
    if eq_list (op aconv) (ts, ts'') andalso passed_or_abbrev then NONE
wenzelm@43285
    92
    else if passed_or_abbrev then SOME (ts'', ctxt)
wenzelm@43285
    93
    else
wenzelm@43285
    94
      SOME (ts'', ctxt
wenzelm@43285
    95
        |> fold (Proof_Context.add_const_constraint o apsnd SOME) secondary_constraints
wenzelm@43285
    96
        |> mark_passed)
haftmann@26238
    97
  end;
haftmann@25519
    98
haftmann@31698
    99
fun rewrite_liberal thy unchecks t =
wenzelm@43285
   100
  (case try (Pattern.rewrite_term thy unchecks []) t of
wenzelm@43285
   101
    NONE => NONE
wenzelm@43285
   102
  | SOME t' => if t aconv t' then NONE else SOME t');
haftmann@31698
   103
haftmann@26238
   104
fun improve_term_uncheck ts ctxt =
haftmann@25519
   105
  let
wenzelm@43231
   106
    val thy = Proof_Context.theory_of ctxt;
wenzelm@43285
   107
    val {unchecks, ...} = Improvable_Syntax.get ctxt;
haftmann@31698
   108
    val ts' = map (rewrite_liberal thy unchecks) ts;
haftmann@31698
   109
  in if exists is_some ts' then SOME (map2 the_default ts ts', ctxt) else NONE end;
haftmann@26238
   110
haftmann@26520
   111
fun set_primary_constraints ctxt =
wenzelm@43285
   112
  let val {primary_constraints, ...} = Improvable_Syntax.get ctxt;
wenzelm@43231
   113
  in fold (Proof_Context.add_const_constraint o apsnd SOME) primary_constraints ctxt end;
haftmann@26259
   114
haftmann@39624
   115
val activate_improvable_syntax =
haftmann@26259
   116
  Context.proof_map
wenzelm@46315
   117
    (Syntax_Phases.term_check' 0 "improvement" improve_term_check
wenzelm@46315
   118
    #> Syntax_Phases.term_uncheck' 0 "improvement" improve_term_uncheck)
haftmann@26520
   119
  #> set_primary_constraints;
haftmann@26259
   120
haftmann@26259
   121
wenzelm@43285
   122
(* overloading target *)
haftmann@26259
   123
haftmann@38608
   124
structure Data = Proof_Data
haftmann@26259
   125
(
haftmann@26259
   126
  type T = ((string * typ) * (string * bool)) list;
haftmann@26259
   127
  fun init _ = [];
haftmann@26259
   128
);
haftmann@26259
   129
haftmann@38608
   130
val get_overloading = Data.get o Local_Theory.target_of;
haftmann@38608
   131
val map_overloading = Local_Theory.target o Data.map;
haftmann@26259
   132
wenzelm@43285
   133
fun operation lthy b =
wenzelm@43285
   134
  get_overloading lthy
haftmann@30511
   135
  |> get_first (fn ((c, _), (v, checked)) =>
haftmann@38608
   136
      if Binding.name_of b = v then SOME (c, (v, checked)) else NONE);
haftmann@26259
   137
haftmann@32343
   138
fun synchronize_syntax ctxt =
haftmann@32343
   139
  let
haftmann@38608
   140
    val overloading = Data.get ctxt;
wenzelm@43285
   141
    fun subst (c, ty) =
wenzelm@43285
   142
      (case AList.lookup (op =) overloading (c, ty) of
wenzelm@43285
   143
        SOME (v, _) => SOME (ty, Free (v, ty))
wenzelm@43285
   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))
wenzelm@47798
   150
  end;
haftmann@32343
   151
haftmann@38608
   152
fun define_overloaded (c, U) (v, checked) (b_def, rhs) =
wenzelm@39032
   153
  Local_Theory.background_theory_result
wenzelm@43246
   154
    (Thm.add_def_global (not checked) true
wenzelm@47798
   155
      (Thm.def_binding_optional (Binding.name v) b_def,
wenzelm@47798
   156
        Logic.mk_equals (Const (c, Term.fastype_of rhs), rhs)))
haftmann@38608
   157
  ##> map_overloading (filter_out (fn (_, (v', _)) => v' = v))
wenzelm@48121
   158
  ##> Local_Theory.map_contexts (K synchronize_syntax)
wenzelm@47798
   159
  #-> (fn (_, def) => pair (Const (c, U), def));
haftmann@26259
   160
haftmann@38608
   161
fun foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
wenzelm@43285
   162
  (case operation lthy b of
wenzelm@43285
   163
    SOME (c, (v, checked)) =>
wenzelm@43285
   164
      if mx <> NoSyn
wenzelm@43285
   165
      then error ("Illegal mixfix syntax for overloaded constant " ^ quote c)
wenzelm@43285
   166
      else lthy |> define_overloaded (c, U) (v, checked) (b_def, rhs)
wenzelm@43285
   167
  | NONE => lthy
wenzelm@43285
   168
      |> Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params));
haftmann@25519
   169
haftmann@25606
   170
fun pretty lthy =
haftmann@25606
   171
  let
haftmann@25606
   172
    val overloading = get_overloading lthy;
haftmann@25606
   173
    fun pr_operation ((c, ty), (v, _)) =
wenzelm@43230
   174
      Pretty.block (Pretty.breaks
wenzelm@43231
   175
        [Pretty.str v, Pretty.str "==", Pretty.str (Proof_Context.extern_const lthy c),
wenzelm@43230
   176
          Pretty.str "::", Syntax.pretty_typ lthy ty]);
wenzelm@47805
   177
  in Pretty.command "overloading" :: map pr_operation overloading end;
haftmann@25606
   178
haftmann@38608
   179
fun conclude lthy =
haftmann@38608
   180
  let
haftmann@38608
   181
    val overloading = get_overloading lthy;
wenzelm@41032
   182
    val _ =
wenzelm@41032
   183
      if null overloading then ()
wenzelm@41032
   184
      else
wenzelm@43285
   185
        error ("Missing definition(s) for parameter(s) " ^
wenzelm@43285
   186
          commas_quote (map (Syntax.string_of_term lthy o Const o fst) overloading));
haftmann@38608
   187
  in lthy end;
haftmann@38578
   188
haftmann@38608
   189
fun gen_overloading prep_const raw_overloading thy =
haftmann@38578
   190
  let
wenzelm@43231
   191
    val ctxt = Proof_Context.init_global thy;
wenzelm@47938
   192
    val naming = Sign.naming_of thy;
haftmann@38608
   193
    val _ = if null raw_overloading then error "At least one parameter must be given" else ();
haftmann@38608
   194
    val overloading = raw_overloading |> map (fn (v, const, checked) =>
haftmann@38608
   195
      (Term.dest_Const (prep_const ctxt const), (v, checked)));
haftmann@38578
   196
  in
haftmann@38578
   197
    thy
haftmann@38608
   198
    |> Theory.checkpoint
wenzelm@43231
   199
    |> Proof_Context.init_global
haftmann@38608
   200
    |> Data.put overloading
haftmann@38608
   201
    |> fold (fn ((_, ty), (v, _)) => Variable.declare_names (Free (v, ty))) overloading
haftmann@39624
   202
    |> activate_improvable_syntax
haftmann@38608
   203
    |> synchronize_syntax
wenzelm@47938
   204
    |> Local_Theory.init naming
haftmann@38608
   205
       {define = Generic_Target.define foundation,
wenzelm@48126
   206
        notes = Generic_Target.notes Generic_Target.theory_notes,
wenzelm@48170
   207
        abbrev = Generic_Target.abbrev Generic_Target.theory_abbrev,
wenzelm@48163
   208
        declaration = K Generic_Target.theory_declaration,
haftmann@38608
   209
        pretty = pretty,
haftmann@38578
   210
        exit = Local_Theory.target_of o conclude}
haftmann@38578
   211
  end;
haftmann@38578
   212
haftmann@38578
   213
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
haftmann@38578
   214
val overloading_cmd = gen_overloading Syntax.read_term;
haftmann@38578
   215
haftmann@25519
   216
end;