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