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