src/Pure/axclass.ML
author wenzelm
Wed May 13 12:17:49 1998 +0200 (1998-05-13)
changeset 4917 7c22890a7a9b
parent 4845 fdc7d8949d82
child 4934 683eae4b5d0f
permissions -rw-r--r--
tuned msg;
     1 (*  Title:      Pure/axclass.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 User interfaces for axiomatic type classes.
     6 *)
     7 
     8 signature AX_CLASS =
     9 sig
    10   val add_classrel_thms: thm list -> theory -> theory
    11   val add_arity_thms: thm list -> theory -> theory
    12   val add_axclass: bclass * xclass list -> (string * string) list
    13     -> theory -> theory
    14   val add_axclass_i: bclass * class list -> (string * term) list
    15     -> theory -> theory
    16   val add_inst_subclass: xclass * xclass -> string list -> thm list
    17     -> tactic option -> theory -> theory
    18   val add_inst_subclass_i: class * class -> string list -> thm list
    19     -> tactic option -> theory -> theory
    20   val add_inst_arity: xstring * xsort list * xclass list -> string list
    21     -> thm list -> tactic option -> theory -> theory
    22   val add_inst_arity_i: string * sort list * class list -> string list
    23     -> thm list -> tactic option -> theory -> theory
    24   val axclass_tac: theory -> thm list -> tactic
    25   val prove_subclass: theory -> class * class -> thm list
    26     -> tactic option -> thm
    27   val prove_arity: theory -> string * sort list * class -> thm list
    28     -> tactic option -> thm
    29   val goal_subclass: theory -> xclass * xclass -> thm list
    30   val goal_arity: theory -> xstring * xsort list * xclass -> thm list
    31 end;
    32 
    33 structure AxClass : AX_CLASS =
    34 struct
    35 
    36 
    37 (** utilities **)
    38 
    39 (* type vars *)
    40 
    41 fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
    42   | map_typ_frees f (TFree a) = f a
    43   | map_typ_frees _ a = a;
    44 
    45 val map_term_tfrees = map_term_types o map_typ_frees;
    46 
    47 fun aT S = TFree ("'a", S);
    48 
    49 fun dest_varT (TFree (x, S)) = ((x, ~1), S)
    50   | dest_varT (TVar xi_S) = xi_S
    51   | dest_varT T = raise TYPE ("dest_varT", [T], []);
    52 
    53 
    54 (* get axioms and theorems *)
    55 
    56 fun get_ax thy name =
    57   Some (get_axiom thy name) handle THEORY _ => None;
    58 
    59 val get_axioms = mapfilter o get_ax;
    60 
    61 val is_def = Logic.is_equals o #prop o rep_thm;
    62 
    63 fun witnesses thy axms thms =
    64   map (get_axiom thy) axms @ thms @ filter is_def (map snd (axioms_of thy));
    65 
    66 
    67 
    68 (** abstract syntax operations **)
    69 
    70 (* subclass relations as terms *)
    71 
    72 fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
    73 
    74 fun dest_classrel tm =
    75   let
    76     fun err () = raise TERM ("dest_classrel", [tm]);
    77 
    78     val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
    79     val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
    80       handle TYPE _ => err ();
    81   in
    82     (c1, c2)
    83   end;
    84 
    85 
    86 (* arities as terms *)
    87 
    88 fun mk_arity (t, ss, c) =
    89   let
    90     val names = tl (variantlist (replicate (length ss + 1) "'", []));
    91     val tfrees = ListPair.map TFree (names, ss);
    92   in
    93     Logic.mk_inclass (Type (t, tfrees), c)
    94   end;
    95 
    96 fun dest_arity tm =
    97   let
    98     fun err () = raise TERM ("dest_arity", [tm]);
    99 
   100     val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
   101     val (t, tvars) =
   102       (case ty of
   103         Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
   104       | _ => err ());
   105     val ss =
   106       if null (gen_duplicates eq_fst tvars)
   107       then map snd tvars else err ();
   108   in
   109     (t, ss, c)
   110   end;
   111 
   112 
   113 
   114 (** add theorems as axioms **)
   115 
   116 fun prep_thm_axm thy thm =
   117   let
   118     fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
   119 
   120     val {sign, hyps, prop, ...} = rep_thm thm;
   121   in
   122     if not (Sign.subsig (sign, sign_of thy)) then
   123       err "theorem not of same theory"
   124     else if not (null (extra_shyps thm)) orelse not (null hyps) then
   125       err "theorem may not contain hypotheses"
   126     else prop
   127   end;
   128 
   129 (*theorems expressing class relations*)
   130 fun add_classrel_thms thms thy =
   131   let
   132     fun prep_thm thm =
   133       let
   134         val prop = prep_thm_axm thy thm;
   135         val (c1, c2) = dest_classrel prop handle TERM _ =>
   136           raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
   137       in (c1, c2) end;
   138   in
   139     Theory.add_classrel (map prep_thm thms) thy
   140   end;
   141 
   142 (*theorems expressing arities*)
   143 fun add_arity_thms thms thy =
   144   let
   145     fun prep_thm thm =
   146       let
   147         val prop = prep_thm_axm thy thm;
   148         val (t, ss, c) = dest_arity prop handle TERM _ =>
   149           raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
   150       in (t, ss, [c]) end;
   151   in
   152     Theory.add_arities (map prep_thm thms) thy
   153   end;
   154 
   155 
   156 
   157 (** add axiomatic type classes **)
   158 
   159 (* errors *)
   160 
   161 fun err_not_logic c =
   162   error ("Axiomatic class " ^ quote c ^ " not subclass of " ^ quote logicC);
   163 
   164 fun err_bad_axsort ax c =
   165   error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
   166 
   167 fun err_bad_tfrees ax =
   168   error ("More than one type variable in axiom " ^ quote ax);
   169 
   170 
   171 (* ext_axclass *)
   172 
   173 fun ext_axclass int prep_axm (raw_class, raw_super_classes) raw_axioms old_thy =
   174   let
   175     val old_sign = sign_of old_thy;
   176     val axioms = map (prep_axm old_sign) raw_axioms;
   177     val class = Sign.full_name old_sign raw_class;
   178 
   179     val thy =
   180       (if int then Theory.add_classes else Theory.add_classes_i)
   181         [(raw_class, raw_super_classes)] old_thy;
   182     val sign = sign_of thy;
   183     val super_classes =
   184       if int then map (Sign.intern_class sign) raw_super_classes
   185       else raw_super_classes;
   186 
   187 
   188     (* prepare abstract axioms *)
   189 
   190     fun abs_axm ax =
   191       if null (term_tfrees ax) then
   192         Logic.mk_implies (Logic.mk_inclass (aT logicS, class), ax)
   193       else map_term_tfrees (K (aT [class])) ax;
   194 
   195     val abs_axioms = map (apsnd abs_axm) axioms;
   196 
   197 
   198     (* prepare introduction orule *)
   199 
   200     val _ =
   201       if Sign.subsort sign ([class], logicS) then ()
   202       else err_not_logic class;
   203 
   204     fun axm_sort (name, ax) =
   205       (case term_tfrees ax of
   206         [] => []
   207       | [(_, S)] =>
   208           if Sign.subsort sign ([class], S) then S
   209           else err_bad_axsort name class
   210       | _ => err_bad_tfrees name);
   211 
   212     val axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms))
   213 
   214     val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
   215     fun inclass c = Logic.mk_inclass (aT axS, c);
   216 
   217     val intro_axm = Logic.list_implies
   218       (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
   219   in
   220     thy
   221     |> PureThy.add_axioms_i (map Attribute.none ((raw_class ^ "I", intro_axm) :: abs_axioms))
   222   end;
   223 
   224 
   225 (* external interfaces *)
   226 
   227 val add_axclass = ext_axclass true read_axm;
   228 val add_axclass_i = ext_axclass false cert_axm;
   229 
   230 
   231 
   232 (** prove class relations and type arities **)
   233 
   234 (* class_axms *)
   235 
   236 fun class_axms thy =
   237   let
   238     val classes = Sign.classes (sign_of thy);
   239     val intros = map (fn c => c ^ "I") classes;
   240   in
   241     map (class_triv thy) classes @
   242     get_axioms thy intros
   243   end;
   244 
   245 
   246 (* axclass_tac *)
   247 
   248 (*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
   249       try class_trivs first, then "cI" axioms
   250   (2) rewrite goals using user supplied definitions
   251   (3) repeatedly resolve goals with user supplied non-definitions*)
   252 
   253 fun axclass_tac thy thms =
   254   let
   255     val defs = filter is_def thms;
   256     val non_defs = filter_out is_def thms;
   257   in
   258     TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
   259     TRY (rewrite_goals_tac defs) THEN
   260     TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
   261   end;
   262 
   263 
   264 (* provers *)
   265 
   266 fun prove term_of str_of thy sig_prop thms usr_tac =
   267   let
   268     val sign = sign_of thy;
   269     val goal = cterm_of sign (term_of sig_prop);
   270     val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
   271   in
   272     prove_goalw_cterm [] goal (K [tac])
   273   end
   274   handle ERROR => error ("The error(s) above occurred while trying to prove "
   275     ^ quote (str_of (sign_of thy, sig_prop)));
   276 
   277 val prove_subclass =
   278   prove mk_classrel (fn (sg, c1_c2) => Sign.str_of_classrel sg c1_c2);
   279 
   280 val prove_arity =
   281   prove mk_arity (fn (sg, (t, Ss, c)) => Sign.str_of_arity sg (t, Ss, [c]));
   282 
   283 
   284 
   285 (** add proved subclass relations and arities **)
   286 
   287 fun intrn_classrel sg c1_c2 =
   288   pairself (Sign.intern_class sg) c1_c2;
   289 
   290 fun ext_inst_subclass int raw_c1_c2 axms thms usr_tac thy =
   291   let
   292     val c1_c2 =
   293       if int then intrn_classrel (sign_of thy) raw_c1_c2
   294       else raw_c1_c2;
   295   in
   296     writeln ("Proving class inclusion " ^
   297       quote (Sign.str_of_classrel (sign_of thy) c1_c2) ^ " ...");
   298     add_classrel_thms
   299       [prove_subclass thy c1_c2 (witnesses thy axms thms) usr_tac] thy
   300   end;
   301 
   302 
   303 fun intrn_arity sg intrn (t, Ss, x) =
   304   (Sign.intern_tycon sg t, map (Sign.intern_sort sg) Ss, intrn sg x);
   305 
   306 fun ext_inst_arity int (raw_t, raw_Ss, raw_cs) axms thms usr_tac thy =
   307   let
   308     val sign = sign_of thy;
   309     val (t, Ss, cs) =
   310       if int then intrn_arity sign Sign.intern_sort (raw_t, raw_Ss, raw_cs)
   311       else (raw_t, raw_Ss, raw_cs);
   312     val wthms = witnesses thy axms thms;
   313     fun prove c =
   314      (writeln ("Proving type arity " ^
   315         quote (Sign.str_of_arity sign (t, Ss, [c])) ^ " ...");
   316         prove_arity thy (t, Ss, c) wthms usr_tac);
   317   in
   318     add_arity_thms (map prove cs) thy
   319   end;
   320 
   321 val add_inst_subclass = ext_inst_subclass true;
   322 val add_inst_subclass_i = ext_inst_subclass false;
   323 val add_inst_arity = ext_inst_arity true;
   324 val add_inst_arity_i = ext_inst_arity false;
   325 
   326 
   327 (* make goals (for interactive use) *)
   328 
   329 fun mk_goal term_of thy sig_prop =
   330   goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
   331 
   332 fun goal_subclass thy =
   333   mk_goal (mk_classrel o intrn_classrel (sign_of thy)) thy;
   334 
   335 fun goal_arity thy =
   336   mk_goal (mk_arity o intrn_arity (sign_of thy) Sign.intern_class) thy;
   337 
   338 
   339 end;