src/Pure/Isar/proof_context.ML
author wenzelm
Sun Apr 17 19:38:53 2005 +0200 (2005-04-17 ago)
changeset 15758 07e382399a96
parent 15750 860c282e6ca6
child 15798 016f3be5a5ec
permissions -rw-r--r--
binds/thms: do not store options, but delete from table;
tuned;
wenzelm@5819
     1
(*  Title:      Pure/Isar/proof_context.ML
wenzelm@5819
     2
    ID:         $Id$
wenzelm@5819
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5819
     4
wenzelm@15758
     5
The key concept of Isar proof contexts.
wenzelm@5819
     6
*)
wenzelm@5819
     7
wenzelm@14707
     8
val show_structs = ref false;
wenzelm@14707
     9
wenzelm@5819
    10
signature PROOF_CONTEXT =
wenzelm@5819
    11
sig
wenzelm@5819
    12
  type context
wenzelm@9470
    13
  type exporter
wenzelm@5819
    14
  exception CONTEXT of string * context
wenzelm@5819
    15
  val theory_of: context -> theory
wenzelm@5819
    16
  val sign_of: context -> Sign.sg
wenzelm@14828
    17
  val is_fixed: context -> string -> bool
wenzelm@12057
    18
  val fixed_names_of: context -> string list
wenzelm@12057
    19
  val assumptions_of: context -> (cterm list * exporter) list
wenzelm@7557
    20
  val prems_of: context -> thm list
wenzelm@5819
    21
  val print_proof_data: theory -> unit
wenzelm@5874
    22
  val init: theory -> context
wenzelm@14828
    23
  val pretty_term: context -> term -> Pretty.T
wenzelm@14828
    24
  val pretty_typ: context -> typ -> Pretty.T
wenzelm@14828
    25
  val pretty_sort: context -> sort -> Pretty.T
wenzelm@14828
    26
  val pp: context -> Pretty.pp
wenzelm@14828
    27
  val pretty_thm: context -> thm -> Pretty.T
wenzelm@14828
    28
  val pretty_thms: context -> thm list -> Pretty.T
wenzelm@14828
    29
  val pretty_fact: context -> string * thm list -> Pretty.T
wenzelm@14828
    30
  val string_of_term: context -> term -> string
wenzelm@12414
    31
  val default_type: context -> string -> typ option
wenzelm@12504
    32
  val used_types: context -> string list
wenzelm@5819
    33
  val read_typ: context -> string -> typ
wenzelm@14780
    34
  val read_typ_raw: context -> string -> typ
wenzelm@5819
    35
  val cert_typ: context -> typ -> typ
wenzelm@14780
    36
  val cert_typ_raw: context -> typ -> typ
wenzelm@10583
    37
  val get_skolem: context -> string -> string
wenzelm@9133
    38
  val extern_skolem: context -> term -> term
wenzelm@14720
    39
  val read_termTs: context -> (string -> bool) -> (indexname -> typ option)
wenzelm@14720
    40
    -> (indexname -> sort option) -> string list -> (string * typ) list
wenzelm@14720
    41
    -> term list * (indexname * typ) list
wenzelm@14720
    42
  val read_termTs_schematic: context -> (string -> bool) -> (indexname -> typ option)
wenzelm@14720
    43
    -> (indexname -> sort option) -> string list -> (string * typ) list
wenzelm@14720
    44
    -> term list * (indexname * typ) list
wenzelm@14720
    45
  val read_term_liberal: context -> string -> term
wenzelm@5819
    46
  val read_term: context -> string -> term
wenzelm@5819
    47
  val read_prop: context -> string -> term
wenzelm@11925
    48
  val read_prop_schematic: context -> string -> term
wenzelm@10465
    49
  val read_terms: context -> string list -> term list
wenzelm@8096
    50
  val read_term_pats: typ -> context -> string list -> term list
wenzelm@8096
    51
  val read_prop_pats: context -> string list -> term list
wenzelm@5819
    52
  val cert_term: context -> term -> term
wenzelm@5819
    53
  val cert_prop: context -> term -> term
wenzelm@8096
    54
  val cert_term_pats: typ -> context -> term list -> term list
wenzelm@8096
    55
  val cert_prop_pats: context -> term list -> term list
wenzelm@5819
    56
  val declare_term: term -> context -> context
wenzelm@5819
    57
  val declare_terms: term list -> context -> context
wenzelm@15703
    58
  val read_tyname: context -> string -> typ
wenzelm@15703
    59
  val read_const: context -> string -> term
wenzelm@7925
    60
  val warn_extra_tfrees: context -> context -> context
wenzelm@12550
    61
  val generalize: context -> context -> term list -> term list
wenzelm@9470
    62
  val find_free: term -> string -> term option
wenzelm@12008
    63
  val export: bool -> context -> context -> thm -> thm Seq.seq
wenzelm@13415
    64
  val export_standard: cterm list -> context -> context -> thm -> thm
ballarin@15696
    65
  val export_plain: cterm list -> context -> context -> thm -> thm
wenzelm@10810
    66
  val drop_schematic: indexname * term option -> indexname * term option
wenzelm@10810
    67
  val add_binds: (indexname * string option) list -> context -> context
wenzelm@10810
    68
  val add_binds_i: (indexname * term option) list -> context -> context
wenzelm@12147
    69
  val auto_bind_goal: term list -> context -> context
wenzelm@12147
    70
  val auto_bind_facts: term list -> context -> context
wenzelm@8616
    71
  val match_bind: bool -> (string list * string) list -> context -> context
wenzelm@8616
    72
  val match_bind_i: bool -> (term list * term) list -> context -> context
wenzelm@10465
    73
  val read_propp: context * (string * (string list * string list)) list list
wenzelm@10465
    74
    -> context * (term * (term list * term list)) list list
wenzelm@10465
    75
  val cert_propp: context * (term * (term list * term list)) list list
wenzelm@10465
    76
    -> context * (term * (term list * term list)) list list
wenzelm@10554
    77
  val read_propp_schematic: context * (string * (string list * string list)) list list
wenzelm@10554
    78
    -> context * (term * (term list * term list)) list list
wenzelm@10554
    79
  val cert_propp_schematic: context * (term * (term list * term list)) list list
wenzelm@10554
    80
    -> context * (term * (term list * term list)) list list
wenzelm@10465
    81
  val bind_propp: context * (string * (string list * string list)) list list
wenzelm@10465
    82
    -> context * (term list list * (context -> context))
wenzelm@10465
    83
  val bind_propp_i: context * (term * (term list * term list)) list list
wenzelm@10465
    84
    -> context * (term list list * (context -> context))
wenzelm@10554
    85
  val bind_propp_schematic: context * (string * (string list * string list)) list list
wenzelm@10554
    86
    -> context * (term list list * (context -> context))
wenzelm@10554
    87
  val bind_propp_schematic_i: context * (term * (term list * term list)) list list
wenzelm@10554
    88
    -> context * (term list list * (context -> context))
berghofe@15456
    89
  val get_thm: context -> thmref -> thm
berghofe@15456
    90
  val get_thm_closure: context -> thmref -> thm
berghofe@15456
    91
  val get_thms: context -> thmref -> thm list
berghofe@15456
    92
  val get_thms_closure: context -> thmref -> thm list
wenzelm@12309
    93
  val cond_extern: context -> string -> xstring
wenzelm@13399
    94
  val qualified: bool -> context -> context
wenzelm@13399
    95
  val restore_qualified: context -> context -> context
wenzelm@13425
    96
  val hide_thms: bool -> string list -> context -> context
wenzelm@6091
    97
  val put_thm: string * thm -> context -> context
wenzelm@6091
    98
  val put_thms: string * thm list -> context -> context
wenzelm@6091
    99
  val put_thmss: (string * thm list) list -> context -> context
wenzelm@7606
   100
  val reset_thms: string -> context -> context
wenzelm@14564
   101
  val note_thmss:
ballarin@15696
   102
    ((bstring * context attribute list) *
ballarin@15696
   103
      (thmref * context attribute list) list) list ->
ballarin@15696
   104
    context -> context * (bstring * thm list) list
wenzelm@14564
   105
  val note_thmss_i:
ballarin@15696
   106
    ((bstring * context attribute list) *
ballarin@15696
   107
      (thm list * context attribute list) list) list ->
ballarin@15696
   108
    context -> context * (bstring * thm list) list
ballarin@15696
   109
  val note_thmss_accesses:
ballarin@15696
   110
    (string -> string list) ->
ballarin@15696
   111
    ((bstring * context attribute list) *
ballarin@15696
   112
      (thmref * context attribute list) list) list ->
ballarin@15696
   113
    context -> context * (bstring * thm list) list
ballarin@15696
   114
  val note_thmss_accesses_i:
ballarin@15696
   115
    (string -> string list) ->
ballarin@15696
   116
    ((bstring * context attribute list) *
ballarin@15696
   117
      (thm list * context attribute list) list) list ->
ballarin@15696
   118
    context -> context * (bstring * thm list) list
wenzelm@11918
   119
  val export_assume: exporter
wenzelm@11918
   120
  val export_presume: exporter
wenzelm@12086
   121
  val cert_def: context -> term -> string * term
wenzelm@11918
   122
  val export_def: exporter
wenzelm@9470
   123
  val assume: exporter
wenzelm@10465
   124
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@12835
   125
    -> context -> context * (bstring * thm list) list
wenzelm@9470
   126
  val assume_i: exporter
wenzelm@10465
   127
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@12835
   128
    -> context -> context * (bstring * thm list) list
wenzelm@14720
   129
  val read_vars: context * (string list * string option)
wenzelm@14720
   130
    -> context * (string list * typ option)
wenzelm@14720
   131
  val cert_vars: context * (string list * typ option)
wenzelm@14720
   132
    -> context * (string list * typ option)
wenzelm@14720
   133
  val read_vars_liberal: context * (string list * string option)
wenzelm@14720
   134
    -> context * (string list * typ option)
wenzelm@14720
   135
  val cert_vars_liberal: context * (string list * typ option)
wenzelm@14720
   136
    -> context * (string list * typ option)
wenzelm@7411
   137
  val fix: (string list * string option) list -> context -> context
wenzelm@7663
   138
  val fix_i: (string list * typ option) list -> context -> context
wenzelm@14643
   139
  val add_fixes: (string * typ option * Syntax.mixfix option) list -> context -> context
wenzelm@14720
   140
  val add_fixes_liberal: (string * typ option * Syntax.mixfix option) list -> context -> context
wenzelm@12016
   141
  val fix_frees: term list -> context -> context
wenzelm@9291
   142
  val bind_skolem: context -> string list -> term -> term
wenzelm@11793
   143
  val get_case: context -> string -> string option list -> RuleCases.T
wenzelm@8373
   144
  val add_cases: (string * RuleCases.T) list -> context -> context
wenzelm@13425
   145
  val apply_case: RuleCases.T -> context
wenzelm@13425
   146
    -> context * ((indexname * term option) list * (string * term list) list)
wenzelm@10810
   147
  val verbose: bool ref
wenzelm@10810
   148
  val setmp_verbose: ('a -> 'b) -> 'a -> 'b
wenzelm@12072
   149
  val print_syntax: context -> unit
wenzelm@10810
   150
  val print_binds: context -> unit
wenzelm@12057
   151
  val print_lthms: context -> unit
wenzelm@10810
   152
  val print_cases: context -> unit
wenzelm@10810
   153
  val prems_limit: int ref
wenzelm@12057
   154
  val pretty_asms: context -> Pretty.T list
wenzelm@10810
   155
  val pretty_context: context -> Pretty.T list
wenzelm@13284
   156
  val thms_containing_limit: int ref
wenzelm@13284
   157
  val print_thms_containing: context -> int option -> string list -> unit
wenzelm@5819
   158
  val setup: (theory -> theory) list
wenzelm@5819
   159
end;
wenzelm@5819
   160
wenzelm@8151
   161
signature PRIVATE_PROOF_CONTEXT =
wenzelm@5819
   162
sig
wenzelm@5819
   163
  include PROOF_CONTEXT
wenzelm@5819
   164
  val init_data: Object.kind -> (theory -> Object.T) * (context -> Object.T -> unit)
wenzelm@5819
   165
    -> theory -> theory
wenzelm@5819
   166
  val print_data: Object.kind -> context -> unit
