Files
why3/stdlib/stack.mlw

54 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

2011-02-18 11:20:16 +01:00
2012-05-07 15:12:08 +02:00
(** {1 Stacks} *)
(** {2 Polymorphic mutable stacks} *)
2011-02-18 11:20:16 +01:00
module Stack
use mach.peano.Peano
use list.List
use list.Length as L
2011-02-18 11:20:16 +01:00
type t 'a = abstract { mutable elts: list 'a }
2011-02-18 11:20:16 +01:00
2012-10-13 00:23:29 +02:00
val create () : t 'a ensures { result.elts = Nil }
2011-02-18 11:20:16 +01:00
2012-10-13 00:23:29 +02:00
val push (x: 'a) (s: t 'a) : unit writes {s}
ensures { s.elts = Cons x (old s.elts) }
2011-02-18 11:20:16 +01:00
exception Empty
2012-10-13 00:23:29 +02:00
val pop (s: t 'a) : 'a writes {s}
ensures { match old s.elts with Nil -> false
| Cons x t -> result = x /\ s.elts = t end }
raises { Empty -> s.elts = old s.elts = Nil }
2011-02-18 11:20:16 +01:00
val top (s: t 'a) : 'a
2012-10-13 00:23:29 +02:00
ensures { match s.elts with Nil -> false
| Cons x _ -> result = x end }
raises { Empty -> s.elts = Nil }
2011-02-18 11:20:16 +01:00
val safe_pop (s: t 'a) : 'a writes {s}
requires { s.elts <> Nil }
ensures { match old s.elts with Nil -> false
| Cons x t -> result = x /\ s.elts = t end }
val safe_top (s: t 'a) : 'a
requires { s.elts <> Nil }
ensures { match s.elts with Nil -> false
| Cons x _ -> result = x end }
2012-10-13 00:23:29 +02:00
val clear (s: t 'a) : unit writes {s} ensures { s.elts = Nil }
2011-02-18 11:20:16 +01:00
val copy (s: t 'a) : t 'a ensures { result = s }
2011-02-18 11:20:16 +01:00
val is_empty (s: t 'a) : bool
2012-10-13 00:23:29 +02:00
ensures { result = True <-> s.elts = Nil }
2011-02-18 11:20:16 +01:00
function length (s: t 'a) : int = L.length s.elts
2011-02-18 11:20:16 +01:00
val length (s: t 'a) : Peano.t
2012-10-13 00:23:29 +02:00
ensures { result = L.length s.elts }
2011-02-18 11:20:16 +01:00
end