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