src/Pure/Isar/element.ML
author ballarin
Tue Jul 11 11:17:09 2006 +0200 (2006-07-11)
changeset 20068 19c7361db4a3
parent 20058 7d035e26e5f9
child 20150 baa589c574ff
permissions -rw-r--r--
New function transfer_witness lifting Thm.transfer to witnesses.
     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 = Variable.rename_wrt ctxt [] (Logic.strip_params prop);
   220     val args = rev (map Free xs);
   221     val As = Logic.strip_assums_hyp prop |> map (fn t => Term.subst_bounds (args, t));
   222     val ctxt' = ctxt |> fold Variable.declare_term args;
   223   in (("", (map (apsnd SOME) xs, As)), ctxt') end;
   224 
   225 in
   226 
   227 fun pretty_statement ctxt kind raw_th =
   228   let
   229     val thy = ProofContext.theory_of ctxt;
   230     val th = Goal.norm_hhf raw_th;
   231 
   232     fun standard_thesis t =
   233       let
   234         val C = ObjectLogic.drop_judgment thy (Thm.concl_of th);
   235         val C' = Var ((AutoBind.thesisN, Thm.maxidx_of th + 1), Term.fastype_of C);
   236       in Term.subst_atomic [(C, C')] t end;
   237 
   238     val raw_prop =
   239       Thm.prop_of th
   240       |> singleton (Variable.monomorphic ctxt)
   241       |> K (ObjectLogic.is_elim th) ? standard_thesis
   242       |> Term.zero_var_indexes;
   243 
   244     val vars = Term.add_vars raw_prop [];
   245     val frees = Variable.rename_wrt ctxt [raw_prop] (map (apfst fst) vars);
   246     val fixes = rev (filter_out (fn (x, _) => x = AutoBind.thesisN) frees);
   247 
   248     val prop = Term.instantiate ([], vars ~~ map Free frees) raw_prop;
   249     val (prems, concl) = Logic.strip_horn prop;
   250     val thesis = ObjectLogic.fixed_judgment thy AutoBind.thesisN;
   251     val (asms, cases) = take_suffix (fn prem => thesis aconv Logic.strip_assums_concl prem) prems;
   252   in
   253     pretty_ctxt ctxt (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @
   254     pretty_ctxt ctxt (Assumes (map (fn t => (("", []), [(t, [])])) asms)) @
   255     pretty_stmt ctxt
   256      (if null cases then Shows [(("", []), [(concl, [])])]
   257       else Obtains (#1 (fold_map obtain cases (ctxt |> Variable.declare_term prop))))
   258   end |> thm_name kind raw_th;
   259 
   260 end;
   261 
   262 
   263 
   264 (** logical operations **)
   265 
   266 (* witnesses -- hypotheses as protected facts *)
   267 
   268 datatype witness = Witness of term * thm;
   269 
   270 fun map_witness f (Witness witn) = Witness (f witn);
   271 
   272 fun witness_prop (Witness (t, _)) = t;
   273 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
   274 
   275 fun assume_witness thy t =
   276   Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t)));
   277 
   278 fun prove_witness ctxt t tac =
   279   Witness (t, Goal.prove ctxt [] [] (Logic.protect t) (fn _ =>
   280     Tactic.rtac Drule.protectI 1 THEN tac));
   281 
   282 fun conclude_witness (Witness (_, th)) = Goal.norm_hhf (Goal.conclude th);
   283 
   284 val mark_witness = Logic.protect;
   285 
   286 fun make_witness t th = Witness (t, th);
   287 
   288 fun dest_witness (Witness w) = w;
   289 
   290 fun transfer_witness thy (Witness (t, th)) = Witness (t, Thm.transfer thy th);
   291 
   292 val refine_witness =
   293   Proof.refine (Method.Basic (K (Method.RAW_METHOD
   294     (K (ALLGOALS
   295       (PRECISE_CONJUNCTS ~1 (ALLGOALS
   296         (PRECISE_CONJUNCTS ~1 (TRYALL (Tactic.rtac Drule.protectI))))))))));
   297 
   298 
   299 (* derived rules *)
   300 
   301 fun instantiate_tfrees thy subst th =
   302   let
   303     val certT = Thm.ctyp_of thy;
   304     val idx = Thm.maxidx_of th + 1;
   305     fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
   306 
   307     fun add_inst (a, S) insts =
   308       if AList.defined (op =) insts a then insts
   309       else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
   310     val insts =
   311       Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
   312         (Thm.full_prop_of th) [];
   313   in
   314     th
   315     |> Thm.generalize (map fst insts, []) idx
   316     |> Thm.instantiate (map cert_inst insts, [])
   317   end;
   318 
   319 fun instantiate_frees thy subst =
   320   let val cert = Thm.cterm_of thy in
   321     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   322     Drule.forall_elim_list (map (cert o snd) subst)
   323   end;
   324 
   325 fun hyps_rule rule th =
   326   let
   327     val cterm_rule = Drule.mk_term #> rule #> Drule.dest_term;
   328     val {hyps, ...} = Thm.crep_thm th;
   329   in
   330     Drule.implies_elim_list
   331       (rule (Drule.implies_intr_list hyps th))
   332       (map (Thm.assume o cterm_rule) hyps)
   333   end;
   334 
   335 
   336 (* rename *)
   337 
   338 fun rename ren x =
   339   (case AList.lookup (op =) ren (x: string) of
   340     NONE => x
   341   | SOME (x', _) => x');
   342 
   343 fun rename_var ren (x, mx) =
   344   (case (AList.lookup (op =) ren (x: string), mx) of
   345     (NONE, _) => (x, mx)
   346   | (SOME (x', NONE), Structure) => (x', mx)
   347   | (SOME (x', SOME _), Structure) =>
   348       error ("Attempt to change syntax of structure parameter " ^ quote x)
   349   | (SOME (x', NONE), _) => (x', NoSyn)
   350   | (SOME (x', SOME mx'), _) => (x', mx'));
   351 
   352 fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
   353   | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
   354   | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
   355   | rename_term _ a = a;
   356 
   357 fun rename_thm ren th =
   358   let
   359     val subst = Drule.frees_of th
   360       |> map_filter (fn (x, T) =>
   361         let val x' = rename ren x
   362         in if x = x' then NONE else SOME ((x, T), (Free (x', T))) end);
   363   in
   364     if null subst then th
   365     else th |> hyps_rule (instantiate_frees (Thm.theory_of_thm th) 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_term_types (instT_type env);
   385 
   386 fun instT_subst env th =
   387   Drule.tfrees_of th
   388   |> map_filter (fn (a, S) =>
   389     let
   390       val T = TFree (a, S);
   391       val T' = the_default T (Symtab.lookup env a);
   392     in if T = T' then NONE else SOME (a, T') end);
   393 
   394 fun instT_thm thy env th =
   395   if Symtab.is_empty env then th
   396   else
   397     let val subst = instT_subst env th
   398     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   399 
   400 fun instT_witness thy env =
   401   map_witness (fn (t, th) => (instT_term env t, instT_thm thy env th));
   402 
   403 fun instT_ctxt thy env =
   404   map_ctxt_values (instT_type env) (instT_term env) (instT_thm thy env);
   405 
   406 
   407 (* instantiate types and terms *)
   408 
   409 fun inst_term (envT, env) =
   410   if Symtab.is_empty env then instT_term envT
   411   else
   412     let
   413       val instT = instT_type envT;
   414       fun inst (Const (x, T)) = Const (x, instT T)
   415         | inst (Free (x, T)) =
   416             (case Symtab.lookup env x of
   417               NONE => Free (x, instT T)
   418             | SOME t => t)
   419         | inst (Var (xi, T)) = Var (xi, instT T)
   420         | inst (b as Bound _) = b
   421         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   422         | inst (t $ u) = inst t $ inst u;
   423     in Envir.beta_norm o inst end;
   424 
   425 fun inst_thm thy (envT, env) th =
   426   if Symtab.is_empty env then instT_thm thy envT th
   427   else
   428     let
   429       val substT = instT_subst envT th;
   430       val subst = Drule.frees_of th
   431         |> map_filter (fn (x, T) =>
   432           let
   433             val T' = instT_type envT T;
   434             val t = Free (x, T');
   435             val t' = the_default t (Symtab.lookup env x);
   436           in if t aconv t' then NONE else SOME ((x, T'), t') end);
   437     in
   438       if null substT andalso null subst then th
   439       else th |> hyps_rule
   440        (instantiate_tfrees thy substT #>
   441         instantiate_frees thy subst #>
   442         Drule.fconv_rule (Thm.beta_conversion true))
   443     end;
   444 
   445 fun inst_witness thy envs =
   446   map_witness (fn (t, th) => (inst_term envs t, inst_thm thy envs th));
   447 
   448 fun inst_ctxt thy envs =
   449   map_ctxt_values (instT_type (#1 envs)) (inst_term envs) (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_witness witns = map_witness (apsnd (satisfy_thm witns));
   461 
   462 fun satisfy_ctxt witns = map_ctxt_values I I (satisfy_thm witns);
   463 
   464 end;