-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.js
157 lines (142 loc) · 5.5 KB
/
random.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var pl;
(function( pl ) {
var predicates = function() {
return {
// maybe/0
"maybe/0": function( thread, point, _ ) {
if( Math.random() < 0.5 ) {
thread.success( point );
}
},
// maybe/1
"maybe/1": function( thread, point, atom ) {
var num = atom.args[0];
if( Math.random() < num.value ) {
thread.success( point );
}
},
// random/1
"random/1": function( thread, point, atom ) {
var rand = atom.args[0];
if( !pl.type.is_variable( rand ) && !pl.type.is_number( rand ) ) {
thread.throw_error( pl.error.type( "number", rand, atom.indicator ) );
} else {
var gen = Math.random();
thread.prepend( [new pl.type.State(
point.goal.replace( new pl.type.Term( "=", [rand, new pl.type.Num( gen, true )] ) ),
point.substitution, point
)] );
}
},
// random/3
"random/3": function( thread, point, atom ) {
var lower = atom.args[0], upper = atom.args[1], rand = atom.args[2];
if( pl.type.is_variable( lower ) || pl.type.is_variable( upper ) ) {
thread.throw_error( pl.error.instantiation( atom.indicator ) );
} else if( !pl.type.is_number( lower ) ) {
thread.throw_error( pl.error.type( "number", lower, atom.indicator ) );
} else if( !pl.type.is_number( upper ) ) {
thread.throw_error( pl.error.type( "number", upper, atom.indicator ) );
} else if( !pl.type.is_variable( rand ) && !pl.type.is_number( rand ) ) {
thread.throw_error( pl.error.type( "number", rand, atom.indicator ) );
} else {
if( lower.value < upper.value ) {
var float = lower.is_float || upper.is_float;
var gen = lower.value + Math.random() * (upper.value - lower.value);
if( !float )
gen = Math.floor( gen );
thread.prepend( [new pl.type.State(
point.goal.replace( new pl.type.Term( "=", [rand, new pl.type.Num( gen, float )] ) ),
point.substitution, point
)] );
}
}
},
// random_between/3
"random_between/3": function( thread, point, atom ) {
var lower = atom.args[0], upper = atom.args[1], rand = atom.args[2];
if( pl.type.is_variable( lower ) || pl.type.is_variable( upper ) ) {
thread.throw_error( pl.error.instantiation( atom.indicator ) );
} else if( !pl.type.is_integer( lower ) ) {
thread.throw_error( pl.error.type( "integer", lower, atom.indicator ) );
} else if( !pl.type.is_integer( upper ) ) {
thread.throw_error( pl.error.type( "integer", upper, atom.indicator ) );
} else if( !pl.type.is_variable( rand ) && !pl.type.is_integer( rand ) ) {
thread.throw_error( pl.error.type( "integer", rand, atom.indicator ) );
} else {
if( lower.value < upper.value ) {
var gen = Math.floor(lower.value + Math.random() * (upper.value - lower.value + 1));
thread.prepend( [new pl.type.State(
point.goal.replace( new pl.type.Term( "=", [rand, new pl.type.Num( gen, false )] ) ),
point.substitution, point
)] );
}
}
},
// random_member/2
"random_member/2": function( thread, point, atom ) {
var member = atom.args[0], list = atom.args[1];
if( pl.type.is_variable( list ) ) {
thread.throw_error( pl.error.instantiation( atom.indicator ) );
} else {
var array = [];
var pointer = list;
while( pointer.indicator === "./2" ) {
array.push(pointer.args[0]);
pointer = pointer.args[1];
}
if( array.length > 0 ) {
var gen = Math.floor(Math.random() * array.length);
thread.prepend( [new pl.type.State(
point.goal.replace( new pl.type.Term( "=", [member, array[gen]] ) ),
point.substitution, point
)] );
}
}
},
// random_permutation/2
"random_permutation/2": function( thread, point, atom ) {
var i;
var list = atom.args[0], permutation = atom.args[1];
var ins_list = pl.type.is_instantiated_list( list );
var ins_permutation = pl.type.is_instantiated_list( permutation );
if( pl.type.is_variable( list ) && pl.type.is_variable( permutation ) || !ins_list && !ins_permutation ) {
thread.throw_error( pl.error.instantiation( atom.indicator ) );
} else if( !pl.type.is_variable( list ) && !pl.type.is_fully_list( list ) ) {
thread.throw_error( pl.error.type( "list", list, atom.indicator ) );
} else if( !pl.type.is_variable( permutation ) && !pl.type.is_fully_list( permutation ) ) {
thread.throw_error( pl.error.type( "list", permutation, atom.indicator ) );
} else {
var pointer = ins_list ? list : permutation;
var array = [];
while( pointer.indicator === "./2" ) {
array.push( pointer.args[0] );
pointer = pointer.args[1];
}
for( i = 0; i < array.length; i++ ) {
var rand = Math.floor( Math.random() * array.length );
var tmp = array[i];
array[i] = array[rand];
array[rand] = tmp;
}
var new_list = new pl.type.Term( "[]", [] );
for( i = array.length-1; i >= 0; i-- )
new_list = new pl.type.Term( ".", [array[i], new_list] );
thread.prepend( [new pl.type.State(
point.goal.replace( new pl.type.Term( "=", [new_list, ins_list ? permutation : list] ) ),
point.substitution, point
)] );
}
}
};
};
var exports = ["maybe/0", "maybe/1", "random/1", "random/3", "random_between/3", "random_member/2", "random_permutation/2"];
if( typeof module !== 'undefined' ) {
module.exports = function( p ) {
pl = p;
new pl.type.Module( "random", predicates(), exports );
};
} else {
new pl.type.Module( "random", predicates(), exports );
}
})( pl );