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