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