265 lines
8 KiB
Text
265 lines
8 KiB
Text
(
|
|
( number
|
|
= n done ntest oldFT
|
|
. !arg:(?done.(=?ntest).?oldFT)
|
|
& 0:?n
|
|
& ( !done
|
|
: ?
|
|
( !ntest
|
|
. !oldFT&1+!n:?n&~
|
|
)
|
|
?
|
|
| !n
|
|
)
|
|
)
|
|
& ( STATEMENTS
|
|
= ( (1."This is a numbered list of twelve statements.")
|
|
. 1
|
|
. (
|
|
= n nr done toDo
|
|
. !arg:(?done.?toDo)
|
|
& 0:?n
|
|
& whl
|
|
' ( !done:(?nr.?) ?done
|
|
& 1+!n:!nr:?n
|
|
)
|
|
& whl
|
|
' ( !toDo:((?nr.?).?) ?toDo
|
|
& 1+!n:!nr:?n
|
|
)
|
|
& (!n:12&true|false)
|
|
)
|
|
)
|
|
( (2."Exactly 3 of the last 6 statements are true.")
|
|
. end
|
|
. (
|
|
= done toDo lastSix
|
|
. !arg:(?done.?toDo)
|
|
& !done:? [-7 ?lastSix
|
|
& ( number$(!lastSix.(=?).true):3
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (3."Exactly 2 of the even-numbered statements are true.")
|
|
. end
|
|
. (
|
|
= done toDo ii
|
|
. !arg:(?done.?toDo)
|
|
& ( number
|
|
$ ( !done
|
|
. (=?ii&!ii*1/2:~/)
|
|
. true
|
|
)
|
|
: 2
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (4."If statement 5 is true, then statements 6 and 7 are both true.")
|
|
. 7
|
|
. (
|
|
= done toDo
|
|
. !arg:(?done.?toDo)
|
|
& ( !done
|
|
: ( ? (5.false) ?
|
|
| ? (6.true) ?
|
|
: ? (7.true) ?
|
|
)
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (5."The 3 preceding statements are all false.")
|
|
. 5
|
|
. (
|
|
= done toDo
|
|
. !arg:(?done.?toDo)
|
|
& ( !done
|
|
: ?
|
|
(?.false)
|
|
(?.false)
|
|
(?.false)
|
|
(?.?)
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (6."Exactly 4 of the odd-numbered statements are true.")
|
|
. end
|
|
. (
|
|
= done toDo i
|
|
. !arg:(?done.?toDo)
|
|
& ( number
|
|
$ ( !done
|
|
. (=?i&!i*1/2:/)
|
|
. true
|
|
)
|
|
: 4
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (7."Either statement 2 or 3 is true, but not both.")
|
|
. 7
|
|
. (
|
|
= done toDo
|
|
. !arg:(?done.?toDo)
|
|
& ( number
|
|
$ (!done.(=2|3).true)
|
|
: 1
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (8."If statement 7 is true, then 5 and 6 are both true.")
|
|
. 8
|
|
. (
|
|
= done toDo
|
|
. !arg:(?done.?toDo)
|
|
& ( !done
|
|
: ( ? (7.false) ?
|
|
| ? (5.true) ?
|
|
: ? (6.true) ?
|
|
)
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (9."Exactly 3 of the first 6 statements are true.")
|
|
. 9
|
|
. (
|
|
= done toDo firstSix
|
|
. !arg:(?done.?toDo)
|
|
& !done:?firstSix [6 ?
|
|
& ( number$(!firstSix.(=?).true):3
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (10."The next two statements are both true.")
|
|
. 12
|
|
. (
|
|
= done toDo
|
|
. !arg:(?done.?toDo)
|
|
& ( !done:? (?.true) (?.true)
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (11."Exactly 1 of statements 7, 8 and 9 are true.")
|
|
. 11
|
|
. (
|
|
= done toDo
|
|
. !arg:(?done.?toDo)
|
|
& ( number
|
|
$ ( !done
|
|
. (=7|8|9)
|
|
. true
|
|
)
|
|
: 1
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
( (12."Exactly 4 of the preceding statements are true.")
|
|
. 12
|
|
. (
|
|
= done toDo preceding
|
|
. !arg:(?done.?toDo)
|
|
& !done:?preceding (?.?)
|
|
& ( number$(!preceding.(=?).true):4
|
|
& true
|
|
| false
|
|
)
|
|
)
|
|
)
|
|
)
|
|
& ( TestTruth
|
|
= done toDo postponedTests testToBePostponed
|
|
, n when test FT oldFT A Z text
|
|
, postponedTest testNow
|
|
. !arg:(?done.?toDo.?postponedTests)
|
|
& ( !toDo:
|
|
& "We have come to the end of the list of tests.
|
|
Perform any tests that had to be postponed until now."
|
|
& whl
|
|
' ( !postponedTests
|
|
: (?.?oldFT.(=?postponedTest)) ?A
|
|
& postponedTest$(!done.):!oldFT
|
|
& !A:?postponedTests
|
|
)
|
|
& !postponedTests:
|
|
& out$("Solution:" !done)
|
|
& ~
|
|
| !toDo
|
|
: ((?n.?text).?when.(=?test)) ?toDo
|
|
& "'false' and 'true' are just two symbols, not 'boolean values'.
|
|
You can choose other symbols if you like.
|
|
The program first guesses the first symbol and assigns it to the variable FT.
|
|
After backtracking, the second symbol is guessed and assigned to FT.
|
|
This is done for each statement."
|
|
& false true
|
|
: ?
|
|
%@?FT
|
|
( ?
|
|
& 1+!guesses:?guesses
|
|
& (!n.!FT):?testNow
|
|
& "Do all tests that had to be postponed until now, unless one of those tests
|
|
fails. Remove the successful tests from the list of postponed tests."
|
|
& whl
|
|
' ( !postponedTests
|
|
: ?A
|
|
(!n.?oldFT.(=?postponedTest))
|
|
?Z
|
|
& postponedTest$(!done !testNow.!toDo)
|
|
: !oldFT
|
|
& !A !Z:?postponedTests
|
|
)
|
|
& "Check that all tests that had to be postponed until now are removed from
|
|
the list of postponed tests. Only then go on with looking at testing
|
|
the current statement. Backtrack if a test failed."
|
|
& !postponedTests:~(? (!n.?) ?)
|
|
& ( !when:>!n
|
|
& "The current statement cannot be tested right now. Postpone it to
|
|
the earliest coming statement where the current statement can be
|
|
tested.
|
|
(The earliest statement, denoted by 'when', is computed manually.)"
|
|
& (!when.!FT.'$test):?testToBePostponed
|
|
| "No need to postpone. Test the current statement now."
|
|
& :?testToBePostponed
|
|
& "If the test fails, backtrack. If it succeeds, go on to the next
|
|
statement."
|
|
& test$(!done !testNow.!toDo):!FT
|
|
)
|
|
& "So far so good. Test the next statements. (recursively)"
|
|
& TestTruth
|
|
$ ( !done !testNow
|
|
. !toDo
|
|
. !testToBePostponed !postponedTests
|
|
)
|
|
)
|
|
)
|
|
)
|
|
& 0:?guesses
|
|
& TestTruth$(.!STATEMENTS.)
|
|
| out
|
|
$ ( str
|
|
$ ( "That's it. I made "
|
|
!guesses
|
|
" true/false guesses in all. (A brute force method needs 2^12="
|
|
2^12
|
|
" guesses."
|
|
)
|
|
)
|
|
);
|