1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(random),[random/3]). | |
5 | ||
6 | % Options: | |
7 | % positive, negative | |
8 | ||
9 | generate(rational(Options),Value) :- | |
10 | member(positive,Options) , | |
11 | !, | |
12 | delete(Options,positive,NOptions) , | |
13 | random(0,2,R) , | |
14 | generate_rational_aux(positive,NOptions,R,Z,N) , | |
15 | Value = Z/N. | |
16 | ||
17 | generate(rational(Options),Value) :- | |
18 | member(negative,Options) , | |
19 | !, | |
20 | delete(Options,negative,NOptions) , | |
21 | random(0,2,R) , | |
22 | generate_rational_aux(negative,NOptions,R,Z,N) , | |
23 | Value = Z/N. | |
24 | ||
25 | generate(rational(Options),Value) :- | |
26 | random_member(Pol,[positive,negative]) , | |
27 | generate(rational([Pol|Options]),Value). | |
28 | ||
29 | generate_rational_aux(positive,Options,0,Z,N) :- | |
30 | generate(integer([positive|Options]),Z) , | |
31 | generate(integer([positive|Options]),N) , | |
32 | N \= 0. | |
33 | generate_rational_aux(positive,Options,1,Z,N) :- | |
34 | generate(integer([negative|Options]),Z) , | |
35 | generate(integer([negative|Options]),N) , | |
36 | N \= 0. | |
37 | generate_rational_aux(negative,Options,0,Z,N) :- | |
38 | generate(integer([positive|Options]),Z) , | |
39 | generate(integer([negative|Options]),N) , | |
40 | N \= 0. | |
41 | generate_rational_aux(negative,Options,1,Z,N) :- | |
42 | generate(integer([negative|Options]),Z) , | |
43 | generate(integer([positive|Options]),N) , | |
44 | N \= 0. | |
45 | ||
46 | shrink(rational(_),Val1/Val2,ShrunkenVal1/ShrunkenVal2) :- | |
47 | shrink(integer([]),Val1,ShrunkenVal1) , | |
48 | shrink(integer([]),Val2,ShrunkenVal2) , | |
49 | ShrunkenVal2 \= 0. | |
50 | ||
51 | shrink(rational(_),Value,Value). |