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