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