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