aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/bstack.ml
blob: c52b0e713a47ca7b355bbd267db83685a6ab5425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(***********************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team    *)
(* <O___,, *        INRIA-Rocquencourt  &  LRI-CNRS-Orsay              *)
(*   \VV/  *************************************************************)
(*    //   *      This file is distributed under the terms of the      *)
(*         *       GNU Lesser General Public License Version 2.1       *)
(***********************************************************************)

(* $Id$ *)

open Util

type 'a t = {mutable depth : int option;
             mutable stack : 'a list}

let create depth = {depth = depth;
                 stack = []}

let set_depth bs n = bs.depth <- n

let safe_chop_list len l =
  if List.length l <= len then (l,[]) else list_chop len l
        
let push bs e =
  match bs.depth with
    | None -> bs.stack <- e :: bs.stack
    | Some d -> bs.stack <- fst(safe_chop_list d (e :: bs.stack))
	  
let pop bs =
  match bs.stack with
    | [] -> None
    | h::tl -> (bs.stack <- tl; Some h)

let top bs =
  match bs.stack with
    | [] -> None
    | h::_ -> Some h
	  
let app_push bs f =
  match bs.stack with
    | [] -> error "Nothing on the stack"
    | h::_ -> push bs (f h)

let app_repl bs f =
  match bs.stack with
    | [] -> error "Nothing on the stack"
    | h::t -> bs.stack <- (f h)::t

let is_empty bs = bs.stack = []

let depth bs = List.length bs.stack