src/Pure/section_utils.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3536 8fb4150e2ad3
child 3934 a9c8960e4da6
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
clasohm@1460
     1
(*  Title: 	Pure/section-utils
lcp@579
     2
    ID:         $Id$
clasohm@1460
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
lcp@579
     4
    Copyright   1994  University of Cambridge
lcp@579
     5
lcp@579
     6
Utilities for writing new theory sections
lcp@579
     7
*)
lcp@579
     8
lcp@579
     9
fun ap t u = t$u;
lcp@579
    10
fun app t (u1,u2) = t $ u1 $ u2;
lcp@579
    11
lcp@579
    12
(*Make distinct individual variables a1, a2, a3, ..., an. *)
lcp@579
    13
fun mk_frees a [] = []
lcp@579
    14
  | mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts;
lcp@579
    15
lcp@579
    16
(*Make a definition lhs==rhs*)
lcp@579
    17
fun mk_defpair (lhs, rhs) = 
lcp@579
    18
  let val Const(name, _) = head_of lhs
lcp@579
    19
  in (name ^ "_def", Logic.mk_equals (lhs, rhs)) end;
lcp@579
    20
lcp@579
    21
fun get_def thy s = get_axiom thy (s^"_def");
lcp@579
    22
lcp@579
    23
lcp@579
    24
(*Read an assumption in the given theory*)
lcp@579
    25
fun assume_read thy a = assume (read_cterm (sign_of thy) (a,propT));
lcp@579
    26
paulson@1390
    27
(*Read a term from string "b", with expected type T*)
paulson@1390
    28
fun readtm sign T b = 
paulson@1390
    29
    read_cterm sign (b,T) |> term_of
wenzelm@3536
    30
    handle ERROR => error ("The error(s) above occurred for " ^ b);
lcp@579
    31
lcp@579
    32
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
lcp@579
    33
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
lcp@579
    34
  | tryres (th, []) = raise THM("tryres", 0, [th]);
lcp@579
    35
lcp@579
    36
fun gen_make_elim elim_rls rl = 
lcp@579
    37
      standard (tryres (rl, elim_rls @ [revcut_rl]));
lcp@579
    38
lcp@579
    39
(** String manipulation **)
lcp@579
    40
lcp@579
    41
(*Skipping initial blanks, find the first identifier*)
lcp@579
    42
fun scan_to_id s = 
lcp@579
    43
    s |> explode |> take_prefix is_blank |> #2 |> Scanner.scan_id |> #1
lcp@579
    44
    handle LEXICAL_ERROR => error ("Expected to find an identifier in " ^ s);
lcp@579
    45
lcp@579
    46
fun is_backslash c = c = "\\";
lcp@579
    47
lcp@579
    48
(*Apply string escapes to a quoted string; see Def of Standard ML, page 3
lcp@579
    49
  Does not handle the \ddd form;  no error checking*)
lcp@579
    50
fun escape [] = []
lcp@579
    51
  | escape cs = (case take_prefix (not o is_backslash) cs of
clasohm@1460
    52
	 (front, []) => front
lcp@579
    53
       | (front, _::"n"::rest) => front @ ("\n" :: escape rest)
lcp@579
    54
       | (front, _::"t"::rest) => front @ ("\t" :: escape rest)
lcp@579
    55
       | (front, _::"^"::c::rest) => front @ (chr(ord(c)-64) :: escape rest)
lcp@579
    56
       | (front, _::"\""::rest) => front @ ("\"" :: escape rest)
lcp@579
    57
       | (front, _::"\\"::rest) => front @ ("\\" :: escape rest)
lcp@579
    58
       | (front, b::c::rest) => 
clasohm@1460
    59
	   if is_blank c   (*remove any further blanks and the following \ *)
clasohm@1460
    60
	   then front @ escape (tl (snd (take_prefix is_blank rest)))
clasohm@1460
    61
	   else error ("Unrecognized string escape: " ^ implode(b::c::rest)));
lcp@579
    62
lcp@579
    63
(*Remove the first and last charaters -- presumed to be quotes*)
lcp@579
    64
val trim = implode o escape o rev o tl o rev o tl o explode;
wenzelm@654
    65
wenzelm@654
    66
wenzelm@654
    67
(*Check for some named theory*)
wenzelm@654
    68
fun require_thy thy name sect =
wenzelm@654
    69
  if exists (equal name o !) (stamps_of_thy thy) then ()
paulson@2863
    70
  else error ("Need theory " ^ quote name ^ " as an ancestor for " ^ sect);