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