src/Pure/section_utils.ML
author wenzelm
Wed, 14 Sep 1994 16:11:19 +0200
changeset 613 f9eb0f819642
parent 579 08f465e23dc5
child 654 65435e2c6512
permissions -rw-r--r--
removed lookup_const (use Sign.const_type instead);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
579
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     1
(*  Title: 	Pure/section-utils
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     2
    ID:         $Id$
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     5
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     6
Utilities for writing new theory sections
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     7
*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     8
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
     9
fun ap t u = t$u;
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    10
fun app t (u1,u2) = t $ u1 $ u2;
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    11
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    12
(*Make distinct individual variables a1, a2, a3, ..., an. *)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    13
fun mk_frees a [] = []
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    14
  | mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts;
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    15
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    16
(*Make a definition lhs==rhs*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    17
fun mk_defpair (lhs, rhs) = 
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    18
  let val Const(name, _) = head_of lhs
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    19
  in (name ^ "_def", Logic.mk_equals (lhs, rhs)) end;
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    20
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    21
fun get_def thy s = get_axiom thy (s^"_def");
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    22
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    23
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    24
(*Read an assumption in the given theory*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    25
fun assume_read thy a = assume (read_cterm (sign_of thy) (a,propT));
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    26
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    27
fun readtm sign T a = 
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    28
    read_cterm sign (a,T) |> term_of
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    29
    handle ERROR => error ("The error above occurred for " ^ a);
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    30
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    31
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    32
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    33
  | tryres (th, []) = raise THM("tryres", 0, [th]);
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    34
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    35
fun gen_make_elim elim_rls rl = 
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    36
      standard (tryres (rl, elim_rls @ [revcut_rl]));
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    37
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    38
(** String manipulation **)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    39
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    40
(*Skipping initial blanks, find the first identifier*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    41
fun scan_to_id s = 
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    42
    s |> explode |> take_prefix is_blank |> #2 |> Scanner.scan_id |> #1
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    43
    handle LEXICAL_ERROR => error ("Expected to find an identifier in " ^ s);
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    44
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    45
fun is_backslash c = c = "\\";
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    46
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    47
(*Apply string escapes to a quoted string; see Def of Standard ML, page 3
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    48
  Does not handle the \ddd form;  no error checking*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    49
fun escape [] = []
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    50
  | escape cs = (case take_prefix (not o is_backslash) cs of
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    51
	 (front, []) => front
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    52
       | (front, _::"n"::rest) => front @ ("\n" :: escape rest)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    53
       | (front, _::"t"::rest) => front @ ("\t" :: escape rest)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    54
       | (front, _::"^"::c::rest) => front @ (chr(ord(c)-64) :: escape rest)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    55
       | (front, _::"\""::rest) => front @ ("\"" :: escape rest)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    56
       | (front, _::"\\"::rest) => front @ ("\\" :: escape rest)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    57
       | (front, b::c::rest) => 
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    58
	   if is_blank c   (*remove any further blanks and the following \ *)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    59
	   then front @ escape (tl (snd (take_prefix is_blank rest)))
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    60
	   else error ("Unrecognized string escape: " ^ implode(b::c::rest)));
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    61
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    62
(*Remove the first and last charaters -- presumed to be quotes*)
08f465e23dc5 new file of useful things for writing theory sections
lcp
parents:
diff changeset
    63
val trim = implode o escape o rev o tl o rev o tl o explode;