wenzelm@5819
   167
  val get_data: Object.kind -> (Object.T -> 'a) -> context -> 'a
wenzelm@5819
   168
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> context -> context
wenzelm@5819
   169
end;
wenzelm@5819
   170
wenzelm@8151
   171
structure ProofContext: PRIVATE_PROOF_CONTEXT =
wenzelm@5819
   172
struct
wenzelm@5819
   173
wenzelm@5819
   174
wenzelm@5819
   175
(** datatype context **)
wenzelm@5819
   176
wenzelm@11816
   177
type exporter = bool -> cterm list -> thm -> thm Seq.seq;
wenzelm@9470
   178
wenzelm@5819
   179
datatype context =
wenzelm@5819
   180
  Context of
wenzelm@12309
   181
   {thy: theory,                                              (*current theory*)
wenzelm@12309
   182
    syntax: Syntax.syntax * string list * string list,        (*syntax with structs and mixfixed*)
wenzelm@12309
   183
    data: Object.T Symtab.table,                              (*user data*)
wenzelm@5819
   184
    asms:
wenzelm@12309
   185
      ((cterm list * exporter) list *                         (*assumes: A ==> _*)
wenzelm@14697
   186
        (string * thm list) list) *                           (*prems: A |- A *)
wenzelm@12309
   187
      (string * string) list,                                 (*fixes: !!x. _*)
wenzelm@15758
   188
    binds: (term * typ) Vartab.table,                         (*term bindings*)
wenzelm@15758
   189
    thms: bool * NameSpace.T * thm list Symtab.table
wenzelm@13278
   190
      * FactIndex.T,                                          (*local thms*)
wenzelm@14697
   191
    (*thms is of the form (q, n, t, i) where
ballarin@14508
   192
       q: indicates whether theorems with qualified names may be stored;
ballarin@14508
   193
          this is initially forbidden (false); flag may be changed with
ballarin@14508
   194
          qualified and restore_qualified;
ballarin@14508
   195
       n: theorem namespace;
ballarin@14508
   196
       t: table of theorems;
wenzelm@14697
   197
       i: index for theorem lookup (cf. thms_containing) *)
wenzelm@12309
   198
    cases: (string * RuleCases.T) list,                       (*local contexts*)
wenzelm@5819
   199
    defs:
wenzelm@12309
   200
      typ Vartab.table *                                      (*type constraints*)
wenzelm@12309
   201
      sort Vartab.table *                                     (*default sorts*)
wenzelm@12309
   202
      string list *                                           (*used type variables*)
ballarin@15735
   203
      term list Symtab.table};                                (*type variable occurrences*)
wenzelm@5819
   204
wenzelm@5819
   205
exception CONTEXT of string * context;
wenzelm@5819
   206
wenzelm@5819
   207
ballarin@15735
   208
fun make_context (thy, syntax, data, asms, binds, thms, cases, defs) =
wenzelm@12072
   209
  Context {thy = thy, syntax = syntax, data = data, asms = asms, binds = binds,
ballarin@15735
   210
    thms = thms, cases = cases, defs = defs};
wenzelm@5819
   211
ballarin@15735
   212
fun map_context f (Context {thy, syntax, data, asms, binds, thms, cases, defs}) =
ballarin@15735
   213
  make_context (f (thy, syntax, data, asms, binds, thms, cases, defs));
wenzelm@5819
   214
wenzelm@5819
   215
fun theory_of (Context {thy, ...}) = thy;
wenzelm@5819
   216
val sign_of = Theory.sign_of o theory_of;
wenzelm@12072
   217
fun syntax_of (Context {syntax, ...}) = syntax;
wenzelm@5819
   218
wenzelm@12309
   219
fun fixes_of (Context {asms = (_, fixes), ...}) = fixes;
wenzelm@12057
   220
val fixed_names_of = map #2 o fixes_of;
wenzelm@12309
   221
fun is_fixed ctxt x = exists (equal x o #2) (fixes_of ctxt);
wenzelm@13278
   222
fun is_known (ctxt as Context {defs = (types, _, _, _), ...}) x =
skalberg@15570
   223
  isSome (Vartab.lookup (types, (x, ~1))) orelse is_fixed ctxt x;
wenzelm@12291
   224
fun type_occs (Context {defs = (_, _, _, tab), ...}) = tab;
wenzelm@12057
   225
wenzelm@12057
   226
fun assumptions_of (Context {asms = ((asms, _), _), ...}) = asms;
skalberg@15570
   227
fun prems_of (Context {asms = ((_, prems), _), ...}) = List.concat (map #2 prems);
wenzelm@12057
   228
wenzelm@7270
   229
wenzelm@5819
   230
wenzelm@5819
   231
(** user data **)
wenzelm@5819
   232
wenzelm@5819
   233
(* errors *)
wenzelm@5819
   234
wenzelm@5819
   235
fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy);
wenzelm@5819
   236
wenzelm@5819
   237
fun err_inconsistent kinds =
wenzelm@5819
   238
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " proof data");
wenzelm@5819
   239
wenzelm@5819
   240
fun err_dup_init thy kind =
wenzelm@5819
   241
  error ("Duplicate initialization of " ^ quote kind ^ " proof data" ^ of_theory thy);
wenzelm@5819
   242
wenzelm@5819
   243
fun err_undef ctxt kind =
wenzelm@5819
   244
  raise CONTEXT ("Tried to access undefined " ^ quote kind ^ " proof data", ctxt);
wenzelm@5819
   245
wenzelm@5819
   246
fun err_uninit ctxt kind =
wenzelm@5819
   247
  raise CONTEXT ("Tried to access uninitialized " ^ quote kind ^ " proof data" ^
wenzelm@5819
   248
    of_theory (theory_of ctxt), ctxt);
wenzelm@5819
   249
wenzelm@5819
   250
fun err_access ctxt kind =
wenzelm@5819
   251
  raise CONTEXT ("Unauthorized access to " ^ quote kind ^ " proof data" ^
wenzelm@5819
   252
    of_theory (theory_of ctxt), ctxt);
wenzelm@5819
   253
wenzelm@5819
   254
wenzelm@5819
   255
(* data kind 'Isar/proof_data' *)
wenzelm@5819
   256
wenzelm@5819
   257
structure ProofDataDataArgs =
wenzelm@5819
   258
struct
wenzelm@5819
   259
  val name = "Isar/proof_data";
wenzelm@5819
   260
  type T = (Object.kind * ((theory -> Object.T) * (context -> Object.T -> unit))) Symtab.table;
wenzelm@5819
   261
wenzelm@5819
   262
  val empty = Symtab.empty;
wenzelm@6550
   263
  val copy = I;
wenzelm@5819
   264
  val prep_ext = I;
wenzelm@5819
   265
  fun merge tabs = Symtab.merge (Object.eq_kind o pairself fst) tabs
wenzelm@5819
   266
    handle Symtab.DUPS kinds => err_inconsistent kinds;
wenzelm@5819
   267
  fun print _ tab = Pretty.writeln (Pretty.strs (map #1 (Symtab.dest tab)));
wenzelm@5819
   268
end;
wenzelm@5819
   269
wenzelm@5819
   270
structure ProofDataData = TheoryDataFun(ProofDataDataArgs);
wenzelm@5819
   271
val print_proof_data = ProofDataData.print;
wenzelm@5819
   272
wenzelm@5819
   273
wenzelm@5819
   274
(* init proof data *)
wenzelm@5819
   275
wenzelm@5819
   276
fun init_data kind meths thy =
wenzelm@5819
   277
  let
wenzelm@5819
   278
    val name = Object.name_of_kind kind;
wenzelm@5819
   279
    val tab = Symtab.update_new ((name, (kind, meths)), ProofDataData.get thy)
wenzelm@5819
   280
      handle Symtab.DUP _ => err_dup_init thy name;
wenzelm@5819
   281
  in thy |> ProofDataData.put tab end;
wenzelm@5819
   282
wenzelm@5819
   283
wenzelm@5819
   284
(* access data *)
wenzelm@5819
   285
wenzelm@5819
   286
fun lookup_data (ctxt as Context {data, ...}) kind =
wenzelm@5819
   287
  let
wenzelm@5819
   288
    val thy = theory_of ctxt;
wenzelm@5819
   289
    val name = Object.name_of_kind kind;
wenzelm@5819
   290
  in
wenzelm@5819
   291
    (case Symtab.lookup (ProofDataData.get thy, name) of
skalberg@15531
   292
      SOME (k, meths) =>
wenzelm@5819
   293
        if Object.eq_kind (kind, k) then
wenzelm@5819
   294
          (case Symtab.lookup (data, name) of
skalberg@15531
   295
            SOME x => (x, meths)
skalberg@15531
   296
          | NONE => err_undef ctxt name)
wenzelm@5819
   297
        else err_access ctxt name
skalberg@15531
   298
    | NONE => err_uninit ctxt name)
wenzelm@5819
   299
  end;
wenzelm@5819
   300
wenzelm@5819
   301
fun get_data kind f ctxt =
wenzelm@5819
   302
  let val (x, _) = lookup_data ctxt kind
wenzelm@5819
   303
  in f x handle Match => Object.kind_error kind end;
wenzelm@5819
   304
wenzelm@5819
   305
fun print_data kind ctxt =
wenzelm@5819
   306
  let val (x, (_, prt)) = lookup_data ctxt kind
wenzelm@5819
   307
  in prt ctxt x end;
wenzelm@5819
   308
wenzelm@5819
   309
fun put_data kind f x ctxt =
wenzelm@8373
   310
 (lookup_data ctxt kind;
ballarin@15735
   311
  map_context (fn (thy, syntax, data, asms, binds, thms, cases, defs) =>
wenzelm@12072
   312
    (thy, syntax, Symtab.update ((Object.name_of_kind kind, f x), data),
ballarin@15735
   313
      asms, binds, thms, cases, defs)) ctxt);
wenzelm@5819
   314
wenzelm@5819
   315
wenzelm@5819
   316
(* init context *)
wenzelm@5819
   317
wenzelm@5874
   318
fun init thy =
wenzelm@5874
   319
  let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in
wenzelm@12309
   320
    make_context (thy, (Theory.syn_of thy, [], []), data, (([], []), []), Vartab.empty,
wenzelm@13278
   321
      (false, NameSpace.empty, Symtab.empty, FactIndex.empty), [],
ballarin@15735
   322
      (Vartab.empty, Vartab.empty, [], Symtab.empty))
wenzelm@5819
   323
  end;
wenzelm@5819
   324
wenzelm@5819
   325
wenzelm@8096
   326
wenzelm@12093
   327
(** local syntax **)
wenzelm@12093
   328
wenzelm@12093
   329
val fixedN = "\\<^fixed>";
wenzelm@12093
   330
val structN = "\\<^struct>";
wenzelm@12093
   331
wenzelm@12100
   332
wenzelm@14697
   333
(* translation functions *)
wenzelm@14665
   334
wenzelm@14665
   335
fun fixed_tr x = (fixedN ^ x, curry Term.list_comb (Syntax.free x));
wenzelm@14665
   336
wenzelm@12100
   337
fun context_tr' ctxt =
wenzelm@12100
   338
  let
wenzelm@12100
   339
    val (_, structs, mixfixed) = syntax_of ctxt;
wenzelm@12100
   340
wenzelm@12100
   341
    fun tr' (t $ u) = tr' t $ tr' u
wenzelm@12100
   342
      | tr' (Abs (x, T, t)) = Abs (x, T, tr' t)
wenzelm@12100
   343
      | tr' (t as Free (x, T)) =
wenzelm@14697
   344
          let val i = Library.find_index_eq x structs + 1 in
wenzelm@14697
   345
            if i = 0 andalso x mem_string mixfixed then Const (fixedN ^ x, T)
wenzelm@14707
   346
            else if i = 1 andalso not (! show_structs) then
wenzelm@14707
   347
              Syntax.const "_struct" $ Syntax.const "_indexdefault"
wenzelm@12100
   348
            else t
wenzelm@12100
   349
          end
wenzelm@12100
   350
      | tr' a = a;
wenzelm@12100
   351
  in tr' end;
wenzelm@12100
   352
wenzelm@12100
   353
wenzelm@12576
   354
(* add syntax *)
wenzelm@12576
   355
wenzelm@12576
   356
fun mixfix_type mx = replicate (Syntax.mixfix_args mx) TypeInfer.logicT ---> TypeInfer.logicT;
wenzelm@12093
   357
wenzelm@12093
   358
local
wenzelm@12093
   359
skalberg@15531
   360
fun mixfix x NONE mx = (fixedN ^ x, mixfix_type mx, Syntax.fix_mixfix x mx)
skalberg@15531
   361
  | mixfix x (SOME T) mx = (fixedN ^ x, T, Syntax.fix_mixfix x mx);
wenzelm@12093
   362
skalberg@15531
   363
fun prep_mixfix (_, _, NONE) = NONE
skalberg@15531
   364
  | prep_mixfix (x, opt_T, SOME mx) = SOME (mixfix x opt_T mx);
wenzelm@12093
   365
skalberg@15531
   366
fun prep_mixfix' (_, _, NONE) = NONE
skalberg@15531
   367
  | prep_mixfix' (x, _, SOME Syntax.NoSyn) = NONE
skalberg@15531
   368
  | prep_mixfix' (x, opt_T, _) = SOME (x, mixfix x opt_T (Syntax.literal x));
wenzelm@12093
   369
skalberg@15531
   370
fun prep_struct (x, _, NONE) = SOME x
skalberg@15531
   371
  | prep_struct _ = NONE;
wenzelm@12093
   372
wenzelm@15750
   373
fun mk trs = map Syntax.mk_trfun trs;
wenzelm@15750
   374
wenzelm@12093
   375
in
wenzelm@12093
   376
wenzelm@12093
   377
fun add_syntax decls =
ballarin@15735
   378
  map_context (fn (thy, (syn, structs, mixfixed), data, asms, binds, thms, cases, defs) =>
wenzelm@12093
   379
    let
wenzelm@14901
   380
      val is_logtype = Sign.is_logtype (Theory.sign_of thy);
skalberg@15570
   381
      val structs' = structs @ List.mapPartial prep_struct decls;
skalberg@15570
   382
      val mxs = List.mapPartial prep_mixfix decls;
skalberg@15570
   383
      val (fixed, mxs_output) = Library.split_list (List.mapPartial prep_mixfix' decls);
wenzelm@12093
   384
      val trs = map fixed_tr fixed;
wenzelm@12093
   385
      val syn' = syn
wenzelm@14901
   386
        |> Syntax.extend_const_gram is_logtype ("", false) mxs_output
wenzelm@14901
   387
        |> Syntax.extend_const_gram is_logtype ("", true) mxs
wenzelm@15750
   388
        |> Syntax.extend_trfuns ([], mk trs, [], []);
wenzelm@15750
   389
    in (thy, (syn', structs', fixed @ mixfixed), data, asms, binds, thms, cases, defs) end);
wenzelm@12093
   390
wenzelm@12093
   391
fun syn_of (Context {syntax = (syn, structs, _), ...}) =
wenzelm@15750
   392
  let val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs
wenzelm@15750
   393
  in syn |> Syntax.extend_trfuns (mk atrs, mk trs, mk trs', mk atrs') end;
wenzelm@14697
   394
wenzelm@12093
   395
end;
wenzelm@12093
   396
wenzelm@12093
   397
wenzelm@12093
   398
wenzelm@14828
   399
(** pretty printing **)
wenzelm@14828
   400
wenzelm@14974
   401
fun pretty_term ctxt t = Sign.pretty_term' (syn_of ctxt) (sign_of ctxt) (context_tr' ctxt t);
wenzelm@14974
   402
fun pretty_typ ctxt T = Sign.pretty_typ (sign_of ctxt) T;
wenzelm@14974
   403
fun pretty_sort ctxt S = Sign.pretty_sort (sign_of ctxt) S;
wenzelm@14974
   404
fun pretty_classrel ctxt cs = Sign.pretty_classrel (sign_of ctxt) cs;
wenzelm@14974
   405
fun pretty_arity ctxt ar = Sign.pretty_arity (sign_of ctxt) ar;
wenzelm@14828
   406
wenzelm@14974
   407
fun pp ctxt = Pretty.pp (pretty_term ctxt, pretty_typ ctxt, pretty_sort ctxt,
wenzelm@14974
   408
  pretty_classrel ctxt, pretty_arity ctxt);
wenzelm@14828
   409
wenzelm@14828
   410
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@14828
   411
wenzelm@14828
   412
fun pretty_thm ctxt thm =
wenzelm@14828
   413
  if ! Display.show_hyps then
wenzelm@14876
   414
    Display.pretty_thm_aux (pp ctxt) false thm
wenzelm@14828
   415
  else pretty_term ctxt (Thm.prop_of thm);
wenzelm@14828
   416
wenzelm@14828
   417
fun pretty_thms ctxt [th] = pretty_thm ctxt th
wenzelm@14828
   418
  | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths));
