src/Pure/goals.ML
author wenzelm
Mon, 20 Jun 2005 22:13:59 +0200
changeset 16486 1a12cdb6ee6b
parent 16458 4c6fd0c01d28
child 16787 b6b6e2faaa41
permissions -rw-r--r--
get_thm(s): Name;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
     1
(*  Title:      Pure/goals.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
     3
    Author:     Lawrence C Paulson and Florian Kammueller, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
     6
Old-style locales and goal stack package.  The goal stack initially
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
     7
holds a dummy proof, and can never become empty.  Each goal stack
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
     8
consists of a list of levels.  The undo list is a list of goal stacks.
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
     9
Finally, there may be a stack of pending proofs.  Additional support
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    10
for locales.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    13
signature BASIC_GOALS =
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    14
sig
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    15
  val Open_locale: xstring -> unit
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    16
  val Close_locale: xstring -> unit
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    17
  val Print_scope: unit -> unit
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    18
  val thm: xstring -> thm
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
    19
  val thms: xstring -> thm list
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
  type proof
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
    21
  val reset_goals       : unit -> unit
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    22
  val atomic_goal       : theory -> string -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    23
  val atomic_goalw      : theory -> thm list -> string -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    24
  val Goal              : string -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    25
  val Goalw             : thm list -> string -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    26
  val ba                : int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    27
  val back              : unit -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    28
  val bd                : thm -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    29
  val bds               : thm list -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    30
  val be                : thm -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    31
  val bes               : thm list -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    32
  val br                : thm -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    33
  val brs               : thm list -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    34
  val bw                : thm -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    35
  val bws               : thm list -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    36
  val by                : tactic -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    37
  val byev              : tactic list -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    38
  val chop              : unit -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    39
  val choplev           : int -> unit
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
    40
  val export_thy        : theory -> thm -> thm
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
    41
  val export            : thm -> thm
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    42
  val Export            : thm -> thm
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    43
  val fa                : unit -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    44
  val fd                : thm -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    45
  val fds               : thm list -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    46
  val fe                : thm -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    47
  val fes               : thm list -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    48
  val filter_goal       : (term*term->bool) -> thm list -> int -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    49
  val fr                : thm -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    50
  val frs               : thm list -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    51
  val getgoal           : int -> term
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    52
  val gethyps           : int -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    53
  val goal              : theory -> string -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    54
  val goalw             : theory -> thm list -> string -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    55
  val goalw_cterm       : thm list -> cterm -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    56
  val pop_proof         : unit -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    57
  val pr                : unit -> unit
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
    58
  val disable_pr        : unit -> unit
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
    59
  val enable_pr         : unit -> unit
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    60
  val prlev             : int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    61
  val prlim             : int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    62
  val premises          : unit -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    63
  val prin              : term -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    64
  val printyp           : typ -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    65
  val pprint_term       : term -> pprint_args -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    66
  val pprint_typ        : typ -> pprint_args -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    67
  val print_exn         : exn -> 'a
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    68
  val print_sign_exn    : theory -> exn -> 'a
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    69
  val prove_goal        : theory -> string -> (thm list -> tactic list) -> thm
577
776b5ba748d8 print_sign_exn: now exported, with a polymorphic type
lcp
parents: 545
diff changeset
    70
  val prove_goalw      : theory->thm list->string->(thm list->tactic list)->thm
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    71
  val prove_goalw_cterm : thm list->cterm->(thm list->tactic list)->thm
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    72
  val prove_goalw_cterm_nocheck : thm list->cterm->(thm list->tactic list)->thm
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    73
  val quick_and_dirty_prove_goalw_cterm: theory -> thm list -> cterm
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    74
    -> (thm list -> tactic list) -> thm
13712
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
    75
  val simple_prove_goal_cterm : cterm->(thm list->tactic list)->thm
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    76
  val push_proof        : unit -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    77
  val read              : string -> term
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    78
  val ren               : string -> int -> unit
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    79
  val restore_proof     : proof -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    80
  val result            : unit -> thm
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
    81
  val result_error_fn   : (thm -> string -> thm) ref
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    82
  val rotate_proof      : unit -> thm list
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    83
  val uresult           : unit -> thm
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    84
  val save_proof        : unit -> proof
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    85
  val topthm            : unit -> thm
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
    86
  val undo              : unit -> unit
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    87
  val bind_thm          : string * thm -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    88
  val bind_thms         : string * thm list -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    89
  val qed               : string -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    90
  val qed_goal          : string -> theory -> string -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    91
  val qed_goalw         : string -> theory -> thm list -> string
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    92
    -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    93
  val qed_spec_mp       : string -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    94
  val qed_goal_spec_mp  : string -> theory -> string -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    95
  val qed_goalw_spec_mp : string -> theory -> thm list -> string
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    96
    -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    97
  val no_qed: unit -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    98
  val thms_containing   : xstring list -> (string * thm) list
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    99
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   101
signature GOALS =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   102
sig
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   103
  include BASIC_GOALS
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   104
  type locale
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   105
  val print_locales: theory -> unit
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   106
  val get_thm: theory -> xstring -> thm
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   107
  val get_thms: theory -> xstring -> thm list
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   108
  val add_locale: bstring -> (bstring option) -> (string * string * mixfix) list ->
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   109
    (string * string) list -> (string * string) list -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   110
  val add_locale_i: bstring -> (bstring option) -> (string * typ * mixfix) list ->
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   111
    (string * term) list -> (string * term) list -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   112
  val open_locale: xstring -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   113
  val close_locale: xstring -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   114
  val print_scope: theory -> unit
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   115
  val read_cterm: theory -> string * typ -> cterm
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   116
end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   117
13272
eb0b565909a0 emulate old thms_containing;
wenzelm
parents: 12311
diff changeset
   118
structure Goals: GOALS =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   121
(*** Old-style locales ***)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   122
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   123
(* Locales. The theory section 'locale' declarings constants,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   124
assumptions and definitions that have local scope.  Typical form is
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   125
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   126
    locale Locale_name =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   127
      fixes   (*variables that are fixed in the locale's scope*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   128
        v :: T
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   129
      assumes (*meta-hypothesis that hold in the locale*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   130
        Asm_name "meta-formula"
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   131
      defines (*local definitions of fixed variables in terms of others*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   132
        v_def "v x == ...x..."
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   133
*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   134
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   135
(** type locale **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   136
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   137
type locale =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   138
 {ancestor: string option,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   139
  consts: (string * typ) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   140
  nosyn: string list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   141
  rules: (string * term) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   142
  defs: (string * term) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   143
  thms: (string * thm) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   144
  defaults: (string * sort) list * (string * typ) list * string list};
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   145
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   146
fun make_locale ancestor consts nosyn rules defs thms defaults =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   147
  {ancestor = ancestor, consts = consts, nosyn = nosyn, rules = rules,
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   148
   defs = defs, thms = thms, defaults = defaults}: locale;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   149
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   150
fun pretty_locale thy (name, {ancestor, consts, rules, defs, nosyn = _, thms = _, defaults = _}) =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   151
  let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   152
    val prt_typ = Pretty.quote o Sign.pretty_typ thy;
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   153
    val prt_term = Pretty.quote o Sign.pretty_term thy;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   154
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   155
    fun pretty_const (c, T) = Pretty.block
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   156
      [Pretty.str (c ^ " ::"), Pretty.brk 1, prt_typ T];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   157
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   158
    fun pretty_axiom (a, t) = Pretty.block
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   159
      [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   160
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   161
    val anc = case ancestor of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   162
                  NONE => ""
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   163
                | SOME(loc) => ((Sign.base_name loc) ^ " +")
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   164
  in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   165
    Pretty.big_list (name ^ " = " ^ anc)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   166
     [Pretty.big_list "consts:" (map pretty_const consts),
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   167
      Pretty.big_list "rules:" (map pretty_axiom rules),
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   168
      Pretty.big_list "defs:" (map pretty_axiom defs)]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   169
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   170
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   171
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   172
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   173
(** theory data **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   174
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   175
(* data kind 'Pure/old-locales' *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   176
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   177
type locale_data =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   178
  {space: NameSpace.T,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   179
    locales: locale Symtab.table,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   180
    scope: (string * locale) list ref};
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   181
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   182
fun make_locale_data space locales scope =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   183
  {space = space, locales = locales, scope = scope}: locale_data;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   184
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   185
structure LocalesData = TheoryDataFun
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   186
(struct
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   187
  val name = "Pure/old-locales";
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   188
  type T = locale_data;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   189
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   190
  val empty = make_locale_data NameSpace.empty Symtab.empty (ref []);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   191
  fun copy {space, locales, scope = ref locs} = make_locale_data space locales (ref locs);
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   192
  fun extend {space, locales, scope = _} = make_locale_data space locales (ref []);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   193
  fun merge _ ({space = space1, locales = locales1, scope = _},
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   194
    {space = space2, locales = locales2, scope = _}) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   195
      make_locale_data (NameSpace.merge (space1, space2))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   196
        (Symtab.merge (K true) (locales1, locales2))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   197
        (ref []);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   198
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   199
  fun print thy {space, locales, scope} =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   200
    let
16335
37aabcf75ee1 NameSpace.extern_table;
wenzelm
parents: 16128
diff changeset
   201
      val locs = NameSpace.extern_table (space, locales);
16128
927627fafca5 Sign.declare_name replaces NameSpace.extend;
wenzelm
parents: 16071
diff changeset
   202
      val scope_names = rev (map (NameSpace.extern space o fst) (! scope));
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   203
    in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   204
      [Pretty.big_list "locales:" (map (pretty_locale thy) locs),
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   205
        Pretty.strs ("current scope:" :: scope_names)]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   206
      |> Pretty.chunks |> Pretty.writeln
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   207
    end;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   208
end);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   209
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15574
diff changeset
   210
val _ = Context.add_setup [LocalesData.init];
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   211
val print_locales = LocalesData.print;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   212
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   213
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   214
(* access locales *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   215
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   216
fun get_locale thy name = Symtab.lookup (#locales (LocalesData.get thy), name);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   217
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   218
fun put_locale (name, locale) thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   219
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   220
    val {space, locales, scope} = LocalesData.get thy;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   221
    val space' = Sign.declare_name thy name space;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   222
    val locales' = Symtab.update ((name, locale), locales);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   223
  in thy |> LocalesData.put (make_locale_data space' locales' scope) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   224
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   225
fun lookup_locale thy xname =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   226
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   227
    val {space, locales, ...} = LocalesData.get thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   228
    val name = NameSpace.intern space xname;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   229
  in Option.map (pair name) (get_locale thy name) end;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   230
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   231
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   232
(* access scope *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   233
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   234
fun get_scope thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   235
  if eq_thy (thy, ProtoPure.thy) then []
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   236
  else ! (#scope (LocalesData.get thy));
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   237
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   238
fun change_scope f thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   239
  let val {scope, ...} = LocalesData.get thy
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   240
  in scope := f (! scope) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   241
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   242
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   243
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   244
(** scope operations **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   245
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   246
(* change scope *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   247
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   248
fun the_locale thy xname =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   249
  (case lookup_locale thy xname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   250
    SOME loc => loc
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   251
  | NONE => error ("Unknown locale " ^ quote xname));
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   252
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   253
fun open_locale xname thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   254
  let val loc = the_locale thy xname;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   255
      val anc = #ancestor(#2(loc));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   256
      val cur_sc = get_scope thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   257
      fun opn lc th = (change_scope (cons lc) th; th)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   258
  in case anc of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   259
         NONE => opn loc thy
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   260
       | SOME(loc') =>
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   261
           if loc' mem (map fst cur_sc)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   262
           then opn loc thy
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   263
           else (warning ("Opening locale " ^ quote loc' ^ ", required by " ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   264
                          quote xname);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   265
                 opn loc (open_locale (Sign.base_name loc') thy))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   266
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   267
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   268
fun pop_locale [] = error "Currently no open locales"
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   269
  | pop_locale (_ :: locs) = locs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   270
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   271
fun close_locale name thy =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   272
   let val lname = (case get_scope thy of (ln,_)::_ => ln
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   273
                                        | _ => error "No locales are open!")
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   274
       val ok = name = Sign.base_name lname
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   275
   in if ok then (change_scope pop_locale thy; thy)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   276
      else error ("locale " ^ name ^ " is not top of scope; top is " ^ lname)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   277
   end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   278
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   279
fun print_scope thy =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   280
Pretty.writeln (Pretty.strs ("current scope:" :: rev(map (Sign.base_name o fst) (get_scope thy))));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   281
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   282
(*implicit context versions*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   283
fun Open_locale xname = (open_locale xname (Context.the_context ()); ());
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   284
fun Close_locale xname = (close_locale xname (Context.the_context ()); ());
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   285
fun Print_scope () = (print_scope (Context.the_context ()); ());
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   286
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   287
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   288
(** functions for goals.ML **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   289
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   290
(* in_locale: check if hyps (: term list) of a proof are contained in the
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   291
   (current) scope. This function is needed in prepare_proof. *)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   292
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   293
fun in_locale hyps thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   294
    let val cur_sc = get_scope thy;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   295
        val rule_lists = map (#rules o snd) cur_sc;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   296
        val def_lists = map (#defs o snd) cur_sc;
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   297
        val rules = map snd (foldr (op union) [] rule_lists);
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   298
        val defs = map snd (foldr (op union) [] def_lists);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   299
        val defnrules = rules @ defs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   300
    in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   301
        hyps subset defnrules
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   302
    end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   303
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   304
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   305
(* is_open_loc: check if any locale is open, i.e. in the scope of the current thy *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   306
fun is_open_loc thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   307
    let val cur_sc = get_scope thy
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   308
    in not(null(cur_sc)) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   309
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   310
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   311
(* get theorems *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   312
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   313
fun get_thm_locale name ((_, {thms, ...}: locale)) = assoc (thms, name);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   314
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   315
fun get_thmx f get thy name =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   316
  (case get_first (get_thm_locale name) (get_scope thy) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   317
    SOME thm => f thm
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16458
diff changeset
   318
  | NONE => get thy (Name name));
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   319
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   320
val get_thm = get_thmx I PureThy.get_thm;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   321
val get_thms = get_thmx (fn x => [x]) PureThy.get_thms;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   322
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   323
fun thm name = get_thm (Context.the_context ()) name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   324
fun thms name = get_thms (Context.the_context ()) name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   325
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   326
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   327
(* get the defaults of a locale, for extension *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   328
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   329
fun get_defaults thy name =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   330
  let val (lname, loc) = the_locale thy name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   331
  in #defaults(loc)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   332
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   333
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   334
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   335
(** define locales **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   336
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   337
(* prepare types *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   338
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   339
fun read_typ thy (envT, s) =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   340
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   341
    fun def_sort (x, ~1) = assoc (envT, x)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   342
      | def_sort _ = NONE;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   343
    val T = Type.no_tvars (Sign.read_typ (thy, def_sort) s) handle TYPE (msg, _, _) => error msg;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   344
  in (Term.add_typ_tfrees (T, envT), T) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   345
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   346
fun cert_typ thy (envT, raw_T) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   347
  let val T = Type.no_tvars (Sign.certify_typ thy raw_T) handle TYPE (msg, _, _) => error msg
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   348
  in (Term.add_typ_tfrees (T, envT), T) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   349
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   350
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   351
(* prepare props *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   352
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   353
val add_frees = foldl_aterms (fn (vs, Free v) => v ins vs | (vs, _) => vs);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   354
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   355
fun enter_term t (envS, envT, used) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   356
  (Term.add_term_tfrees (t, envS), add_frees (envT, t), Term.add_term_tfree_names (t, used));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   357
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   358
fun read_axm thy ((envS, envT, used), (name, s)) =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   359
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   360
    fun def_sort (x, ~1) = assoc (envS, x)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   361
      | def_sort _ = NONE;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   362
    fun def_type (x, ~1) = assoc (envT, x)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   363
      | def_type _ = NONE;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   364
    val (_, t) = Theory.read_def_axm (thy, def_type, def_sort) used (name, s);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   365
  in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   366
    (enter_term t (envS, envT, used), t)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   367
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   368
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   369
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   370
fun cert_axm thy ((envS, envT, used), (name, raw_t)) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   371
  let val (_, t) = Theory.cert_axm thy (name, raw_t)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   372
  in (enter_term t (envS, envT, used), t) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   373
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   374
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   375
(* read_cterm: read in a string as a certified term, and respect the bindings
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   376
   that already exist for subterms. If no locale is open, this function is equal to
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   377
   Thm.read_cterm  *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   378
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   379
fun read_cterm thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   380
    let val cur_sc = get_scope thy;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   381
        val defaults = map (#defaults o snd) cur_sc;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   382
        val envS = List.concat (map #1 defaults);
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   383
        val envT = List.concat (map #2 defaults);
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   384
        val used = List.concat (map #3 defaults);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   385
        fun def_sort (x, ~1) = assoc (envS, x)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   386
          | def_sort _ = NONE;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   387
        fun def_type (x, ~1) = assoc (envT, x)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   388
          | def_type _ = NONE;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   389
    in (if (is_open_loc thy)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   390
        then (#1 o read_def_cterm (thy, def_type, def_sort) used true)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   391
        else Thm.read_cterm thy)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   392
    end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   393
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   394
(* basic functions needed for definitions and display *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   395
(* collect all locale constants of a scope, i.e. a list of locales *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   396
fun collect_consts thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   397
    let val cur_sc = get_scope thy;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   398
        val locale_list = map snd cur_sc;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   399
        val const_list = List.concat (map #consts locale_list)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   400
    in map fst const_list end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   401
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   402
(* filter out the Free's in a term *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   403
fun list_frees t =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   404
    case t of Const(c,T) => []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   405
  | Var(v,T) => []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   406
  | Free(v,T)=> [Free(v,T)]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   407
  | Bound x  => []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   408
  | Abs(a,T,u) => list_frees u
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   409
  | t1 $ t2  => (list_frees t1)  @ (list_frees t2);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   410
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   411
(* filter out all Free's in a term that are not contained
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   412
   in a list of strings. Used to prepare definitions. The list of strings
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   413
   will be the consts of the scope. We filter out the "free" Free's to be
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   414
   able to bind them *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   415
fun difflist term clist =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   416
    let val flist = list_frees term;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   417
        fun builddiff [] sl = []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   418
          | builddiff (t :: tl) sl =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   419
            let val Free(v,T) = t
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   420
            in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   421
                if (v mem sl)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   422
                then builddiff tl sl
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   423
                else t :: (builddiff tl sl)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   424
            end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   425
    in distinct(builddiff flist clist) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   426
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   427
(* Bind a term with !! over a list of "free" Free's.
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   428
   To enable definitions like x + y == .... (without quantifier).
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   429
   Complications, because x and y have to be removed from defaults *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   430
fun abs_over_free clist ((defaults: (string * sort) list * (string * typ) list * string list), (s, term)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   431
    let val diffl = rev(difflist term clist);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   432
        fun abs_o (t, (x as Free(v,T))) = all(T) $ Abs(v, T, abstract_over (x,t))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   433
          | abs_o (_ , _) = error ("Can't be: abs_over_free");
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   434
        val diffl' = map (fn (Free (s, T)) => s) diffl;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   435
        val defaults' = (#1 defaults, List.filter (fn x => not((fst x) mem diffl')) (#2 defaults), #3 defaults)
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   436
    in (defaults', (s, Library.foldl abs_o (term, diffl))) end;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   437
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   438
(* assume a definition, i.e assume the cterm of a definiton term and then eliminate
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   439
   the binding !!, so that the def can be applied as rewrite. The meta hyp will still contain !! *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   440
fun prep_hyps clist thy = forall_elim_vars(0) o Thm.assume o (Thm.cterm_of thy);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   441
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   442
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   443
(* concrete syntax *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   444
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   445
fun mark_syn c = "\\<^locale>" ^ c;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   446
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   447
fun mk_loc_tr c ts = list_comb (Free (c, dummyT), ts);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   448
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   449
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   450
(* add_locale *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   451
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   452
fun gen_add_locale prep_typ prep_term bname bancestor raw_consts raw_rules raw_defs thy =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   453
  let
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   454
    val name = Sign.full_name thy bname;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   455
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   456
    val (envSb, old_loc_consts, _) =
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   457
                    case bancestor of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   458
                       SOME(loc) => (get_defaults thy loc)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   459
                     | NONE      => ([],[],[]);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   460
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   461
    val old_nosyn = case bancestor of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   462
                       SOME(loc) => #nosyn(#2(the_locale thy loc))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   463
                     | NONE      => [];
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   464
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   465
    (* Get the full name of the ancestor *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   466
    val ancestor = case bancestor of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   467
                       SOME(loc) => SOME(#1(the_locale thy loc))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   468
                     | NONE      => NONE;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   469
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   470
     (* prepare locale consts *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   471
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   472
    fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   473
      let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   474
        val c = Syntax.const_name raw_c raw_mx;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   475
        val c_syn = mark_syn c;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   476
        val mx = Syntax.fix_mixfix raw_c raw_mx;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   477
        val (envS', T) = prep_typ thy (envS, raw_T) handle ERROR =>
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   478
          error ("The error(s) above occured in locale constant " ^ quote c);
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   479
        val trfun = if mx = Syntax.NoSyn then NONE else SOME (c_syn, mk_loc_tr c);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   480
      in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   481
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   482
    val (envS0, loc_consts_syn) = foldl_map prep_const (envSb, raw_consts);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   483
    val loc_consts = map #1 loc_consts_syn;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   484
    val loc_consts = old_loc_consts @ loc_consts;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   485
    val loc_syn = map #2 loc_consts_syn;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   486
    val nosyn = old_nosyn @ (map (#1 o #1) (List.filter (fn x => (#3(#2 x)) = NoSyn) loc_consts_syn));
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   487
    val loc_trfuns = List.mapPartial #3 loc_consts_syn;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   488
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   489
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   490
    (* 1st stage: syntax_thy *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   491
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   492
    val syntax_thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   493
      thy
14900
c66394c408f7 Syntax.default_mode;
wenzelm
parents: 14825
diff changeset
   494
      |> Theory.add_modesyntax_i Syntax.default_mode loc_syn
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   495
      |> Theory.add_trfuns ([], loc_trfuns, [], []);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   496
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   497
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   498
    (* prepare rules and defs *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   499
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   500
    fun prep_axiom (env, (a, raw_t)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   501
      let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   502
        val (env', t) = prep_term syntax_thy (env, (a, raw_t)) handle ERROR =>
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   503
          error ("The error(s) above occured in locale rule / definition " ^ quote a);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   504
      in (env', (a, t)) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   505
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   506
    val ((envS1, envT1, used1), loc_rules) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   507
      foldl_map prep_axiom ((envS0, loc_consts, map fst envS0), raw_rules);
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   508
    val (defaults, loc_defs) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   509
        foldl_map prep_axiom ((envS1, envT1, used1), raw_defs);
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   510
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   511
    val old_loc_consts = collect_consts syntax_thy;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   512
    val new_loc_consts = (map #1 loc_consts);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   513
    val all_loc_consts = old_loc_consts @ new_loc_consts;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   514
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   515
    val (defaults, loc_defs_terms) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   516
        foldl_map (abs_over_free all_loc_consts) (defaults, loc_defs);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   517
    val loc_defs_thms =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   518
        map (apsnd (prep_hyps (map #1 loc_consts) syntax_thy)) loc_defs_terms;
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   519
    val (defaults, loc_thms_terms) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   520
        foldl_map (abs_over_free all_loc_consts) (defaults, loc_rules);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   521
    val loc_thms = map (apsnd (prep_hyps (map #1 loc_consts) syntax_thy))
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   522
                       (loc_thms_terms)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   523
                   @ loc_defs_thms;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   524
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   525
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   526
    (* error messages *)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   527
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   528
    fun locale_error msg = error (msg ^ "\nFor locale " ^ quote name);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   529
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   530
    val err_dup_locale =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   531
      if is_none (get_locale thy name) then []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   532
      else ["Duplicate definition of locale " ^ quote name];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   533
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   534
    (* check if definientes are locale constants
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   535
       (in the same locale, so no redefining!) *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   536
    val err_def_head =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   537
      let fun peal_appl t =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   538
            case t of
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   539
                 t1 $ t2 => peal_appl t1
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   540
               | Free(t) => t
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   541
               | _ => locale_error ("Bad form of LHS in locale definition");
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   542
          fun lhs (_, Const ("==" , _) $  d1 $ d2) = peal_appl d1
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   543
            | lhs _ = locale_error ("Definitions must use the == relation");
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   544
          val defs = map lhs loc_defs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   545
          val check = defs subset loc_consts
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   546
      in if check then []
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   547
         else ["defined item not declared fixed in locale " ^ quote name]
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   548
      end;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   549
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   550
    (* check that variables on rhs of definitions are either fixed or on lhs *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   551
    val err_var_rhs =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   552
      let fun compare_var_sides (t, (_, Const ("==", _) $ d1 $ d2)) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   553
                let val varl1 = difflist d1 all_loc_consts;
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   554
                    val varl2 = difflist d2 all_loc_consts
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   555
                in t andalso (varl2 subset varl1)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   556
                end
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   557
            | compare_var_sides (_,_) =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   558
                locale_error ("Definitions must use the == relation")
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   559
          val check = Library.foldl compare_var_sides (true, loc_defs)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   560
      in if check then []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   561
         else ["nonfixed variable on right hand side of a locale definition in locale " ^ quote name]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   562
      end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   563
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   564
    val errs = err_dup_locale @ err_def_head @ err_var_rhs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   565
  in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   566
    if null errs then ()
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   567
    else error (cat_lines errs);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   568
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   569
    syntax_thy
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   570
    |> put_locale (name,
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   571
                   make_locale ancestor loc_consts nosyn loc_thms_terms
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   572
                                        loc_defs_terms   loc_thms defaults)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   573
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   574
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   575
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   576
val add_locale = gen_add_locale read_typ read_axm;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   577
val add_locale_i = gen_add_locale cert_typ cert_axm;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   578
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   579
(** print functions **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   580
(* idea: substitute all locale contants (Free's) that are syntactical by their
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   581
         "real" constant representation (i.e. \\<^locale>constname).
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   582
   - function const_ssubst does this substitution
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   583
   - function pretty_term:
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   584
             if locale is open then do this substitution & then call Sign.pretty_term
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   585
             else call Sign.pretty_term
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   586
*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   587
(* substitutes all Free variables s in t by Const's s *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   588
fun const_ssubst t s =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   589
    case t  of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   590
        Free(v,T) => if v = s then Const("\\<^locale>" ^ s,T) else Free(v,T)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   591
      | Const(c,T) => Const(c,T)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   592
      | Var(v,T) => Var(v,T)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   593
      | Bound x  => Bound x
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   594
      | Abs(a,T,u) => Abs(a,T, const_ssubst u s)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   595
      | t1 $ t2  => const_ssubst t1 s $ const_ssubst t2 s;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   596
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   597
(* FIXME: improve: can be expressed with foldl *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   598
fun const_ssubst_list [] t = t
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   599
  | const_ssubst_list (s :: l) t = const_ssubst_list l (const_ssubst t s);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   600
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   601
(* pretty_term *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   602
fun pretty_term thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   603
    if (is_open_loc thy) then
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   604
        let val locale_list = map snd(get_scope thy);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   605
            val nosyn = List.concat (map #nosyn locale_list);
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   606
            val str_list = (collect_consts thy) \\ nosyn
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   607
        in Sign.pretty_term thy o (const_ssubst_list str_list)
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   608
        end
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   609
    else Sign.pretty_term thy;
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   610
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   611
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   612
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   613
(*** Goal package ***)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   614
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   615
(*Each level of goal stack includes a proof state and alternative states,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   616
  the output of the tactic applied to the preceeding level.  *)
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   617
type gstack = (thm * thm Seq.seq) list;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   618
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   619
datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   620
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   621
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   622
(*** References ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   623
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   624
(*Current assumption list -- set by "goal".*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   625
val curr_prems = ref([] : thm list);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   626
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   627
(*Return assumption list -- useful if you didn't save "goal"'s result. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   628
fun premises() = !curr_prems;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   629
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   630
(*Current result maker -- set by "goal", used by "result".  *)
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   631
fun init_mkresult _ = error "No goal has been supplied in subgoal module";
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   632
val curr_mkresult = ref (init_mkresult: bool*thm->thm);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   633
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   634
val dummy = Thm.trivial (read_cterm ProtoPure.thy ("PROP No_goal_has_been_supplied", propT));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   635
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   636
(*List of previous goal stacks, for the undo operation.  Set by setstate.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   637
  A list of lists!*)
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   638
val undo_list = ref([[(dummy, Seq.empty)]] : gstack list);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   639
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   640
(* Stack of proof attempts *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   641
val proofstack = ref([]: proof list);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   642
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   643
(*reset all refs*)
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   644
fun reset_goals () =
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   645
  (curr_prems := []; curr_mkresult := init_mkresult;
7942
4f8cf6552787 reset_goals no longer empties the proof stack
oheimb
parents: 7637
diff changeset
   646
    undo_list := [[(dummy, Seq.empty)]]);
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   647
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   648
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   649
(*** Setting up goal-directed proof ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   650
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   651
(*Generates the list of new theories when the proof state's theory changes*)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   652
fun thy_error (thy,thy') =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   653
  let val names = Context.names_of thy' \\ Context.names_of thy
3974
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   654
  in  case names of
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   655
        [name] => "\nNew theory: " ^ name
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   656
      | _       => "\nNew theories: " ^ space_implode ", " names
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   657
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   658
1928
3d1d73e3d185 Added ref to allow suppression of error msgs
paulson
parents: 1628
diff changeset
   659
(*Default action is to print an error message; could be suppressed for
3d1d73e3d185 Added ref to allow suppression of error msgs
paulson
parents: 1628
diff changeset
   660
  special applications.*)
3669
3384c6f1f095 removed print_goals_ref (which was broken anyway);
wenzelm
parents: 3536
diff changeset
   661
fun result_error_default state msg : thm =
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   662
  Pretty.str "Bad final proof state:" :: Display.pretty_goals (!goals_limit) state @
11562
804ee65ee1a1 result_error_default: include msg;
wenzelm
parents: 11554
diff changeset
   663
    [Pretty.str msg, Pretty.str "Proof failed!"] |> Pretty.chunks |> Pretty.string_of |> error;
1928
3d1d73e3d185 Added ref to allow suppression of error msgs
paulson
parents: 1628
diff changeset
   664
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   665
val result_error_fn = ref result_error_default;
1928
3d1d73e3d185 Added ref to allow suppression of error msgs
paulson
parents: 1628
diff changeset
   666
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   667
(* alternative to standard: this function does not discharge the hypotheses
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   668
   first. Is used for locales, in the following function prepare_proof *)
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   669
fun varify th =
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   670
  let val {maxidx,...} = rep_thm th
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   671
  in
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   672
    th |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
7637
16347ef1d222 use Drule.strip_shyps_warning;
wenzelm
parents: 7265
diff changeset
   673
       |> Drule.strip_shyps_warning
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   674
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   675
  end;
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   676
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   677
(** exporting a theorem out of a locale means turning all meta-hyps into assumptions
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   678
    and expand and cancel the locale definitions.
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   679
    export goes through all levels in case of nested locales, whereas
6189
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   680
    export_thy just goes one up. **)
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   681
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   682
fun get_top_scope_thms thy =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   683
   let val sc = get_scope thy
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   684
   in if null sc then (warning "No locale in theory"; [])
14643
wenzelm
parents: 13799
diff changeset
   685
      else map Thm.prop_of (map #2 (#thms(snd(hd sc))))
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   686
   end;
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   687
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   688
fun implies_intr_some_hyps thy A_set th =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   689
   let
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   690
       val used_As = A_set inter #hyps(rep_thm(th));
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   691
       val ctl = map (cterm_of thy) used_As
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   692
   in Library.foldl (fn (x, y) => Thm.implies_intr y x) (th, ctl)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   693
   end;
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   694
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   695
fun standard_some thy A_set th =
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   696
  let val {maxidx,...} = rep_thm th
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   697
  in
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   698
    th |> implies_intr_some_hyps thy A_set
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   699
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
7637
16347ef1d222 use Drule.strip_shyps_warning;
wenzelm
parents: 7265
diff changeset
   700
       |> Drule.strip_shyps_warning
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   701
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   702
  end;
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   703
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   704
fun export_thy thy th =
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   705
  let val A_set = get_top_scope_thms thy
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   706
  in
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   707
     standard_some thy [] (Seq.hd ((REPEAT (FIRSTGOAL (rtac reflexive_thm))) (standard_some thy A_set th)))
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   708
  end;
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   709
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   710
val export = standard o Seq.hd o (REPEAT (FIRSTGOAL (rtac reflexive_thm))) o standard;
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   711
6189
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   712
fun Export th = export_thy (the_context ()) th;
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   713
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   714
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   715
(*Common treatment of "goal" and "prove_goal":
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   716
  Return assumptions, initial proof state, and function to make result.
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   717
  "atomic" indicates if the goal should be wrapped up in the function
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   718
  "Goal::prop=>prop" to avoid assumptions being returned separately.
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   719
*)
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   720
fun prepare_proof atomic rths chorn =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   721
  let val {thy, t=horn,...} = rep_cterm chorn;
9533
fb68b7163969 Term.no_dummy_patterns;
wenzelm
parents: 8999
diff changeset
   722
      val _ = Term.no_dummy_patterns horn handle TERM (msg, _) => error msg;
13660
e36798726ca4 Changed type of Logic.strip_horn.
berghofe
parents: 13646
diff changeset
   723
      val (As, B) = Logic.strip_horn horn;
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   724
      val atoms = atomic andalso
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   725
            forall (fn t => not(Logic.is_implies t orelse Logic.is_all t)) As;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   726
      val (As,B) = if atoms then ([],horn) else (As,B);
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   727
      val cAs = map (cterm_of thy) As;
11963
a6608d44a46b impose hyps on initial goal configuration (prevents res_inst_tac problems);
wenzelm
parents: 11884
diff changeset
   728
      val prems = map (rewrite_rule rths o forall_elim_vars 0 o Thm.assume) cAs;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   729
      val cB = cterm_of thy B;
11963
a6608d44a46b impose hyps on initial goal configuration (prevents res_inst_tac problems);
wenzelm
parents: 11884
diff changeset
   730
      val st0 = let val st = Drule.impose_hyps cAs (Drule.mk_triv_goal cB)
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   731
                in  rewrite_goals_rule rths st end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   732
      (*discharges assumptions from state in the order they appear in goal;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   733
        checks (if requested) that resulting theorem is equivalent to goal. *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   734
      fun mkresult (check,state) =
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   735
        let val state = Seq.hd (flexflex_rule state)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   736
                        handle THM _ => state   (*smash flexflex pairs*)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   737
            val ngoals = nprems_of state
7637
16347ef1d222 use Drule.strip_shyps_warning;
wenzelm
parents: 7265
diff changeset
   738
            val ath = implies_intr_list cAs state
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   739
            val th = ath RS Drule.rev_triv_goal
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   740
            val {hyps,prop,thy=thy',...} = rep_thm th
9573
5cadc8146573 the "nocheck" versions of goal functions now standardize their result
paulson
parents: 9533
diff changeset
   741
            val final_th = if (null(hyps)) then standard th else varify th
5cadc8146573 the "nocheck" versions of goal functions now standardize their result
paulson
parents: 9533
diff changeset
   742
        in  if not check then final_th
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   743
            else if not (eq_thy(thy,thy')) then !result_error_fn state
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   744
                ("Theory of proof state has changed!" ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   745
                 thy_error (thy,thy'))
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   746
            else if ngoals>0 then !result_error_fn state
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   747
                (string_of_int ngoals ^ " unsolved goals!")
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   748
            else if (not (null hyps) andalso (not (in_locale hyps thy)))
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   749
                 then !result_error_fn state
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   750
                  ("Additional hypotheses:\n" ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   751
                   cat_lines
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   752
                    (map (Sign.string_of_term thy)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   753
                     (List.filter (fn x => (not (in_locale [x] thy)))
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   754
                      hyps)))
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   755
            else if Pattern.matches (Sign.tsig_of thy)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   756
                                    (Envir.beta_norm (term_of chorn), Envir.beta_norm prop)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   757
                 then final_th
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   758
            else  !result_error_fn state "proved a different theorem"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   759
        end
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 577
diff changeset
   760
  in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   761
     if eq_thy(thy, Thm.theory_of_thm st0)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   762
     then (prems, st0, mkresult)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   763
     else error ("Definitions would change the proof state's theory" ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   764
                 thy_error (thy, Thm.theory_of_thm st0))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   765
  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   766
  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   767
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   768
(*Prints exceptions readably to users*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   769
fun print_sign_exn_unit thy e =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   770
  case e of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   771
     THM (msg,i,thms) =>
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   772
         (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   773
          List.app print_thm thms)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   774
   | THEORY (msg,thys) =>
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   775
         (writeln ("Exception THEORY raised:\n" ^ msg);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   776
          List.app (writeln o Context.str_of_thy) thys)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   777
   | TERM (msg,ts) =>
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   778
         (writeln ("Exception TERM raised:\n" ^ msg);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   779
          List.app (writeln o Sign.string_of_term thy) ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   780
   | TYPE (msg,Ts,ts) =>
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   781
         (writeln ("Exception TYPE raised:\n" ^ msg);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   782
          List.app (writeln o Sign.string_of_typ thy) Ts;
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   783
          List.app (writeln o Sign.string_of_term thy) ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   784
   | e => raise e;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   785
577
776b5ba748d8 print_sign_exn: now exported, with a polymorphic type
lcp
parents: 545
diff changeset
   786
(*Prints an exception, then fails*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   787
fun print_sign_exn thy e = (print_sign_exn_unit thy e; raise ERROR);
577
776b5ba748d8 print_sign_exn: now exported, with a polymorphic type
lcp
parents: 545
diff changeset
   788
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   789
(** the prove_goal.... commands
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   790
    Prove theorem using the listed tactics; check it has the specified form.
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   791
    Augment theory with all type assignments of goal.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   792
    Syntax is similar to "goal" command for easy keyboard use. **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   793
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   794
(*Version taking the goal as a cterm*)
5311
f3f71669878e Rule mk_triv_goal for making instances of triv_goal
paulson
parents: 5246
diff changeset
   795
fun prove_goalw_cterm_general check rths chorn tacsf =
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   796
  let val (prems, st0, mkresult) = prepare_proof false rths chorn
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   797
      val tac = EVERY (tacsf prems)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   798
      fun statef() =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   799
          (case Seq.pull (tac st0) of
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   800
               SOME(st,_) => st
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   801
             | _ => error ("prove_goal: tactic failed"))
14825
8cdf5a813cec Output.timing;
wenzelm
parents: 14643
diff changeset
   802
  in  mkresult (check, cond_timeit (!Output.timing) statef)  end
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   803
  handle e => (print_sign_exn_unit (#thy (rep_cterm chorn)) e;
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   804
               writeln ("The exception above was raised for\n" ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   805
                      Display.string_of_cterm chorn); raise e);
545
fc4ff96bb0e9 Pure/goals.ML: prove_goalw_cterm now does its own exception-handling, moved
lcp
parents: 253
diff changeset
   806
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   807
(*Two variants: one checking the result, one not.
5614
0e8b45a7d104 Now prove_goalw_cterm never prints timing statistics
paulson
parents: 5311
diff changeset
   808
  Neither prints runtime messages: they are for internal packages.*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   809
fun prove_goalw_cterm rths chorn =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   810
        setmp Output.timing false (prove_goalw_cterm_general true rths chorn)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   811
and prove_goalw_cterm_nocheck rths chorn =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   812
        setmp Output.timing false (prove_goalw_cterm_general false rths chorn);
5311
f3f71669878e Rule mk_triv_goal for making instances of triv_goal
paulson
parents: 5246
diff changeset
   813
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   814
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   815
(*Version taking the goal as a string*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   816
fun prove_goalw thy rths agoal tacsf =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   817
  let val chorn = read_cterm thy (agoal, propT)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   818
  in prove_goalw_cterm_general true rths chorn tacsf end
545
fc4ff96bb0e9 Pure/goals.ML: prove_goalw_cterm now does its own exception-handling, moved
lcp
parents: 253
diff changeset
   819
  handle ERROR => error (*from read_cterm?*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   820
                ("The error(s) above occurred for " ^ quote agoal);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   821
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   822
(*String version with no meta-rewrite-rules*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   823
fun prove_goal thy = prove_goalw thy [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   824
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   825
(*quick and dirty version (conditional)*)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   826
fun quick_and_dirty_prove_goalw_cterm thy rews ct tacs =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   827
  prove_goalw_cterm rews ct
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   828
    (if ! quick_and_dirty then (K [SkipProof.cheat_tac thy]) else tacs);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   829
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   830
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   831
(*** Commands etc ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   832
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   833
(*Return the current goal stack, if any, from undo_list*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   834
fun getstate() : gstack = case !undo_list of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   835
      []   => error"No current state in subgoal module"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   836
    | x::_ => x;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   837
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   838
(*Pops the given goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   839
fun pop [] = error"Cannot go back past the beginning of the proof!"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   840
  | pop (pair::pairs) = (pair,pairs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   841
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   842
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   843
(* Print a level of the goal stack -- subject to quiet mode *)
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   844
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   845
val quiet = ref false;
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   846
fun disable_pr () = quiet := true;
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   847
fun enable_pr () = quiet := false;
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   848
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   849
fun print_top ((th, _), pairs) =
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   850
  if ! quiet then ()
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   851
  else ! Display.print_current_goals_fn (length pairs) (! goals_limit) th;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   852
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   853
(*Printing can raise exceptions, so the assignment occurs last.
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   854
  Can do   setstate[(st,Seq.empty)]  to set st as the state.  *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   855
fun setstate newgoals =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   856
  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   857
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   858
(*Given a proof state transformation, return a command that updates
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   859
    the goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   860
fun make_command com = setstate (com (pop (getstate())));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   861
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   862
(*Apply a function on proof states to the current goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   863
fun apply_fun f = f (pop(getstate()));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   864
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   865
(*Return the top theorem, representing the proof state*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   866
fun topthm () = apply_fun  (fn ((th,_), _) => th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   867
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   868
(*Return the final result.  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   869
fun result () = !curr_mkresult (true, topthm());
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   870
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   871
(*Return the result UNCHECKED that it equals the goal -- for synthesis,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   872
  answer extraction, or other instantiation of Vars *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   873
fun uresult () = !curr_mkresult (false, topthm());
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   874
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   875
(*Get subgoal i from goal stack*)
13799
77614fe09362 Moved get_goal, prems_of_goal and concl_of_goal from goals.ML to logic.ML
berghofe
parents: 13712
diff changeset
   876
fun getgoal i = Logic.get_goal (prop_of (topthm())) i;
13646
46ed3d042ba5 Ported find_intro/elim to Isar.
nipkow
parents: 13272
diff changeset
   877
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   878
(*Return subgoal i's hypotheses as meta-level assumptions.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   879
  For debugging uses of METAHYPS*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   880
local exception GETHYPS of thm list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   881
in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   882
fun gethyps i =
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   883
    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   884
    handle GETHYPS hyps => hyps
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   885
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   886
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   887
(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   888
fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   889
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   890
(*For inspecting earlier levels of the backward proof*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   891
fun chop_level n (pair,pairs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   892
  let val level = length pairs
2126
d927beecedf8 Allowing negative levels (as offsets) in prlev and choplev
paulson
parents: 1928
diff changeset
   893
  in  if n<0 andalso ~n <= level
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   894
      then  List.drop (pair::pairs, ~n)
2126
d927beecedf8 Allowing negative levels (as offsets) in prlev and choplev
paulson
parents: 1928
diff changeset
   895
      else if 0<=n andalso n<= level
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   896
      then  List.drop (pair::pairs, level - n)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   897
      else  error ("Level number must lie between 0 and " ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   898
                   string_of_int level)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   899
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   900
2514
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   901
(*Print the given level of the proof; prlev ~1 prints previous level*)
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   902
fun prlev n = (enable_pr (); apply_fun (print_top o pop o (chop_level n)));
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   903
fun pr () = (enable_pr (); apply_fun print_top);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   904
2514
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   905
(*Set goals_limit and print again*)
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   906
fun prlim n = (goals_limit:=n; pr());
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   907
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   908
(** the goal.... commands
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   909
    Read main goal.  Set global variables curr_prems, curr_mkresult.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   910
    Initial subgoal and premises are rewritten using rths. **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   911
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   912
(*Version taking the goal as a cterm; if you have a term t and theory thy, use
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   913
    goalw_cterm rths (cterm_of thy t);      *)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   914
fun agoalw_cterm atomic rths chorn =
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   915
  let val (prems, st0, mkresult) = prepare_proof atomic rths chorn
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   916
  in  undo_list := [];
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   917
      setstate [ (st0, Seq.empty) ];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   918
      curr_prems := prems;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   919
      curr_mkresult := mkresult;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   920
      prems
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   921
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   922
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   923
val goalw_cterm = agoalw_cterm false;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   924
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   925
(*Version taking the goal as a string*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   926
fun agoalw atomic thy rths agoal =
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   927
    agoalw_cterm atomic rths (read_cterm thy (agoal, propT))
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   928
    handle ERROR => error (*from type_assign, etc via prepare_proof*)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   929
        ("The error(s) above occurred for " ^ quote agoal);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   930
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   931
val goalw = agoalw false;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   932
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   933
(*String version with no meta-rewrite-rules*)
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   934
fun agoal atomic thy = agoalw atomic thy [];
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   935
val goal = agoal false;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   936
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   937
(*now the versions that wrap the goal up in `Goal' to make it atomic*)
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   938
val atomic_goalw = agoalw true;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   939
val atomic_goal = agoal true;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   940
6189
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   941
(*implicit context versions*)
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   942
fun Goal s = atomic_goal (Context.the_context ()) s;
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   943
fun Goalw thms s = atomic_goalw (Context.the_context ()) thms s;
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   944
13712
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   945
(*simple version with minimal amount of checking and postprocessing*)
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   946
fun simple_prove_goal_cterm G f =
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   947
  let
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   948
    val As = Drule.strip_imp_prems G;
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   949
    val B = Drule.strip_imp_concl G;
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   950
    val asms = map (norm_hhf_rule o assume) As;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   951
    fun check NONE = error "prove_goal: tactic failed"
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   952
      | check (SOME (thm, _)) = (case nprems_of thm of
13712
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   953
            0 => thm
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   954
          | i => !result_error_fn thm (string_of_int i ^ " unsolved goals!"))
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   955
  in
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   956
    standard (implies_intr_list As
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   957
      (check (Seq.pull (EVERY (f asms) (trivial B)))))
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   958
  end;
82d7fc25a225 Added simple_prove_goal_cterm.
berghofe
parents: 13660
diff changeset
   959
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   960
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   961
(*Proof step "by" the given tactic -- apply tactic to the proof state*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   962
fun by_com tac ((th,ths), pairs) : gstack =
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   963
  (case  Seq.pull(tac th)  of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   964
     NONE      => error"by: tactic failed"
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   965
   | SOME(th2,ths2) =>
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   966
       (if eq_thm(th,th2)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   967
          then warning "Warning: same as previous level"
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   968
          else if eq_thm_thy(th,th2) then ()
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   969
          else warning ("Warning: theory of proof state has changed" ^
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   970
                       thy_error (Thm.theory_of_thm th, Thm.theory_of_thm th2));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   971
       ((th2,ths2)::(th,ths)::pairs)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   972
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   973
fun by tac = cond_timeit (!Output.timing)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   974
    (fn() => make_command (by_com tac));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   975
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   976
(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   977
   Good for debugging proofs involving prove_goal.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   978
val byev = by o EVERY;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   979
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   980
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   981
(*Backtracking means find an alternative result from a tactic.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   982
  If none at this level, try earlier levels*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   983
fun backtrack [] = error"back: no alternatives"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   984
  | backtrack ((th,thstr) :: pairs) =
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   985
     (case Seq.pull thstr of
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   986
          NONE      => (writeln"Going back a level..."; backtrack pairs)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   987
        | SOME(th2,thstr2) =>
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   988
           (if eq_thm(th,th2)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   989
              then warning "Warning: same as previous choice at this level"
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   990
              else if eq_thm_thy(th,th2) then ()
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   991
              else warning "Warning: theory of proof state has changed";
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
   992
            (th2,thstr2)::pairs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   993
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   994
fun back() = setstate (backtrack (getstate()));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   995
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   996
(*Chop back to previous level of the proof*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   997
fun choplev n = make_command (chop_level n);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   998
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   999
(*Chopping back the goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1000
fun chop () = make_command (fn (_,pairs) => pairs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1001
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1002
(*Restore the previous proof state;  discard current state. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1003
fun undo() = case !undo_list of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1004
      [] => error"No proof state"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1005
    | [_] => error"Already at initial state"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1006
    | _::newundo =>  (undo_list := newundo;  pr()) ;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1007
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1008
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1009
(*** Managing the proof stack ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1010
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1011
fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1012
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1013
fun restore_proof(Proof(ul,prems,mk)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1014
 (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1015
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1016
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1017
fun top_proof() = case !proofstack of
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1018
        [] => error("Stack of proof attempts is empty!")
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1019
    | p::ps => (p,ps);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1020
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1021
(*  push a copy of the current proof state on to the stack *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1022
fun push_proof() = (proofstack := (save_proof() :: !proofstack));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1023
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1024
(* discard the top proof state of the stack *)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1025
fun pop_proof() =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1026
  let val (p,ps) = top_proof()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1027
      val prems = restore_proof p
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1028
  in proofstack := ps;  pr();  prems end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1029
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1030
(* rotate the stack so that the top element goes to the bottom *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1031
fun rotate_proof() = let val (p,ps) = top_proof()
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1032
                    in proofstack := ps@[save_proof()];
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1033
                       restore_proof p;
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1034
                       pr();
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1035
                       !curr_prems
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1036
                    end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1037
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1038
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1039
(** Shortcuts for commonly-used tactics **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1040
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1041
fun bws rls = by (rewrite_goals_tac rls);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1042
fun bw rl = bws [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1043
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1044
fun brs rls i = by (resolve_tac rls i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1045
fun br rl = brs [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1046
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1047
fun bes rls i = by (eresolve_tac rls i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1048
fun be rl = bes [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1049
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1050
fun bds rls i = by (dresolve_tac rls i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1051
fun bd rl = bds [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1052
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1053
fun ba i = by (assume_tac i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1054
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1055
fun ren str i = by (rename_tac str i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1056
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1057
(** Shortcuts to work on the first applicable subgoal **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1058
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1059
fun frs rls = by (FIRSTGOAL (trace_goalno_tac (resolve_tac rls)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1060
fun fr rl = frs [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1061
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1062
fun fes rls = by (FIRSTGOAL (trace_goalno_tac (eresolve_tac rls)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1063
fun fe rl = fes [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1064
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1065
fun fds rls = by (FIRSTGOAL (trace_goalno_tac (dresolve_tac rls)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1066
fun fd rl = fds [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1067
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1068
fun fa() = by (FIRSTGOAL (trace_goalno_tac assume_tac));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1069
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1070
(** Reading and printing terms wrt the current theory **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1071
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1072
fun top_sg() = Thm.theory_of_thm (topthm());
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1073
8086
78e254305ae6 TypeInfer.logicT;
wenzelm
parents: 7942
diff changeset
  1074
fun read s = term_of (read_cterm (top_sg()) (s, TypeInfer.logicT));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1075
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1076
(*Print a term under the current theory of the proof state*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1077
fun prin t = writeln (Sign.string_of_term (top_sg()) t);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1078
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1079
fun printyp T = writeln (Sign.string_of_typ (top_sg()) T);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1080
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1081
fun pprint_term t = Sign.pprint_term (top_sg()) t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1082
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1083
fun pprint_typ T = Sign.pprint_typ (top_sg()) T;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1084
1628
60136fdd80c4 Added functions pr_latex and printgoal_latex which
berghofe
parents: 1580
diff changeset
  1085
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1086
(*Prints exceptions nicely at top level;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1087
  raises the exception in order to have a polymorphic type!*)
914
cae574c09137 Now prove_goalw_cterm calls print_sign_exn_unit, so that the rest
lcp
parents: 696
diff changeset
  1088
fun print_exn e = (print_sign_exn_unit (top_sg()) e;  raise e);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1089
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1090
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1091
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1092
(** theorem database operations **)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1093
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1094
(* storing *)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1095
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1096
fun bind_thm (name, thm) = ThmDatabase.ml_store_thm (name, standard thm);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1097
fun bind_thms (name, thms) = ThmDatabase.ml_store_thms (name, map standard thms);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1098
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1099
fun qed name = ThmDatabase.ml_store_thm (name, result ());
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1100
fun qed_goal name thy goal tacsf = ThmDatabase.ml_store_thm (name, prove_goal thy goal tacsf);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1101
fun qed_goalw name thy rews goal tacsf =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1102
  ThmDatabase.ml_store_thm (name, prove_goalw thy rews goal tacsf);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1103
fun qed_spec_mp name =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1104
  ThmDatabase.ml_store_thm (name, ObjectLogic.rulify_no_asm (result ()));
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1105
fun qed_goal_spec_mp name thy s p =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1106
  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goal thy s p));
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1107
fun qed_goalw_spec_mp name thy defs s p =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1108
  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goalw thy defs s p));
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1109
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1110
fun no_qed () = ();
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1111
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1112
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1113
(* retrieval *)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1114
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1115
fun theory_of_goal () = Thm.theory_of_thm (topthm ());
16021
ff83bc2151bf findI/Es/E: adapted to FindTheorems.find_XXX, results use thmref instead of string;
wenzelm
parents: 15801
diff changeset
  1116
val context_of_goal = ProofContext.init o theory_of_goal;
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1117
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1118
fun thms_containing raw_consts =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1119
  let
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1120
    val thy = theory_of_goal ();
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1121
    val consts = map (Sign.intern_const thy) raw_consts;
13272
eb0b565909a0 emulate old thms_containing;
wenzelm
parents: 12311
diff changeset
  1122
  in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16335
diff changeset
  1123
    (case List.filter (is_none o Sign.const_type thy) consts of
13272
eb0b565909a0 emulate old thms_containing;
wenzelm
parents: 12311
diff changeset
  1124
      [] => ()
eb0b565909a0 emulate old thms_containing;
wenzelm
parents: 12311
diff changeset
  1125
    | cs => error ("thms_containing: undeclared consts " ^ commas_quote cs));
13646
46ed3d042ba5 Ported find_intro/elim to Isar.
nipkow
parents: 13272
diff changeset
  1126
    PureThy.thms_containing_consts thy consts
13272
eb0b565909a0 emulate old thms_containing;
wenzelm
parents: 12311
diff changeset
  1127
  end;
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1128
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1129
end;
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
  1130
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
  1131
structure BasicGoals: BASIC_GOALS = Goals;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
  1132
open BasicGoals;