src/Pure/Isar/element.ML
author wenzelm
Thu Feb 02 16:31:34 2006 +0100 (2006-02-02)
changeset 18906 2487aea6ff12
parent 18894 9c8c60853966
child 19259 196d3b7c8ad1
permissions -rw-r--r--
always use Attrib.src;
     1 (*  Title:      Pure/Isar/element.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Explicit data structures for some Isar language elements.
     6 *)
     7 
     8 signature ELEMENT =
     9 sig
    10   datatype ('typ, 'term, 'fact) ctxt =
    11     Fixes of (string * 'typ option * mixfix) list |
    12     Constrains of (string * 'typ) list |
    13     Assumes of ((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list |
    14     Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
    15     Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list
    16   type context (*= (string, string, thmref) ctxt*)
    17   type context_i (*= (typ, term, thm list) ctxt*)
    18   val map_ctxt: {name: string -> string,
    19     var: string * mixfix -> string * mixfix,
    20     typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c,
    21     attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
    22   val map_ctxt_values: (typ -> typ) -> (term -> term) -> (thm -> thm) -> context_i -> context_i
    23   val pretty_ctxt: ProofContext.context -> context_i -> Pretty.T list
    24   val rename: (string * (string * mixfix option)) list -> string -> string
    25   val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix
    26   val rename_term: (string * (string * mixfix option)) list -> term -> term
    27   val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
    28   val rename_ctxt: (string * (string * mixfix option)) list -> context_i -> context_i
    29   val instT_type: typ Symtab.table -> typ -> typ
    30   val instT_term: typ Symtab.table -> term -> term
    31   val instT_thm: theory -> typ Symtab.table -> thm -> thm
    32   val instT_ctxt: theory -> typ Symtab.table -> context_i -> context_i
    33   val inst_term: typ Symtab.table * term Symtab.table -> term -> term
    34   val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
    35   val inst_ctxt: theory -> typ Symtab.table * term Symtab.table -> context_i -> context_i
    36   datatype ('typ, 'term) stmt =
    37     Shows of ((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list |
    38     Obtains of (string * ((string * 'typ option) list * 'term list)) list
    39   type statement  (*= (string, string) stmt*)
    40   type statement_i  (*= (typ, term) stmt*)
    41 end;
    42 
    43 structure Element: ELEMENT =
    44 struct
    45 
    46 (** context elements **)
    47 
    48 (* datatype ctxt *)
    49 
    50 datatype ('typ, 'term, 'fact) ctxt =
    51   Fixes of (string * 'typ option * mixfix) list |
    52   Constrains of (string * 'typ) list |
    53   Assumes of ((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list |
    54   Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
    55   Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
    56 
    57 type context = (string, string, thmref) ctxt;
    58 type context_i = (typ, term, thm list) ctxt;
    59 
    60 fun map_ctxt {name, var, typ, term, fact, attrib} =
    61   fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) =>
    62        let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end))
    63    | Constrains xs => Constrains (xs |> map (fn (x, T) => (#1 (var (x, NoSyn)), typ T)))
    64    | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
    65       ((name a, map attrib atts), propps |> map (fn (t, (ps, qs)) =>
    66         (term t, (map term ps, map term qs))))))
    67    | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
    68       ((name a, map attrib atts), (term t, map term ps))))
    69    | Notes facts => Notes (facts |> map (fn ((a, atts), bs) =>
    70       ((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
    71 
    72 fun map_ctxt_values typ term thm = map_ctxt
    73   {name = I, var = I, typ = typ, term = term, fact = map thm,
    74     attrib = Args.map_values I typ term thm};
    75 
    76 
    77 (* pretty_ctxt *)
    78 
    79 fun pretty_ctxt ctxt =
    80   let
    81     val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
    82     val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
    83     val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
    84     val prt_atts = Args.pretty_attribs ctxt;
    85 
    86     fun prt_mixfix mx =
    87       let val s = Syntax.string_of_mixfix mx
    88       in if s = "" then [] else [Pretty.brk 2, Pretty.str s] end;
    89     fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
    90           prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
    91       | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx);
    92     fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn);
    93 
    94     fun prt_name name = Pretty.str (ProofContext.extern_thm ctxt name);
    95     fun prt_name_atts (name, atts) sep =
    96       if name = "" andalso null atts then []
    97       else [Pretty.block (Pretty.breaks (prt_name name :: prt_atts atts @ [Pretty.str sep]))];
    98 
    99     fun prt_asm (a, ts) =
   100       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
   101     fun prt_def (a, (t, _)) =
   102       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
   103 
   104     fun prt_fact (ths, []) = map prt_thm ths
   105       | prt_fact (ths, atts) =
   106           Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths)) :: prt_atts atts;
   107     fun prt_note (a, ths) =
   108       Pretty.block (Pretty.breaks (List.concat (prt_name_atts a "=" :: map prt_fact ths)));
   109 
   110     fun items _ [] = []
   111       | items prfx (x :: xs) =
   112           Pretty.block [Pretty.str prfx, Pretty.brk 1, x] :: items "  and" xs;
   113   in
   114     fn Fixes fixes => items "fixes" (map prt_fix fixes)
   115      | Constrains xs => items "constrains" (map prt_constrain xs)
   116      | Assumes asms => items "assumes" (map prt_asm asms)
   117      | Defines defs => items "defines" (map prt_def defs)
   118      | Notes facts => items "notes" (map prt_note facts)
   119   end;
   120 
   121 
   122 
   123 (** logical operations **)
   124 
   125 (* derived rules *)
   126 
   127 fun instantiate_tfrees thy subst =
   128   let
   129     val certT = Thm.ctyp_of thy;
   130     fun inst vs (a, T) = AList.lookup (op =) vs a
   131       |> Option.map (fn v => (certT (TVar v), certT T));
   132   in
   133     Drule.tvars_intr_list (map fst subst) #->
   134     (fn vs => Thm.instantiate (List.mapPartial (inst vs) subst, []))
   135   end;
   136 
   137 fun instantiate_frees thy subst =
   138   let val cert = Thm.cterm_of thy in
   139     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   140     Drule.forall_elim_list (map (cert o snd) subst)
   141   end;
   142 
   143 fun hyps_rule rule th =
   144   let
   145     val cterm_rule = Thm.reflexive #> rule #> Thm.cprop_of #> Drule.dest_equals #> #1;
   146     val {hyps, ...} = Thm.crep_thm th;
   147   in
   148     Drule.implies_elim_list
   149       (rule (Drule.implies_intr_list hyps th))
   150       (map (Thm.assume o cterm_rule) hyps)
   151   end;
   152 
   153 
   154 (* renaming *)
   155 
   156 fun rename ren x =
   157   (case AList.lookup (op =) ren (x: string) of
   158     NONE => x
   159   | SOME (x', _) => x');
   160 
   161 fun rename_var ren (x, mx) =
   162   (case (AList.lookup (op =) ren (x: string), mx) of
   163     (NONE, _) => (x, mx)
   164   | (SOME (x', NONE), Structure) => (x', mx)
   165   | (SOME (x', SOME _), Structure) =>
   166       error ("Attempt to change syntax of structure parameter " ^ quote x)
   167   | (SOME (x', NONE), _) => (x', NoSyn)
   168   | (SOME (x', SOME mx'), _) => (x', mx'));
   169 
   170 fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
   171   | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
   172   | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
   173   | rename_term _ a = a;
   174 
   175 fun rename_thm ren th =
   176   let
   177     val subst = Drule.frees_of th
   178       |> List.mapPartial (fn (x, T) =>
   179         let val x' = rename ren x
   180         in if x = x' then NONE else SOME ((x, T), (Free (x', T))) end);
   181   in
   182     if null subst then th
   183     else th |> hyps_rule (instantiate_frees (Thm.theory_of_thm th) subst)
   184   end;
   185 
   186 fun rename_ctxt ren =
   187   map_ctxt_values I (rename_term ren) (rename_thm ren)
   188   #> map_ctxt {name = I, typ = I, term = I, fact = I, attrib = I, var = rename_var ren};
   189 
   190 
   191 (* type instantiation *)
   192 
   193 fun instT_type env =
   194   if Symtab.is_empty env then I
   195   else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   196 
   197 fun instT_term env =
   198   if Symtab.is_empty env then I
   199   else Term.map_term_types (instT_type env);
   200 
   201 fun instT_subst env th =
   202   Drule.tfrees_of th
   203   |> List.mapPartial (fn (a, S) =>
   204     let
   205       val T = TFree (a, S);
   206       val T' = the_default T (Symtab.lookup env a);
   207     in if T = T' then NONE else SOME (a, T') end);
   208 
   209 fun instT_thm thy env th =
   210   if Symtab.is_empty env then th
   211   else
   212     let val subst = instT_subst env th
   213     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   214 
   215 fun instT_ctxt thy env =
   216   map_ctxt_values (instT_type env) (instT_term env) (instT_thm thy env);
   217 
   218 
   219 (* type and term instantiation *)
   220 
   221 fun inst_term (envT, env) =
   222   if Symtab.is_empty env then instT_term envT
   223   else
   224     let
   225       val instT = instT_type envT;
   226       fun inst (Const (x, T)) = Const (x, instT T)
   227         | inst (Free (x, T)) =
   228             (case Symtab.lookup env x of
   229               NONE => Free (x, instT T)
   230             | SOME t => t)
   231         | inst (Var (xi, T)) = Var (xi, instT T)
   232         | inst (b as Bound _) = b
   233         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   234         | inst (t $ u) = inst t $ inst u;
   235     in Envir.beta_norm o inst end;
   236 
   237 fun inst_thm thy (envT, env) th =
   238   if Symtab.is_empty env then instT_thm thy envT th
   239   else
   240     let
   241       val substT = instT_subst envT th;
   242       val subst = Drule.frees_of th
   243         |> List.mapPartial (fn (x, T) =>
   244           let
   245             val T' = instT_type envT T;
   246             val t = Free (x, T');
   247             val t' = the_default t (Symtab.lookup env x);
   248           in if t aconv t' then NONE else SOME ((x, T'), t') end);
   249     in
   250       if null substT andalso null subst then th
   251       else th |> hyps_rule
   252        (instantiate_tfrees thy substT #>
   253         instantiate_frees thy subst #>
   254         Drule.fconv_rule (Thm.beta_conversion true))
   255     end;
   256 
   257 fun inst_ctxt thy envs =
   258   map_ctxt_values (instT_type (#1 envs)) (inst_term envs) (inst_thm thy envs);
   259 
   260 
   261 
   262 (** concluding statements **)
   263 
   264 datatype ('typ, 'term) stmt =
   265   Shows of ((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list |
   266   Obtains of (string * ((string * 'typ option) list * 'term list)) list;
   267 
   268 type statement = (string, string) stmt;
   269 type statement_i = (typ, term) stmt;
   270 
   271 end;