1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(lists)). | |
5 | :- use_module(library(random),[random/3,random_member/2]). | |
6 | ||
7 | % Options: | |
8 | % size:X with X the amount of elements in the list | |
9 | ||
10 | % list of any type | |
11 | generate(list(Options),Value) :- | |
12 | is_list(Options) , | |
13 | generate(ground_type,Type) , | |
14 | generate(list(Type,Options),Value). | |
15 | ||
16 | generate(list(Type),Value) :- | |
17 | generate(list(Type,[]),Value). | |
18 | ||
19 | generate(list(_,Options),[]) :- | |
20 | member(size:0,Options), | |
21 | !. | |
22 | ||
23 | % list of given type | |
24 | generate(list(Type,Options),Value) :- | |
25 | (member(size:Size,Options) | |
26 | -> Size > 0 | |
27 | ; random(1,50,Size)) , | |
28 | length(Value,Size) , | |
29 | maplist(generate(Type),Value). | |
30 | ||
31 | % shrink list of list | |
32 | shrink(Type,Value,Shrunken) :- | |
33 | Type =..[list|_] , | |
34 | \+ flattened(Value) , | |
35 | maplist(shrink(list),Value,Shrunken). | |
36 | ||
37 | % check empty list | |
38 | shrink(Type,[_],[]) :- | |
39 | Type =..[list|_]. | |
40 | ||
41 | % remove random elements | |
42 | shrink(Type,L,NL) :- | |
43 | Type =.. [list|_] , | |
44 | is_list(L) , | |
45 | random_member(E,L) , | |
46 | delete(L,E,NL). | |
47 | ||
48 | % remove last element of a list | |
49 | remove_last([],[]). | |
50 | remove_last([_],[]). | |
51 | remove_last([H|T],[H|T2]) :- | |
52 | remove_last(T,T2). |