src/Pure/Isar/element.ML
changeset 18140 691c64d615a5
child 18606 46e7fc90fde3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Isar/element.ML	Wed Nov 09 16:26:55 2005 +0100
     1.3 @@ -0,0 +1,257 @@
     1.4 +(*  Title:      Pure/Isar/element.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Makarius
     1.7 +
     1.8 +Explicit data structures for some Isar language elements.
     1.9 +*)
    1.10 +
    1.11 +signature ELEMENT =
    1.12 +sig
    1.13 +  datatype ('typ, 'term, 'fact) ctxt =
    1.14 +    Fixes of (string * 'typ option * mixfix option) list |
    1.15 +    Constrains of (string * 'typ) list |
    1.16 +    Assumes of ((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list |
    1.17 +    Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
    1.18 +    Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list
    1.19 +  type context (*= (string, string, thmref) ctxt*)
    1.20 +  type context_i (*= (typ, term, thm list) ctxt*)
    1.21 +  val map_ctxt: {name: string -> string,
    1.22 +    var: string * mixfix option -> string * mixfix option,
    1.23 +    typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c,
    1.24 +    attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
    1.25 +  val map_ctxt_values: (typ -> typ) -> (term -> term) -> (thm -> thm) -> context_i -> context_i
    1.26 +  val pretty_ctxt: ProofContext.context -> context_i -> Pretty.T list
    1.27 +  val rename: (string * (string * mixfix option)) list -> string -> string
    1.28 +  val rename_var: (string * (string * mixfix option)) list ->
    1.29 +   string * mixfix option -> string * mixfix option
    1.30 +  val rename_term: (string * (string * mixfix option)) list -> term -> term
    1.31 +  val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
    1.32 +  val rename_ctxt: (string * (string * mixfix option)) list -> context_i -> context_i
    1.33 +  val instT_type: typ Symtab.table -> typ -> typ
    1.34 +  val instT_term: typ Symtab.table -> term -> term
    1.35 +  val instT_thm: theory -> typ Symtab.table -> thm -> thm
    1.36 +  val instT_ctxt: theory -> typ Symtab.table -> context_i -> context_i
    1.37 +  val inst_term: typ Symtab.table * term Symtab.table -> term -> term
    1.38 +  val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
    1.39 +  val inst_ctxt: theory -> typ Symtab.table * term Symtab.table -> context_i -> context_i
    1.40 +end;
    1.41 +
    1.42 +structure Element: ELEMENT =
    1.43 +struct
    1.44 +
    1.45 +(** context elements **)
    1.46 +
    1.47 +(* datatype ctxt *)
    1.48 +
    1.49 +datatype ('typ, 'term, 'fact) ctxt =
    1.50 +  Fixes of (string * 'typ option * mixfix option) list |
    1.51 +  Constrains of (string * 'typ) list |
    1.52 +  Assumes of ((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list |
    1.53 +  Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
    1.54 +  Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
    1.55 +
    1.56 +type context = (string, string, thmref) ctxt;
    1.57 +type context_i = (typ, term, thm list) ctxt;
    1.58 +
    1.59 +fun map_ctxt {name, var, typ, term, fact, attrib} =
    1.60 +  fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) =>
    1.61 +       let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end))
    1.62 +   | Constrains xs => Constrains (xs |> map (fn (x, T) =>
    1.63 +       (#1 (var (x, SOME Syntax.NoSyn)), typ T)))
    1.64 +   | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
    1.65 +      ((name a, map attrib atts), propps |> map (fn (t, (ps, qs)) =>
    1.66 +        (term t, (map term ps, map term qs))))))
    1.67 +   | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
    1.68 +      ((name a, map attrib atts), (term t, map term ps))))
    1.69 +   | Notes facts => Notes (facts |> map (fn ((a, atts), bs) =>
    1.70 +      ((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
    1.71 +
    1.72 +fun map_ctxt_values typ term thm = map_ctxt
    1.73 +  {name = I, var = I, typ = typ, term = term, fact = map thm,
    1.74 +    attrib = Args.map_values I typ term thm};
    1.75 +
    1.76 +
    1.77 +(* pretty_ctxt *)
    1.78 +
    1.79 +fun pretty_ctxt ctxt =
    1.80 +  let
    1.81 +    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
    1.82 +    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
    1.83 +    val prt_thm = Pretty.quote o ProofContext.pretty_thm ctxt;
    1.84 +    val prt_atts = Args.pretty_attribs ctxt;
    1.85 +
    1.86 +    fun prt_syn syn =
    1.87 +      let val s = (case syn of NONE => "(structure)" | SOME mx => Syntax.string_of_mixfix mx)
    1.88 +      in if s = "" then [] else [Pretty.brk 2, Pretty.str s] end;
    1.89 +    fun prt_fix (x, SOME T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
    1.90 +          prt_typ T :: Pretty.brk 1 :: prt_syn syn)
    1.91 +      | prt_fix (x, NONE, syn) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_syn syn);
    1.92 +    fun prt_constrain (x, T) = prt_fix (x, SOME T, SOME (Syntax.NoSyn));
    1.93 +
    1.94 +    fun prt_name name = Pretty.str (ProofContext.extern_thm ctxt name);
    1.95 +    fun prt_name_atts (name, atts) =
    1.96 +      if name = "" andalso null atts then []
    1.97 +      else [Pretty.block (Pretty.breaks (prt_name name :: prt_atts atts @ [Pretty.str ":"]))];
    1.98 +
    1.99 +    fun prt_asm (a, ts) =
   1.100 +      Pretty.block (Pretty.breaks (prt_name_atts a @ map (prt_term o fst) ts));
   1.101 +    fun prt_def (a, (t, _)) =
   1.102 +      Pretty.block (Pretty.breaks (prt_name_atts a @ [prt_term t]));
   1.103 +
   1.104 +    fun prt_fact (ths, []) = map prt_thm ths
   1.105 +      | prt_fact (ths, atts) =
   1.106 +          Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths)) :: prt_atts atts;
   1.107 +    fun prt_note (a, ths) =
   1.108 +      Pretty.block (Pretty.breaks (List.concat (prt_name_atts a :: map prt_fact ths)));
   1.109 +
   1.110 +    fun items _ [] = []
   1.111 +      | items prfx (x :: xs) =
   1.112 +          Pretty.block [Pretty.str prfx, Pretty.brk 1, x] :: items "  and" xs;
   1.113 +  in
   1.114 +    fn Fixes fixes => items "fixes" (map prt_fix fixes)
   1.115 +     | Constrains xs => items "constrains" (map prt_constrain xs)
   1.116 +     | Assumes asms => items "assumes" (map prt_asm asms)
   1.117 +     | Defines defs => items "defines" (map prt_def defs)
   1.118 +     | Notes facts => items "notes" (map prt_note facts)
   1.119 +  end;
   1.120 +
   1.121 +
   1.122 +
   1.123 +(** logical operations **)
   1.124 +
   1.125 +(* derived rules *)
   1.126 +
   1.127 +fun instantiate_tfrees thy subst =
   1.128 +  let
   1.129 +    val certT = Thm.ctyp_of thy;
   1.130 +    fun inst vs (a, T) = AList.lookup (op =) vs a
   1.131 +      |> Option.map (fn v => (certT (TVar v), certT T));
   1.132 +  in
   1.133 +    Drule.tvars_intr_list (map fst subst) #->
   1.134 +    (fn vs => Thm.instantiate (List.mapPartial (inst vs) subst, []))
   1.135 +  end;
   1.136 +
   1.137 +fun instantiate_frees thy subst =
   1.138 +  let val cert = Thm.cterm_of thy in
   1.139 +    Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   1.140 +    Drule.forall_elim_list (map (cert o snd) subst)
   1.141 +  end;
   1.142 +
   1.143 +fun hyps_rule rule th =
   1.144 +  let
   1.145 +    val cterm_rule = Thm.reflexive #> rule #> Thm.cprop_of #> Drule.dest_equals #> #1;
   1.146 +    val {hyps, ...} = Thm.crep_thm th;
   1.147 +  in
   1.148 +    Drule.implies_elim_list
   1.149 +      (rule (Drule.implies_intr_list hyps th))
   1.150 +      (map (Thm.assume o cterm_rule) hyps)
   1.151 +  end;
   1.152 +
   1.153 +
   1.154 +(* renaming *)
   1.155 +
   1.156 +fun rename ren x =
   1.157 +  (case AList.lookup (op =) ren (x: string) of
   1.158 +    NONE => x
   1.159 +  | SOME (x', _) => x');
   1.160 +
   1.161 +fun rename_var ren (x, mx) =
   1.162 +  (case (AList.lookup (op =) ren (x: string), is_some mx) of
   1.163 +    (NONE, _) => (x, mx)
   1.164 +  | (SOME (x', NONE), true) => (x', SOME Syntax.NoSyn)
   1.165 +  | (SOME (x', NONE), false) => (x', mx)
   1.166 +  | (SOME (x', SOME mx'), true) => (x', SOME mx')
   1.167 +  | (SOME (x', SOME _), false) =>
   1.168 +      error ("Attempt to change syntax of structure parameter " ^ quote x));
   1.169 +
   1.170 +fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
   1.171 +  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
   1.172 +  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
   1.173 +  | rename_term _ a = a;
   1.174 +
   1.175 +fun rename_thm ren th =
   1.176 +  let
   1.177 +    val subst = Drule.frees_of th
   1.178 +      |> List.mapPartial (fn (x, T) =>
   1.179 +        let val x' = rename ren x
   1.180 +        in if x = x' then NONE else SOME ((x, T), (Free (x', T))) end);
   1.181 +  in
   1.182 +    if null subst then th
   1.183 +    else th |> hyps_rule (instantiate_frees (Thm.theory_of_thm th) subst)
   1.184 +  end;
   1.185 +
   1.186 +fun rename_ctxt ren =
   1.187 +  map_ctxt_values I (rename_term ren) (rename_thm ren)
   1.188 +  #> map_ctxt {name = I, typ = I, term = I, fact = I, attrib = I, var = rename_var ren};
   1.189 +
   1.190 +
   1.191 +(* type instantiation *)
   1.192 +
   1.193 +fun instT_type env =
   1.194 +  if Symtab.is_empty env then I
   1.195 +  else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   1.196 +
   1.197 +fun instT_term env =
   1.198 +  if Symtab.is_empty env then I
   1.199 +  else Term.map_term_types (instT_type env);
   1.200 +
   1.201 +fun instT_subst env th =
   1.202 +  Drule.tfrees_of th
   1.203 +  |> List.mapPartial (fn (a, S) =>
   1.204 +    let
   1.205 +      val T = TFree (a, S);
   1.206 +      val T' = the_default T (Symtab.lookup env a);
   1.207 +    in if T = T' then NONE else SOME (a, T') end);
   1.208 +
   1.209 +fun instT_thm thy env th =
   1.210 +  if Symtab.is_empty env then th
   1.211 +  else
   1.212 +    let val subst = instT_subst env th
   1.213 +    in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   1.214 +
   1.215 +fun instT_ctxt thy env =
   1.216 +  map_ctxt_values (instT_type env) (instT_term env) (instT_thm thy env);
   1.217 +
   1.218 +
   1.219 +(* type and term instantiation *)
   1.220 +
   1.221 +fun inst_term (envT, env) =
   1.222 +  if Symtab.is_empty env then instT_term envT
   1.223 +  else
   1.224 +    let
   1.225 +      val instT = instT_type envT;
   1.226 +      fun inst (Const (x, T)) = Const (x, instT T)
   1.227 +        | inst (Free (x, T)) =
   1.228 +            (case Symtab.lookup env x of
   1.229 +              NONE => Free (x, instT T)
   1.230 +            | SOME t => t)
   1.231 +        | inst (Var (xi, T)) = Var (xi, instT T)
   1.232 +        | inst (b as Bound _) = b
   1.233 +        | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   1.234 +        | inst (t $ u) = inst t $ inst u;
   1.235 +    in Envir.beta_norm o inst end;
   1.236 +
   1.237 +fun inst_thm thy (envT, env) th =
   1.238 +  if Symtab.is_empty env then instT_thm thy envT th
   1.239 +  else
   1.240 +    let
   1.241 +      val substT = instT_subst envT th;
   1.242 +      val subst = Drule.frees_of th
   1.243 +        |> List.mapPartial (fn (x, T) =>
   1.244 +          let
   1.245 +            val T' = instT_type envT T;
   1.246 +            val t = Free (x, T');
   1.247 +            val t' = the_default t (Symtab.lookup env x);
   1.248 +          in if t aconv t' then NONE else SOME ((x, T'), t') end);
   1.249 +    in
   1.250 +      if null substT andalso null subst then th
   1.251 +      else th |> hyps_rule
   1.252 +       (instantiate_tfrees thy substT #>
   1.253 +        instantiate_frees thy subst #>
   1.254 +        Drule.fconv_rule (Thm.beta_conversion true))
   1.255 +    end;
   1.256 +
   1.257 +fun inst_ctxt thy envs =
   1.258 +  map_ctxt_values (instT_type (#1 envs)) (inst_term envs) (inst_thm thy envs);
   1.259 +
   1.260 +end;