src/Pure/Isar/overloading.ML
author haftmann
Mon Mar 10 21:51:47 2008 +0100 (2008-03-10)
changeset 26249 59ecf1ce8222
parent 26238 c30bb8182da2
child 26259 d30f4a509361
permissions -rw-r--r--
tuned
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@25519
    22
end;
haftmann@25519
    23
haftmann@25519
    24
structure Overloading: OVERLOADING =
haftmann@25519
    25
struct
haftmann@25519
    26
haftmann@25519
    27
(* bookkeeping *)
haftmann@25519
    28
haftmann@25519
    29
structure OverloadingData = ProofDataFun
haftmann@25519
    30
(
haftmann@25519
    31
  type T = ((string * typ) * (string * bool)) list;
haftmann@25519
    32
  fun init _ = [];
haftmann@25519
    33
);
haftmann@25519
    34
haftmann@25519
    35
val get_overloading = OverloadingData.get o LocalTheory.target_of;
haftmann@25519
    36
val map_overloading = LocalTheory.target o OverloadingData.map;
haftmann@25519
    37
haftmann@25519
    38
fun operation lthy v = get_overloading lthy
haftmann@25519
    39
  |> get_first (fn ((c, _), (v', checked)) => if v = v' then SOME (c, checked) else NONE);
haftmann@25519
    40
haftmann@25519
    41
fun confirm c = map_overloading (filter_out (fn (_, (c', _)) => c' = c));
haftmann@25519
    42
haftmann@25519
    43
haftmann@25519
    44
(* overloaded declarations and definitions *)
haftmann@25519
    45
haftmann@25519
    46
fun declare c_ty = pair (Const c_ty);
haftmann@25519
    47
haftmann@25519
    48
fun define checked name (c, t) =
haftmann@25519
    49
  Thm.add_def (not checked) true (name, Logic.mk_equals (Const (c, Term.fastype_of t), t));
haftmann@25519
    50
haftmann@25519
    51
haftmann@26238
    52
(* generic check/uncheck combinators for improvable constants *)
haftmann@26238
    53
haftmann@26249
    54
type improvable_syntax = ((((string * typ) list * (string * typ) list) *
haftmann@26249
    55
  (((string * typ -> (typ * typ) option) * (string * typ -> (typ * term) option)) *
haftmann@26249
    56
    (term * term) list)) * bool);
haftmann@25519
    57
haftmann@26238
    58
structure ImprovableSyntax = ProofDataFun(
haftmann@26249
    59
  type T = {
haftmann@26249
    60
    local_constraints: (string * typ) list,
haftmann@26249
    61
    global_constraints: (string * typ) list,
haftmann@26249
    62
    improve: string * typ -> (typ * typ) option,
haftmann@26249
    63
    subst: string * typ -> (typ * term) option,
haftmann@26249
    64
    unchecks: (term * term) list,
haftmann@26249
    65
    passed: bool
haftmann@26249
    66
  };
haftmann@26238
    67
  fun init _ = {
haftmann@26238
    68
    local_constraints = [],
haftmann@26238
    69
    global_constraints = [],
haftmann@26238
    70
    improve = K NONE,
haftmann@26238
    71
    subst = K NONE,
haftmann@26238
    72
    unchecks = [],
haftmann@26238
    73
    passed = true
haftmann@26238
    74
  };
haftmann@26238
    75
);
haftmann@25536
    76
haftmann@26249
    77
fun map_improvable_syntax f = ImprovableSyntax.map (fn { local_constraints,
haftmann@26249
    78
  global_constraints, improve, subst, unchecks, passed } => let
haftmann@26249
    79
    val (((local_constraints', global_constraints'), ((improve', subst'), unchecks')), passed')
haftmann@26249
    80
      = f (((local_constraints, global_constraints), ((improve, subst), unchecks)), passed)
haftmann@26249
    81
  in { local_constraints = local_constraints', global_constraints = global_constraints',
haftmann@26249
    82
    improve = improve', subst = subst', unchecks = unchecks', passed = passed'
haftmann@26249
    83
  } end);
haftmann@26238
    84
haftmann@26249
    85
val mark_passed = (map_improvable_syntax o apsnd) (K true);
haftmann@26238
    86
haftmann@26238
    87
fun improve_term_check ts ctxt =
haftmann@25519
    88
  let
haftmann@26238
    89
    val { local_constraints, global_constraints, improve, subst, passed, ... } =
haftmann@26238
    90
      ImprovableSyntax.get ctxt;
haftmann@26238
    91
    val tsig = (Sign.tsig_of o ProofContext.theory_of) ctxt;
haftmann@26238
    92
    fun accumulate_improvements (Const (c, ty)) = (case improve (c, ty)
haftmann@26238
    93
         of SOME ty_ty' => (perhaps o try o Type.typ_match tsig) ty_ty'
haftmann@26238
    94
          | _ => I)
haftmann@26238
    95
      | accumulate_improvements _ = I;
haftmann@26238
    96
    val improvements = (fold o fold_aterms) accumulate_improvements ts Vartab.empty;
haftmann@26238
    97
    val ts' = (map o map_types) (Envir.typ_subst_TVars improvements) ts;
haftmann@26238
    98
    fun apply_subst t = Envir.expand_term (fn Const (c, ty) => (case subst (c, ty)
haftmann@26238
    99
         of SOME (ty', t') =>   
haftmann@26238
   100
              if Type.typ_instance tsig (ty, ty')
haftmann@26238
   101
              then SOME (ty', apply_subst t') else NONE
haftmann@26238
   102
          | NONE => NONE)
haftmann@26238
   103
      | _ => NONE) t;
haftmann@26238
   104
    val ts'' = map apply_subst ts';
haftmann@26238
   105
  in if eq_list (op aconv) (ts, ts'') andalso passed then NONE else
haftmann@26238
   106
    if passed then SOME (ts'', ctxt)
haftmann@26238
   107
    else SOME (ts'', ctxt
haftmann@26238
   108
      |> fold (ProofContext.add_const_constraint o apsnd SOME) global_constraints
haftmann@26238
   109
      |> mark_passed)
haftmann@26238
   110
  end;
haftmann@25519
   111
haftmann@26238
   112
fun improve_term_uncheck ts ctxt =
haftmann@25519
   113
  let
haftmann@26238
   114
    val thy = ProofContext.theory_of ctxt;
haftmann@26238
   115
    val unchecks = (#unchecks o ImprovableSyntax.get) ctxt;
haftmann@26238
   116
    val ts' = map (Pattern.rewrite_term thy unchecks []) ts;
haftmann@26238
   117
  in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end;
haftmann@26238
   118
haftmann@26238
   119
fun add_improvable_syntax ctxt = ctxt
haftmann@26238
   120
  |> Context.proof_map
haftmann@26238
   121
    (Syntax.add_term_check 0 "improvement" improve_term_check
haftmann@26238
   122
    #> Syntax.add_term_uncheck 0 "improvement" improve_term_uncheck)
haftmann@26238
   123
  |> fold (ProofContext.add_const_constraint o apsnd SOME)
haftmann@26238
   124
       ((#local_constraints o ImprovableSyntax.get) ctxt);
haftmann@25519
   125
haftmann@25519
   126
haftmann@25519
   127
(* target *)
haftmann@25519
   128
haftmann@26238
   129
fun init raw_overloading thy =
haftmann@25519
   130
  let
haftmann@26238
   131
    val _ = if null raw_overloading then error "At least one parameter must be given" else ();
haftmann@26238
   132
    val overloading = map (fn (v, c_ty, checked) => (c_ty, (v, checked))) raw_overloading;
haftmann@26238
   133
    fun subst (c, ty) = case AList.lookup (op =) overloading (c, ty)
haftmann@26238
   134
     of SOME (v, _) => SOME (ty, Free (v, ty))
haftmann@26238
   135
      | NONE => NONE;
haftmann@26238
   136
    val unchecks =
haftmann@26238
   137
      map (fn (c_ty as (_, ty), (v, _)) => (Free (v, ty), Const c_ty)) overloading;
haftmann@25519
   138
  in
haftmann@25519
   139
    thy
haftmann@25519
   140
    |> ProofContext.init
haftmann@26238
   141
    |> OverloadingData.put overloading
haftmann@26238
   142
    |> fold (fn (v, (_, ty), _) => Variable.declare_term (Free (v, ty))) raw_overloading
haftmann@26249
   143
    |> map_improvable_syntax (K ((([], []), ((K NONE, subst), unchecks)), false))
haftmann@26238
   144
    |> add_improvable_syntax
haftmann@25519
   145
  end;
haftmann@25519
   146
haftmann@25519
   147
fun conclude lthy =
haftmann@25519
   148
  let
haftmann@25519
   149
    val overloading = get_overloading lthy;
haftmann@25519
   150
    val _ = if null overloading then () else
haftmann@25519
   151
      error ("Missing definition(s) for parameters " ^ commas (map (quote
haftmann@25519
   152
        o Syntax.string_of_term lthy o Const o fst) overloading));
haftmann@25519
   153
  in
haftmann@25519
   154
    lthy
haftmann@25519
   155
  end;
haftmann@25519
   156
haftmann@25606
   157
fun pretty lthy =
haftmann@25606
   158
  let
haftmann@25606
   159
    val thy = ProofContext.theory_of lthy;
haftmann@25606
   160
    val overloading = get_overloading lthy;
haftmann@25606
   161
    fun pr_operation ((c, ty), (v, _)) =
haftmann@25861
   162
      (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
haftmann@25861
   163
        Pretty.str (Sign.extern_const thy c), Pretty.str "::", Sign.pretty_typ thy ty];
haftmann@25606
   164
  in
haftmann@25606
   165
    (Pretty.block o Pretty.fbreaks)
haftmann@25606
   166
      (Pretty.str "overloading" :: map pr_operation overloading)
haftmann@25606
   167
  end;
haftmann@25606
   168
haftmann@25519
   169
end;