-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankersQueue.hs
59 lines (49 loc) · 1.34 KB
/
BankersQueue.hs
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
52
53
54
55
56
57
58
59
module Queue.BankersQueue where
{- DONE:
- lenf >= lenr is ensured
- saftey of partial functions is checked
- implement generic Queue typeclass
-}
{- TODO:
- content of queue is preserved
- queue order is presevered
- write some examples demonstrating checked properties
-}
import Prelude hiding (head, tail)
import Queue.Queue
{-@ data BankersQueue a = BQ {
lenf :: Nat,
f :: {v:[a] | len v == lenf},
lenr :: {v:Nat | v <= lenf},
r :: {v:[a] | len v == lenr}
}
@-}
type BQ a = BankersQueue a
data BankersQueue a = BQ {
lenf :: Int,
f :: [a],
lenr :: Int,
r :: [a]
} deriving Show
{-@ check ::
vlenf : Nat ->
{v:[_] | len v == vlenf} ->
vlenr : Nat ->
{v:[_] | len v == vlenr} ->
{q:BQ _ | qlen q == (vlenf + vlenr)}
@-}
check :: Int -> [a] -> Int -> [a] -> BQ a
check lenf f lenr r =
if lenr <= lenf then
BQ lenf f lenr r
else
BQ (lenf + lenr) (f ++ (reverse r)) 0 []
instance Queue BankersQueue where
{-@ instance measure qlen :: BQ a -> Int
qlen (BQ f _ r _) = f + r
@-}
empty = BQ 0 [] 0 []
isEmpty (BQ lenf f lenr r) = (lenf == 0)
snoc (BQ lenf f lenr r) x = check lenf f (lenr + 1) (x : r)
head (BQ lenf (x : f') lenr r) = x
tail (BQ lenf (x : f') lenr r) = check (lenf - 1) f' lenr r