1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(random),[random/3,random_member/2]). | |
5 | :- use_module(library(trees),[list_to_tree/2,tree_to_list/2]). | |
6 | ||
7 | % Options: | |
8 | % size:X with X the amount of elements in the tree | |
9 | ||
10 | % random binary tree | |
11 | generate(tree(Options),Value) :- | |
12 | is_list(Options) , | |
13 | generate(tree(any,Options),Value). | |
14 | ||
15 | generate(tree(Type),Value) :- | |
16 | generate(tree(Type,[]),Value). | |
17 | ||
18 | % tree of given type | |
19 | generate(tree(Type,Options),Value) :- | |
20 | % size specifies amount of elements in the tree | |
21 | (member(size:Size,Options) | |
22 | -> Size > 0 | |
23 | ; random(1,20,Size)) , | |
24 | generate(list(Type,[size:Size]),List) , | |
25 | list_to_tree(List,Value). | |
26 | ||
27 | % shrink binary tree | |
28 | % empty tree | |
29 | shrink(Type,t(_,t,t),t) :- | |
30 | Type =..[tree|_]. | |
31 | ||
32 | shrink(Type,Value,Shrunken) :- | |
33 | Type =.. [tree|_] , | |
34 | tree_to_list(Value,List) , | |
35 | % refer to shrinking of lists | |
36 | shrink(list(_),List,ShrunkenList) , | |
37 | list_to_tree(ShrunkenList,Shrunken). |