src/Pure/axclass.ML
author paulson
Fri May 30 15:14:59 1997 +0200 (1997-05-30)
changeset 3365 86c0d1988622
parent 2961 842be30dc336
child 3395 d8700b008944
permissions -rw-r--r--
flushOut ensures that no recent error message are lost (not certain this is
necessary)
     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_thms_as_axms: (string * thm) list -> theory -> theory
    11   val add_classrel_thms: thm list -> theory -> theory
    12   val add_arity_thms: thm list -> theory -> theory
    13   val add_axclass: class * class list -> (string * string) list
    14     -> theory -> theory
    15   val add_axclass_i: class * class list -> (string * term) list
    16     -> theory -> theory
    17   val add_inst_subclass: class * class -> string list -> thm list
    18     -> tactic option -> theory -> theory
    19   val add_inst_arity: string * sort list * class list -> string list
    20     -> thm list -> tactic option -> theory -> theory
    21   val axclass_tac: theory -> thm list -> tactic
    22   val prove_subclass: theory -> class * class -> thm list
    23     -> tactic option -> thm
    24   val prove_arity: theory -> string * sort list * class -> thm list
    25     -> tactic option -> thm
    26   val goal_subclass: theory -> class * class -> thm list
    27   val goal_arity: theory -> string * sort list * class -> thm list
    28   end;
    29 
    30 structure AxClass : AX_CLASS =
    31 struct
    32 
    33 (** utilities **)
    34 
    35 (* type vars *)
    36 
    37 fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
    38   | map_typ_frees f (TFree a) = f a
    39   | map_typ_frees _ a = a;
    40 
    41 val map_term_tfrees = map_term_types o map_typ_frees;
    42 
    43 fun aT S = TFree ("'a", S);
    44 
    45 
    46 (* get axioms and theorems *)
    47 
    48 fun get_ax thy name =
    49   Some (get_axiom thy name) handle THEORY _ => None;
    50 
    51 val get_axioms = mapfilter o get_ax;
    52 
    53 val is_def = Logic.is_equals o #prop o rep_thm;
    54 
    55 fun witnesses thy axms thms =
    56   map (get_axiom thy) axms @ thms @ filter is_def (map snd (axioms_of thy));
    57 
    58 
    59 
    60 (** abstract syntax operations **)
    61 
    62 (* subclass relations as terms *)
    63 
    64 fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
    65 
    66 fun dest_classrel tm =
    67   let
    68     fun err () = raise_term "dest_classrel" [tm];
    69 
    70     val (ty, c2) = Logic.dest_inclass (Logic.freeze_vars tm)
    71 	           handle TERM _ => err ();
    72     val c1 = (case ty of TFree (_, [c]) => c | _ => err ());
    73   in
    74     (c1, c2)
    75   end;
    76 
    77 
    78 (* arities as terms *)
    79 
    80 fun mk_arity (t, ss, c) =
    81   let
    82     val names = tl (variantlist (replicate (length ss + 1) "'", []));
    83     val tfrees = ListPair.map TFree (names, ss);
    84   in
    85     Logic.mk_inclass (Type (t, tfrees), c)
    86   end;
    87 
    88 fun dest_arity tm =
    89   let
    90     fun err () = raise_term "dest_arity" [tm];
    91 
    92     val (ty, c) = Logic.dest_inclass (Logic.freeze_vars tm) 
    93 	          handle TERM _ => err ();
    94     val (t, tfrees) =
    95       (case ty of
    96         Type (t, tys) => (t, map (fn TFree x => x | _ => err ()) tys)
    97       | _ => err ());
    98     val ss =
    99       if null (gen_duplicates eq_fst tfrees)
   100       then map snd tfrees else err ();
   101   in
   102     (t, ss, c)
   103   end;
   104 
   105 
   106 
   107 (** add theorems as axioms **)
   108 
   109 fun prep_thm_axm thy thm =
   110   let
   111     fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
   112 
   113     val {sign, hyps, prop, ...} = rep_thm thm;
   114   in
   115     if not (Sign.subsig (sign, sign_of thy)) then
   116       err "theorem not of same theory"
   117     else if not (null (extra_shyps thm)) orelse not (null hyps) then
   118       err "theorem may not contain hypotheses"
   119     else prop
   120   end;
   121 
   122 (*general theorems*)
   123 fun add_thms_as_axms thms thy =
   124   add_axioms_i (map (apsnd (prep_thm_axm thy)) thms) thy;
   125 
   126 (*theorems expressing class relations*)
   127 fun add_classrel_thms thms thy =
   128   let
   129     fun prep_thm thm =
   130       let
   131         val prop = prep_thm_axm thy thm;
   132         val (c1, c2) = dest_classrel prop handle TERM _ =>
   133           raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
   134       in (c1, c2) end;
   135   in
   136     add_classrel (map prep_thm thms) thy
   137   end;
   138 
   139 (*theorems expressing arities*)
   140 fun add_arity_thms thms thy =
   141   let
   142     fun prep_thm thm =
   143       let
   144         val prop = prep_thm_axm thy thm;
   145         val (t, ss, c) = dest_arity prop handle TERM _ =>
   146           raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
   147       in (t, ss, [c]) end;
   148   in
   149     add_arities (map prep_thm thms) thy
   150   end;
   151 
   152 
   153 
   154 (** add axiomatic type classes **)
   155 
   156 (* errors *)
   157 
   158 fun err_not_logic c =
   159   error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
   160 
   161 fun err_bad_axsort ax c =
   162   error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
   163 
   164 fun err_bad_tfrees ax =
   165   error ("More than one type variable in axiom " ^ quote ax);
   166 
   167 
   168 (* ext_axclass *)
   169 
   170 fun ext_axclass prep_axm (class, super_classes) raw_axioms old_thy =
   171   let
   172     val axioms = map (prep_axm (sign_of old_thy)) raw_axioms;
   173     val thy = add_classes [(class, super_classes)] old_thy;
   174     val sign = sign_of thy;
   175 
   176 
   177     (* prepare abstract axioms *)
   178 
   179     fun abs_axm ax =
   180       if null (term_tfrees ax) then
   181         Logic.mk_implies (Logic.mk_inclass (aT logicS, class), ax)
   182       else
   183         map_term_tfrees (K (aT [class])) ax;
   184 
   185     val abs_axioms = map (apsnd abs_axm) axioms;
   186 
   187 
   188     (* prepare introduction orule *)
   189 
   190     val _ =
   191       if Sign.subsort sign ([class], logicS) then ()
   192       else err_not_logic class;
   193 
   194     fun axm_sort (name, ax) =
   195       (case term_tfrees ax of
   196         [] => []
   197       | [(_, S)] =>
   198           if Sign.subsort sign ([class], S) then S
   199           else err_bad_axsort name class
   200       | _ => err_bad_tfrees name);
   201 
   202     val axS = Sign.norm_sort sign (logicC :: List.concat(map axm_sort axioms))
   203 
   204     val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
   205     fun inclass c = Logic.mk_inclass (aT axS, c);
   206 
   207     val intro_axm = Logic.list_implies
   208       (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
   209   in
   210     add_axioms_i ((class ^ "I", intro_axm) :: abs_axioms) thy
   211   end;
   212 
   213 
   214 (* external interfaces *)
   215 
   216 val add_axclass = ext_axclass read_axm;
   217 val add_axclass_i = ext_axclass cert_axm;
   218 
   219 
   220 
   221 (** prove class relations and type arities **)
   222 
   223 (* class_axms *)
   224 
   225 fun class_axms thy =
   226   let
   227     val classes = Sign.classes (sign_of thy);
   228     val intros = map (fn c => c ^ "I") classes;
   229   in
   230     map (class_triv thy) classes @
   231     get_axioms thy intros
   232   end;
   233 
   234 
   235 (* axclass_tac *)
   236 
   237 (*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
   238       try class_trivs first, then "cI" axioms
   239   (2) rewrite goals using user supplied definitions
   240   (3) repeatedly resolve goals with user supplied non-definitions*)
   241 
   242 fun axclass_tac thy thms =
   243   let
   244     val defs = filter is_def thms;
   245     val non_defs = filter_out is_def thms;
   246   in
   247     TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
   248     TRY (rewrite_goals_tac defs) THEN
   249     TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
   250   end;
   251 
   252 
   253 (* provers *)
   254 
   255 fun prove term_of str_of thy sig_prop thms usr_tac =
   256   let
   257     val sign = sign_of thy;
   258     val goal = cterm_of sign (term_of sig_prop);
   259     val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
   260   in
   261     prove_goalw_cterm [] goal (K [tac])
   262   end
   263   handle ERROR => error ("The error(s) above occurred while trying to prove "
   264     ^ quote (str_of sig_prop));
   265 
   266 val prove_subclass =
   267   prove mk_classrel (fn (c1, c2) => c1 ^ " < " ^ c2);
   268 
   269 val prove_arity =
   270   prove mk_arity (fn (t, ss, c) => Sorts.str_of_arity (t, ss, [c]));
   271 
   272 
   273 (* make goals (for interactive use) *)
   274 
   275 fun mk_goal term_of thy sig_prop =
   276   goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
   277 
   278 val goal_subclass = mk_goal mk_classrel;
   279 val goal_arity = mk_goal mk_arity;
   280 
   281 
   282 
   283 (** add proved subclass relations and arities **)
   284 
   285 fun add_inst_subclass (c1, c2) axms thms usr_tac thy =
   286   add_classrel_thms
   287   [prove_subclass thy (c1, c2) (witnesses thy axms thms) usr_tac] thy;
   288 
   289 fun add_inst_arity (t, ss, cs) axms thms usr_tac thy =
   290   let
   291     val wthms = witnesses thy axms thms;
   292     fun prove c =
   293       prove_arity thy (t, ss, c) wthms usr_tac;
   294   in
   295     add_arity_thms (map prove cs) thy
   296   end;
   297 
   298 
   299 end;