src/Pure/Isar/element.ML
author wenzelm
Fri Nov 24 22:05:17 2006 +0100 (2006-11-24)
changeset 21521 095f4963beed
parent 21497 4d330a487586
child 21528 84e98b5f5af0
permissions -rw-r--r--
simultaneous fact morphism;
     1 (*  Title:      Pure/Isar/element.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Explicit data structures for some Isar language elements, with derived
     6 logical operations.
     7 *)
     8 
     9 signature ELEMENT =
    10 sig
    11   datatype ('typ, 'term) stmt =
    12     Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
    13     Obtains of (string * ((string * 'typ option) list * 'term list)) list
    14   type statement  (*= (string, string) stmt*)
    15   type statement_i  (*= (typ, term) stmt*)
    16   datatype ('typ, 'term, 'fact) ctxt =
    17     Fixes of (string * 'typ option * mixfix) list |
    18     Constrains of (string * 'typ) list |
    19     Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
    20     Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
    21     Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list
    22   type context (*= (string, string, thmref) ctxt*)
    23   type context_i (*= (typ, term, thm list) ctxt*)
    24   val map_ctxt: {name: string -> string,
    25     var: string * mixfix -> string * mixfix,
    26     typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c,
    27     attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
    28   val morph_ctxt: morphism -> context_i -> context_i
    29   val params_of: context_i -> (string * typ) list
    30   val prems_of: context_i -> term list
    31   val facts_of: theory -> context_i ->
    32     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
    33   val pretty_stmt: Proof.context -> statement_i -> Pretty.T list
    34   val pretty_ctxt: Proof.context -> context_i -> Pretty.T list
    35   val pretty_statement: Proof.context -> string -> thm -> Pretty.T
    36   type witness
    37   val map_witness: (term * thm -> term * thm) -> witness -> witness
    38   val morph_witness: morphism -> witness -> witness
    39   val witness_prop: witness -> term
    40   val witness_hyps: witness -> term list
    41   val assume_witness: theory -> term -> witness
    42   val prove_witness: Proof.context -> term -> tactic -> witness
    43   val conclude_witness: witness -> thm
    44   val mark_witness: term -> term
    45   val make_witness: term -> thm -> witness
    46   val dest_witness: witness -> term * thm
    47   val transfer_witness: theory -> witness -> witness
    48   val refine_witness: Proof.state -> Proof.state Seq.seq
    49   val rename: (string * (string * mixfix option)) list -> string -> string
    50   val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix
    51   val rename_term: (string * (string * mixfix option)) list -> term -> term
    52   val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
    53   val rename_morphism: (string * (string * mixfix option)) list -> morphism
    54   val instT_type: typ Symtab.table -> typ -> typ
    55   val instT_term: typ Symtab.table -> term -> term
    56   val instT_thm: theory -> typ Symtab.table -> thm -> thm
    57   val instT_morphism: theory -> typ Symtab.table -> morphism
    58   val inst_term: typ Symtab.table * term Symtab.table -> term -> term
    59   val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
    60   val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism
    61   val satisfy_thm: witness list -> thm -> thm
    62   val satisfy_morphism: witness list -> morphism
    63   val satisfy_facts: witness list ->
    64     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    65     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
    66   val export_facts: Proof.context -> Proof.context ->
    67     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    68     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
    69   val export_standard_facts: Proof.context -> Proof.context ->
    70     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    71     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
    72 end;
    73 
    74 structure Element: ELEMENT =
    75 struct
    76 
    77 (** language elements **)
    78 
    79 (* statement *)
    80 
    81 datatype ('typ, 'term) stmt =
    82   Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
    83   Obtains of (string * ((string * 'typ option) list * 'term list)) list;
    84 
    85 type statement = (string, string) stmt;
    86 type statement_i = (typ, term) stmt;
    87 
    88 
    89 (* context *)
    90 
    91 datatype ('typ, 'term, 'fact) ctxt =
    92   Fixes of (string * 'typ option * mixfix) list |
    93   Constrains of (string * 'typ) list |
    94   Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
    95   Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
    96   Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
    97 
    98 type context = (string, string, thmref) ctxt;
    99 type context_i = (typ, term, thm list) ctxt;
   100 
   101 fun map_ctxt {name, var, typ, term, fact, attrib} =
   102   fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) =>
   103        let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end))
   104    | Constrains xs => Constrains (xs |> map (fn (x, T) => (#1 (var (x, NoSyn)), typ T)))
   105    | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
   106       ((name a, map attrib atts), propps |> map (fn (t, ps) => (term t, map term ps)))))
   107    | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
   108       ((name a, map attrib atts), (term t, map term ps))))
   109    | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
   110       ((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
   111 
   112 fun morph_ctxt phi = map_ctxt
   113  {name = Morphism.name phi,
   114   var = Morphism.var phi,
   115   typ = Morphism.typ phi,
   116   term = Morphism.term phi,
   117   fact = Morphism.fact phi,
   118   attrib = Args.morph_values phi};
   119 
   120 
   121 (* logical content *)
   122 
   123 fun params_of (Fixes fixes) = fixes |> map
   124     (fn (x, SOME T, _) => (x, T)
   125       | (x, _, _) => raise TERM ("Untyped context element parameter " ^ quote x, []))
   126   | params_of _ = [];
   127 
   128 fun prems_of (Assumes asms) = maps (map fst o snd) asms
   129   | prems_of (Defines defs) = map (fst o snd) defs
   130   | prems_of _ = [];
   131 
   132 fun assume thy t = Assumption.assume (Thm.cterm_of thy t);
   133 
   134 fun facts_of thy (Assumes asms) = map (apsnd (map (fn (t, _) => ([assume thy t], [])))) asms
   135   | facts_of thy (Defines defs) = map (apsnd (fn (t, _) => [([assume thy t], [])])) defs
   136   | facts_of _ (Notes (_, facts)) = facts
   137   | facts_of _ _ = [];
   138 
   139 
   140 
   141 (** pretty printing **)
   142 
   143 fun pretty_items _ _ [] = []
   144   | pretty_items keyword sep (x :: ys) =
   145       Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
   146         map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
   147 
   148 fun pretty_name_atts ctxt (name, atts) sep =
   149   if name = "" andalso null atts then []
   150   else [Pretty.block
   151     (Pretty.breaks (Pretty.str name :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
   152 
   153 
   154 (* pretty_stmt *)
   155 
   156 fun pretty_stmt ctxt =
   157   let
   158     val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
   159     val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
   160     val prt_terms = separate (Pretty.keyword "and") o map prt_term;
   161     val prt_name_atts = pretty_name_atts ctxt;
   162 
   163     fun prt_show (a, ts) =
   164       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
   165 
   166     fun prt_var (x, SOME T) = Pretty.block [Pretty.str (x ^ " ::"), Pretty.brk 1, prt_typ T]
   167       | prt_var (x, NONE) = Pretty.str x;
   168     val prt_vars =  separate (Pretty.keyword "and") o map prt_var;
   169 
   170     fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
   171       | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
   172           (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
   173   in
   174     fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
   175      | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
   176   end;
   177 
   178 
   179 (* pretty_ctxt *)
   180 
   181 fun pretty_ctxt ctxt =
   182   let
   183     val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
   184     val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
   185     val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
   186     val prt_name_atts = pretty_name_atts ctxt;
   187 
   188     fun prt_mixfix NoSyn = []
   189       | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
   190 
   191     fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
   192           prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
   193       | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx);
   194     fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn);
   195 
   196     fun prt_asm (a, ts) =
   197       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
   198     fun prt_def (a, (t, _)) =
   199       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
   200 
   201     fun prt_fact (ths, []) = map prt_thm ths
   202       | prt_fact (ths, atts) = Pretty.enclose "(" ")"
   203           (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
   204     fun prt_note (a, ths) =
   205       Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
   206   in
   207     fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
   208      | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
   209      | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
   210      | Defines defs => pretty_items "defines" "and" (map prt_def defs)
   211      | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
   212      | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
   213   end;
   214 
   215 
   216 (* pretty_statement *)
   217 
   218 local
   219 
   220 fun thm_name kind th prts =
   221   let val head =
   222     (case #1 (Thm.get_name_tags th) of
   223       "" => Pretty.command kind
   224     | a => Pretty.block [Pretty.command kind, Pretty.brk 1, Pretty.str (Sign.base_name a ^ ":")])
   225   in Pretty.block (Pretty.fbreaks (head :: prts)) end;
   226 
   227 fun obtain prop ctxt =
   228   let
   229     val ((xs, prop'), ctxt') = Variable.focus prop ctxt;
   230     val As = Logic.strip_imp_prems (Thm.term_of prop');
   231     fun var (x, T) = (ProofContext.revert_skolem ctxt' x, SOME T);
   232   in (("", (map (var o Term.dest_Free o Thm.term_of) xs, As)), ctxt') end;
   233 
   234 in
   235 
   236 fun pretty_statement ctxt kind raw_th =
   237   let
   238     val thy = ProofContext.theory_of ctxt;
   239     val cert = Thm.cterm_of thy;
   240 
   241     val th = norm_hhf raw_th;
   242     val is_elim = ObjectLogic.is_elim th;
   243 
   244     val ((_, [th']), ctxt') = Variable.import true [th] ctxt;
   245     val prop = Thm.prop_of th';
   246     val (prems, concl) = Logic.strip_horn prop;
   247     val concl_term = ObjectLogic.drop_judgment thy concl;
   248 
   249     val fixes = fold_aterms (fn v as Free (x, T) =>
   250         if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
   251         then insert (op =) (x, T) else I | _ => I) prop []
   252       |> rev |> map (apfst (ProofContext.revert_skolem ctxt'));
   253     val (assumes, cases) = take_suffix (fn prem =>
   254       is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
   255   in
   256     pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @
   257     pretty_ctxt ctxt' (Assumes (map (fn t => (("", []), [(t, [])])) assumes)) @
   258     pretty_stmt ctxt'
   259      (if null cases then Shows [(("", []), [(concl, [])])]
   260       else Obtains (#1 (fold_map (obtain o cert) cases ctxt')))
   261   end |> thm_name kind raw_th;
   262 
   263 end;
   264 
   265 
   266 
   267 (** logical operations **)
   268 
   269 (* witnesses -- hypotheses as protected facts *)
   270 
   271 datatype witness = Witness of term * thm;
   272 
   273 fun map_witness f (Witness witn) = Witness (f witn);
   274 
   275 fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
   276 
   277 fun witness_prop (Witness (t, _)) = t;
   278 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
   279 
   280 fun assume_witness thy t =
   281   Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t)));
   282 
   283 fun prove_witness ctxt t tac =
   284   Witness (t, Goal.prove ctxt [] [] (Logic.protect t) (fn _ =>
   285     Tactic.rtac Drule.protectI 1 THEN tac));
   286 
   287 fun conclude_witness (Witness (_, th)) = norm_hhf (Goal.conclude th);
   288 
   289 val mark_witness = Logic.protect;
   290 
   291 fun make_witness t th = Witness (t, th);
   292 fun dest_witness (Witness w) = w;
   293 
   294 fun transfer_witness thy (Witness (t, th)) = Witness (t, Thm.transfer thy th);
   295 
   296 val refine_witness =
   297   Proof.refine (Method.Basic (K (Method.RAW_METHOD
   298     (K (ALLGOALS
   299       (PRECISE_CONJUNCTS ~1 (ALLGOALS
   300         (PRECISE_CONJUNCTS ~1 (TRYALL (Tactic.rtac Drule.protectI))))))))));
   301 
   302 
   303 (* derived rules *)
   304 
   305 fun instantiate_tfrees thy subst th =
   306   let
   307     val certT = Thm.ctyp_of thy;
   308     val idx = Thm.maxidx_of th + 1;
   309     fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
   310 
   311     fun add_inst (a, S) insts =
   312       if AList.defined (op =) insts a then insts
   313       else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
   314     val insts =
   315       Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
   316         (Thm.full_prop_of th) [];
   317   in
   318     th
   319     |> Thm.generalize (map fst insts, []) idx
   320     |> Thm.instantiate (map cert_inst insts, [])
   321   end;
   322 
   323 fun instantiate_frees thy subst =
   324   let val cert = Thm.cterm_of thy in
   325     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   326     Drule.forall_elim_list (map (cert o snd) subst)
   327   end;
   328 
   329 fun hyps_rule rule th =
   330   let val {hyps, ...} = Thm.crep_thm th in
   331     Drule.implies_elim_list
   332       (rule (Drule.implies_intr_list hyps th))
   333       (map (Thm.assume o Drule.cterm_rule rule) hyps)
   334   end;
   335 
   336 
   337 (* rename *)
   338 
   339 fun rename ren x =
   340   (case AList.lookup (op =) ren (x: string) of
   341     NONE => x
   342   | SOME (x', _) => x');
   343 
   344 fun rename_var ren (x, mx) =
   345   (case (AList.lookup (op =) ren (x: string), mx) of
   346     (NONE, _) => (x, mx)
   347   | (SOME (x', NONE), Structure) => (x', mx)
   348   | (SOME (x', SOME _), Structure) =>
   349       error ("Attempt to change syntax of structure parameter " ^ quote x)
   350   | (SOME (x', NONE), _) => (x', NoSyn)
   351   | (SOME (x', SOME mx'), _) => (x', mx'));
   352 
   353 fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
   354   | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
   355   | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
   356   | rename_term _ a = a;
   357 
   358 fun rename_thm ren th =
   359   let
   360     val thy = Thm.theory_of_thm th;
   361     val subst = (Drule.fold_terms o Term.fold_aterms)
   362       (fn Free (x, T) =>
   363         let val x' = rename ren x
   364         in if x = x' then I else insert (eq_fst (op =)) ((x, T), Free (x', T)) end
   365       | _ => I) th [];
   366   in
   367     if null subst then th
   368     else th |> hyps_rule (instantiate_frees thy subst)
   369   end;
   370 
   371 fun rename_morphism ren = Morphism.morphism
   372   {name = I, var = rename_var ren, typ = I, term = rename_term ren, fact = map (rename_thm ren)};
   373 
   374 
   375 (* instantiate types *)
   376 
   377 fun instT_type env =
   378   if Symtab.is_empty env then I
   379   else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   380 
   381 fun instT_term env =
   382   if Symtab.is_empty env then I
   383   else Term.map_types (instT_type env);
   384 
   385 fun instT_subst env th = (Drule.fold_terms o Term.fold_types o Term.fold_atyps)
   386   (fn T as TFree (a, _) =>
   387     let val T' = the_default T (Symtab.lookup env a)
   388     in if T = T' then I else insert (op =) (a, T') end
   389   | _ => I) th [];
   390 
   391 fun instT_thm thy env th =
   392   if Symtab.is_empty env then th
   393   else
   394     let val subst = instT_subst env th
   395     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   396 
   397 fun instT_morphism thy env = Morphism.morphism
   398   {name = I, var = I, typ = instT_type env, term = instT_term env, fact = map (instT_thm thy env)};
   399 
   400 
   401 (* instantiate types and terms *)
   402 
   403 fun inst_term (envT, env) =
   404   if Symtab.is_empty env then instT_term envT
   405   else
   406     let
   407       val instT = instT_type envT;
   408       fun inst (Const (x, T)) = Const (x, instT T)
   409         | inst (Free (x, T)) =
   410             (case Symtab.lookup env x of
   411               NONE => Free (x, instT T)
   412             | SOME t => t)
   413         | inst (Var (xi, T)) = Var (xi, instT T)
   414         | inst (b as Bound _) = b
   415         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   416         | inst (t $ u) = inst t $ inst u;
   417     in Envir.beta_norm o inst end;
   418 
   419 fun inst_thm thy (envT, env) th =
   420   if Symtab.is_empty env then instT_thm thy envT th
   421   else
   422     let
   423       val substT = instT_subst envT th;
   424       val subst = (Drule.fold_terms o Term.fold_aterms)
   425        (fn Free (x, T) =>
   426           let
   427             val T' = instT_type envT T;
   428             val t = Free (x, T');
   429             val t' = the_default t (Symtab.lookup env x);
   430           in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
   431        | _ => I) th [];
   432     in
   433       if null substT andalso null subst then th
   434       else th |> hyps_rule
   435        (instantiate_tfrees thy substT #>
   436         instantiate_frees thy subst #>
   437         Drule.fconv_rule (Thm.beta_conversion true))
   438     end;
   439 
   440 fun inst_morphism thy envs = Morphism.morphism
   441   {name = I, var = I, typ = instT_type (#1 envs), term = inst_term envs,
   442     fact = map (inst_thm thy envs)};
   443 
   444 
   445 (* satisfy hypotheses *)
   446 
   447 fun satisfy_thm witns thm = thm |> fold (fn hyp =>
   448     (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
   449       NONE => I
   450     | SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th))
   451   (#hyps (Thm.crep_thm thm));
   452 
   453 fun satisfy_morphism witns = Morphism.thm_morphism (satisfy_thm witns);
   454 
   455 fun satisfy_facts witns facts =
   456   morph_ctxt (satisfy_morphism witns) (Notes ("", facts)) |> (fn Notes (_, facts') => facts');
   457 
   458 
   459 (* generalize type/term parameters *)
   460 
   461 local
   462 
   463 val maxidx_atts = fold Args.maxidx_values;
   464 
   465 fun exp_facts std inner outer facts =
   466   let
   467     val thy = ProofContext.theory_of inner;
   468     val maxidx =
   469       fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1;
   470     val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #>
   471       ((if std then ProofContext.export_standard else ProofContext.export) inner outer);
   472     val exp_term = Drule.term_rule thy (singleton exp_fact);
   473     val exp_typ = Logic.type_map exp_term;
   474     val morphism =
   475       Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact};
   476     val Notes (_, facts') = morph_ctxt morphism (Notes ("", facts));
   477   in facts' end;
   478 
   479 in
   480 
   481 val export_facts = exp_facts false;
   482 val export_standard_facts = exp_facts true;
   483 
   484 end;
   485 
   486 end;