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