| 1 | :- multifile generate/2. | |
| 2 | :- multifile shrink/3. | |
| 3 | ||
| 4 | :- use_module(library(lists),[is_list/1,maplist/2]). | |
| 5 | :- use_module(library(random),[random_permutation/2]). | |
| 6 | ||
| 7 | % mutation of a list | |
| 8 | generate(mutation(L:list),Value) :- | |
| 9 | flattened(L) , | |
| 10 | random_permutation(L,Value). | |
| 11 | % mutation of a list of lists | |
| 12 | generate(mutation(ListOfLists:list),Value) :- | |
| 13 | % mutate each list on its own | |
| 14 | maplist(random_permutation,ListOfLists,Temp) , | |
| 15 | % also mutate the whole list | |
| 16 | random_permutation(Temp,Value). | |
| 17 | ||
| 18 | % mutation of trees | |
| 19 | % random permutation of a binary tree | |
| 20 | generate(mutation(Tree:tree(_)),Value) :- | |
| 21 | \+ is_list(Tree) , | |
| 22 | tree_to_list(Tree,TreeList) , | |
| 23 | random_permutation(TreeList,TreePerm) , | |
| 24 | list_to_tree(TreePerm,Value). | |
| 25 | ||
| 26 | % random permutation of several binary trees given in a list | |
| 27 | generate(mutation(List:tree(_)),Value) :- | |
| 28 | maplist(tree_to_list,List,TreeList) , | |
| 29 | flatten(TreeList,FlattenedList) , | |
| 30 | random_permutation(FlattenedList,TreePerm) , | |
| 31 | list_to_tree(TreePerm,Value). | |
| 32 | ||
| 33 | shrink(mutation(_:Type),Value,Shrunken) :- | |
| 34 | shrink(Type,Value,Shrunken). | |
| 35 | ||
| 36 | % flatten a nested list | |
| 37 | flatten([],[]) :- !. | |
| 38 | flatten(H,[H]) :- \+is_list(H). | |
| 39 | flatten([H|T],List) :- | |
| 40 | flatten(H,L1) , | |
| 41 | flatten(T,L2) , | |
| 42 | append(L1,L2,List). | |
| 43 | ||
| 44 | % tests if list is flattened | |
| 45 | flattened(L) :- | |
| 46 | flatten(L,L). | |
| 47 | ||
| 48 | % convert all Key-Value pairs to Key | |
| 49 | avl_list_to_list(AVLList,List) :- | |
| 50 | findall(Key,member(Key-_,AVLList),List). | |
| 51 | ||
| 52 | % convert (Index,Value) to Value | |
| 53 | seq_to_list(Seq,List) :- | |
| 54 | findall(Value,member((_,Value),Seq),List). |