-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoseTree.dfy
67 lines (58 loc) · 1.64 KB
/
RoseTree.dfy
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
datatype RoseTree<A> = RoseTree(data: A, children: seq<RoseTree<A>>)
function RoseTreeSize<A>(r: RoseTree<A>): nat
{
1 + RoseTreeListSize(r.children)
}
function RoseTreeListSize<A>(l: seq<RoseTree<A>>): nat
{
if l == [] then
0
else
1 + RoseTreeSize(l[0]) + RoseTreeListSize(l[1..])
}
function method ExtractChild<A>(r: RoseTree<A>): RoseTree<A>
{
if |r.children| == 1 then
r.children[0]
else r
}
predicate FilterSafe<A>(f: set<A>, r: RoseTree<A>)
{
&& (r.data in f ==> |r.children| == 1)
&& FilterListSafe(f, r.children)
}
predicate FilterListSafe<A>(f: set<A>, l: seq<RoseTree<A>>)
{
l != [] ==>
&& FilterSafe(f, l[0])
&& FilterListSafe(f, l[1..])
}
lemma FilterSafe_ExtractChild<A>(f: set<A>, r: RoseTree<A>)
requires FilterSafe(f, r)
ensures FilterSafe(f, ExtractChild(r))
{}
lemma FilterSafe_RoseTreeSize_ExtractChild<A>(f: set<A>, r: RoseTree<A>)
requires FilterSafe(f, r) && r.data in f
ensures RoseTreeSize(ExtractChild(r)) < RoseTreeSize(r)
{}
function method FilterTree<A>(f: set<A>, r: RoseTree<A>): RoseTree<A>
requires FilterSafe(f, r)
decreases RoseTreeSize(r)
{
if r.data in f then
var r' := ExtractChild(r);
FilterSafe_ExtractChild(f, r);
FilterSafe_RoseTreeSize_ExtractChild(f, r);
FilterTree(f, r')
else
RoseTree(r.data, FilterTreeList(f, r.children))
}
function method FilterTreeList<A>(f: set<A>, l: seq<RoseTree<A>>): seq<RoseTree<A>>
requires FilterListSafe(f, l)
decreases RoseTreeListSize(l)
{
if l == [] then
[]
else
[FilterTree(f, l[0])] + FilterTreeList(f, l[1..])
}