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