wenzelm@14828
   419
wenzelm@14828
   420
fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths
wenzelm@14828
   421
  | pretty_fact ctxt (a, [th]) =
wenzelm@14828
   422
      Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th]
wenzelm@14828
   423
  | pretty_fact ctxt (a, ths) =
wenzelm@14828
   424
      Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths));
wenzelm@14828
   425
wenzelm@14828
   426
wenzelm@14828
   427
wenzelm@7663
   428
(** default sorts and types **)
wenzelm@7663
   429
wenzelm@12291
   430
fun def_sort (Context {defs = (_, sorts, _, _), ...}) xi = Vartab.lookup (sorts, xi);
wenzelm@7663
   431
wenzelm@14720
   432
fun def_type (Context {binds, defs = (types, _, _, _), ...}) pattern xi =
wenzelm@7663
   433
  (case Vartab.lookup (types, xi) of
skalberg@15531
   434
    NONE =>
wenzelm@15758
   435
      if pattern then NONE
wenzelm@15758
   436
      else Vartab.lookup (binds, xi) |> Option.map (TypeInfer.polymorphicT o #2)
wenzelm@7663
   437
  | some => some);
wenzelm@7663
   438
wenzelm@12504
   439
fun default_type (Context {defs = (types, _, _, _), ...}) x = Vartab.lookup (types, (x, ~1));
wenzelm@12504
   440
fun used_types (Context {defs = (_, _, used, _), ...}) = used;
wenzelm@12414
   441
wenzelm@7663
   442
wenzelm@5819
   443
wenzelm@5819
   444
(** prepare types **)
wenzelm@5819
   445
wenzelm@9504
   446
local
wenzelm@9504
   447
wenzelm@9504
   448
fun read_typ_aux read ctxt s =
wenzelm@12093
   449
  transform_error (read (syn_of ctxt) (sign_of ctxt, def_sort ctxt)) s
wenzelm@7663
   450
    handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt);
wenzelm@5819
   451
wenzelm@10554
   452
fun cert_typ_aux cert ctxt raw_T =
wenzelm@10554
   453
  cert (sign_of ctxt) raw_T
wenzelm@10554
   454
    handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
wenzelm@9504
   455
wenzelm@9504
   456
in
wenzelm@9504
   457
wenzelm@14780
   458
val read_typ     = read_typ_aux Sign.read_typ';
wenzelm@14780
   459
val read_typ_raw = read_typ_aux Sign.read_typ_raw';
wenzelm@14780
   460
val cert_typ     = cert_typ_aux Sign.certify_typ;
wenzelm@14780
   461
val cert_typ_raw = cert_typ_aux Sign.certify_typ_raw;
wenzelm@9504
   462
wenzelm@9504
   463
end;
wenzelm@9504
   464
wenzelm@5819
   465
wenzelm@7679
   466
(* internalize Skolem constants *)
wenzelm@7679
   467
wenzelm@10583
   468
fun lookup_skolem ctxt x = assoc (fixes_of ctxt, x);
skalberg@15570
   469
fun get_skolem ctxt x = getOpt (lookup_skolem ctxt x, x);
wenzelm@7679
   470
wenzelm@12504
   471
fun no_skolem internal ctxt x =
wenzelm@9291
   472
  if can Syntax.dest_skolem x then
wenzelm@7679
   473
    raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
wenzelm@12504
   474
  else if not internal andalso can Syntax.dest_internal x then
wenzelm@9291
   475
    raise CONTEXT ("Illegal reference to internal variable: " ^ quote x, ctxt)
wenzelm@7679
   476
  else x;
wenzelm@7679
   477
wenzelm@14720
   478
fun intern_skolem ctxt internal =
wenzelm@7679
   479
  let
wenzelm@7679
   480
    fun intern (t as Free (x, T)) =
wenzelm@14720
   481
          if internal x then t
wenzelm@14720
   482
          else
wenzelm@14720
   483
            (case lookup_skolem ctxt (no_skolem false ctxt x) of
skalberg@15531
   484
              SOME x' => Free (x', T)
skalberg@15531
   485
            | NONE => t)
wenzelm@7679
   486
      | intern (t $ u) = intern t $ intern u
wenzelm@7679
   487
      | intern (Abs (x, T, t)) = Abs (x, T, intern t)
wenzelm@7679
   488
      | intern a = a;
wenzelm@7679
   489
  in intern end;
wenzelm@7679
   490
wenzelm@7679
   491
wenzelm@9133
   492
(* externalize Skolem constants -- for printing purposes only *)
wenzelm@9133
   493
wenzelm@9133
   494
fun extern_skolem ctxt =
wenzelm@9133
   495
  let
wenzelm@9133
   496
    val rev_fixes = map Library.swap (fixes_of ctxt);
wenzelm@9133
   497
wenzelm@9133
   498
    fun extern (t as Free (x, T)) =
wenzelm@9133
   499
          (case assoc (rev_fixes, x) of
skalberg@15531
   500
            SOME x' => Free (if lookup_skolem ctxt x' = SOME x then x' else NameSpace.hidden x', T)
skalberg@15531
   501
          | NONE => t)
wenzelm@9133
   502
      | extern (t $ u) = extern t $ extern u
wenzelm@9133
   503
      | extern (Abs (x, T, t)) = Abs (x, T, extern t)
wenzelm@9133
   504
      | extern a = a;
wenzelm@9133
   505
  in extern end
wenzelm@9133
   506
wenzelm@8096
   507
wenzelm@5819
   508
(** prepare terms and propositions **)
wenzelm@5819
   509
wenzelm@5819
   510
(*
wenzelm@5819
   511
  (1) read / certify wrt. signature of context
wenzelm@5819
   512
  (2) intern Skolem constants
wenzelm@5819
   513
  (3) expand term bindings
wenzelm@5819
   514
*)
wenzelm@5819
   515
wenzelm@5819
   516
wenzelm@5819
   517
(* read / certify wrt. signature *)     (*exception ERROR*) (*exception TERM*)
wenzelm@5819
   518
wenzelm@14828
   519
fun read_def_termTs freeze pp syn sg (types, sorts, used) sTs =
wenzelm@14901
   520
  Sign.read_def_terms' pp (Sign.is_logtype sg) syn (sg, types, sorts) used freeze sTs;
wenzelm@5874
   521
wenzelm@14828
   522
fun read_def_termT freeze pp syn sg defs sT =
wenzelm@14828
   523
  apfst hd (read_def_termTs freeze pp syn sg defs [sT]);
wenzelm@14828
   524
wenzelm@14828
   525
fun read_term_sg freeze pp syn sg defs s =
wenzelm@14828
   526
  #1 (read_def_termT freeze pp syn sg defs (s, TypeInfer.logicT));
wenzelm@5874
   527
wenzelm@14828
   528
fun read_prop_sg freeze pp syn sg defs s =
wenzelm@14828
   529
  #1 (read_def_termT freeze pp syn sg defs (s, propT));
wenzelm@12072
   530
wenzelm@14828
   531
fun read_terms_sg freeze pp syn sg defs =
wenzelm@14828
   532
  #1 o read_def_termTs freeze pp syn sg defs o map (rpair TypeInfer.logicT);
wenzelm@12072
   533
wenzelm@14828
   534
fun read_props_sg freeze pp syn sg defs =
wenzelm@14828
   535
  #1 o read_def_termTs freeze pp syn sg defs o map (rpair propT);
wenzelm@5819
   536
wenzelm@5819
   537
wenzelm@14828
   538
fun cert_term_sg pp sg t = #1 (Sign.certify_term pp sg t);
wenzelm@5819
   539
wenzelm@14828
   540
fun cert_prop_sg pp sg tm =
wenzelm@14828
   541
  let val (t, T, _) = Sign.certify_term pp sg tm
wenzelm@10465
   542
  in if T = propT then t else raise TERM ("Term not of type prop", [t]) end;
wenzelm@5819
   543
wenzelm@5819
   544
wenzelm@5819
   545
(* norm_term *)
wenzelm@5819
   546
wenzelm@5819
   547
(*beta normal form for terms (not eta normal form), chase variables in
wenzelm@5819
   548
  bindings environment (code taken from Pure/envir.ML)*)
