/
Queue.bosatsu
126 lines (102 loc) · 3.57 KB
/
Queue.bosatsu
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package Queue
from Bosatsu/List import eq_List
from Bosatsu/Rand import (Rand, sequence_Rand, map_Rand, flat_map_Rand,
int_range as int_range_Rand, geometric_Int)
from Bosatsu/Properties import Prop, forall_Prop, suite_Prop, run_Prop
export (Queue,
empty_Queue, fold_Queue, push, unpush, pop_value, pop, reverse_Queue, eq_Queue,
to_List, from_List
)
struct Queue(front: List[a], back: List[a])
empty_Queue: forall a. Queue[a] = Queue([], [])
# convenient local alias
empty = empty_Queue
def from_List(list: List[a]) -> Queue[a]:
Queue(list, [])
def push(Queue(f, b): Queue[a], item: a) -> Queue[a]:
Queue(f, [item, *b])
def unpush(queue: Queue[a]) -> Option[(a, Queue[a])]:
match queue:
Queue([h, *t], b): Some((h, Queue(t, b)))
Queue([], b):
match b.reverse():
[]: None
[h, *t]: Some((h, Queue(t, [])))
def pop_value(queue: Queue[a]) -> Option[a]:
match unpush(queue):
Some((a, _)): Some(a)
None: None
# drop an item off and return the rest, or empty
def pop(queue: Queue[a]) -> Queue[a]:
match unpush(queue):
Some((_, queue)): queue
None: empty
def fold_Queue(Queue(f, b): Queue[a], init: b, fold_fn: (b, a) -> b) -> b:
front = f.foldLeft(init, fold_fn)
b.reverse().foldLeft(front, fold_fn)
def reverse_Queue(Queue(f, b): Queue[a]) -> Queue[a]:
Queue(b.reverse(), f.reverse())
def eq_Queue(eq_fn: (a, a) -> Bool)(left: Queue[a], right: Queue[a]) -> Bool:
res = left.fold_Queue((True, right), \(g, right), al ->
if g:
match unpush(right):
None: (False, empty)
Some((ar, right)):
(eq_fn(al, ar), right)
else:
(False, empty)
)
match res:
(True, Queue([], [])): True
_: False
def to_List(Queue(f, b): Queue[a]) -> List[a]:
f.concat(b.reverse())
########
## TestSuites below
########
def eq_Opt(eq_inner)(a, b):
match (a, b):
(Some(a), Some(b)): eq_inner(a, b)
(None, None): True
_: False
eq_oi = eq_Opt(eq_Int)
eq_qi = eq_Queue(eq_Int)
eq_li = eq_List(eq_Int)
q12 = empty.push(1).push(2)
def samp(r)(fn): flat_map_Rand(r, fn)
rand_geo_List_Int: Rand[List[Int]] = (
rand_int = int_range_Rand(128)
len <- geometric_Int.samp()
lst = replicate_List(rand_int, len)
sequence_Rand(lst)
)
rand_Queue_Int: Rand[Queue[Int]] = rand_geo_List_Int.map_Rand(from_List)
queue_laws = suite_Prop(
"queue properties",
[
forall_Prop(rand_Queue_Int, "pop-law/toList", q -> (
res = match q.pop_value():
case None: to_List(q) matches []
case Some(i):
match to_List(q):
case [h, *_]: eq_Int(h, i)
case _: False
Assertion(res, "check head")
))
]
)
tests = TestSuite("queue tests", [
Assertion(eq_oi(q12.pop_value(), Some(1)), "1"),
Assertion(q12.fold_Queue(0,add).eq_Int(3), "fold_Queue add"),
Assertion(q12.fold_Queue(0,\_, x -> x).eq_Int(2), "take the second"),
Assertion(q12.fold_Queue(0,\x, _ -> x).eq_Int(0), "take the first"),
Assertion(q12.reverse_Queue().reverse_Queue().eq_qi(q12), "reverse is idempotent"),
Assertion(q12.eq_qi(from_List([1, 2])), "from list [1, 2]"),
Assertion(q12.push(3).eq_qi(from_List([1, 2, 3])), "from list [1, 2, 3]"),
Assertion(empty_Queue.eq_qi(from_List([])), "empty_Queue == from_List([])"),
Assertion(q12.eq_qi(from_List([1, 2])), "from list [1, 2]"),
Assertion(from_List([1, 2, 3]).pop().pop().pop().eq_qi(empty), "pop to empty"),
Assertion(empty.pop().eq_qi(empty), "pop empty is okay"),
Assertion(to_List(from_List([1, 1, 2, 2, 3, 3])).eq_li([1, 1, 2, 2, 3, 3]), "to/from List"),
run_Prop(queue_laws, 100, 4242),
])