src/Pure/Isar/overloading.ML
author wenzelm
Sat Apr 16 15:25:25 2011 +0200 (2011-04-16)
changeset 42359 6ca5407863ed
parent 40782 aa533c5e3f48
child 42360 da8817d01e7c
permissions -rw-r--r--
prefer local name spaces;
tuned signatures;
tuned;
     1 (*  Title:      Pure/Isar/overloading.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Overloaded definitions without any discipline.
     5 *)
     6 
     7 signature OVERLOADING =
     8 sig
     9   type improvable_syntax
    10   val activate_improvable_syntax: Proof.context -> Proof.context
    11   val map_improvable_syntax: (improvable_syntax -> improvable_syntax)
    12     -> Proof.context -> Proof.context
    13   val set_primary_constraints: Proof.context -> Proof.context
    14 
    15   val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
    16   val overloading_cmd: (string * string * bool) list -> theory -> local_theory
    17 end;
    18 
    19 structure Overloading: OVERLOADING =
    20 struct
    21 
    22 (** generic check/uncheck combinators for improvable constants **)
    23 
    24 type improvable_syntax = ((((string * typ) list * (string * typ) list) *
    25   ((((string * typ -> (typ * typ) option) * (string * typ -> (typ * term) option)) * bool) *
    26     (term * term) list)) * bool);
    27 
    28 structure ImprovableSyntax = Proof_Data
    29 (
    30   type T = {
    31     primary_constraints: (string * typ) list,
    32     secondary_constraints: (string * typ) list,
    33     improve: string * typ -> (typ * typ) option,
    34     subst: string * typ -> (typ * term) option,
    35     consider_abbrevs: bool,
    36     unchecks: (term * term) list,
    37     passed: bool
    38   };
    39   fun init _ = {
    40     primary_constraints = [],
    41     secondary_constraints = [],
    42     improve = K NONE,
    43     subst = K NONE,
    44     consider_abbrevs = false,
    45     unchecks = [],
    46     passed = true
    47   };
    48 );
    49 
    50 fun map_improvable_syntax f = ImprovableSyntax.map (fn { primary_constraints,
    51     secondary_constraints, improve, subst, consider_abbrevs, unchecks, passed } =>
    52   let
    53     val (((primary_constraints', secondary_constraints'),
    54       (((improve', subst'), consider_abbrevs'), unchecks')), passed')
    55         = f (((primary_constraints, secondary_constraints),
    56             (((improve, subst), consider_abbrevs), unchecks)), passed)
    57   in { primary_constraints = primary_constraints', secondary_constraints = secondary_constraints',
    58     improve = improve', subst = subst', consider_abbrevs = consider_abbrevs',
    59     unchecks = unchecks', passed = passed' }
    60   end);
    61 
    62 val mark_passed = (map_improvable_syntax o apsnd) (K true);
    63 
    64 fun improve_term_check ts ctxt =
    65   let
    66     val { secondary_constraints, improve, subst, consider_abbrevs, passed, ... } =
    67       ImprovableSyntax.get ctxt;
    68     val tsig = (Sign.tsig_of o ProofContext.theory_of) ctxt;
    69     val is_abbrev = consider_abbrevs andalso ProofContext.abbrev_mode ctxt;
    70     val passed_or_abbrev = passed orelse is_abbrev;
    71     fun accumulate_improvements (Const (c, ty)) = (case improve (c, ty)
    72          of SOME ty_ty' => Type.typ_match tsig ty_ty'
    73           | _ => I)
    74       | accumulate_improvements _ = I;
    75     val improvements = (fold o fold_aterms) accumulate_improvements ts Vartab.empty;
    76     val ts' = (map o map_types) (Envir.subst_type improvements) ts;
    77     fun apply_subst t = Envir.expand_term (fn Const (c, ty) => (case subst (c, ty)
    78          of SOME (ty', t') =>
    79               if Type.typ_instance tsig (ty, ty')
    80               then SOME (ty', apply_subst t') else NONE
    81           | NONE => NONE)
    82         | _ => NONE) t;
    83     val ts'' = if is_abbrev then ts' else map apply_subst ts';
    84   in
    85     if eq_list (op aconv) (ts, ts'') andalso passed_or_abbrev then NONE else
    86     if passed_or_abbrev then SOME (ts'', ctxt)
    87     else SOME (ts'', ctxt
    88       |> fold (ProofContext.add_const_constraint o apsnd SOME) secondary_constraints
    89       |> mark_passed)
    90   end;
    91 
    92 fun rewrite_liberal thy unchecks t =
    93   case try (Pattern.rewrite_term thy unchecks []) t
    94    of NONE => NONE
    95     | SOME t' => if t aconv t' then NONE else SOME t';
    96 
    97 fun improve_term_uncheck ts ctxt =
    98   let
    99     val thy = ProofContext.theory_of ctxt;
   100     val unchecks = (#unchecks o ImprovableSyntax.get) ctxt;
   101     val ts' = map (rewrite_liberal thy unchecks) ts;
   102   in if exists is_some ts' then SOME (map2 the_default ts ts', ctxt) else NONE end;
   103 
   104 fun set_primary_constraints ctxt =
   105   let
   106     val { primary_constraints, ... } = ImprovableSyntax.get ctxt;
   107   in fold (ProofContext.add_const_constraint o apsnd SOME) primary_constraints ctxt end;
   108 
   109 val activate_improvable_syntax =
   110   Context.proof_map
   111     (Syntax.add_term_check 0 "improvement" improve_term_check
   112     #> Syntax.add_term_uncheck 0 "improvement" improve_term_uncheck)
   113   #> set_primary_constraints;
   114 
   115 
   116 (** overloading target **)
   117 
   118 structure Data = Proof_Data
   119 (
   120   type T = ((string * typ) * (string * bool)) list;
   121   fun init _ = [];
   122 );
   123 
   124 val get_overloading = Data.get o Local_Theory.target_of;
   125 val map_overloading = Local_Theory.target o Data.map;
   126 
   127 fun operation lthy b = get_overloading lthy
   128   |> get_first (fn ((c, _), (v, checked)) =>
   129       if Binding.name_of b = v then SOME (c, (v, checked)) else NONE);
   130 
   131 fun synchronize_syntax ctxt =
   132   let
   133     val overloading = Data.get ctxt;
   134     fun subst (c, ty) = case AList.lookup (op =) overloading (c, ty)
   135      of SOME (v, _) => SOME (ty, Free (v, ty))
   136       | NONE => NONE;
   137     val unchecks =
   138       map (fn (c_ty as (_, ty), (v, _)) => (Free (v, ty), Const c_ty)) overloading;
   139   in 
   140     ctxt
   141     |> map_improvable_syntax (K ((([], []), (((K NONE, subst), false), unchecks)), false))
   142   end
   143 
   144 fun define_overloaded (c, U) (v, checked) (b_def, rhs) =
   145   Local_Theory.background_theory_result
   146     (Thm.add_def (not checked) true (b_def, Logic.mk_equals (Const (c, Term.fastype_of rhs), rhs)))
   147   ##> map_overloading (filter_out (fn (_, (v', _)) => v' = v))
   148   ##> Local_Theory.target synchronize_syntax
   149   #-> (fn (_, def) => pair (Const (c, U), def))
   150 
   151 fun foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   152   case operation lthy b
   153    of SOME (c, (v, checked)) => if mx <> NoSyn
   154        then error ("Illegal mixfix syntax for overloaded constant " ^ quote c)
   155         else lthy |> define_overloaded (c, U) (v, checked) (b_def, rhs)
   156     | NONE => lthy |>
   157         Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params);
   158 
   159 fun pretty lthy =
   160   let
   161     val overloading = get_overloading lthy;
   162     fun pr_operation ((c, ty), (v, _)) =
   163       Pretty.block (Pretty.breaks
   164         [Pretty.str v, Pretty.str "==", Pretty.str (ProofContext.extern_const lthy c),
   165           Pretty.str "::", Syntax.pretty_typ lthy ty]);
   166   in Pretty.str "overloading" :: map pr_operation overloading end;
   167 
   168 fun conclude lthy =
   169   let
   170     val overloading = get_overloading lthy;
   171     val _ =
   172       if null overloading then ()
   173       else
   174         error ("Missing definition(s) for parameter(s) " ^ commas (map (quote
   175           o Syntax.string_of_term lthy o Const o fst) overloading));
   176   in lthy end;
   177 
   178 fun gen_overloading prep_const raw_overloading thy =
   179   let
   180     val ctxt = ProofContext.init_global thy;
   181     val _ = if null raw_overloading then error "At least one parameter must be given" else ();
   182     val overloading = raw_overloading |> map (fn (v, const, checked) =>
   183       (Term.dest_Const (prep_const ctxt const), (v, checked)));
   184   in
   185     thy
   186     |> Theory.checkpoint
   187     |> ProofContext.init_global
   188     |> Data.put overloading
   189     |> fold (fn ((_, ty), (v, _)) => Variable.declare_names (Free (v, ty))) overloading
   190     |> activate_improvable_syntax
   191     |> synchronize_syntax
   192     |> Local_Theory.init NONE ""
   193        {define = Generic_Target.define foundation,
   194         notes = Generic_Target.notes
   195           (fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts),
   196         abbrev = Generic_Target.abbrev
   197           (fn prmode => fn (b, mx) => fn (t, _) => fn _ =>
   198             Generic_Target.theory_abbrev prmode ((b, mx), t)),
   199         declaration = K Generic_Target.theory_declaration,
   200         syntax_declaration = K Generic_Target.theory_declaration,
   201         pretty = pretty,
   202         exit = Local_Theory.target_of o conclude}
   203   end;
   204 
   205 val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
   206 val overloading_cmd = gen_overloading Syntax.read_term;
   207 
   208 end;