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
|
|
|
|
|
|
2018-10-26 15:47:03 +02:00
|
|
|
use mach.peano.Peano
|
2018-06-15 16:45:31 +02:00
|
|
|
use list.List
|
|
|
|
|
use list.Length as L
|
2011-02-18 11:20:16 +01:00
|
|
|
|
2015-08-20 13:25:30 +02: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
|
|
|
|
2013-04-07 17:59:24 +02: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
|
|
|
|
2012-12-21 17:13:40 +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 }
|
|
|
|
|
|
2013-04-07 17:59:24 +02:00
|
|
|
val safe_top (s: t 'a) : 'a
|
2012-12-21 17:13:40 +01:00
|
|
|
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
|
|
|
|
2013-04-07 17:59:24 +02:00
|
|
|
val copy (s: t 'a) : t 'a ensures { result = s }
|
2011-02-18 11:20:16 +01:00
|
|
|
|
2013-04-07 17:59:24 +02: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
|
|
|
|
2011-12-17 21:32:59 +01:00
|
|
|
function length (s: t 'a) : int = L.length s.elts
|
2011-02-18 11:20:16 +01:00
|
|
|
|
2018-10-26 15:47:03 +02:00
|
|
|
val length (s: t 'a) : Peano.t
|
2012-10-13 00:23:29 +02:00
|
|
|
ensures { result = L.length s.elts }
|
2012-07-14 14:50:39 +02:00
|
|
|
|
2011-02-18 11:20:16 +01:00
|
|
|
end
|