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