src/Pure/goals.ML
author wenzelm
Wed, 28 Nov 2001 00:46:26 +0100
changeset 12311 ce5f9e61c037
parent 12123 739eba13e2cd
child 13272 eb0b565909a0
permissions -rw-r--r--
theory data: removed obsolete finish method;
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
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    75
  val push_proof	: unit -> unit
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    76
  val read		: string -> term
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    77
  val ren		: string -> int -> unit
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    78
  val restore_proof	: proof -> thm list
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    79
  val result		: unit -> thm  
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
    80
  val result_error_fn   : (thm -> string -> thm) ref
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    81
  val rotate_proof	: unit -> thm list
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    82
  val uresult		: unit -> thm  
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    83
  val save_proof	: unit -> proof
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    84
  val topthm		: unit -> thm
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    85
  val undo		: unit -> unit
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    86
  val bind_thm          : string * thm -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    87
  val bind_thms         : string * thm list -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    88
  val qed               : string -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    89
  val qed_goal          : string -> theory -> string -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    90
  val qed_goalw         : string -> theory -> thm list -> string
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    91
    -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    92
  val qed_spec_mp       : string -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    93
  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
    94
  val qed_goalw_spec_mp : string -> theory -> thm list -> string
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    95
    -> (thm list -> tactic list) -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    96
  val no_qed: unit -> unit
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    97
  val thms_containing   : xstring list -> (string * thm) list
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    98
  val findI             : int -> (string * thm) list
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
    99
  val findEs            : int -> (string * thm) list
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   100
  val findE             : int -> int -> (string * thm) list
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   101
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   103
signature GOALS =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   104
sig
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   105
  include BASIC_GOALS
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   106
  type locale
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   107
  val print_locales: theory -> unit
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   108
  val get_thm: theory -> xstring -> thm
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   109
  val get_thms: theory -> xstring -> thm list
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   110
  val add_locale: bstring -> (bstring option) -> (string * string * mixfix) list ->
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   111
    (string * string) list -> (string * string) list -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   112
  val add_locale_i: bstring -> (bstring option) -> (string * typ * mixfix) list ->
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   113
    (string * term) list -> (string * term) list -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   114
  val open_locale: xstring -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   115
  val close_locale: xstring -> theory -> theory
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   116
  val print_scope: theory -> unit
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   117
  val read_cterm: Sign.sg -> string * typ -> cterm
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   118
  val setup: (theory -> theory) list
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   119
end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   120
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   121
structure Goals : GOALS =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   124
(*** Old-style locales ***)
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
(* Locales. The theory section 'locale' declarings constants,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   127
assumptions and definitions that have local scope.  Typical form is
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   128
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   129
    locale Locale_name =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   130
      fixes   (*variables that are fixed in the locale's scope*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   131
	v :: T
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   132
      assumes (*meta-hypothesis that hold in the locale*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   133
	Asm_name "meta-formula"  
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   134
      defines (*local definitions of fixed variables in terms of others*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   135
	v_def "v x == ...x..."
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
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   138
(** type locale **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   139
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   140
type locale =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   141
 {ancestor: string option,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   142
  consts: (string * typ) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   143
  nosyn: string list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   144
  rules: (string * term) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   145
  defs: (string * term) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   146
  thms: (string * thm) list,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   147
  defaults: (string * sort) list * (string * typ) list * string list};
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   148
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   149
fun make_locale ancestor consts nosyn rules defs thms defaults =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   150
  {ancestor = ancestor, consts = consts, nosyn = nosyn, rules = rules, 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   151
   defs = defs, thms = thms, defaults = defaults}: locale;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   152
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   153
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
   154
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   155
    val prt_typ = Pretty.quote o Sign.pretty_typ sg;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   156
    val prt_term = Pretty.quote o Sign.pretty_term sg;
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_const (c, T) = Pretty.block
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   159
      [Pretty.str (c ^ " ::"), Pretty.brk 1, prt_typ 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
    fun pretty_axiom (a, t) = Pretty.block
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   162
      [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   163
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   164
    val anc = case ancestor of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   165
                  None => ""
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   166
                | Some(loc) => ((Sign.base_name loc) ^ " +")
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   167
  in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   168
    Pretty.big_list (name ^ " = " ^ anc)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   169
     [Pretty.big_list "consts:" (map pretty_const consts),
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   170
      Pretty.big_list "rules:" (map pretty_axiom rules),
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   171
      Pretty.big_list "defs:" (map pretty_axiom defs)]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   172
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   173
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
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   176
(** theory data **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   177
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   178
(* data kind 'Pure/old-locales' *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   179
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   180
type locale_data =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   181
  {space: NameSpace.T,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   182
    locales: locale Symtab.table,
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   183
    scope: (string * locale) list ref};
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
fun make_locale_data space locales scope =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   186
  {space = space, locales = locales, scope = scope}: locale_data;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   187
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   188
structure LocalesArgs =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   189
struct
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   190
  val name = "Pure/old-locales";
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   191
  type T = locale_data;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   192
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   193
  val empty = make_locale_data NameSpace.empty Symtab.empty (ref []);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   194
  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
   195
  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
   196
  fun merge ({space = space1, locales = locales1, scope = _},
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   197
    {space = space2, locales = locales2, scope = _}) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   198
      make_locale_data (NameSpace.merge (space1, space2))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   199
        (Symtab.merge (K true) (locales1, locales2))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   200
        (ref []);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   201
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   202
  fun print sg {space, locales, scope} =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   203
    let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   204
      fun extrn name =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   205
        if ! long_names then name else NameSpace.extern space name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   206
      val locs = map (apfst extrn) (Symtab.dest locales);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   207
      val scope_names = rev (map (extrn o fst) (! scope));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   208
    in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   209
      [Display.pretty_name_space ("locale name space", space),
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   210
        Pretty.big_list "locales:" (map (pretty_locale sg) locs),
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   211
        Pretty.strs ("current scope:" :: scope_names)]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   212
      |> Pretty.chunks |> Pretty.writeln
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   213
    end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   214
end;
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
structure LocalesData = TheoryDataFun(LocalesArgs);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   218
val print_locales = LocalesData.print;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   219
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
(* access locales *)
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 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
   224
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   225
val get_locale = get_locale_sg o Theory.sign_of;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   226
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   227
fun put_locale (name, locale) thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   228
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   229
    val {space, locales, scope} = LocalesData.get thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   230
    val space' = NameSpace.extend (space, [name]);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   231
    val locales' = Symtab.update ((name, locale), locales);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   232
  in thy |> LocalesData.put (make_locale_data space' locales' scope) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   233
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   234
fun lookup_locale thy xname =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   235
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   236
    val {space, locales, ...} = LocalesData.get thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   237
    val name = NameSpace.intern space xname;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   238
  in apsome (pair name) (get_locale thy name) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   239
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   240
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   241
(* access scope *)
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
fun get_scope_sg sg =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   244
  if Sign.eq_sg (sg, Theory.sign_of ProtoPure.thy) then []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   245
  else ! (#scope (LocalesData.get_sg sg));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   246
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   247
val get_scope = get_scope_sg o Theory.sign_of;
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
fun change_scope f thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   250
  let val {scope, ...} = LocalesData.get thy
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   251
  in scope := f (! scope) end;
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
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
(** scope operations **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   256
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   257
(* change scope *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   258
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   259
fun the_locale thy xname =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   260
  (case lookup_locale thy xname of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   261
    Some loc => loc
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   262
  | None => error ("Unknown locale " ^ quote xname));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   263
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   264
fun open_locale xname thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   265
  let val loc = the_locale thy xname;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   266
      val anc = #ancestor(#2(loc));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   267
      val cur_sc = get_scope thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   268
      fun opn lc th = (change_scope (cons lc) th; th)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   269
  in case anc of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   270
         None => opn loc thy
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   271
       | Some(loc') => 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   272
           if loc' mem (map fst cur_sc) 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   273
           then opn loc thy
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   274
           else (warning ("Opening locale " ^ quote loc' ^ ", required by " ^ 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   275
			  quote xname);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   276
                 opn loc (open_locale (Sign.base_name loc') thy))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   277
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   278
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   279
fun pop_locale [] = error "Currently no open locales"
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   280
  | pop_locale (_ :: locs) = locs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   281
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   282
fun close_locale name thy = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   283
   let val lname = (case get_scope thy of (ln,_)::_ => ln
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   284
                                        | _ => error "No locales are open!")
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   285
       val ok = (name = Sign.base_name lname) handle _ => false
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   286
   in if ok then (change_scope pop_locale thy; thy)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   287
      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
   288
   end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   289
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   290
fun print_scope thy = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   291
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
   292
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   293
(*implicit context versions*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   294
fun Open_locale xname = (open_locale xname (Context.the_context ()); ());
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   295
fun Close_locale xname = (close_locale xname (Context.the_context ()); ());
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   296
fun Print_scope () = (print_scope (Context.the_context ()); ());
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   297
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   298
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   299
(** functions for goals.ML **)
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
(* 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
   302
   (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
   303
   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
   304
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   305
fun in_locale hyps sg =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   306
    let val cur_sc = get_scope_sg sg;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   307
        val rule_lists = map (#rules o snd) cur_sc;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   308
        val def_lists = map (#defs o snd) cur_sc;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   309
        val rules = map snd (foldr (op union) (rule_lists, []));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   310
        val defs = map snd (foldr (op union) (def_lists, []));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   311
        val defnrules = rules @ defs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   312
    in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   313
        hyps subset defnrules
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   314
    end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   315
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   316
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   317
(* 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
   318
fun is_open_loc_sg sign =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   319
    let val cur_sc = get_scope_sg sign
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   320
    in not(null(cur_sc)) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   321
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   322
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
   323
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
(* get theorems *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   326
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   327
fun get_thm_locale name ((_, {thms, ...}: locale)) = assoc (thms, name);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   328
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   329
fun get_thmx f get thy name =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   330
  (case get_first (get_thm_locale name) (get_scope thy) of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   331
    Some thm => f thm
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   332
  | None => get thy name);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   333
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   334
val get_thm = get_thmx I PureThy.get_thm;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   335
val get_thms = get_thmx (fn x => [x]) PureThy.get_thms;
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
fun thm name = get_thm (Context.the_context ()) name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   338
fun thms name = get_thms (Context.the_context ()) name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   339
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   340
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   341
(* get the defaults of a locale, for extension *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   342
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   343
fun get_defaults thy name = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   344
  let val (lname, loc) = the_locale thy name;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   345
  in #defaults(loc)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   346
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   347
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
(** define locales **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   350
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   351
(* prepare types *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   352
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   353
fun read_typ sg (envT, s) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   354
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   355
    fun def_sort (x, ~1) = assoc (envT, x)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   356
      | def_sort _ = None;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   357
    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
   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
fun cert_typ sg (envT, raw_T) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   361
  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
   362
  in (Term.add_typ_tfrees (T, envT), T) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   363
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
(* prepare props *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   366
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   367
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
   368
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   369
fun enter_term t (envS, envT, used) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   370
  (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
   371
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   372
fun read_axm sg ((envS, envT, used), (name, s)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   373
  let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   374
    fun def_sort (x, ~1) = assoc (envS, x)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   375
      | def_sort _ = None;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   376
    fun def_type (x, ~1) = assoc (envT, x)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   377
      | def_type _ = None;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   378
    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
   379
  in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   380
    (enter_term t (envS, envT, used), t)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   381
  end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   382
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
fun cert_axm sg ((envS, envT, used), (name, raw_t)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   385
  let val (_, t) = Theory.cert_axm sg (name, raw_t)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   386
  in (enter_term t (envS, envT, used), t) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   387
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
(* 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
   390
   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
   391
   Thm.read_cterm  *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   392
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   393
fun read_cterm sign =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   394
    let val cur_sc = get_scope_sg sign;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   395
        val defaults = map (#defaults o snd) cur_sc;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   396
        val envS = flat (map #1 defaults);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   397
        val envT = flat (map #2 defaults);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   398
        val used = flat (map #3 defaults);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   399
        fun def_sort (x, ~1) = assoc (envS, x)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   400
          | def_sort _ = None;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   401
        fun def_type (x, ~1) = assoc (envT, x)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   402
          | def_type _ = None;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   403
    in (if (is_open_loc_sg sign)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   404
        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
   405
        else Thm.read_cterm sign)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   406
    end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   407
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   408
(* basic functions needed for definitions and display *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   409
(* 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
   410
fun collect_consts sg =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   411
    let val cur_sc = get_scope_sg sg;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   412
        val locale_list = map snd cur_sc;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   413
        val const_list = flat (map #consts locale_list)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   414
    in map fst const_list end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   415
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   416
(* filter out the Free's in a term *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   417
fun list_frees t =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   418
    case t of Const(c,T) => []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   419
  | Var(v,T) => []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   420
  | Free(v,T)=> [Free(v,T)]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   421
  | Bound x  => []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   422
  | Abs(a,T,u) => list_frees u
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   423
  | t1 $ t2  => (list_frees t1)  @ (list_frees t2);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   424
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   425
(* 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
   426
   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
   427
   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
   428
   able to bind them *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   429
fun difflist term clist =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   430
    let val flist = list_frees term;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   431
        fun builddiff [] sl = []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   432
          | builddiff (t :: tl) sl =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   433
            let val Free(v,T) = t
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   434
            in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   435
                if (v mem sl)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   436
                then builddiff tl sl
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   437
                else t :: (builddiff tl sl)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   438
            end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   439
    in distinct(builddiff flist clist) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   440
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   441
(* Bind a term with !! over a list of "free" Free's.
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   442
   To enable definitions like x + y == .... (without quantifier).
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   443
   Complications, because x and y have to be removed from defaults *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   444
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
   445
    let val diffl = rev(difflist term clist);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   446
        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
   447
          | abs_o (_ , _) = error ("Can't be: abs_over_free");
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   448
        val diffl' = map (fn (Free (s, T)) => s) diffl;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   449
        val defaults' = (#1 defaults, filter (fn x => not((fst x) mem diffl')) (#2 defaults), #3 defaults)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   450
    in (defaults', (s, foldl abs_o (term, diffl))) end;
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
(* 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
   453
   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
   454
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
   455
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
(* concrete syntax *)
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
fun mark_syn c = "\\<^locale>" ^ c;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   460
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   461
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
   462
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   463
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   464
(* add_locale *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   465
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   466
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
   467
  let val sign = Theory.sign_of thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   468
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   469
    val name = Sign.full_name sign bname;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   470
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   471
    val (envSb, old_loc_consts, _) = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   472
                    case bancestor of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   473
                       Some(loc) => (get_defaults thy loc)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   474
                     | None      => ([],[],[]);
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
    val old_nosyn = case bancestor of 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   477
                       Some(loc) => #nosyn(#2(the_locale thy loc))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   478
                     | None      => [];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   479
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   480
    (* Get the full name of the ancestor *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   481
    val ancestor = case bancestor of 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   482
                       Some(loc) => Some(#1(the_locale thy loc))
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   483
                     | None      => None;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   484
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   485
     (* prepare locale consts *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   486
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   487
    fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   488
      let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   489
        val c = Syntax.const_name raw_c raw_mx;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   490
        val c_syn = mark_syn c;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   491
        val mx = Syntax.fix_mixfix raw_c raw_mx;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   492
        val (envS', T) = prep_typ sign (envS, raw_T) handle ERROR =>
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   493
          error ("The error(s) above occured in locale constant " ^ quote c);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   494
        val trfun = if mx = Syntax.NoSyn then None else Some (c_syn, mk_loc_tr c);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   495
      in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   496
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   497
    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
   498
    val loc_consts = map #1 loc_consts_syn;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   499
    val loc_consts = old_loc_consts @ loc_consts;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   500
    val loc_syn = map #2 loc_consts_syn;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   501
    val nosyn = old_nosyn @ (map (#1 o #1) (filter (fn x => (#3(#2 x)) = NoSyn) loc_consts_syn));
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   502
    val loc_trfuns = mapfilter #3 loc_consts_syn;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   503
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   504
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   505
    (* 1st stage: syntax_thy *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   506
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   507
    val syntax_thy =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   508
      thy
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   509
      |> Theory.add_modesyntax_i ("", true) loc_syn
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   510
      |> Theory.add_trfuns ([], loc_trfuns, [], []);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   511
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   512
    val syntax_sign = Theory.sign_of syntax_thy;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   513
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   514
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   515
    (* prepare rules and defs *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   516
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   517
    fun prep_axiom (env, (a, raw_t)) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   518
      let
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   519
        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
   520
          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
   521
      in (env', (a, t)) end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   522
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   523
    val ((envS1, envT1, used1), loc_rules) =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   524
      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
   525
    val (defaults, loc_defs) = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   526
	foldl_map prep_axiom ((envS1, envT1, used1), raw_defs);
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 old_loc_consts = collect_consts syntax_sign;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   529
    val new_loc_consts = (map #1 loc_consts);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   530
    val all_loc_consts = old_loc_consts @ new_loc_consts;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   531
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   532
    val (defaults, loc_defs_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_defs);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   534
    val loc_defs_thms = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   535
	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
   536
    val (defaults, loc_thms_terms) = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   537
	foldl_map (abs_over_free all_loc_consts) (defaults, loc_rules);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   538
    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
   539
		       (loc_thms_terms)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   540
                   @ loc_defs_thms;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   541
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
    (* error messages *) 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   544
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   545
    fun locale_error msg = error (msg ^ "\nFor 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
    val err_dup_locale =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   548
      if is_none (get_locale thy name) then []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   549
      else ["Duplicate definition of locale " ^ quote name];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   550
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   551
    (* check if definientes are locale constants 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   552
       (in the same locale, so no redefining!) *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   553
    val err_def_head =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   554
      let fun peal_appl t = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   555
            case t of 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   556
                 t1 $ t2 => peal_appl t1
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   557
               | Free(t) => t
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   558
               | _ => locale_error ("Bad form of LHS in locale definition");
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   559
	  fun lhs (_, Const ("==" , _) $  d1 $ d2) = peal_appl d1
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   560
	    | lhs _ = locale_error ("Definitions must use the == relation");
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   561
          val defs = map lhs loc_defs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   562
          val check = defs subset loc_consts
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   563
      in if check then [] 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   564
         else ["defined item not declared fixed in locale " ^ quote name]
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   565
      end; 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   566
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   567
    (* 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
   568
    val err_var_rhs = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   569
      let fun compare_var_sides (t, (_, Const ("==", _) $ d1 $ d2)) = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   570
		let val varl1 = difflist d1 all_loc_consts;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   571
		    val varl2 = difflist d2 all_loc_consts
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   572
		in t andalso (varl2 subset varl1)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   573
		end
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   574
            | compare_var_sides (_,_) = 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   575
		locale_error ("Definitions must use the == relation")
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   576
          val check = foldl compare_var_sides (true, loc_defs)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   577
      in if check then []
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   578
         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
   579
      end;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   580
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   581
    val errs = err_dup_locale @ err_def_head @ err_var_rhs;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   582
  in
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   583
    if null errs then ()
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   584
    else error (cat_lines errs);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   585
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   586
    syntax_thy
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   587
    |> put_locale (name, 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   588
		   make_locale ancestor loc_consts nosyn loc_thms_terms 
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   589
                                        loc_defs_terms   loc_thms defaults)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   590
  end;
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
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   593
val add_locale = gen_add_locale read_typ read_axm;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   594
val add_locale_i = gen_add_locale cert_typ cert_axm;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   595
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   596
(** print functions **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   597
(* 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
   598
         "real" constant representation (i.e. \\<^locale>constname).
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   599
   - function const_ssubst does this substitution
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   600
   - function pretty_term:
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   601
             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
   602
             else call Sign.pretty_term
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   603
*)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   604
(* substitutes all Free variables s in t by Const's s *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   605
fun const_ssubst t s =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   606
    case t  of
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   607
        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
   608
      | Const(c,T) => Const(c,T)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   609
      | Var(v,T) => Var(v,T)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   610
      | Bound x  => Bound x
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   611
      | Abs(a,T,u) => Abs(a,T, const_ssubst u s)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   612
      | t1 $ t2  => const_ssubst t1 s $ const_ssubst t2 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
(* FIXME: improve: can be expressed with foldl *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   615
fun const_ssubst_list [] t = t
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   616
  | 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
   617
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   618
(* pretty_term *)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   619
fun pretty_term sign =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   620
    if (is_open_loc_sg sign) then
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   621
        let val locale_list = map snd(get_scope_sg sign);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   622
            val nosyn = flat (map #nosyn locale_list);
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   623
            val str_list = (collect_consts sign) \\ nosyn
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   624
        in Sign.pretty_term sign o (const_ssubst_list str_list)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   625
        end
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   626
    else Sign.pretty_term sign;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   627
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   628
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   629
(** locale theory setup **)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   630
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   631
val setup =
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   632
 [LocalesData.init];
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   633
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   634
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   635
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   636
(*** Goal package ***)
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   637
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   638
(*Each level of goal stack includes a proof state and alternative states,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   639
  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
   640
type gstack = (thm * thm Seq.seq) list;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   641
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   642
datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   643
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   644
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   645
(*** References ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   646
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   647
(*Current assumption list -- set by "goal".*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   648
val curr_prems = ref([] : thm list);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   649
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   650
(*Return assumption list -- useful if you didn't save "goal"'s result. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   651
fun premises() = !curr_prems;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   652
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   653
(*Current result maker -- set by "goal", used by "result".  *)
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   654
fun init_mkresult _ = error "No goal has been supplied in subgoal module";
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   655
val curr_mkresult = ref (init_mkresult: bool*thm->thm);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   656
6390
5d58c100ca3f qualify Theory.sign_of etc.;
wenzelm
parents: 6189
diff changeset
   657
val dummy = trivial(read_cterm (Theory.sign_of ProtoPure.thy)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   658
    ("PROP No_goal_has_been_supplied",propT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   659
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   660
(*List of previous goal stacks, for the undo operation.  Set by setstate. 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   661
  A list of lists!*)
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   662
val undo_list = ref([[(dummy, Seq.empty)]] : gstack list);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   663
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   664
(* Stack of proof attempts *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   665
val proofstack = ref([]: proof list);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   666
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   667
(*reset all refs*)
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   668
fun reset_goals () =
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   669
  (curr_prems := []; curr_mkresult := init_mkresult;
7942
4f8cf6552787 reset_goals no longer empties the proof stack
oheimb
parents: 7637
diff changeset
   670
    undo_list := [[(dummy, Seq.empty)]]);
7234
1ba436add81b reset_goals;
wenzelm
parents: 7063
diff changeset
   671
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   672
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   673
(*** Setting up goal-directed proof ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   674
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   675
(*Generates the list of new theories when the proof state's signature changes*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   676
fun sign_error (sign,sign') =
3974
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   677
  let val names = Sign.stamp_names_of sign' \\ Sign.stamp_names_of sign
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   678
  in  case names of
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   679
        [name] => "\nNew theory: " ^ name
d3c2159b75fa Sign.stamp_names_of;
wenzelm
parents: 3853
diff changeset
   680
      | _       => "\nNew theories: " ^ space_implode ", " names
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   681
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   682
1928
3d1d73e3d185 Added ref to allow suppression of error msgs
paulson
parents: 1628
diff changeset
   683
(*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
   684
  special applications.*)
3669
3384c6f1f095 removed print_goals_ref (which was broken anyway);
wenzelm
parents: 3536
diff changeset
   685
fun result_error_default state msg : thm =
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   686
  Pretty.str "Bad final proof state:" :: Display.pretty_goals (!goals_limit) state @
11562
804ee65ee1a1 result_error_default: include msg;
wenzelm
parents: 11554
diff changeset
   687
    [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
   688
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   689
val result_error_fn = ref result_error_default;
1928
3d1d73e3d185 Added ref to allow suppression of error msgs
paulson
parents: 1628
diff changeset
   690
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   691
(* alternative to standard: this function does not discharge the hypotheses
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   692
   first. Is used for locales, in the following function prepare_proof *)
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   693
fun varify th =
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   694
  let val {maxidx,...} = rep_thm th
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   695
  in
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   696
    th |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
7637
16347ef1d222 use Drule.strip_shyps_warning;
wenzelm
parents: 7265
diff changeset
   697
       |> Drule.strip_shyps_warning
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   698
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   699
  end;
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   700
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   701
(** 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
   702
    and expand and cancel the locale definitions. 
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   703
    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
   704
    export_thy just goes one up. **)
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   705
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   706
fun get_top_scope_thms thy = 
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   707
   let val sc = (get_scope_sg (Theory.sign_of thy))
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   708
   in if (null sc) then (warning "No locale in theory"; [])
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   709
      else map (#prop o rep_thm) (map #2 (#thms(snd(hd sc))))
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   710
   end;
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   711
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   712
fun implies_intr_some_hyps thy A_set th = 
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   713
   let 
6390
5d58c100ca3f qualify Theory.sign_of etc.;
wenzelm
parents: 6189
diff changeset
   714
       val sign = Theory.sign_of thy;
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   715
       val used_As = A_set inter #hyps(rep_thm(th));
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   716
       val ctl = map (cterm_of sign) used_As
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   717
   in foldl (fn (x, y) => Thm.implies_intr y x) (th, ctl)
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   718
   end; 
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   719
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   720
fun standard_some thy A_set th =
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   721
  let val {maxidx,...} = rep_thm th
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   722
  in
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   723
    th |> implies_intr_some_hyps thy A_set
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   724
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
7637
16347ef1d222 use Drule.strip_shyps_warning;
wenzelm
parents: 7265
diff changeset
   725
       |> Drule.strip_shyps_warning
6017
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   726
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   727
  end;
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   728
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   729
fun export_thy thy th = 
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   730
  let val A_set = get_top_scope_thms thy
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   731
  in
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   732
     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
   733
  end;
dbb33359a7ab better export for nested locales
paulson
parents: 5955
diff changeset
   734
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   735
val export = standard o Seq.hd o (REPEAT (FIRSTGOAL (rtac reflexive_thm))) o standard;
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   736
6189
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   737
fun Export th = export_thy (the_context ()) th;
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   738
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   739
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   740
(*Common treatment of "goal" and "prove_goal":
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   741
  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
   742
  "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
   743
  "Goal::prop=>prop" to avoid assumptions being returned separately.
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   744
*)
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   745
fun prepare_proof atomic rths chorn =
230
ec8a2b6aa8a7 Many other files modified as follows:
lcp
parents: 68
diff changeset
   746
  let val {sign, t=horn,...} = rep_cterm chorn;
9533
fb68b7163969 Term.no_dummy_patterns;
wenzelm
parents: 8999
diff changeset
   747
      val _ = Term.no_dummy_patterns horn handle TERM (msg, _) => error msg;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   748
      val (_,As,B) = Logic.strip_horn(horn);
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   749
      val atoms = atomic andalso
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   750
            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
   751
      val (As,B) = if atoms then ([],horn) else (As,B);
230
ec8a2b6aa8a7 Many other files modified as follows:
lcp
parents: 68
diff changeset
   752
      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
   753
      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
   754
      val cB = cterm_of sign B;
11963
a6608d44a46b impose hyps on initial goal configuration (prevents res_inst_tac problems);
wenzelm
parents: 11884
diff changeset
   755
      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
   756
                in  rewrite_goals_rule rths st end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   757
      (*discharges assumptions from state in the order they appear in goal;
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   758
	checks (if requested) that resulting theorem is equivalent to goal. *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   759
      fun mkresult (check,state) =
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   760
        let val state = Seq.hd (flexflex_rule state)
1565
70dd38777109 Made an exception handler more specific
paulson
parents: 1500
diff changeset
   761
	    		handle THM _ => state   (*smash flexflex pairs*)
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   762
	    val ngoals = nprems_of state
7637
16347ef1d222 use Drule.strip_shyps_warning;
wenzelm
parents: 7265
diff changeset
   763
            val ath = implies_intr_list cAs state
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   764
            val th = ath RS Drule.rev_triv_goal
1240
0901441a7ebf adapted to new version of shyps-stuff;
wenzelm
parents: 1219
diff changeset
   765
            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
   766
            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
   767
        in  if not check then final_th
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   768
	    else if not (Sign.eq_sg(sign,sign')) then !result_error_fn state
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   769
		("Signature of proof state has changed!" ^
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   770
		 sign_error (sign,sign'))
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   771
            else if ngoals>0 then !result_error_fn state
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   772
		(string_of_int ngoals ^ " unsolved goals!")
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   773
            else if (not (null hyps) andalso (not (in_locale hyps sign)))
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   774
		 then !result_error_fn state
5748
5a571d57183f better reporting of "Additional hypotheses" in a locale
paulson
parents: 5729
diff changeset
   775
                  ("Additional hypotheses:\n" ^ 
5a571d57183f better reporting of "Additional hypotheses" in a locale
paulson
parents: 5729
diff changeset
   776
		   cat_lines 
5a571d57183f better reporting of "Additional hypotheses" in a locale
paulson
parents: 5729
diff changeset
   777
		    (map (Sign.string_of_term sign) 
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   778
		     (filter (fn x => (not (in_locale [x] sign))) 
5748
5a571d57183f better reporting of "Additional hypotheses" in a locale
paulson
parents: 5729
diff changeset
   779
		      hyps)))
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   780
	    else if Pattern.matches (#tsig(Sign.rep_sg sign)) 
10487
97d25c125c61 check result: Envir.beta_norm;
wenzelm
parents: 9573
diff changeset
   781
			            (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
   782
		 then final_th
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   783
	    else  !result_error_fn state "proved a different theorem"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   784
        end
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 577
diff changeset
   785
  in
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 577
diff changeset
   786
     if Sign.eq_sg(sign, #sign(rep_thm st0))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   787
     then (prems, st0, mkresult)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   788
     else error ("Definitions would change the proof state's signature" ^
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   789
		 sign_error (sign, #sign(rep_thm st0)))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   790
  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   791
  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   792
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   793
(*Prints exceptions readably to users*)
577
776b5ba748d8 print_sign_exn: now exported, with a polymorphic type
lcp
parents: 545
diff changeset
   794
fun print_sign_exn_unit sign e = 
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   795
  case e of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   796
     THM (msg,i,thms) =>
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   797
	 (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   798
	  seq print_thm thms)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   799
   | THEORY (msg,thys) =>
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   800
	 (writeln ("Exception THEORY raised:\n" ^ msg);
4994
8b361563d470 tuned print_exn;
wenzelm
parents: 4280
diff changeset
   801
	  seq (Pretty.writeln o Display.pretty_theory) thys)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   802
   | TERM (msg,ts) =>
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   803
	 (writeln ("Exception TERM raised:\n" ^ msg);
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   804
	  seq (writeln o Sign.string_of_term sign) ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   805
   | TYPE (msg,Ts,ts) =>
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   806
	 (writeln ("Exception TYPE raised:\n" ^ msg);
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   807
	  seq (writeln o Sign.string_of_typ sign) Ts;
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   808
	  seq (writeln o Sign.string_of_term sign) ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   809
   | e => raise e;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   810
577
776b5ba748d8 print_sign_exn: now exported, with a polymorphic type
lcp
parents: 545
diff changeset
   811
(*Prints an exception, then fails*)
776b5ba748d8 print_sign_exn: now exported, with a polymorphic type
lcp
parents: 545
diff changeset
   812
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
   813
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   814
(** the prove_goal.... commands
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   815
    Prove theorem using the listed tactics; check it has the specified form.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   816
    Augment signature with all type assignments of goal.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   817
    Syntax is similar to "goal" command for easy keyboard use. **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   818
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   819
(*Version taking the goal as a cterm*)
5311
f3f71669878e Rule mk_triv_goal for making instances of triv_goal
paulson
parents: 5246
diff changeset
   820
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
   821
  let val (prems, st0, mkresult) = prepare_proof false rths chorn
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   822
      val tac = EVERY (tacsf prems)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   823
      fun statef() = 
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   824
	  (case Seq.pull (tac st0) of 
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   825
	       Some(st,_) => st
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   826
	     | _ => error ("prove_goal: tactic failed"))
8999
ad8260dc6e4a global timing flag;
wenzelm
parents: 8884
diff changeset
   827
  in  mkresult (check, cond_timeit (!Library.timing) statef)  end
914
cae574c09137 Now prove_goalw_cterm calls print_sign_exn_unit, so that the rest
lcp
parents: 696
diff changeset
   828
  handle e => (print_sign_exn_unit (#sign (rep_cterm chorn)) e;
6960
54d4d1602068 prove_goalw_cterm_general: pass exeption;
wenzelm
parents: 6912
diff changeset
   829
	       writeln ("The exception above was raised for\n" ^ 
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   830
		      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
   831
5614
0e8b45a7d104 Now prove_goalw_cterm never prints timing statistics
paulson
parents: 5311
diff changeset
   832
(*Two variants: one checking the result, one not.  
0e8b45a7d104 Now prove_goalw_cterm never prints timing statistics
paulson
parents: 5311
diff changeset
   833
  Neither prints runtime messages: they are for internal packages.*)
0e8b45a7d104 Now prove_goalw_cterm never prints timing statistics
paulson
parents: 5311
diff changeset
   834
fun prove_goalw_cterm rths chorn = 
8999
ad8260dc6e4a global timing flag;
wenzelm
parents: 8884
diff changeset
   835
	setmp Library.timing false (prove_goalw_cterm_general true rths chorn)
5614
0e8b45a7d104 Now prove_goalw_cterm never prints timing statistics
paulson
parents: 5311
diff changeset
   836
and prove_goalw_cterm_nocheck rths chorn = 
8999
ad8260dc6e4a global timing flag;
wenzelm
parents: 8884
diff changeset
   837
	setmp Library.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
   838
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   839
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   840
(*Version taking the goal as a string*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   841
fun prove_goalw thy rths agoal tacsf =
6390
5d58c100ca3f qualify Theory.sign_of etc.;
wenzelm
parents: 6189
diff changeset
   842
  let val sign = Theory.sign_of thy
230
ec8a2b6aa8a7 Many other files modified as follows:
lcp
parents: 68
diff changeset
   843
      val chorn = read_cterm sign (agoal,propT)
5614
0e8b45a7d104 Now prove_goalw_cterm never prints timing statistics
paulson
parents: 5311
diff changeset
   844
  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
   845
  handle ERROR => error (*from read_cterm?*)
3536
8fb4150e2ad3 tuned error propagation msg;
wenzelm
parents: 3532
diff changeset
   846
		("The error(s) above occurred for " ^ quote agoal);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   847
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   848
(*String version with no meta-rewrite-rules*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   849
fun prove_goal thy = prove_goalw thy [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   850
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   851
(*quick and dirty version (conditional)*)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   852
fun quick_and_dirty_prove_goalw_cterm thy rews ct tacs =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   853
  prove_goalw_cterm rews ct
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   854
    (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
   855
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   856
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   857
(*** Commands etc ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   858
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   859
(*Return the current goal stack, if any, from undo_list*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   860
fun getstate() : gstack = case !undo_list of 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   861
      []   => error"No current state in subgoal module"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   862
    | x::_ => x;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   863
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   864
(*Pops the given goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   865
fun pop [] = error"Cannot go back past the beginning of the proof!"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   866
  | pop (pair::pairs) = (pair,pairs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   867
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   868
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   869
(* Print a level of the goal stack -- subject to quiet mode *)
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   870
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   871
val quiet = ref false;
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   872
fun disable_pr () = quiet := true;
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   873
fun enable_pr () = quiet := false;
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   874
3532
de271ba8086e tuned warnings;
wenzelm
parents: 2962
diff changeset
   875
fun print_top ((th, _), pairs) =
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   876
  if ! quiet then ()
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
   877
  else ! Display.print_current_goals_fn (length pairs) (! goals_limit) th;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   878
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   879
(*Printing can raise exceptions, so the assignment occurs last.
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   880
  Can do   setstate[(st,Seq.empty)]  to set st as the state.  *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   881
fun setstate newgoals = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   882
  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   883
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   884
(*Given a proof state transformation, return a command that updates
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   885
    the goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   886
fun make_command com = setstate (com (pop (getstate())));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   887
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   888
(*Apply a function on proof states to the current goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   889
fun apply_fun f = f (pop(getstate()));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   890
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   891
(*Return the top theorem, representing the proof state*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   892
fun topthm () = apply_fun  (fn ((th,_), _) => th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   893
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   894
(*Return the final result.  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   895
fun result () = !curr_mkresult (true, topthm());
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   896
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   897
(*Return the result UNCHECKED that it equals the goal -- for synthesis,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   898
  answer extraction, or other instantiation of Vars *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   899
fun uresult () = !curr_mkresult (false, topthm());
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   900
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   901
(*Get subgoal i from goal stack*)
2580
e3f680709487 Gradual switching to Basis Library functions nth, drop, etc.
paulson
parents: 2514
diff changeset
   902
fun getgoal i = List.nth (prems_of (topthm()), i-1)
e3f680709487 Gradual switching to Basis Library functions nth, drop, etc.
paulson
parents: 2514
diff changeset
   903
                handle Subscript => error"getgoal: Goal number out of range";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   904
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   905
(*Return subgoal i's hypotheses as meta-level assumptions.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   906
  For debugging uses of METAHYPS*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   907
local exception GETHYPS of thm list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   908
in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   909
fun gethyps i = 
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   910
    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   911
    handle GETHYPS hyps => hyps
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   912
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   913
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   914
(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   915
fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   916
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   917
(*For inspecting earlier levels of the backward proof*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   918
fun chop_level n (pair,pairs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   919
  let val level = length pairs
2126
d927beecedf8 Allowing negative levels (as offsets) in prlev and choplev
paulson
parents: 1928
diff changeset
   920
  in  if n<0 andalso ~n <= level
2580
e3f680709487 Gradual switching to Basis Library functions nth, drop, etc.
paulson
parents: 2514
diff changeset
   921
      then  List.drop (pair::pairs, ~n) 
2126
d927beecedf8 Allowing negative levels (as offsets) in prlev and choplev
paulson
parents: 1928
diff changeset
   922
      else if 0<=n andalso n<= level
2580
e3f680709487 Gradual switching to Basis Library functions nth, drop, etc.
paulson
parents: 2514
diff changeset
   923
      then  List.drop (pair::pairs, level - n) 
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   924
      else  error ("Level number must lie between 0 and " ^ 
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   925
		   string_of_int level)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   926
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   927
2514
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   928
(*Print the given level of the proof; prlev ~1 prints previous level*)
8884
d1c85d427e29 added disable_pr, enable_pr;
wenzelm
parents: 8086
diff changeset
   929
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
   930
fun pr () = (enable_pr (); apply_fun print_top);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   931
2514
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   932
(*Set goals_limit and print again*)
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   933
fun prlim n = (goals_limit:=n; pr());
ea8881e70f9c Added the prlim command
paulson
parents: 2169
diff changeset
   934
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   935
(** the goal.... commands
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   936
    Read main goal.  Set global variables curr_prems, curr_mkresult. 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   937
    Initial subgoal and premises are rewritten using rths. **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   938
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   939
(*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
   940
    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
   941
fun agoalw_cterm atomic rths chorn = 
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   942
  let val (prems, st0, mkresult) = prepare_proof atomic rths chorn
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   943
  in  undo_list := [];
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   944
      setstate [ (st0, Seq.empty) ];  
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   945
      curr_prems := prems;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   946
      curr_mkresult := mkresult;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   947
      prems
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   948
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   949
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   950
val goalw_cterm = agoalw_cterm false;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   951
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   952
(*Version taking the goal as a string*)
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   953
fun agoalw atomic thy rths agoal = 
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
   954
    agoalw_cterm atomic rths (read_cterm(Theory.sign_of thy)(agoal,propT))
5246
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   955
    handle ERROR => error (*from type_assign, etc via prepare_proof*)
99116a9e88f8 added export: thm -> thm;
wenzelm
parents: 5092
diff changeset
   956
	("The error(s) above occurred for " ^ quote agoal);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   957
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   958
val goalw = agoalw false;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   959
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   960
(*String version with no meta-rewrite-rules*)
5041
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   961
fun agoal atomic thy = agoalw atomic thy [];
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   962
val goal = agoal false;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   963
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   964
(*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
   965
val atomic_goalw = agoalw true;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   966
val atomic_goal = agoal true;
a1d0a6d555cd Goals may now contain assumptions, which are not returned.
nipkow
parents: 4994
diff changeset
   967
6189
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   968
(*implicit context versions*)
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   969
fun Goal s = atomic_goal (Context.the_context ()) s;
e9dc9ec28a2d added Goal(w) and Export (from context.ML);
wenzelm
parents: 6133
diff changeset
   970
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
   971
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   972
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   973
(*Proof step "by" the given tactic -- apply tactic to the proof state*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   974
fun by_com tac ((th,ths), pairs) : gstack =
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   975
  (case  Seq.pull(tac th)  of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   976
     None      => error"by: tactic failed"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   977
   | Some(th2,ths2) => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   978
       (if eq_thm(th,th2) 
3707
40856b593501 Prints warnings using the "warning" function instead of "writeln"
paulson
parents: 3669
diff changeset
   979
	  then warning "Warning: same as previous level"
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   980
	  else if eq_thm_sg(th,th2) then ()
4280
278660f52716 fixed warning;
wenzelm
parents: 4270
diff changeset
   981
	  else warning ("Warning: signature of proof state has changed" ^
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   982
		       sign_error (#sign(rep_thm th), #sign(rep_thm th2)));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   983
       ((th2,ths2)::(th,ths)::pairs)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   984
8999
ad8260dc6e4a global timing flag;
wenzelm
parents: 8884
diff changeset
   985
fun by tac = cond_timeit (!Library.timing) 
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   986
    (fn() => make_command (by_com tac));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   987
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   988
(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   989
   Good for debugging proofs involving prove_goal.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   990
val byev = by o EVERY;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   991
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   992
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   993
(*Backtracking means find an alternative result from a tactic.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   994
  If none at this level, try earlier levels*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   995
fun backtrack [] = error"back: no alternatives"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   996
  | backtrack ((th,thstr) :: pairs) =
4270
957c887b89b5 changed Sequence interface (now Seq, in seq.ML);
wenzelm
parents: 4211
diff changeset
   997
     (case Seq.pull thstr of
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   998
	  None      => (writeln"Going back a level..."; backtrack pairs)
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   999
	| Some(th2,thstr2) =>  
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1000
	   (if eq_thm(th,th2) 
3707
40856b593501 Prints warnings using the "warning" function instead of "writeln"
paulson
parents: 3669
diff changeset
  1001
	      then warning "Warning: same as previous choice at this level"
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1002
	      else if eq_thm_sg(th,th2) then ()
3707
40856b593501 Prints warnings using the "warning" function instead of "writeln"
paulson
parents: 3669
diff changeset
  1003
	      else warning "Warning: signature of proof state has changed";
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1004
	    (th2,thstr2)::pairs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1005
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1006
fun back() = setstate (backtrack (getstate()));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1007
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1008
(*Chop back to previous level of the proof*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1009
fun choplev n = make_command (chop_level n);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1010
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1011
(*Chopping back the goal stack*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1012
fun chop () = make_command (fn (_,pairs) => pairs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1013
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1014
(*Restore the previous proof state;  discard current state. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1015
fun undo() = case !undo_list of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1016
      [] => error"No proof state"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1017
    | [_] => error"Already at initial state"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1018
    | _::newundo =>  (undo_list := newundo;  pr()) ;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1019
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1020
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1021
(*** Managing the proof stack ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1022
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1023
fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1024
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1025
fun restore_proof(Proof(ul,prems,mk)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1026
 (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1027
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1028
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1029
fun top_proof() = case !proofstack of
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1030
	[] => error("Stack of proof attempts is empty!")
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1031
    | p::ps => (p,ps);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1032
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1033
(*  push a copy of the current proof state on to the stack *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1034
fun push_proof() = (proofstack := (save_proof() :: !proofstack));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1035
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1036
(* discard the top proof state of the stack *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1037
fun pop_proof() = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1038
  let val (p,ps) = top_proof()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1039
      val prems = restore_proof p
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1040
  in proofstack := ps;  pr();  prems end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1041
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1042
(* rotate the stack so that the top element goes to the bottom *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1043
fun rotate_proof() = let val (p,ps) = top_proof()
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1044
		    in proofstack := ps@[save_proof()];
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1045
		       restore_proof p;
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1046
		       pr();
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1047
		       !curr_prems
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
  1048
		    end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1049
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1050
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1051
(** Shortcuts for commonly-used tactics **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1052
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1053
fun bws rls = by (rewrite_goals_tac rls);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1054
fun bw rl = bws [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1055
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1056
fun brs rls i = by (resolve_tac rls i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1057
fun br rl = brs [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1058
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1059
fun bes rls i = by (eresolve_tac rls i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1060
fun be rl = bes [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1061
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1062
fun bds rls i = by (dresolve_tac rls i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1063
fun bd rl = bds [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1064
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1065
fun ba i = by (assume_tac i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1066
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1067
fun ren str i = by (rename_tac str i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1068
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1069
(** Shortcuts to work on the first applicable subgoal **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1070
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1071
fun frs rls = by (FIRSTGOAL (trace_goalno_tac (resolve_tac rls)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1072
fun fr rl = frs [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1073
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1074
fun fes rls = by (FIRSTGOAL (trace_goalno_tac (eresolve_tac rls)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1075
fun fe rl = fes [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1076
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1077
fun fds rls = by (FIRSTGOAL (trace_goalno_tac (dresolve_tac rls)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1078
fun fd rl = fds [rl];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1079
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1080
fun fa() = by (FIRSTGOAL (trace_goalno_tac assume_tac));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1081
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1082
(** Reading and printing terms wrt the current theory **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1083
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1084
fun top_sg() = #sign(rep_thm(topthm()));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1085
8086
78e254305ae6 TypeInfer.logicT;
wenzelm
parents: 7942
diff changeset
  1086
fun read s = term_of (read_cterm (top_sg()) (s, TypeInfer.logicT));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1087
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1088
(*Print a term under the current signature of the proof state*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1089
fun prin t = writeln (Sign.string_of_term (top_sg()) t);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1090
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1091
fun printyp T = writeln (Sign.string_of_typ (top_sg()) T);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1092
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1093
fun pprint_term t = Sign.pprint_term (top_sg()) t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1094
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1095
fun pprint_typ T = Sign.pprint_typ (top_sg()) T;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1096
1628
60136fdd80c4 Added functions pr_latex and printgoal_latex which
berghofe
parents: 1580
diff changeset
  1097
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1098
(*Prints exceptions nicely at top level; 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1099
  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
  1100
fun print_exn e = (print_sign_exn_unit (top_sg()) e;  raise e);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1101
11884
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1102
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1103
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1104
(** theorem database operations **)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1105
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1106
(* storing *)
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
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
  1109
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
  1110
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1111
fun qed name = ThmDatabase.ml_store_thm (name, result ());
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1112
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
  1113
fun qed_goalw name thy rews goal tacsf =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1114
  ThmDatabase.ml_store_thm (name, prove_goalw thy rews goal tacsf);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1115
fun qed_spec_mp name =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1116
  ThmDatabase.ml_store_thm (name, ObjectLogic.rulify_no_asm (result ()));
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1117
fun qed_goal_spec_mp name thy s p =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1118
  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
  1119
fun qed_goalw_spec_mp name thy defs s p =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1120
  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
  1121
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1122
fun no_qed () = ();
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1123
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1124
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1125
(* retrieval *)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1126
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1127
fun theory_of_goal () = ThyInfo.theory_of_sign (Thm.sign_of_thm (topthm ()));
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
fun thms_containing raw_consts =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1130
  let
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1131
    val thy = theory_of_goal ();
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1132
    val consts = map (Sign.intern_const (Theory.sign_of thy)) raw_consts;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1133
  in ThmDatabase.thms_containing thy consts end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1134
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1135
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1136
(*top_const: main constant, ignoring Trueprop*)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1137
fun top_const (_ $ t) = (case head_of t of Const (c, _) => Some c | _ => None)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1138
  | top_const _ = None;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1139
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1140
val intro_const = top_const o concl_of;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1141
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1142
fun elim_const thm = case prems_of thm of [] => None | p::_ => top_const p;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1143
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1144
(* In case faster access is necessary, the thm db should provide special
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1145
functions
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1146
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1147
index_intros, index_elims: string -> (string * thm) list
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1148
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1149
where thm [| A1 ; ...; An |] ==> B is returned by
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1150
- index_intros c if B  is of the form c t1 ... tn
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1151
- index_elims c  if A1 is of the form c t1 ... tn
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1152
*)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1153
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1154
(* could be provided by thm db *)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1155
fun index_intros c =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1156
  let fun topc(_,thm) = intro_const thm = Some(c);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1157
      val named_thms = ThmDatabase.thms_containing (theory_of_goal ()) [c]
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1158
  in filter topc named_thms end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1159
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1160
(* could be provided by thm db *)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1161
fun index_elims c =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1162
  let fun topc(_,thm) = elim_const thm = Some(c);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1163
      val named_thms = ThmDatabase.thms_containing (theory_of_goal ()) [c]
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1164
  in filter topc named_thms end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1165
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1166
(*assume that parameters have unique names; reverse them for substitution*)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1167
fun goal_params i =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1168
  let val gi = getgoal i
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1169
      val rfrees = rev(map Free (Logic.strip_params gi))
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1170
  in (gi,rfrees) end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1171
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1172
fun concl_of_goal i =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1173
  let val (gi,rfrees) = goal_params i
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1174
      val B = Logic.strip_assums_concl gi
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1175
  in subst_bounds(rfrees,B) end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1176
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1177
fun prems_of_goal i =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1178
  let val (gi,rfrees) = goal_params i
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1179
      val As = Logic.strip_assums_hyp gi
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1180
  in map (fn A => subst_bounds(rfrees,A)) As end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1181
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1182
(*returns all those named_thms whose subterm extracted by extract can be
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1183
  instantiated to obj; the list is sorted according to the number of premises
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1184
  and the size of the required substitution.*)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1185
fun select_match(obj, signobj, named_thms, extract) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1186
  let fun matches(prop, tsig) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1187
            case extract prop of
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1188
              None => false
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1189
            | Some pat => Pattern.matches tsig (pat, obj);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1190
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1191
      fun substsize(prop, tsig) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1192
            let val Some pat = extract prop
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1193
                val (_,subst) = Pattern.match tsig (pat,obj)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1194
            in foldl op+
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1195
                (0, map (fn (_,t) => size_of_term t) subst)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1196
            end
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1197
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1198
      fun thm_ord ((p0,s0,_),(p1,s1,_)) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1199
            prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) int_ord ((p0,s0),(p1,s1));
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1200
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1201
      fun select((p as (_,thm))::named_thms, sels) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1202
            let val {prop, sign, ...} = rep_thm thm
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1203
            in select(named_thms,
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1204
                      if Sign.subsig(sign, signobj) andalso
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1205
                         matches(prop,#tsig(Sign.rep_sg signobj))
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1206
                      then
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1207
                        (nprems_of thm,substsize(prop,#tsig(Sign.rep_sg signobj)),p)::sels
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1208
                      else sels)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1209
            end
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1210
        | select([],sels) = sels
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1211
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1212
  in map (fn (_,_,t) => t) (sort thm_ord (select(named_thms, []))) end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1213
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1214
fun find_matching(prop,sign,index,extract) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1215
  (case top_const prop of
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1216
     Some c => select_match(prop,sign,index c,extract)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1217
   | _      => []);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1218
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1219
fun find_intros(prop,sign) =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1220
  find_matching(prop,sign,index_intros,Some o Logic.strip_imp_concl);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1221
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1222
fun find_elims sign prop =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1223
  let fun major prop = case Logic.strip_imp_prems prop of
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1224
                         [] => None | p::_ => Some p
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1225
  in find_matching(prop,sign,index_elims,major) end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1226
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1227
fun findI i = find_intros(concl_of_goal i,#sign(rep_thm(topthm())));
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1228
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1229
fun findEs i =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1230
  let fun eq_nth((n1,th1),(n2,th2)) = n1=n2 andalso eq_thm(th1,th2);
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1231
      val sign = #sign(rep_thm(topthm()))
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1232
      val thmss = map (find_elims sign) (prems_of_goal i)
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1233
  in foldl (gen_union eq_nth) ([],thmss) end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1234
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1235
fun findE i j =
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1236
  let val sign = #sign(rep_thm(topthm()))
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1237
  in find_elims sign (nth_elem(j-1, prems_of_goal i)) end;
341b1fbc6130 make this module appeat late in Pure;
wenzelm
parents: 11562
diff changeset
  1238
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1239
end;
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
  1240
12012
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
  1241
structure BasicGoals: BASIC_GOALS = Goals;
1d534baa2827 parking code for old-style locales here;
wenzelm
parents: 11963
diff changeset
  1242
open BasicGoals;