src/Pure/Isar/overloading.ML
author haftmann
Tue Apr 22 08:33:12 2008 +0200 (2008-04-22)
changeset 26730 bbb5e6904d78
parent 26597 ff250dde68d6
child 26939 1035c89b4c02
permissions -rw-r--r--
proper abbreviations in class
haftmann@25519
     1
(*  Title:      Pure/Isar/overloading.ML
haftmann@25519
     2
    ID:         $Id$
haftmann@25519
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@25519
     4
haftmann@25519
     5
Overloaded definitions without any discipline.
haftmann@25519
     6
*)
haftmann@25519
     7
haftmann@25519
     8
signature OVERLOADING =
haftmann@25519
     9
sig
haftmann@25861
    10
  val init: (string * (string * typ) * bool) list -> theory -> local_theory
haftmann@25519
    11
  val conclude: local_theory -> local_theory
haftmann@25519
    12
  val declare: string * typ -> theory -> term * theory
haftmann@25519
    13
  val confirm: string -> local_theory -> local_theory
haftmann@25519
    14
  val define: bool -> string -> string * term -> theory -> thm * theory
haftmann@25519
    15
  val operation: Proof.context -> string -> (string * bool) option
haftmann@25606
    16
  val pretty: Proof.context -> Pretty.T
haftmann@26238
    17
  
haftmann@26238
    18
  type improvable_syntax
haftmann@26238
    19
  val add_improvable_syntax: Proof.context -> Proof.context
haftmann@26238
    20
  val map_improvable_syntax: (improvable_syntax -> improvable_syntax)
haftmann@26238
    21
    -> Proof.context -> Proof.context
haftmann@26520
    22
  val set_primary_constraints: Proof.context -> Proof.context
haftmann@25519
    23
end;
haftmann@25519
    24
haftmann@25519
    25
structure Overloading: OVERLOADING =
haftmann@25519
    26
struct
haftmann@25519
    27
haftmann@26259
    28
(** generic check/uncheck combinators for improvable constants **)
haftmann@26238
    29
haftmann@26249
    30
type improvable_syntax = ((((string * typ) list * (string * typ) list) *
haftmann@26730
    31
  ((((string * typ -> (typ * typ) option) * (string * typ -> (typ * term) option)) * bool) *
haftmann@26249
    32
    (term * term) list)) * bool);
haftmann@25519
    33
haftmann@26238
    34
