src/Pure/General/stack.ML
author wenzelm
Thu Apr 03 18:42:36 2008 +0200 (2008-04-03)
changeset 26538 d65504ffb47d
parent 21272 a87b27cdd142
child 28129 8f647d24b49f
permissions -rw-r--r--
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm@17368
     1
(*  Title:      Pure/General/stack.ML
wenzelm@17368
     2
    ID:         $Id$
wenzelm@17368
     3
    Author:     Makarius
wenzelm@17368
     4
wenzelm@17368
     5
Non-empty stacks.
wenzelm@17368
     6
*)
wenzelm@17368
     7
wenzelm@17368
     8
signature STACK =
wenzelm@17368
     9
sig
wenzelm@17756
    10
  type 'a T (*= 'a * 'a list*)
wenzelm@17368
    11
  val level: 'a T -> int
wenzelm@17368
    12
  val init: 'a -> 'a T
wenzelm@17368
    13
  val top: 'a T -> 'a
wenzelm@21272
    14
  val map_top: ('a -> 'a) -> 'a T -> 'a T
wenzelm@21272
    15
  val map_all: ('a -> 'a) -> 'a T -> 'a T
wenzelm@17368
    16
  val push: 'a T -> 'a T
wenzelm@17368
    17
  val pop: 'a T -> 'a T      (*exception Empty*)
wenzelm@17368
    18
end;
wenzelm@17368
    19
wenzelm@17368
    20
structure Stack: STACK =
wenzelm@17368
    21
struct
wenzelm@17368
    22
wenzelm@17368
    23
type 'a T = 'a * 'a list;
wenzelm@17368
    24
wenzelm@17368
    25
fun level (_, xs) = length xs;
wenzelm@17368
    26
wenzelm@17368
    27
fun init x = (x, []);
wenzelm@17368
    28
wenzelm@17368
    29
fun top (x, _) = x;
wenzelm@17368
    30
wenzelm@21272
    31
fun map_top f (x, xs) = (f x, xs);
wenzelm@21272
    32
wenzelm@21272
    33
fun map_all f (x, xs) = (f x, map f xs);
wenzelm@17368
    34
wenzelm@17368
    35
fun push (x, xs) = (x, x :: xs);
wenzelm@17368
    36
wenzelm@17368
    37
fun pop (_, x :: xs) = (x, xs)
wenzelm@17368
    38
  | pop (_, []) = raise Empty;
wenzelm@17368
    39
wenzelm@17368
    40
end;