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