structure ImprovableSyntax = ProofDataFun(
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
haftmann@26730
    70
    val { primary_constraints, secondary_constraints, improve, subst,
haftmann@26730
    71
      consider_abbrevs, passed, ... } = ImprovableSyntax.get ctxt;
haftmann@26238
    72
    val tsig = (Sign.tsig_of o ProofContext.theory_of) ctxt;
haftmann@26730
    73
    val is_abbrev = consider_abbrevs andalso ProofContext.is_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;
haftmann@26238
    80
    val ts' = (map o map_types) (Envir.typ_subst_TVars 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@26238
    95
fun improve_term_uncheck ts ctxt =
haftmann@25519
    96
  let
haftmann@26238
    97
    val thy = ProofContext.theory_of ctxt;
haftmann@26238
    98
    val unchecks = (#unchecks o ImprovableSyntax.get) ctxt;
haftmann@26238
    99
    val ts' = map (Pattern.rewrite_term thy unchecks []) ts;
haftmann@26238
   100
  in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end;
haftmann@26238
   101
haftmann@26520
   102
fun set_primary_constraints ctxt =
haftmann@26259
   103
  let
haftmann@26520
   104
    val { primary_constraints, ... } = ImprovableSyntax.get ctxt;
haftmann@26520
   105
  in fold (ProofContext.add_const_constraint o apsnd SOME) primary_constraints ctxt end;
haftmann@26259
   106
haftmann@26259
   107
val add_improvable_syntax =
haftmann@26259
   108
  Context.proof_map
haftmann@26238
   109
    (Syntax.add_term_check 0 "improvement" improve_term_check
haftmann@26238
   110
    #> Syntax.add_term_uncheck 0 "improvement" improve_term_uncheck)
haftmann@26520
   111
  #> set_primary_constraints;
haftmann@26259
   112
haftmann@26259
   113
haftmann@26259
   114
(** overloading target **)
haftmann@26259
   115
haftmann@26259
   116
(* bookkeeping *)
haftmann@26259
   117
haftmann@26259
   118
structure OverloadingData = ProofDataFun
haftmann@26259
   119
(
haftmann@26259
   120
  type T = ((string * typ) * (string * bool)) list;
haftmann@26259
   121
  fun init _ = [];
haftmann@26259
   122
);
haftmann@26259
   123
haftmann@26259
   124
val get_overloading = OverloadingData.get o LocalTheory.target_of;
haftmann@26259
   125
val map_overloading = LocalTheory.target o OverloadingData.map;
haftmann@26259
   126
haftmann@26259
   127
fun operation lthy v = get_overloading lthy
haftmann@26259
   128
  |> get_first (fn ((c, _), (v', checked)) => if v = v' then SOME (c, checked) else NONE);
haftmann@26259
   129
haftmann@26259
   130
fun confirm c = map_overloading (filter_out (fn (_, (c', _)) => c' = c));
haftmann@26259
   131
haftmann@26259
   132
haftmann@26259
   133
(* overloaded declarations and definitions *)
haftmann@26259
   134
haftmann@26259
   135
fun declare c_ty = pair (Const c_ty);
haftmann@26259
   136
haftmann@26259
   137
fun define checked name (c, t) =
haftmann@26259
   138
  Thm.add_def (not checked) true (name, Logic.mk_equals (Const (c, Term.fastype_of t), t));
haftmann@25519
   139
haftmann@25519
   140
haftmann@25519
   141
(* target *)
haftmann@25519
   142
haftmann@26238
   143
fun init raw_overloading thy =
haftmann@25519
   144
  let
haftmann@26238
   145
    val _ = if null raw_overloading then error "At least one parameter must be given" else ();
haftmann@26238
   146
    val overloading = map (fn (v, c_ty, checked) => (c_ty, (v, checked))) raw_overloading;
haftmann@26238
   147
    fun subst (c, ty) = case AList.lookup (op =) overloading (c, ty)
haftmann@26238
   148
     of SOME (v, _) => SOME (ty, Free (v, ty))
haftmann@26238
   149
      | NONE => NONE;
haftmann@26238
   150
    val unchecks =
haftmann@26238
   151
      map (fn (c_ty as (_, ty), (v, _)) => (Free (v, ty), Const c_ty)) overloading;
haftmann@25519
   152
  in
haftmann@25519
   153
    thy
haftmann@25519
   154
    |> ProofContext.init
haftmann@26238
   155
    |> OverloadingData.put overloading
haftmann@26259
   156
    |> fold (fn ((_, ty), (v, _)) => Variable.declare_names (Free (v, ty))) overloading
haftmann@26730
   157
    |> map_improvable_syntax (K ((([], []), (((K NONE, subst), false), unchecks)), false))
haftmann@26238
   158
    |> add_improvable_syntax
haftmann@25519
   159
  end;
haftmann@25519
   160
haftmann@25519
   161
fun conclude lthy =
haftmann@25519
   162
  let
haftmann@25519
   163
    val overloading = get_overloading lthy;
haftmann@25519
   164
    val _ = if null overloading then () else
haftmann@26259
   165
      error ("Missing definition(s) for parameter(s) " ^ commas (map (quote
haftmann@25519
   166
        o Syntax.string_of_term lthy o Const o fst) overloading));
haftmann@25519
   167
  in
haftmann@25519
   168
    lthy
haftmann@25519
   169
  end;
haftmann@25519
   170
haftmann@25606
   171
fun pretty lthy =
haftmann@25606
   172
  let
haftmann@25606
   173
    val thy = ProofContext.theory_of lthy;
haftmann@25606
   174
    val overloading = get_overloading lthy;
haftmann@25606
   175
    fun pr_operation ((c, ty), (v, _)) =
haftmann@25861
   176
      (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
haftmann@25861
   177
        Pretty.str (Sign.extern_const thy c), Pretty.str "::", Sign.pretty_typ thy ty];
haftmann@25606
   178
  in
haftmann@25606
   179
    (Pretty.block o Pretty.fbreaks)
haftmann@25606
   180
      (Pretty.str "overloading" :: map pr_operation overloading)
haftmann@25606
   181
  end;
haftmann@25606
   182
haftmann@25519
   183
end;