wenzelm@5819
   549
wenzelm@8616
   550
fun unifyT ctxt (T, U) =
wenzelm@8616
   551
  let val maxidx = Int.max (Term.maxidx_of_typ T, Term.maxidx_of_typ U)
wenzelm@12530
   552
  in #1 (Type.unify (Sign.tsig_of (sign_of ctxt)) (Vartab.empty, maxidx) (T, U)) end;
wenzelm@8616
   553
wenzelm@14720
   554
fun norm_term (ctxt as Context {binds, ...}) schematic =
wenzelm@5819
   555
  let
wenzelm@5819
   556
    (*raised when norm has no effect on a term, to do sharing instead of copying*)
wenzelm@5819
   557
    exception SAME;
wenzelm@5819
   558
wenzelm@5819
   559
    fun norm (t as Var (xi, T)) =
wenzelm@5819
   560
          (case Vartab.lookup (binds, xi) of
wenzelm@15758
   561
            SOME (u, U) =>
wenzelm@8616
   562
              let
wenzelm@8616
   563
                val env = unifyT ctxt (T, U) handle Type.TUNIFY =>
ballarin@14174
   564
                  raise TYPE ("norm_term: ill-typed variable assignment", [T, U], [t, u]);
wenzelm@8616
   565
                val u' = Term.subst_TVars_Vartab env u;
wenzelm@8616
   566
              in norm u' handle SAME => u' end
wenzelm@15758
   567
          | NONE =>
wenzelm@10554
   568
            if schematic then raise SAME
wenzelm@10554
   569
            else raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt))
wenzelm@5819
   570
      | norm (Abs (a, T, body)) =  Abs (a, T, norm body)
wenzelm@5819
   571
      | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body))
wenzelm@5819
   572
      | norm (f $ t) =
wenzelm@5819
   573
          ((case norm f of
wenzelm@5819
   574
            Abs (_, _, body) => normh (subst_bound (t, body))
wenzelm@5819
   575
          | nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t)
wenzelm@5819
   576
      | norm _ =  raise SAME
wenzelm@5819
   577
    and normh t = norm t handle SAME => t
wenzelm@5819
   578
  in normh end;
wenzelm@5819
   579
wenzelm@5819
   580
wenzelm@6550
   581
(* dummy patterns *)
wenzelm@6550
   582
wenzelm@9540
   583
fun prepare_dummies t = #2 (Term.replace_dummy_patterns (1, t));
wenzelm@6762
   584
wenzelm@9540
   585
fun reject_dummies ctxt t = Term.no_dummy_patterns t
wenzelm@9540
   586
  handle TERM _ => raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt);
wenzelm@6550
   587
wenzelm@6550
   588
wenzelm@5819
   589
(* read terms *)
wenzelm@5819
   590
wenzelm@10554
   591
local
wenzelm@10554
   592
skalberg@15531
   593
fun append_env e1 e2 x = (case e2 x of NONE => e1 x | some => some);
wenzelm@14720
   594
wenzelm@14720
   595
fun gen_read' read app pattern dummies schematic
wenzelm@14720
   596
    ctxt internal more_types more_sorts more_used s =
ballarin@14174
   597
  let
wenzelm@14720
   598
    val types = append_env (def_type ctxt pattern) more_types;
wenzelm@14720
   599
    val sorts = append_env (def_sort ctxt) more_sorts;
wenzelm@14720
   600
    val used = used_types ctxt @ more_used;
ballarin@14174
   601
  in
wenzelm@14828
   602
    (transform_error (read (pp ctxt) (syn_of ctxt) (sign_of ctxt) (types, sorts, used)) s
ballarin@14174
   603
      handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
wenzelm@14720
   604
        | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
wenzelm@14720
   605
    |> app (intern_skolem ctxt internal)
wenzelm@14720
   606
    |> app (if pattern then I else norm_term ctxt schematic)
wenzelm@14720
   607
    |> app (if pattern then prepare_dummies else if dummies then I else reject_dummies ctxt)
wenzelm@14720
   608
  end;
wenzelm@14720
   609
wenzelm@14720
   610
fun gen_read read app pattern dummies schematic ctxt =
skalberg@15531
   611
  gen_read' read app pattern dummies schematic ctxt (K false) (K NONE) (K NONE) [];
wenzelm@14720
   612
wenzelm@10554
   613
in
wenzelm@10554
   614
wenzelm@14720
   615
val read_termTs           = gen_read' (read_def_termTs false) (apfst o map) false false false;
wenzelm@14720
   616
val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false false true;
wenzelm@8096
   617
wenzelm@14720
   618
fun read_term_pats T ctxt =
wenzelm@14720
   619
  #1 o gen_read (read_def_termTs false) (apfst o map) true false false ctxt o map (rpair T);
wenzelm@8096
   620
val read_prop_pats = read_term_pats propT;
wenzelm@8096
   621
wenzelm@14720
   622
fun read_term_liberal ctxt =
skalberg@15531
   623
  gen_read' (read_term_sg true) I false false false ctxt (K true) (K NONE) (K NONE) [];
wenzelm@14720
   624
wenzelm@14720
   625
val read_term              = gen_read (read_term_sg true) I false false false;
wenzelm@14720
   626
val read_term_dummies      = gen_read (read_term_sg true) I false true false;
wenzelm@14720
   627
val read_prop              = gen_read (read_prop_sg true) I false false false;
wenzelm@14720
   628
val read_prop_schematic    = gen_read (read_prop_sg true) I false false true;
wenzelm@14720
   629
val read_terms             = gen_read (read_terms_sg true) map false false false;
wenzelm@14720
   630
fun read_props schematic   = gen_read (read_props_sg true) map false false schematic;
wenzelm@5819
   631
wenzelm@10554
   632
end;
wenzelm@10554
   633
wenzelm@5819
   634
wenzelm@5819
   635
(* certify terms *)
wenzelm@5819
   636
wenzelm@10554
   637
local
wenzelm@10554
   638
wenzelm@14720
   639
fun gen_cert cert pattern schematic ctxt t = t
wenzelm@14720
   640
  |> (if pattern then I else norm_term ctxt schematic)
wenzelm@14828
   641
  |> (fn t' => cert (pp ctxt) (sign_of ctxt) t'
wenzelm@14828
   642
    handle TERM (msg, _) => raise CONTEXT (msg, ctxt));
wenzelm@5819
   643
wenzelm@10554
   644
in
wenzelm@8096
   645
wenzelm@10554
   646
val cert_term = gen_cert cert_term_sg false false;
wenzelm@10554
   647
val cert_prop = gen_cert cert_prop_sg false false;
wenzelm@10554
   648
val cert_props = map oo gen_cert cert_prop_sg false;
wenzelm@10554
   649
wenzelm@10554
   650
fun cert_term_pats _ = map o gen_cert cert_term_sg true false;
wenzelm@10554
   651
val cert_prop_pats = map o gen_cert cert_prop_sg true false;
wenzelm@10554
   652
wenzelm@10554
   653
end;
wenzelm@5819
   654
wenzelm@5819
   655
wenzelm@5819
   656
(* declare terms *)
wenzelm@5819
   657
wenzelm@10381
   658
local
wenzelm@10381
   659
wenzelm@5819
   660
val ins_types = foldl_aterms
wenzelm@5819
   661
  (fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types)
wenzelm@5819
   662
    | (types, Var v) => Vartab.update (v, types)
wenzelm@5819
   663
    | (types, _) => types);
wenzelm@5819
   664
wenzelm@5819
   665
val ins_sorts = foldl_types (foldl_atyps
wenzelm@5819
   666
  (fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts)
wenzelm@5819
   667
    | (sorts, TVar v) => Vartab.update (v, sorts)
wenzelm@5819
   668
    | (sorts, _) => sorts));
wenzelm@5819
   669
wenzelm@8616
   670
val ins_used = foldl_term_types (fn t => foldl_atyps
wenzelm@12504
   671
  (fn (used, TFree (x, _)) => x ins_string used
wenzelm@12504
   672
    | (used, _) => used));
wenzelm@12291
   673
wenzelm@12291
   674
val ins_occs = foldl_term_types (fn t => foldl_atyps
wenzelm@12291
   675
  (fn (tab, TFree (x, _)) => Symtab.update_multi ((x, t), tab) | (tab, _) => tab));
wenzelm@5819
   676
skalberg@15574
   677
fun ins_skolem def_ty = foldr
wenzelm@5994
   678
  (fn ((x, x'), types) =>
wenzelm@7663
   679
    (case def_ty x' of
skalberg@15531
   680
      SOME T => Vartab.update (((x, ~1), T), types)
skalberg@15531
   681
    | NONE => types));
wenzelm@5994
   682
ballarin@15735
   683
fun map_defaults f = map_context (fn (thy, syntax, data, asms, binds, thms, cases, defs) =>
ballarin@15735
   684
  (thy, syntax, data, asms, binds, thms, cases, f defs));
wenzelm@5819
   685
wenzelm@10381
   686
fun declare_syn (ctxt, t) =
wenzelm@5819
   687
  ctxt
wenzelm@12291
   688
  |> map_defaults (fn (types, sorts, used, occ) => (ins_types (types, t), sorts, used, occ))
wenzelm@12291
   689
  |> map_defaults (fn (types, sorts, used, occ) => (types, ins_sorts (sorts, t), used, occ))
wenzelm@12291
   690
  |> map_defaults (fn (types, sorts, used, occ) => (types, sorts, ins_used (used, t), occ));
wenzelm@10381
   691
wenzelm@12309
   692
fun declare_occ (ctxt as Context {asms = (_, fixes), ...}, t) =
wenzelm@10381
   693
  declare_syn (ctxt, t)
wenzelm@12291
   694
  |> map_defaults (fn (types, sorts, used, occ) => (types, sorts, used, ins_occs (occ, t)))
wenzelm@12291
   695
  |> map_defaults (fn (types, sorts, used, occ) =>
skalberg@15574
   696
      (ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) types fixes, sorts, used, occ));
wenzelm@5819
   697
wenzelm@10381
   698
in
wenzelm@5819
   699
wenzelm@10381
   700
fun declare_term t ctxt = declare_occ (ctxt, t);
skalberg@15570
   701
fun declare_terms ts ctxt = Library.foldl declare_occ (ctxt, ts);
skalberg@15570
   702
fun declare_terms_syntax ts ctxt = Library.foldl declare_syn (ctxt, ts);
wenzelm@10381
   703
wenzelm@10381
   704
end;
wenzelm@5819
   705
wenzelm@5819
   706
wenzelm@15703
   707
(* type and constant names *)
wenzelm@15703
   708
wenzelm@15703
   709
fun read_tyname ctxt c =
wenzelm@15703
   710
  if c mem_string used_types ctxt then
wenzelm@15703
   711
    TFree (c, getOpt (def_sort ctxt (c, ~1), Sign.defaultS (sign_of ctxt)))
wenzelm@15703
   712
  else Sign.read_tyname (sign_of ctxt) c;
wenzelm@15703
   713
wenzelm@15703
   714
fun read_const ctxt c =
wenzelm@15703
   715
  (case lookup_skolem ctxt c of
wenzelm@15703
   716
    SOME c' => Free (c', dummyT)
wenzelm@15703
   717
  | NONE => Sign.read_const (sign_of ctxt) c);
wenzelm@15703
   718
wenzelm@15703
   719
wenzelm@8616
   720
wenzelm@8616
   721
(** Hindley-Milner polymorphism **)
wenzelm@8616
   722
wenzelm@7925
   723
(* warn_extra_tfrees *)
wenzelm@7925
   724
wenzelm@12130
   725
fun warn_extra_tfrees
wenzelm@12291
   726
    (ctxt1 as Context {defs = (_, _, _, occ1), ...})
wenzelm@12291
   727
    (ctxt2 as Context {defs = (_, _, _, occ2), ...}) =
wenzelm@12130
   728
  let
wenzelm@12130
   729
    fun known_tfree a (Type (_, Ts)) = exists (known_tfree a) Ts
