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