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