| as yet unclassified |
| pickANumber
|
^ self pickANumberGreaterThan: 0
|
| pickANumberGreaterThan: aNumber
|
^ amb valueOf: [aNumber + 1] or: [self pickANumberGreaterThan: aNumber + 1]
|
| setUp
|
amb _ Amb new
|
| testAllValuesAboveFive
|
|x results|
results _ amb allValues:
[x _ amb oneOf: (1 to: 10).
amb assert: (x > 5).
x].
self assert: results = #(6 7 8 9 10).
|
| testMaybe
|
|x y z|
x _ amb maybe.
y _ amb maybe.
z _ amb maybe not.
amb deny: x = y.
amb deny: x = z.
self assert: x.
self deny: y.
self deny: z.
|
| testPickANumber
|
self assert: self pickANumber = 1.
|
| testPickANumberAboveFive
|
|x|
x _ self pickANumber.
amb assert: (x > 5).
self assert: x = 6.
|
| testSicpLogicProblem
|
"Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher's. Fletcher does not live on a floor adjacent to Cooper's. Where does everyone live?"
"This implementation is too slow - uncomment to actually run it."
" |baker cooper fletcher miller smith|
baker _ amb oneOf: (1 to: 5).
cooper _ amb oneOf: (1 to: 5).
fletcher _ amb oneOf: (1 to: 5).
miller _ amb oneOf: (1 to: 5).
smith _ amb oneOf: (1 to: 5).
amb assert: (Set new add: baker; add: cooper; add: fletcher; add: miller; add: smith; size) = 5.
amb deny: baker = 5.
amb deny: cooper = 1.
amb deny: fletcher = 5.
amb deny: fletcher = 1.
amb assert: miller > cooper.
amb deny: (smith - fletcher) abs = 1.
amb deny: (fletcher - cooper) abs = 1.
self assert: baker = 3.
self assert: cooper = 2.
self assert: fletcher = 4.
self assert: miller = 5.
self assert: smith = 1.
"
|
| testSicpLogicProblemFaster
|
"Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher's. Fletcher does not live on a floor adjacent to Cooper's. Where does everyone live?"
|baker cooper fletcher miller smith|
fletcher _ amb oneOf: (1 to: 5).
amb deny: fletcher = 5.
amb deny: fletcher = 1.
smith _ amb oneOf: (1 to: 5).
amb deny: (smith - fletcher) abs = 1.
cooper _ amb oneOf: (1 to: 5).
amb deny: cooper = 1.
amb deny: (fletcher - cooper) abs = 1.
miller _ amb oneOf: (1 to: 5).
amb assert: miller > cooper.
baker _ amb oneOf: (1 to: 5).
amb deny: baker = 5.
amb assert: (Set new add: baker; add: cooper; add: fletcher; add: miller; add: smith; size) = 5.
self assert: baker = 3.
self assert: cooper = 2.
self assert: fletcher = 4.
self assert: miller = 5.
self assert: smith = 1.
|
| testSolveAnEquation
|
|x y|
x _ amb oneOf: (1 to: 10).
y _ amb oneOf: (1 to: 10).
amb assert: (y * x) = 42.
self assert: x = 6.
self assert: y = 7.
|