src/Pure/section_utils.ML
author lcp
Thu Aug 25 12:21:00 1994 +0200 (1994-08-25)
changeset 579 08f465e23dc5
child 613 f9eb0f819642
permissions -rw-r--r--
new file of useful things for writing theory sections
lcp@579
     1
(*  Title: 	Pure/section-utils
lcp@579
     2
    ID:         $Id$
lcp@579
     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
fun lookup_const sign a = Symtab.lookup(#const_tab (Sign.rep_sg sign), a);
lcp@579
    24
lcp@579
    25
lcp@579
    26
(*Read an assumption in the given theory*)
lcp@579
    27
fun assume_read thy a = assume (read_cterm (sign_of thy) (a,propT));
lcp@579
    28
lcp@579
    29
fun readtm sign T a = 
lcp@579
    30
    read_cterm sign (a,T) |> term_of
lcp@579
    31
    handle ERROR => error ("The error above occurred for " ^ a);
lcp@579
    32
lcp@579
    33
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
lcp@579
    34
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
lcp@579
    35
  | tryres (th, []) = raise THM("tryres", 0, [th]);
lcp@579
    36
lcp@579
    37
fun gen_make_elim elim_rls rl = 
lcp@579
    38
      standard (tryres (rl, elim_rls @ [revcut_rl]));
lcp@579
    39
lcp@579
    40
(** String manipulation **)
lcp@579
    41
lcp@579
    42
(*Skipping initial blanks, find the first identifier*)
lcp@579
    43
fun scan_to_id s = 
lcp@579
    44
    s |> explode |> take_prefix is_blank |> #2 |> Scanner.scan_id |> #1
lcp@579
    45
    handle LEXICAL_ERROR => error ("Expected to find an identifier in " ^ s);
lcp@579
    46
lcp@579
    47
fun is_backslash c = c = "\\";
lcp@579
    48
lcp@579
    49
(*Apply string escapes to a quoted string; see Def of Standard ML, page 3
lcp@579
    50
  Does not handle the \ddd form;  no error checking*)
lcp@579
    51
fun escape [] = []
lcp@579
    52
  | escape cs = (case take_prefix (not o is_backslash) cs of
lcp@579
    53
	 (front, []) => front
lcp@579
    54
       | (front, _::"n"::rest) => front @ ("\n" :: escape rest)
lcp@579
    55
       | (front, _::"t"::rest) => front @ ("\t" :: escape rest)
lcp@579
    56
       | (front, _::"^"::c::rest) => front @ (chr(ord(c)-64) :: escape rest)
lcp@579
    57
       | (front, _::"\""::rest) => front @ ("\"" :: escape rest)
lcp@579
    58
       | (front, _::"\\"::rest) => front @ ("\\" :: escape rest)
lcp@579
    59
       | (front, b::c::rest) => 
lcp@579
    60
	   if is_blank c   (*remove any further blanks and the following \ *)
lcp@579
    61
	   then front @ escape (tl (snd (take_prefix is_blank rest)))
lcp@579
    62
	   else error ("Unrecognized string escape: " ^ implode(b::c::rest)));
lcp@579
    63
lcp@579
    64
(*Remove the first and last charaters -- presumed to be quotes*)
lcp@579
    65
val trim = implode o escape o rev o tl o rev o tl o explode;