wenzelm@12130
   730
      | known_tfree a (TFree (a', _)) = a = a'
wenzelm@12130
   731
      | known_tfree _ _ = false;
wenzelm@8616
   732
wenzelm@12130
   733
    val extras =
wenzelm@12291
   734
      Library.gen_rems Library.eq_fst (Symtab.dest occ2, Symtab.dest occ1)
skalberg@15570
   735
      |> map (fn (a, ts) => map (pair a) (List.mapPartial (try (#1 o Term.dest_Free)) ts)) |> List.concat
skalberg@15570
   736
      |> List.mapPartial (fn (a, x) =>
skalberg@15531
   737
          (case def_type ctxt1 false (x, ~1) of NONE => SOME (a, x)
skalberg@15531
   738
          | SOME T => if known_tfree a T then NONE else SOME (a, x)));
wenzelm@12130
   739
    val tfrees = map #1 extras |> Library.sort_strings |> Library.unique_strings;
wenzelm@12130
   740
    val frees = map #2 extras |> Library.sort_strings |> Library.unique_strings;
wenzelm@12130
   741
  in
wenzelm@12130
   742
    if null extras then ()
wenzelm@12130
   743
    else warning ("Just introduced free type variable(s): " ^ commas tfrees ^ " in " ^
wenzelm@12130
   744
      space_implode " or " frees);
wenzelm@12130
   745
    ctxt2
wenzelm@12130
   746
  end;
wenzelm@8616
   747
wenzelm@8616
   748
wenzelm@8616
   749
(* generalize type variables *)
wenzelm@8616
   750
wenzelm@12550
   751
fun generalize_tfrees inner outer =
wenzelm@8616
   752
  let
wenzelm@12057
   753
    val extra_fixes = fixed_names_of inner \\ fixed_names_of outer;
wenzelm@8616
   754
    fun still_fixed (Free (x, _)) = not (x mem_string extra_fixes)
wenzelm@8616
   755
      | still_fixed _ = false;
wenzelm@12550
   756
    val occs_inner = type_occs inner;
wenzelm@12550
   757
    val occs_outer = type_occs outer;
wenzelm@12550
   758
    fun add (gen, a) =
skalberg@15570
   759
      if isSome (Symtab.lookup (occs_outer, a)) orelse
wenzelm@12550
   760
        exists still_fixed (Symtab.lookup_multi (occs_inner, a))
wenzelm@8616
   761
      then gen else a :: gen;
skalberg@15570
   762
  in fn tfrees => Library.foldl add ([], tfrees) end;
wenzelm@8616
   763
wenzelm@12550
   764
fun generalize inner outer ts =
wenzelm@8616
   765
  let
skalberg@15574
   766
    val tfrees = generalize_tfrees inner outer (foldr Term.add_term_tfree_names [] ts);
wenzelm@8616
   767
    fun gen (x, S) = if x mem_string tfrees then TVar ((x, 0), S) else TFree (x, S);
wenzelm@12550
   768
  in map (Term.map_term_types (Term.map_type_tfree gen)) ts end;
wenzelm@8616
   769
wenzelm@8616
   770
wenzelm@9553
   771
wenzelm@9553
   772
(** export theorems **)
wenzelm@8616
   773
skalberg@15531
   774
fun get_free x (NONE, t as Free (y, _)) = if x = y then SOME t else NONE
wenzelm@8616
   775
  | get_free _ (opt, _) = opt;
wenzelm@8616
   776
skalberg@15531
   777
fun find_free t x = foldl_aterms (get_free x) (NONE, t);
wenzelm@8616
   778
wenzelm@13415
   779
fun export_view view is_goal inner outer =
wenzelm@8616
   780
  let
wenzelm@12550
   781
    val gen = generalize_tfrees inner outer;
wenzelm@12057
   782
    val fixes = fixed_names_of inner \\ fixed_names_of outer;
wenzelm@12057
   783
    val asms = Library.drop (length (assumptions_of outer), assumptions_of inner);
wenzelm@11816
   784
    val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms;
wenzelm@12550
   785
  in fn thm => thm
wenzelm@12804
   786
    |> Tactic.norm_hhf_rule
wenzelm@11816
   787
    |> Seq.EVERY (rev exp_asms)
wenzelm@13415
   788
    |> Seq.map (Drule.implies_intr_list view)
wenzelm@11816
   789
    |> Seq.map (fn rule =>
wenzelm@11816
   790
      let
wenzelm@11816
   791
        val {sign, prop, ...} = Thm.rep_thm rule;
skalberg@15570
   792
        val frees = map (Thm.cterm_of sign) (List.mapPartial (find_free prop) fixes);
wenzelm@12550
   793
        val tfrees = gen (Term.add_term_tfree_names (prop, []));
wenzelm@11816
   794
      in
wenzelm@11816
   795
        rule
wenzelm@11816
   796
        |> Drule.forall_intr_list frees
wenzelm@12804
   797
        |> Tactic.norm_hhf_rule
wenzelm@12504
   798
        |> (#1 o Drule.tvars_intr_list tfrees)
wenzelm@11816
   799
      end)
wenzelm@11816
   800
  end;
wenzelm@8616
   801
wenzelm@15758
   802
(*without varification*)
ballarin@15696
   803
fun export_view' view is_goal inner outer =
ballarin@15696
   804
  let
ballarin@15696
   805
    val asms = Library.drop (length (assumptions_of outer), assumptions_of inner);
ballarin@15696
   806
    val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms;
ballarin@15696
   807
  in fn thm =>thm
ballarin@15696
   808
    |> Tactic.norm_hhf_plain
ballarin@15696
   809
    |> Seq.EVERY (rev exp_asms)
ballarin@15696
   810
    |> Seq.map (Drule.implies_intr_list view)
ballarin@15696
   811
    |> Seq.map Tactic.norm_hhf_plain
ballarin@15696
   812
  end;
ballarin@15696
   813
wenzelm@13415
   814
val export = export_view [];
wenzelm@13399
   815
ballarin@15696
   816
fun gen_export_std exp_view view inner outer =
ballarin@15696
   817
  let val exp = exp_view view false inner outer in
wenzelm@13378
   818
    fn th =>
wenzelm@13378
   819
      (case Seq.pull (exp th) of
skalberg@15531
   820
        SOME (th', _) => th' |> Drule.local_standard
skalberg@15531
   821
      | NONE => raise CONTEXT ("Internal failure while exporting theorem", inner))
wenzelm@13378
   822
  end;
wenzelm@12704
   823
ballarin@15696
   824
val export_standard = gen_export_std export_view;
ballarin@15696
   825
val export_plain = gen_export_std export_view';
wenzelm@7925
   826
wenzelm@5819
   827
wenzelm@15758
   828
wenzelm@5819
   829
(** bindings **)
wenzelm@5819
   830
wenzelm@15758
   831
(* delete_update_binds *)
wenzelm@15758
   832
wenzelm@15758
   833
local
wenzelm@5819
   834
wenzelm@15758
   835
fun del_bind xi = map_context (fn (thy, syntax, data, asms, binds, thms, cases, defs) =>
wenzelm@15758
   836
  (thy, syntax, data, asms, Vartab.delete_safe xi binds, thms, cases, defs));
wenzelm@7606
   837
wenzelm@15758
   838
fun upd_bind ((x, i), t) =
wenzelm@8616
   839
  let
wenzelm@8616
   840
    val T = Term.fastype_of t;
wenzelm@8616
   841
    val t' =
wenzelm@8616
   842
      if null (Term.term_tvars t \\ Term.typ_tvars T) then t
wenzelm@8637
   843
      else Var ((x ^ "_has_extra_type_vars_on_rhs", i), T);
wenzelm@8616
   844
  in
wenzelm@15758
   845
    map_context (fn (thy, syntax, data, asms, binds, thms, cases, defs) =>
wenzelm@15758
   846
      (thy, syntax, data, asms, Vartab.update (((x, i), (t', T)), binds), thms, cases, defs))
wenzelm@15758
   847
    o declare_term t'
wenzelm@5819
   848
  end;
wenzelm@5819
   849
wenzelm@15758
   850
fun del_upd_bind (xi, NONE) = del_bind xi
wenzelm@15758
   851
  | del_upd_bind (xi, SOME t) = upd_bind (xi, t);
wenzelm@7606
   852
wenzelm@15758
   853
in
wenzelm@15758
   854
wenzelm@15758
   855
val delete_update_binds = fold del_upd_bind;
wenzelm@15758
   856
wenzelm@15758
   857
end;
wenzelm@7606
   858
wenzelm@5819
   859
wenzelm@8096
   860
(* simult_matches *)
wenzelm@8096
   861
wenzelm@8616
   862
fun simult_matches ctxt [] = []
wenzelm@8616
   863
  | simult_matches ctxt pairs =
wenzelm@8096
   864
      let
wenzelm@10554
   865
        fun fail () = raise CONTEXT ("Pattern match failed!", ctxt);
wenzelm@10554
   866
skalberg@15570
   867
        val maxidx = Library.foldl (fn (i, (t1, t2)) =>
wenzelm@8096
   868
          Int.max (i, Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2))) (~1, pairs);
wenzelm@10554
   869
        val envs = Unify.smash_unifiers (sign_of ctxt, Envir.empty maxidx,
wenzelm@10554
   870
          map swap pairs);    (*prefer assignment of variables from patterns*)
wenzelm@10554
   871
        val env =
wenzelm@8096
   872
          (case Seq.pull envs of
skalberg@15531
   873
            NONE => fail ()
skalberg@15531
   874
          | SOME (env, _) => env);    (*ignore further results*)
wenzelm@12309
   875
        val domain =
wenzelm@12309
   876
          filter_out Term.is_replaced_dummy_pattern (map #1 (Drule.vars_of_terms (map #1 pairs)));
wenzelm@10554
   877
        val _ =    (*may not assign variables from text*)
wenzelm@12309
   878
          if null (map #1 (Envir.alist_of env) inter (map #1 (Drule.vars_of_terms (map #2 pairs))))
wenzelm@12309
   879
          then () else fail ();
skalberg@15531
   880
        fun norm_bind (xi, t) = if xi mem domain then SOME (xi, Envir.norm_term env t) else NONE;
skalberg@15570
   881
      in List.mapPartial norm_bind (Envir.alist_of env) end;
wenzelm@8096
   882
wenzelm@8096
   883
wenzelm@8096
   884
(* add_binds(_i) *)
wenzelm@5819
   885
wenzelm@7925
   886
local
wenzelm@7925
   887
wenzelm@5819
   888
fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) =
skalberg@15570
   889
  ctxt |> delete_update_binds [(xi, Option.map (prep ctxt) raw_t)];
wenzelm@5819
   890
skalberg@15570
   891
fun gen_binds prep binds ctxt = Library.foldl (gen_bind prep) (ctxt, binds);
wenzelm@7925
   892
wenzelm@10810
   893
in
wenzelm@10810
   894
skalberg@15531
   895
fun drop_schematic (b as (xi, SOME t)) = if null (Term.term_vars t) then b else (xi, NONE)
wenzelm@10554
   896
  | drop_schematic b = b;
wenzelm@10554
   897
wenzelm@5819
   898
val add_binds = gen_binds read_term;
wenzelm@5819
   899
val add_binds_i = gen_binds cert_term;
wenzelm@8616
   900
wenzelm@12147
   901
fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (sign_of ctxt) ts));
wenzelm@12147
   902
val auto_bind_goal = auto_bind AutoBind.goal;
wenzelm@12147
   903
val auto_bind_facts = auto_bind AutoBind.facts;
wenzelm@7925
   904
wenzelm@7925
   905
end;
wenzelm@5819
   906
wenzelm@5819
   907
wenzelm@8096
   908
(* match_bind(_i) *)
wenzelm@5819
   909
wenzelm@8096
   910
local
wenzelm@8096
   911
wenzelm@10465
   912
fun prep_bind prep_pats (ctxt, (raw_pats, t)) =
wenzelm@5819
   913
  let
wenzelm@8096
   914
    val ctxt' = declare_term t ctxt;
wenzelm@8096
   915
    val pats = prep_pats (fastype_of t) ctxt' raw_pats;
wenzelm@8616
   916
    val binds = simult_matches ctxt' (map (rpair t) pats);
wenzelm@8616
   917
  in (ctxt', binds) end;
wenzelm@7670
   918
wenzelm@10465
   919
fun gen_binds prep_terms prep_pats gen raw_binds ctxt =
wenzelm@8616
   920
  let
wenzelm@10465
   921
    val ts = prep_terms ctxt (map snd raw_binds);
wenzelm@10465
   922
    val (ctxt', binds) =
skalberg@15570
   923
      apsnd List.concat (foldl_map (prep_bind prep_pats) (ctxt, map fst raw_binds ~~ ts));
wenzelm@8616
   924
    val binds' =
wenzelm@12550
   925
      if gen then map #1 binds ~~ generalize ctxt' ctxt (map #2 binds)
wenzelm@8616
   926
      else binds;
skalberg@15531
   927
    val binds'' = map (apsnd SOME) binds';
wenzelm@8616
   928
  in
wenzelm@8616
   929
    warn_extra_tfrees ctxt
wenzelm@8616
   930
     (if gen then ctxt (*sic!*) |> declare_terms (map #2 binds') |> add_binds_i binds''
wenzelm@8616
   931
      else ctxt' |> add_binds_i binds'')
wenzelm@8616
   932
  end;
wenzelm@8096
   933
wenzelm@8096
   934
in
wenzelm@5935
   935
wenzelm@10465
   936
val match_bind = gen_binds read_terms read_term_pats;
wenzelm@10465
   937
val match_bind_i = gen_binds (map o cert_term) cert_term_pats;
wenzelm@8096
   938
wenzelm@8096
   939
end;
wenzelm@5935
   940
wenzelm@5935
   941
wenzelm@10465
   942
(* propositions with patterns *)
wenzelm@5935
   943
wenzelm@10465
   944
local
wenzelm@8096
   945
wenzelm@10554
   946
fun prep_propp schematic prep_props prep_pats (context, args) =
wenzelm@10465
   947
  let
wenzelm@10465
   948
    fun prep ((ctxt, prop :: props), (_, (raw_pats1, raw_pats2))) =
wenzelm@10465
   949
          let
wenzelm@10465
   950
            val ctxt' = declare_term prop ctxt;
wenzelm@10465
   951
            val pats = prep_pats ctxt' (raw_pats1 @ raw_pats2);    (*simultaneous type inference!*)
nipkow@13629
   952
          in ((ctxt', props), (prop, splitAt(length raw_pats1, pats))) end
wenzelm@10465
   953
      | prep _ = sys_error "prep_propp";
wenzelm@10465
   954
    val ((context', _), propp) = foldl_map (foldl_map prep)
skalberg@15570
   955
        ((context, prep_props schematic context (List.concat (map (map fst) args))), args);
wenzelm@10465
   956
  in (context', propp) end;
wenzelm@5935
   957
wenzelm@10465
   958
fun matches ctxt (prop, (pats1, pats2)) =
wenzelm@10465
   959
  simult_matches ctxt (map (rpair prop) pats1 @ map (rpair (Logic.strip_imp_concl prop)) pats2);
wenzelm@8096
   960
wenzelm@10465
   961
fun gen_bind_propp prepp (ctxt, raw_args) =
wenzelm@8096
   962
  let
wenzelm@10465
   963
    val (ctxt', args) = prepp (ctxt, raw_args);
skalberg@15570
   964
    val binds = List.concat (List.concat (map (map (matches ctxt')) args));
wenzelm@10465
   965
    val propss = map (map #1) args;
wenzelm@8616
   966
wenzelm@10554
   967
    (*generalize result: context evaluated now, binds added later*)
wenzelm@8616
   968
    val gen = generalize ctxt' ctxt;
skalberg@15531
   969
    fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
skalberg@15531
   970
  in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
wenzelm@8096
   971
wenzelm@10465
   972
in
wenzelm@10465
   973
wenzelm@11925
   974
val read_propp = prep_propp false read_props read_prop_pats;
wenzelm@11925
   975
val cert_propp = prep_propp false cert_props cert_prop_pats;
wenzelm@10554
   976
val read_propp_schematic = prep_propp true read_props read_prop_pats;
wenzelm@10554
   977
val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
wenzelm@10554
   978
wenzelm@11925
   979
val bind_propp = gen_bind_propp read_propp;
wenzelm@11925
   980
val bind_propp_i = gen_bind_propp cert_propp;
wenzelm@11925
   981
val bind_propp_schematic = gen_bind_propp read_propp_schematic;
wenzelm@10554
   982
val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
wenzelm@6789
   983
wenzelm@10465
   984
end;
wenzelm@10465
   985
wenzelm@6789
   986
wenzelm@5819
   987
wenzelm@5819
   988
(** theorems **)
wenzelm@5819
   989
wenzelm@6091
   990
(* get_thm(s) *)
wenzelm@5819
   991
wenzelm@9566
   992
(*beware of proper order of evaluation!*)
wenzelm@13278
   993
fun retrieve_thms f g (ctxt as Context {thy, thms = (_, space, tab, _), ...}) =
wenzelm@9566
   994
  let
wenzelm@9566
   995
    val sg_ref = Sign.self_ref (Theory.sign_of thy);
wenzelm@9566
   996
    val get_from_thy = f thy;
wenzelm@9566
   997
  in
berghofe@15456
   998
    fn xnamei as (xname, _) =>
wenzelm@12309
   999
      (case Symtab.lookup (tab, NameSpace.intern space xname) of
wenzelm@15758
  1000
        SOME ths => map (Thm.transfer_sg (Sign.deref sg_ref)) (PureThy.select_thm xnamei ths)
berghofe@15456
  1001
      | _ => get_from_thy xnamei) |> g xname
wenzelm@9566
  1002
  end;
wenzelm@5819
  1003
wenzelm@9566
  1004
val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm;
wenzelm@9566
  1005
val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm;
wenzelm@9566
  1006
val get_thms = retrieve_thms PureThy.get_thms (K I);
wenzelm@9566
  1007
val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I);
wenzelm@5819
  1008
wenzelm@5819
  1009
wenzelm@13425
  1010
(* name space operations *)
wenzelm@12309
  1011
wenzelm@13278
  1012
fun cond_extern (Context {thms = (_, space, _, _), ...}) = NameSpace.cond_extern space;
wenzelm@12309
  1013
wenzelm@13399
  1014
fun qualified q = map_context (fn (thy, syntax, data, asms, binds,
ballarin@15735
  1015
    (_, space, tab, index), cases, defs) =>
ballarin@15735
  1016
  (thy, syntax, data, asms, binds, (q, space, tab, index), cases, defs));
wenzelm@12309
  1017
wenzelm@13399
  1018
fun restore_qualified (Context {thms, ...}) = qualified (#1 thms);
wenzelm@12309
  1019
wenzelm@13425
  1020
fun hide_thms fully names =
ballarin@15735
  1021
  map_context (fn (thy, syntax, data, asms, binds, (q, space, tab, index), cases, defs) =>
wenzelm@13425
  1022
    (thy, syntax, data, asms, binds, (q, NameSpace.hide fully (space, names), tab, index),
ballarin@15735
  1023
      cases, defs));
wenzelm@13425
  1024
wenzelm@12309
  1025
wenzelm@6091
  1026
(* put_thm(s) *)
wenzelm@5819
  1027
ballarin@15696
  1028
fun gen_put_thms _ _ ("", _) ctxt = ctxt
ballarin@15696
  1029
  | gen_put_thms override_q acc (name, ths) ctxt = ctxt |> map_context
ballarin@15735
  1030
      (fn (thy, syntax, data, asms, binds, (q, space, tab, index), cases, defs) =>
ballarin@15696
  1031
        if not override_q andalso not q andalso NameSpace.is_qualified name then
wenzelm@12309
  1032
          raise CONTEXT ("Attempt to declare qualified name " ^ quote name, ctxt)
ballarin@15696
  1033
        else (thy, syntax, data, asms, binds, (q, NameSpace.extend' acc (space, [name]),
wenzelm@15758
  1034
          Symtab.update ((name, ths), tab),
ballarin@15735
  1035
            FactIndex.add (is_known ctxt) (index, (name, ths))), cases, defs));
wenzelm@5819
  1036
ballarin@15696
  1037
fun gen_put_thm q acc (name, th) = gen_put_thms q acc (name, [th]);
wenzelm@15758
  1038
fun gen_put_thmss q acc = fold (gen_put_thms q acc);
wenzelm@5819
  1039
ballarin@15696
  1040
val put_thm = gen_put_thm false NameSpace.accesses;
ballarin@15696
  1041
val put_thms = gen_put_thms false NameSpace.accesses;
ballarin@15696
  1042
val put_thmss = gen_put_thmss false NameSpace.accesses;
wenzelm@5819
  1043
wenzelm@15758
  1044
wenzelm@7606
  1045
(* reset_thms *)
wenzelm@7606
  1046
wenzelm@12309
  1047
fun reset_thms name =
ballarin@15735
  1048
  map_context (fn (thy, syntax, data, asms, binds, (q, space, tab, index), cases, defs) =>
wenzelm@15758
  1049
    (thy, syntax, data, asms, binds, (q, space, Symtab.delete_safe name tab, index),
ballarin@15735
  1050
      cases, defs));
wenzelm@7606
  1051
wenzelm@7606
  1052
wenzelm@14564
  1053
(* note_thmss *)
wenzelm@5819
  1054
wenzelm@12711
  1055
local
wenzelm@12711
  1056
ballarin@15696
  1057
fun gen_note_thss get acc (ctxt, ((name, more_attrs), ths_attrs)) =
wenzelm@5819
  1058
  let
wenzelm@5819
  1059
    fun app ((ct, ths), (th, attrs)) =
wenzelm@12711
  1060
      let val (ct', th') = Thm.applys_attributes ((ct, get ctxt th), attrs @ more_attrs)
wenzelm@12711
  1061
      in (ct', th' :: ths) end;
skalberg@15570
  1062
    val (ctxt', rev_thms) = Library.foldl app ((ctxt, []), ths_attrs);
skalberg@15570
  1063
    val thms = List.concat (rev rev_thms);
ballarin@15696
  1064
  in (ctxt' |> gen_put_thms true acc (name, thms), (name, thms)) end;
wenzelm@5819
  1065
ballarin@15696
  1066
fun gen_note_thmss get acc args ctxt =
ballarin@15696
  1067
  foldl_map (gen_note_thss get acc) (ctxt, args);
wenzelm@12711
  1068
wenzelm@12711
  1069
in
wenzelm@12711
  1070
ballarin@15696
  1071
val note_thmss = gen_note_thmss get_thms NameSpace.accesses;
ballarin@15696
  1072
val note_thmss_i = gen_note_thmss (K I) NameSpace.accesses;
ballarin@15696
  1073
ballarin@15696
  1074
val note_thmss_accesses = gen_note_thmss get_thms;
ballarin@15696
  1075
val note_thmss_accesses_i = gen_note_thmss (K I);
wenzelm@12711
  1076
wenzelm@12711
  1077
end;
wenzelm@9196
  1078
wenzelm@5819
  1079
wenzelm@5819
  1080
wenzelm@5819
  1081
(** assumptions **)
wenzelm@5819
  1082
wenzelm@11918
  1083
(* basic exporters *)
wenzelm@11918
  1084
wenzelm@11918
  1085
fun export_assume true = Seq.single oo Drule.implies_intr_goals
wenzelm@11918
  1086
  | export_assume false = Seq.single oo Drule.implies_intr_list;
wenzelm@11918
  1087
wenzelm@11918
  1088
fun export_presume _ = export_assume false;
wenzelm@11918
  1089
wenzelm@11918
  1090
wenzelm@12057
  1091
(* defs *)
wenzelm@12057
  1092
wenzelm@12066
  1093
fun cert_def ctxt eq =
wenzelm@12016
  1094
  let
wenzelm@12057
  1095
    fun err msg = raise CONTEXT (msg ^
wenzelm@12057
  1096
      "\nThe error(s) above occurred in local definition: " ^ string_of_term ctxt eq, ctxt);
wenzelm@12057
  1097
    val (lhs, rhs) = Logic.dest_equals (Term.strip_all_body eq)
wenzelm@12057
  1098
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@12086
  1099
    val (f, xs) = Term.strip_comb lhs;
wenzelm@12086
  1100
    val (c, _) = Term.dest_Free f handle TERM _ =>
wenzelm@13430
  1101
      err "Head of lhs must be a free/fixed variable";
wenzelm@12057
  1102
wenzelm@12086
  1103
    fun is_free (Free (x, _)) = not (is_fixed ctxt x)
wenzelm@12086
  1104
      | is_free _ = false;
skalberg@15570
  1105
    val extra_frees = List.filter is_free (term_frees rhs) \\ xs;
wenzelm@12016
  1106
  in
wenzelm@12086
  1107
    conditional (not (forall (is_Bound orf is_free) xs andalso null (duplicates xs))) (fn () =>
wenzelm@13430
  1108
      err "Arguments of lhs must be distinct free/bound variables");
wenzelm@12086
  1109
    conditional (f mem Term.term_frees rhs) (fn () =>
wenzelm@12086
  1110
      err "Element to be defined occurs on rhs");
wenzelm@12086
  1111
    conditional (not (null extra_frees)) (fn () =>
wenzelm@12086
  1112
      err ("Extra free variables on rhs: " ^ commas_quote (map (#1 o dest_Free) extra_frees)));
skalberg@15570
  1113
    (c, Term.list_all_free (List.mapPartial (try Term.dest_Free) xs, eq))
wenzelm@12057
  1114
  end;
wenzelm@12057
  1115
wenzelm@12057
  1116
fun head_of_def cprop =
wenzelm@12057
  1117
  #1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop)))))
wenzelm@12057
  1118
  |> Thm.cterm_of (Thm.sign_of_cterm cprop);
wenzelm@11918
  1119
wenzelm@11918
  1120
fun export_def _ cprops thm =
wenzelm@11918
  1121
  thm
wenzelm@11918
  1122
  |> Drule.implies_intr_list cprops
wenzelm@12057
  1123
  |> Drule.forall_intr_list (map head_of_def cprops)
wenzelm@11918
  1124
  |> Drule.forall_elim_vars 0
wenzelm@11918
  1125
  |> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1;
wenzelm@11918
  1126
wenzelm@11918
  1127
wenzelm@5819
  1128
(* assume *)
wenzelm@5819
  1129
wenzelm@7270
  1130
local
wenzelm@6797
  1131
wenzelm@10465
  1132
fun add_assm (ctxt, ((name, attrs), props)) =
wenzelm@5819
  1133
  let
wenzelm@10465
  1134
    val cprops = map (Thm.cterm_of (sign_of ctxt)) props;
wenzelm@12804
  1135
    val asms = map (Tactic.norm_hhf_rule o Thm.assume) cprops;
wenzelm@5919
  1136
wenzelm@5919
  1137
    val ths = map (fn th => ([th], [])) asms;
wenzelm@10465
  1138
    val (ctxt', [(_, thms)]) =
wenzelm@10465
  1139
      ctxt
wenzelm@12147
  1140
      |> auto_bind_facts props
wenzelm@14564
  1141
      |> note_thmss_i [((name, attrs), ths)];
wenzelm@10465
  1142
  in (ctxt', (cprops, (name, asms), (name, thms))) end;
wenzelm@5819
  1143
wenzelm@9470
  1144
fun gen_assms prepp exp args ctxt =
wenzelm@9470
  1145
  let
wenzelm@10465
  1146
    val (ctxt1, propss) = prepp (ctxt, map snd args);
wenzelm@10465
  1147
    val (ctxt2, results) = foldl_map add_assm (ctxt1, map fst args ~~ propss);
wenzelm@10465
  1148
skalberg@15570
  1149
    val cprops = List.concat (map #1 results);
wenzelm@9470
  1150
    val asmss = map #2 results;
wenzelm@9470
  1151
    val thmss = map #3 results;
wenzelm@12072
  1152
    val ctxt3 = ctxt2 |> map_context
ballarin@15735
  1153
      (fn (thy, syntax, data, ((asms_ct, asms_th), fixes), binds, thms, cases, defs) =>
wenzelm@12072
  1154
        (thy, syntax, data, ((asms_ct @ [(cprops, exp)], asms_th @ asmss), fixes), binds, thms,
ballarin@15735
  1155
          cases, defs));
wenzelm@11925
  1156
    val ctxt4 = ctxt3 |> put_thms ("prems", prems_of ctxt3);
wenzelm@11925
  1157
  in (warn_extra_tfrees ctxt ctxt4, thmss) end;
wenzelm@5819
  1158
wenzelm@7270
  1159
in
wenzelm@7270
  1160
wenzelm@8616
  1161
val assume = gen_assms (apsnd #1 o bind_propp);
wenzelm@8616
  1162
val assume_i = gen_assms (apsnd #1 o bind_propp_i);
wenzelm@7270
  1163
wenzelm@7270
  1164
end;
wenzelm@5819
  1165
wenzelm@5819
  1166
wenzelm@8096
  1167
(* variables *)
wenzelm@8096
  1168
wenzelm@10381
  1169
local
wenzelm@10381
  1170
wenzelm@14720
  1171
fun prep_vars prep_typ internal liberal (ctxt, (xs, raw_T)) =
wenzelm@8096
  1172
  let
wenzelm@12504
  1173
    fun cond_tvars T =
wenzelm@12504
  1174
      if internal then T
wenzelm@12504
  1175
      else Type.no_tvars T handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
wenzelm@12504
  1176
wenzelm@14720
  1177
    val _ = if liberal then () else
skalberg@15570
  1178
      (case List.filter (not o Syntax.is_identifier) (map (no_skolem internal ctxt) xs) of
wenzelm@8096
  1179
      [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
wenzelm@8096
  1180
skalberg@15570
  1181
    val opt_T = Option.map (cond_tvars o prep_typ ctxt) raw_T;
skalberg@15570
  1182
    val T = getOpt (opt_T, TypeInfer.logicT);
wenzelm@12130
  1183
    val ctxt' = ctxt |> declare_terms_syntax (map (fn x => Free (x, T)) xs);
wenzelm@8096
  1184
  in (ctxt', (xs, opt_T)) end;
wenzelm@8096
  1185
wenzelm@10381
  1186
in
wenzelm@10381
  1187
wenzelm@14720
  1188
val read_vars         = prep_vars read_typ false false;
wenzelm@14720
  1189
val cert_vars         = prep_vars cert_typ true false;
wenzelm@14720
  1190
val read_vars_liberal = prep_vars read_typ false true;
wenzelm@14720
  1191
val cert_vars_liberal = prep_vars cert_typ true true;
wenzelm@8096
  1192
wenzelm@10381
  1193
end;
wenzelm@10381
  1194
wenzelm@8096
  1195
wenzelm@5819
  1196
(* fix *)
wenzelm@5819
  1197
wenzelm@8096
  1198
local
wenzelm@8096
  1199
wenzelm@12309
  1200
fun map_fixes f =
ballarin@15735
  1201
  map_context (fn (thy, syntax, data, (assumes, fixes), binds, thms, cases, defs) =>
ballarin@15735
  1202
    (thy, syntax, data, (assumes, f fixes), binds, thms, cases, defs));
wenzelm@5819
  1203
wenzelm@11925
  1204
fun err_dups ctxt xs = raise CONTEXT ("Duplicate variable(s): " ^ commas_quote xs, ctxt);
wenzelm@11925
  1205
wenzelm@12130
  1206
val declare =
skalberg@15570
  1207
  declare_terms_syntax o List.mapPartial (fn (_, NONE) => NONE | (x, SOME T) => SOME (Free (x, T)));
wenzelm@12130
  1208
wenzelm@12130
  1209
fun add_vars xs Ts ctxt =
wenzelm@12309
  1210
  let val xs' = Term.variantlist (map Syntax.skolem xs, map #2 (fixes_of ctxt)) in
wenzelm@12309
  1211
    ctxt
wenzelm@12309
  1212
    |> declare (xs' ~~ Ts)
wenzelm@12309
  1213
    |> map_fixes (fn fixes => (xs ~~ xs') @ fixes)
wenzelm@12130
  1214
  end;
wenzelm@11925
  1215
wenzelm@12130
  1216
fun add_vars_direct xs Ts ctxt =
wenzelm@12309
  1217
  ctxt
wenzelm@12309
  1218
  |> declare (xs ~~ Ts)
wenzelm@12309
  1219
  |> map_fixes (fn fixes =>
wenzelm@12130
  1220
    (case xs inter_string map #1 fixes of
wenzelm@12309
  1221
      [] => (xs ~~ xs) @ fixes
wenzelm@12130
  1222
    | dups => err_dups ctxt dups));
wenzelm@12130
  1223
wenzelm@11925
  1224
wenzelm@11925
  1225
fun gen_fix prep add raw_vars ctxt =
wenzelm@8096
  1226
  let
wenzelm@8096
  1227
    val (ctxt', varss) = foldl_map prep (ctxt, raw_vars);
skalberg@15570
  1228
    val vars = rev (List.concat (map (fn (xs, T) => map (rpair T) xs) varss));
wenzelm@12130
  1229
    val xs = map #1 vars;
wenzelm@12130
  1230
    val Ts = map #2 vars;
wenzelm@8096
  1231
  in
wenzelm@11925
  1232
    (case Library.duplicates xs of [] => () | dups => err_dups ctxt dups);
wenzelm@12130
  1233
    ctxt' |> add xs Ts
wenzelm@8096
  1234
  end;
wenzelm@5819
  1235
skalberg@15531
  1236
fun prep_type (x, NONE, SOME mx) = ([x], SOME (mixfix_type mx))
wenzelm@12576
  1237
  | prep_type (x, opt_T, _) = ([x], opt_T);
wenzelm@12576
  1238
wenzelm@8096
  1239
in
wenzelm@7679
  1240
wenzelm@11925
  1241
val fix = gen_fix read_vars add_vars;
wenzelm@11925
  1242
val fix_i = gen_fix cert_vars add_vars;
wenzelm@14720
  1243
wenzelm@14720
  1244
fun fix_direct liberal =
wenzelm@14720
  1245
  gen_fix (if liberal then cert_vars_liberal else cert_vars) add_vars_direct;
wenzelm@14720
  1246
wenzelm@14720
  1247
fun add_fixes decls = add_syntax decls o fix_direct false (map prep_type decls);
wenzelm@14720
  1248
fun add_fixes_liberal decls = add_syntax decls o fix_direct true (map prep_type decls);
wenzelm@8096
  1249
wenzelm@8096
  1250
end;
wenzelm@5819
  1251
wenzelm@12048
  1252
fun fix_frees ts ctxt =
wenzelm@12048
  1253
  let
skalberg@15570
  1254
    val frees = Library.foldl Term.add_frees ([], ts);
skalberg@15531
  1255
    fun new (x, T) = if is_fixed ctxt x then NONE else SOME ([x], SOME T);
skalberg@15570
  1256
  in fix_direct false (rev (List.mapPartial new frees)) ctxt end;
wenzelm@12016
  1257
wenzelm@6895
  1258
wenzelm@9291
  1259
(*Note: improper use may result in variable capture / dynamic scoping!*)
wenzelm@9291
  1260
fun bind_skolem ctxt xs =
wenzelm@9291
  1261
  let
skalberg@15531
  1262
    val ctxt' = ctxt |> fix_i [(xs, NONE)];
wenzelm@9291
  1263
    fun bind (t as Free (x, T)) =
wenzelm@9291
  1264
          if x mem_string xs then
skalberg@15531
  1265
            (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
wenzelm@9291
  1266
          else t
wenzelm@9291
  1267
      | bind (t $ u) = bind t $ bind u
wenzelm@9291
  1268
      | bind (Abs (x, T, t)) = Abs (x, T, bind t)
wenzelm@9291
  1269
      | bind a = a;
wenzelm@9291
  1270
  in bind end;
wenzelm@9291
  1271
wenzelm@9291
  1272
wenzelm@5819
  1273
wenzelm@8373
  1274
(** cases **)
wenzelm@8373
  1275
wenzelm@11793
  1276
fun prep_case ctxt name xs {fixes, assumes, binds} =
wenzelm@11793
  1277
  let
skalberg@15570
  1278
    fun replace (opt_x :: xs) ((y, T) :: ys) = (getOpt (opt_x,y), T) :: replace xs ys
wenzelm@11793
  1279
      | replace [] ys = ys
wenzelm@11793
  1280
      | replace (_ :: _) [] = raise CONTEXT ("Too many parameters for case " ^ quote name, ctxt);
wenzelm@11793
  1281
  in
skalberg@15574
  1282
    if null (foldr Term.add_typ_tvars [] (map snd fixes)) andalso
skalberg@15574
  1283
      null (foldr Term.add_term_vars [] (List.concat (map snd assumes))) then
wenzelm@11793
  1284
        {fixes = replace xs fixes, assumes = assumes, binds = map drop_schematic binds}
wenzelm@11793
  1285
    else raise CONTEXT ("Illegal schematic variable(s) in case " ^ quote name, ctxt)
wenzelm@11793
  1286
  end;
wenzelm@8403
  1287
wenzelm@11793
  1288
fun get_case (ctxt as Context {cases, ...}) name xs =
wenzelm@8426
  1289
  (case assoc (cases, name) of
skalberg@15531
  1290
    NONE => raise CONTEXT ("Unknown case: " ^ quote name, ctxt)
skalberg@15531
  1291
  | SOME c => prep_case ctxt name xs c);
wenzelm@8373
  1292
wenzelm@8384
  1293
ballarin@15735
  1294
fun add_cases xs = map_context (fn (thy, syntax, data, asms, binds, thms, cases, defs) =>
ballarin@15735
  1295
  (thy, syntax, data, asms, binds, thms, rev (filter_out (equal "" o #1) xs) @ cases, defs));
wenzelm@8373
  1296
wenzelm@8373
  1297
wenzelm@8373
  1298
wenzelm@10810
  1299
(** print context information **)
wenzelm@10810
  1300
wenzelm@10810
  1301
val verbose = ref false;
wenzelm@10810
  1302
fun verb f x = if ! verbose then f (x ()) else [];
wenzelm@10810
  1303
fun verb_single x = verb Library.single x;
wenzelm@10810
  1304
wenzelm@10810
  1305
fun setmp_verbose f x = Library.setmp verbose true f x;
wenzelm@10810
  1306
wenzelm@10810
  1307
fun pretty_items prt name items =
wenzelm@10810
  1308
  let
wenzelm@10810
  1309
    fun prt_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x]
wenzelm@10810
  1310
      | prt_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs);
wenzelm@10810
  1311
  in
wenzelm@10810
  1312
    if null items andalso not (! verbose) then []
wenzelm@10810
  1313
    else [Pretty.big_list name (map prt_itms items)]
wenzelm@10810
  1314
  end;
wenzelm@10810
  1315
wenzelm@10810
  1316
wenzelm@12072
  1317
(* local syntax *)
wenzelm@12072
  1318
wenzelm@12093
  1319
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@12072
  1320
wenzelm@12072
  1321
wenzelm@10810
  1322
(* term bindings *)
wenzelm@10810
  1323
wenzelm@10810
  1324
fun pretty_binds (ctxt as Context {binds, ...}) =
wenzelm@10810
  1325
  let
wenzelm@12057
  1326
    fun prt_bind (xi, (t, T)) = pretty_term ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1327
  in
wenzelm@15758
  1328
    if Vartab.is_empty binds andalso not (! verbose) then []
wenzelm@15758
  1329
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1330
  end;
wenzelm@10810
  1331
wenzelm@10810
  1332
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1333
wenzelm@10810
  1334
wenzelm@10810
  1335
(* local theorems *)
wenzelm@10810
  1336
wenzelm@13278
  1337
fun pretty_lthms (ctxt as Context {thms = (_, space, tab, _), ...}) =
wenzelm@15758
  1338
  pretty_items (pretty_thm ctxt) "facts:" (NameSpace.cond_extern_table space tab);
wenzelm@10810
  1339
wenzelm@12057
  1340
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1341
wenzelm@10810
  1342
wenzelm@10810
  1343
(* local contexts *)
wenzelm@10810
  1344
wenzelm@10830
  1345
fun apply_case ({fixes, assumes, binds}: RuleCases.T) ctxt =
wenzelm@10810
  1346
  let
skalberg@15531
  1347
    fun bind (c, (x, T)) = (c |> fix_i [([x], SOME T)], bind_skolem c [x] (Free (x, T)));
wenzelm@10830
  1348
    val (ctxt', xs) = foldl_map bind (ctxt, fixes);
skalberg@15570
  1349
    fun app t = Library.foldl Term.betapply (t, xs);
skalberg@15570
  1350
  in (ctxt', (map (apsnd (Option.map app)) binds, map (apsnd (map app)) assumes)) end;
wenzelm@10810
  1351
wenzelm@10810
  1352
fun pretty_cases (ctxt as Context {cases, ...}) =
wenzelm@10810
  1353
  let
wenzelm@12057
  1354
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1355
wenzelm@10810
  1356
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1357
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1358
        Pretty.quote (prt_term t)];
wenzelm@10810
  1359
wenzelm@13425
  1360
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1361
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1362
wenzelm@10810
  1363
    fun prt_sect _ _ _ [] = []
wenzelm@10810
  1364
      | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
skalberg@15570
  1365
            List.concat (Library.separate sep (map (Library.single o prt) xs))))];
wenzelm@10810
  1366
wenzelm@10830
  1367
    fun prt_case (name, (fixes, (lets, asms))) = Pretty.block (Pretty.fbreaks
wenzelm@10810
  1368
      (Pretty.str (name ^ ":") ::
wenzelm@11915
  1369
        prt_sect "fix" [] (Pretty.str o fst) fixes @
wenzelm@10810
  1370
        prt_sect "let" [Pretty.str "and"] prt_let
skalberg@15570
  1371
          (List.mapPartial (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1372
        (if forall (null o #2) asms then []
wenzelm@13425
  1373
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms)));
wenzelm@10810
  1374
wenzelm@10810
  1375
    val cases' = rev (Library.gen_distinct Library.eq_fst cases);
wenzelm@10810
  1376
  in
wenzelm@10810
  1377
    if null cases andalso not (! verbose) then []
wenzelm@10830
  1378
    else [Pretty.big_list "cases:"
wenzelm@10830
  1379
      (map (prt_case o apsnd (fn c => (#fixes c, #2 (apply_case c ctxt)))) cases')]
wenzelm@10810
  1380
  end;
wenzelm@10810
  1381
wenzelm@10810
  1382
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1383
wenzelm@10810
  1384
wenzelm@12057
  1385
(* core context *)
wenzelm@10810
  1386
wenzelm@10810
  1387
val prems_limit = ref 10;
wenzelm@10810
  1388
wenzelm@12057
  1389
fun pretty_asms ctxt =
wenzelm@10810
  1390
  let
wenzelm@12057
  1391
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1392
wenzelm@12093
  1393
    (*structures*)
wenzelm@12093
  1394
    val (_, structs, _) = syntax_of ctxt;
wenzelm@12093
  1395
    val prt_structs = if null structs then []
wenzelm@12093
  1396
      else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@12093
  1397
        Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1398
wenzelm@12057
  1399
    (*fixes*)
wenzelm@12057
  1400
    fun prt_fix (x, x') =
wenzelm@12057
  1401
      if x = x' then Pretty.str x
wenzelm@12057
  1402
      else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@12093
  1403
    val fixes = rev (filter_out
wenzelm@12093
  1404
      ((can Syntax.dest_internal o #1) orf (fn (_, x') => x' mem_string structs)) (fixes_of ctxt));
wenzelm@12093
  1405
    val prt_fixes = if null fixes then []
wenzelm@12093
  1406
      else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@12093
  1407
        Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1408
wenzelm@12057
  1409
    (*prems*)
wenzelm@10810
  1410
    val limit = ! prems_limit;
wenzelm@10810
  1411
    val prems = prems_of ctxt;
wenzelm@10810
  1412
    val len = length prems;
wenzelm@12093
  1413
    val prt_prems = if null prems then []
wenzelm@12093
  1414
      else [Pretty.big_list "prems:" ((if len <= limit then [] else [Pretty.str "..."]) @
wenzelm@12093
  1415
        map (pretty_thm ctxt) (Library.drop (len - limit, prems)))];
wenzelm@12093
  1416
wenzelm@12093
  1417
  in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1418
wenzelm@10810
  1419
wenzelm@10810
  1420
(* main context *)
wenzelm@10810
  1421
wenzelm@12291
  1422
fun pretty_context (ctxt as Context {cases, defs = (types, sorts, used, _), ...}) =
wenzelm@10810
  1423
  let
wenzelm@12057
  1424
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1425
    val prt_typ = pretty_typ ctxt;
wenzelm@12057
  1426
    val prt_sort = pretty_sort ctxt;
wenzelm@10810
  1427
wenzelm@10810
  1428
    (*theory*)
wenzelm@12057
  1429
    val pretty_thy = Pretty.block
wenzelm@12057
  1430
      [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg (sign_of ctxt)];
wenzelm@10810
  1431
wenzelm@10810
  1432
    (*defaults*)
wenzelm@10810
  1433
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1434
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1435
wenzelm@10810
  1436
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1437
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1438
wenzelm@10810
  1439
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1440
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1441
wenzelm@10810
  1442
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1443
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@10810
  1444
  in
wenzelm@10810
  1445
    verb_single (K pretty_thy) @
wenzelm@12057
  1446
    pretty_asms ctxt @
wenzelm@10810
  1447
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1448
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1449
    verb pretty_cases (K ctxt) @
wenzelm@10810
  1450
    verb_single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@10810
  1451
    verb_single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @
wenzelm@10810
  1452
    verb_single (fn () => Pretty.strs ("used type variable names:" :: used))
wenzelm@10810
  1453
  end;
wenzelm@10810
  1454
wenzelm@10810
  1455
wenzelm@13278
  1456
(* print_thms_containing *)
wenzelm@13278
  1457
wenzelm@13278
  1458
fun lthms_containing (ctxt as Context {thms = (_, _, _, index), ...}) idx =
wenzelm@13278
  1459
  let
wenzelm@13278
  1460
    fun valid (name, ths) =
skalberg@15531
  1461
      (case try (transform_error (fn () => get_thms ctxt (name, NONE))) () of
skalberg@15531
  1462
        NONE => false
skalberg@15531
  1463
      | SOME ths' => Library.equal_lists Thm.eq_thm (ths, ths'));
skalberg@15570
  1464
  in gen_distinct eq_fst (List.filter valid (FactIndex.find idx index)) end;
wenzelm@13278
  1465
wenzelm@13278
  1466
wenzelm@13284
  1467
val thms_containing_limit = ref 40;
wenzelm@13284
  1468
wenzelm@13284
  1469
fun print_thms_containing ctxt opt_limit ss =
wenzelm@13278
  1470
  let
wenzelm@13278
  1471
    val prt_term = pretty_term ctxt;
wenzelm@13282
  1472
    val prt_fact = pretty_fact ctxt;
wenzelm@13278
  1473
    fun prt_consts [] = []
wenzelm@13278
  1474
      | prt_consts cs = [Pretty.block (Pretty.breaks (Pretty.str "constants" ::
wenzelm@13278
  1475
          map (Pretty.quote o prt_term o Syntax.const) cs))];
wenzelm@13278
  1476
    fun prt_frees [] = []
wenzelm@13278
  1477
      | prt_frees xs = [Pretty.block (Pretty.breaks (Pretty.str "variables" ::
wenzelm@13278
  1478
          map (Pretty.quote o prt_term o Syntax.free) xs))];
wenzelm@13278
  1479
skalberg@15570
  1480
    val (cs, xs) = Library.foldl (FactIndex.index_term (is_known ctxt))
wenzelm@13542
  1481
      (([], []), map (read_term_dummies ctxt) ss);
skalberg@15531
  1482
    val empty_idx = null cs andalso null xs;
wenzelm@13278
  1483
wenzelm@13278
  1484
    val header =
wenzelm@13378
  1485
      if empty_idx then [Pretty.block [Pretty.str "Known facts:", Pretty.fbrk]]
wenzelm@13278
  1486
      else [Pretty.block (Pretty.breaks (Pretty.str "Facts containing" ::
wenzelm@13278
  1487
        separate (Pretty.str "and") (prt_consts cs @ prt_frees xs)) @
wenzelm@13278
  1488
          [Pretty.str ":", Pretty.fbrk])]
wenzelm@13284
  1489
    val facts =
wenzelm@13284
  1490
      PureThy.thms_containing (theory_of ctxt) (cs, xs) @ lthms_containing ctxt (cs, xs)
wenzelm@13284
  1491
      |> sort_wrt #1;
wenzelm@13284
  1492
    val len = length facts;
skalberg@15570
  1493
    val limit = getOpt (opt_limit, ! thms_containing_limit);
wenzelm@13278
  1494
  in
skalberg@15531
  1495
    if empty_idx andalso not (null ss) then
wenzelm@13278
  1496
      warning "thms_containing: no consts/frees in specification"
wenzelm@13284
  1497
    else (header @ (if len <= limit then [] else [Pretty.str "..."]) @
wenzelm@13284
  1498
        map prt_fact (Library.drop (len - limit, facts))) |> Pretty.chunks |> Pretty.writeln
wenzelm@13278
  1499
  end;
wenzelm@13278
  1500
wenzelm@13278
  1501
wenzelm@10810
  1502
wenzelm@5819
  1503
(** theory setup **)
wenzelm@5819
  1504
wenzelm@5819
  1505
val setup = [ProofDataData.init];
wenzelm@5819
  1506
wenzelm@5819
  1507
end;