src/Pure/Isar/element.ML
author wenzelm
Tue Nov 21 18:07:37 2006 +0100 (2006-11-21)
changeset 21440 807a39221a58
parent 21032 a4b85340d6bd
child 21481 025ab31286d8
permissions -rw-r--r--
notes: proper kind;
     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 map_ctxt_values: (typ -> typ) -> (term -> term) -> (thm -> thm) -> 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 witness_prop: witness -> term
    39   val witness_hyps: witness -> term list
    40   val assume_witness: theory -> term -> witness
    41   val prove_witness: Proof.context -> term -> tactic -> witness
    42   val conclude_witness: witness -> thm
    43   val mark_witness: term -> term
    44   val make_witness: term -> thm -> witness
    45   val dest_witness: witness -> term * thm
    46   val transfer_witness: theory -> witness -> witness
    47   val refine_witness: Proof.state -> Proof.state Seq.seq
    48   val rename: (string * (string * mixfix option)) list -> string -> string
    49   val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix
    50   val rename_term: (string * (string * mixfix option)) list -> term -> term
    51   val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
    52   val rename_witness: (string * (string * mixfix option)) list -> witness -> witness
    53   val rename_ctxt: (string * (string * mixfix option)) list -> context_i -> context_i
    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_witness: theory -> typ Symtab.table -> witness -> witness
    58   val instT_ctxt: theory -> typ Symtab.table -> context_i -> context_i
    59   val inst_term: typ Symtab.table * term Symtab.table -> term -> term
    60   val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
    61   val inst_witness: theory -> typ Symtab.table * term Symtab.table -> witness -> witness
    62   val inst_ctxt: theory -> typ Symtab.table * term Symtab.table -> context_i -> context_i
    63   val satisfy_thm: witness list -> thm -> thm
    64   val satisfy_witness: witness list -> witness -> witness
    65   val satisfy_ctxt: witness list -> context_i -> context_i
    66   val satisfy_facts: witness list ->
    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_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   val export_standard_facts: Proof.context -> Proof.context ->
    73     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    74     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
    75 end;
    76 
    77 structure Element: ELEMENT =
    78 struct
    79 
    80 
    81 (** language elements **)
    82 
    83 (* statement *)
    84 
    85 datatype ('typ, 'term) stmt =
    86   Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
    87   Obtains of (string * ((string * 'typ option) list * 'term list)) list;
    88 
    89 type statement = (string, string) stmt;
    90 type statement_i = (typ, term) stmt;
    91 
    92 
    93 (* context *)
    94 
    95 datatype ('typ, 'term, 'fact) ctxt =
    96   Fixes of (string * 'typ option * mixfix) list |
    97   Constrains of (string * 'typ) list |
    98   Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
    99   Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
   100   Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
   101 
   102 type context = (string, string, thmref) ctxt;
   103 type context_i = (typ, term, thm list) ctxt;
   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_values typ term thm = map_ctxt
   117   {name = I, var = I, typ = typ, term = term, fact = map thm,
   118     attrib = Args.map_values I typ term thm};
   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 witness_prop (Witness (t, _)) = t;
   276 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
   277 
   278 fun assume_witness thy t =
   279   Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t)));
   280 
   281 fun prove_witness ctxt t tac =
   282   Witness (t, Goal.prove ctxt [] [] (Logic.protect t) (fn _ =>
   283     Tactic.rtac Drule.protectI 1 THEN tac));
   284 
   285 fun conclude_witness (Witness (_, th)) = norm_hhf (Goal.conclude th);
   286 
   287 val mark_witness = Logic.protect;
   288 
   289 fun make_witness t th = Witness (t, th);
   290 fun dest_witness (Witness w) = w;
   291 
   292 fun transfer_witness thy (Witness (t, th)) = Witness (t, Thm.transfer thy th);
   293 
   294 val refine_witness =
   295   Proof.refine (Method.Basic (K (Method.RAW_METHOD
   296     (K (ALLGOALS
   297       (PRECISE_CONJUNCTS ~1 (ALLGOALS
   298         (PRECISE_CONJUNCTS ~1 (TRYALL (Tactic.rtac Drule.protectI))))))))));
   299 
   300 
   301 (* derived rules *)
   302 
   303 fun instantiate_tfrees thy subst th =
   304   let
   305     val certT = Thm.ctyp_of thy;
   306     val idx = Thm.maxidx_of th + 1;
   307     fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
   308 
   309     fun add_inst (a, S) insts =
   310       if AList.defined (op =) insts a then insts
   311       else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
   312     val insts =
   313       Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
   314         (Thm.full_prop_of th) [];
   315   in
   316     th
   317     |> Thm.generalize (map fst insts, []) idx
   318     |> Thm.instantiate (map cert_inst insts, [])
   319   end;
   320 
   321 fun instantiate_frees thy subst =
   322   let val cert = Thm.cterm_of thy in
   323     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   324     Drule.forall_elim_list (map (cert o snd) subst)
   325   end;
   326 
   327 fun hyps_rule rule th =
   328   let
   329     val cterm_rule = Drule.mk_term #> rule #> Drule.dest_term;
   330     val {hyps, ...} = Thm.crep_thm th;
   331   in
   332     Drule.implies_elim_list
   333       (rule (Drule.implies_intr_list hyps th))
   334       (map (Thm.assume o cterm_rule) hyps)
   335   end;
   336 
   337 
   338 (* rename *)
   339 
   340 fun rename ren x =
   341   (case AList.lookup (op =) ren (x: string) of
   342     NONE => x
   343   | SOME (x', _) => x');
   344 
   345 fun rename_var ren (x, mx) =
   346   (case (AList.lookup (op =) ren (x: string), mx) of
   347     (NONE, _) => (x, mx)
   348   | (SOME (x', NONE), Structure) => (x', mx)
   349   | (SOME (x', SOME _), Structure) =>
   350       error ("Attempt to change syntax of structure parameter " ^ quote x)
   351   | (SOME (x', NONE), _) => (x', NoSyn)
   352   | (SOME (x', SOME mx'), _) => (x', mx'));
   353 
   354 fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
   355   | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
   356   | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
   357   | rename_term _ a = a;
   358 
   359 fun rename_thm ren th =
   360   let
   361     val thy = Thm.theory_of_thm th;
   362     val subst = (Drule.fold_terms o Term.fold_aterms)
   363       (fn Free (x, T) =>
   364         let val x' = rename ren x
   365         in if x = x' then I else insert (eq_fst (op =)) ((x, T), Free (x', T)) end
   366       | _ => I) th [];
   367   in
   368     if null subst then th
   369     else th |> hyps_rule (instantiate_frees thy subst)
   370   end;
   371 
   372 fun rename_witness ren =
   373   map_witness (fn (t, th) => (rename_term ren t, rename_thm ren th));
   374 
   375 fun rename_ctxt ren =
   376   map_ctxt_values I (rename_term ren) (rename_thm ren)
   377   #> map_ctxt {name = I, typ = I, term = I, fact = I, attrib = I, var = rename_var ren};
   378 
   379 
   380 (* instantiate types *)
   381 
   382 fun instT_type env =
   383   if Symtab.is_empty env then I
   384   else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   385 
   386 fun instT_term env =
   387   if Symtab.is_empty env then I
   388   else Term.map_types (instT_type env);
   389 
   390 fun instT_subst env th = (Drule.fold_terms o Term.fold_types o Term.fold_atyps)
   391   (fn T as TFree (a, _) =>
   392     let val T' = the_default T (Symtab.lookup env a)
   393     in if T = T' then I else insert (op =) (a, T') end
   394   | _ => I) th [];
   395 
   396 fun instT_thm thy env th =
   397   if Symtab.is_empty env then th
   398   else
   399     let val subst = instT_subst env th
   400     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   401 
   402 fun instT_witness thy env =
   403   map_witness (fn (t, th) => (instT_term env t, instT_thm thy env th));
   404 
   405 fun instT_ctxt thy env =
   406   map_ctxt_values (instT_type env) (instT_term env) (instT_thm thy env);
   407 
   408 
   409 (* instantiate types and terms *)
   410 
   411 fun inst_term (envT, env) =
   412   if Symtab.is_empty env then instT_term envT
   413   else
   414     let
   415       val instT = instT_type envT;
   416       fun inst (Const (x, T)) = Const (x, instT T)
   417         | inst (Free (x, T)) =
   418             (case Symtab.lookup env x of
   419               NONE => Free (x, instT T)
   420             | SOME t => t)
   421         | inst (Var (xi, T)) = Var (xi, instT T)
   422         | inst (b as Bound _) = b
   423         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   424         | inst (t $ u) = inst t $ inst u;
   425     in Envir.beta_norm o inst end;
   426 
   427 fun inst_thm thy (envT, env) th =
   428   if Symtab.is_empty env then instT_thm thy envT th
   429   else
   430     let
   431       val substT = instT_subst envT th;
   432       val subst = (Drule.fold_terms o Term.fold_aterms)
   433        (fn Free (x, T) =>
   434           let
   435             val T' = instT_type envT T;
   436             val t = Free (x, T');
   437             val t' = the_default t (Symtab.lookup env x);
   438           in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
   439        | _ => I) th [];
   440     in
   441       if null substT andalso null subst then th
   442       else th |> hyps_rule
   443        (instantiate_tfrees thy substT #>
   444         instantiate_frees thy subst #>
   445         Drule.fconv_rule (Thm.beta_conversion true))
   446     end;
   447 
   448 fun inst_witness thy envs =
   449   map_witness (fn (t, th) => (inst_term envs t, inst_thm thy envs th));
   450 
   451 fun inst_ctxt thy envs =
   452   map_ctxt_values (instT_type (#1 envs)) (inst_term envs) (inst_thm thy envs);
   453 
   454 
   455 (* satisfy hypotheses *)
   456 
   457 fun satisfy_thm witns thm = thm |> fold (fn hyp =>
   458     (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
   459       NONE => I
   460     | SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th))
   461   (#hyps (Thm.crep_thm thm));
   462 
   463 fun satisfy_witness witns = map_witness (apsnd (satisfy_thm witns));
   464 
   465 fun satisfy_ctxt witns = map_ctxt_values I I (satisfy_thm witns);
   466 
   467 fun satisfy_facts witns facts =
   468   satisfy_ctxt witns (Notes ("", facts)) |> (fn Notes (_, facts') => facts');
   469 
   470 
   471 (* generalize type/term parameters *)
   472 
   473 local
   474 
   475 val maxidx_atts = fold Args.maxidx_values;
   476 
   477 fun exp_facts std inner outer facts =
   478   let
   479     val thy = ProofContext.theory_of inner;
   480     val maxidx =
   481       fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1;
   482     val exp_thm = Thm.adjust_maxidx_thm maxidx #>
   483       singleton ((if std then ProofContext.export_standard else ProofContext.export) inner outer);
   484     val exp_term = Drule.term_rule thy exp_thm;
   485     val exp_typ = Logic.mk_type #> exp_term #> Logic.dest_type;
   486     val Notes (_, facts') = map_ctxt_values exp_typ exp_term exp_thm (Notes ("", facts));
   487   in facts' end;
   488 
   489 in
   490 
   491 val export_facts = exp_facts false;
   492 val export_standard_facts = exp_facts true;
   493 
   494 end;
   495 
   496 end;