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