September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,32 +0,0 @@
|
|||
[Not]
|
||||
F | T
|
||||
? | ?
|
||||
T | F
|
||||
|
||||
[And]
|
||||
F ? T
|
||||
-------
|
||||
F | F F F
|
||||
? | F ? ?
|
||||
T | F ? T
|
||||
|
||||
[Or]
|
||||
F ? T
|
||||
-------
|
||||
F | F ? T
|
||||
? | ? ? T
|
||||
T | T T T
|
||||
|
||||
[Equiv]
|
||||
F ? T
|
||||
-------
|
||||
F | T ? F
|
||||
? | ? ? ?
|
||||
T | F ? T
|
||||
|
||||
[Imply]
|
||||
F ? T
|
||||
-------
|
||||
F | T T T
|
||||
? | ? ? T
|
||||
T | F ? T
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef double half_truth, maybe;
|
||||
|
||||
inline maybe not3(maybe a) { return 1 - a; }
|
||||
|
||||
inline maybe
|
||||
and3(maybe a, maybe b) { return a * b; }
|
||||
|
||||
inline maybe
|
||||
or3(maybe a, maybe b) { return a + b - a * b; }
|
||||
|
||||
inline maybe
|
||||
eq3(maybe a, maybe b) { return 1 - a - b + 2 * a * b; }
|
||||
|
||||
inline maybe
|
||||
imply3(maybe a, maybe b) { return or3(not3(a), b); }
|
||||
|
||||
#define true3(x) ((x) * RAND_MAX > rand())
|
||||
#define if3(x) if (true3(x))
|
||||
|
||||
int main()
|
||||
{
|
||||
maybe roses_are_red = 0.25; /* they can be white or black, too */
|
||||
maybe violets_are_blue = 1; /* aren't they just */
|
||||
int i;
|
||||
|
||||
puts("Verifying flowery truth for 40 times:\n");
|
||||
|
||||
puts("Rose is NOT red:"); /* chance: .75 */
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
printf( true3( not3(roses_are_red) ) ? "T" : "_");
|
||||
|
||||
/* pick a rose and a violet; */
|
||||
puts("Rose is red AND violet is blue:");
|
||||
/* chance of rose being red AND violet being blue is .25 */
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
printf( true3( and3(roses_are_red, violets_are_blue) )
|
||||
? "T" : "_");
|
||||
|
||||
/* chance of rose being red OR violet being blue is 1 */
|
||||
puts("Rose is red OR violet is blue:");
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
printf( true3( or3(roses_are_red, violets_are_blue) )
|
||||
? "T" : "_");
|
||||
|
||||
/* pick two roses; chance of em being both red or both not red is .625 */
|
||||
puts("This rose is as red as that rose:");
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
if3(eq3(roses_are_red, roses_are_red)) putchar('T');
|
||||
else putchar('_');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
(defun tri-not (x) (- 1 x))
|
||||
(defun tri-and (&rest x) (apply #'* x))
|
||||
(defun tri-or (&rest x) (tri-not (apply #'* (mapcar #'tri-not x))))
|
||||
(defun tri-eq (x y) (+ (tri-and x y) (tri-and (- 1 x) (- 1 y))))
|
||||
(defun tri-imply (x y) (tri-or (tri-not x) y))
|
||||
|
||||
(defun tri-test (x) (< (random 1e0) x))
|
||||
(defun tri-string (x) (if (= x 1) "T" (if (= x 0) "F" "?")))
|
||||
|
||||
;; to say (tri-if (condition) (yes) (no))
|
||||
(defmacro tri-if (tri ifcase &optional elsecase)
|
||||
`(if (tri-test ,tri) ,ifcase ,elsecase))
|
||||
|
||||
(defun print-table (func header)
|
||||
(let ((vals '(1 .5 0)))
|
||||
(format t "~%~a:~%" header)
|
||||
(format t " ~{~a ~^~}~%---------~%" (mapcar #'tri-string vals))
|
||||
(loop for row in vals do
|
||||
(format t "~a | " (tri-string row))
|
||||
(loop for col in vals do
|
||||
(format t "~a " (tri-string (funcall func row col))))
|
||||
(write-line ""))))
|
||||
|
||||
(write-line "NOT:")
|
||||
(loop for row in '(1 .5 0) do
|
||||
(format t "~a | ~a~%" (tri-string row) (tri-string (tri-not row))))
|
||||
|
||||
(print-table #'tri-and "AND")
|
||||
(print-table #'tri-or "OR")
|
||||
(print-table #'tri-imply "IMPLY")
|
||||
(print-table #'tri-eq "EQUAL")
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
NOT:
|
||||
T | F
|
||||
? | ?
|
||||
F | T
|
||||
|
||||
AND:
|
||||
T ? F
|
||||
---------
|
||||
T | T ? F
|
||||
? | ? ? F
|
||||
F | F F F
|
||||
|
||||
OR:
|
||||
T ? F
|
||||
---------
|
||||
T | T T T
|
||||
? | T ? ?
|
||||
F | T ? F
|
||||
|
||||
IMPLY:
|
||||
T ? F
|
||||
---------
|
||||
T | T ? F
|
||||
? | T ? ?
|
||||
F | T T T
|
||||
|
||||
EQUAL:
|
||||
T ? F
|
||||
---------
|
||||
T | T ? F
|
||||
? | ? ? ?
|
||||
F | F ? T
|
||||
86
Task/Ternary-logic/Kotlin/ternary-logic.kotlin
Normal file
86
Task/Ternary-logic/Kotlin/ternary-logic.kotlin
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
// version 1.1.2
|
||||
|
||||
enum class Trit {
|
||||
TRUE, MAYBE, FALSE;
|
||||
|
||||
operator fun not() = when (this) {
|
||||
TRUE -> FALSE
|
||||
MAYBE -> MAYBE
|
||||
FALSE -> TRUE
|
||||
}
|
||||
|
||||
infix fun and(other: Trit) = when (this) {
|
||||
TRUE -> other
|
||||
MAYBE -> if (other == FALSE) FALSE else MAYBE
|
||||
FALSE -> FALSE
|
||||
}
|
||||
|
||||
infix fun or(other: Trit) = when (this) {
|
||||
TRUE -> TRUE
|
||||
MAYBE -> if (other == TRUE) TRUE else MAYBE
|
||||
FALSE -> other
|
||||
}
|
||||
|
||||
infix fun imp(other: Trit) = when (this) {
|
||||
TRUE -> other
|
||||
MAYBE -> if (other == TRUE) TRUE else MAYBE
|
||||
FALSE -> TRUE
|
||||
}
|
||||
|
||||
infix fun eqv(other: Trit) = when (this) {
|
||||
TRUE -> other
|
||||
MAYBE -> MAYBE
|
||||
FALSE -> !other
|
||||
}
|
||||
|
||||
override fun toString() = this.name[0].toString()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val ta = arrayOf(Trit.TRUE, Trit.MAYBE, Trit.FALSE)
|
||||
|
||||
// not
|
||||
println("not")
|
||||
println("-------")
|
||||
for (t in ta) println(" $t | ${!t}")
|
||||
println()
|
||||
|
||||
// and
|
||||
println("and | T M F")
|
||||
println("-------------")
|
||||
for (t in ta) {
|
||||
print(" $t | ")
|
||||
for (tt in ta) print("${t and tt} ")
|
||||
println()
|
||||
}
|
||||
println()
|
||||
|
||||
// or
|
||||
println("or | T M F")
|
||||
println("-------------")
|
||||
for (t in ta) {
|
||||
print(" $t | ")
|
||||
for (tt in ta) print("${t or tt} ")
|
||||
println()
|
||||
}
|
||||
println()
|
||||
|
||||
// imp
|
||||
println("imp | T M F")
|
||||
println("-------------")
|
||||
for (t in ta) {
|
||||
print(" $t | ")
|
||||
for (tt in ta) print("${t imp tt} ")
|
||||
println()
|
||||
}
|
||||
println()
|
||||
|
||||
// eqv
|
||||
println("eqv | T M F")
|
||||
println("-------------")
|
||||
for (t in ta) {
|
||||
print(" $t | ")
|
||||
for (tt in ta) print("${t eqv tt} ")
|
||||
println()
|
||||
}
|
||||
}
|
||||
124
Task/Ternary-logic/OoRexx/ternary-logic.rexx
Normal file
124
Task/Ternary-logic/OoRexx/ternary-logic.rexx
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
tritValues = .array~of(.trit~true, .trit~false, .trit~maybe)
|
||||
tab = '09'x
|
||||
|
||||
say "not operation (\)"
|
||||
loop a over tritValues
|
||||
say "\"a":" (\a)
|
||||
end
|
||||
|
||||
say
|
||||
say "and operation (&)"
|
||||
loop aa over tritValues
|
||||
loop bb over tritValues
|
||||
say (aa" & "bb":" (aa&bb))
|
||||
end
|
||||
end
|
||||
|
||||
say
|
||||
say "or operation (|)"
|
||||
loop aa over tritValues
|
||||
loop bb over tritValues
|
||||
say (aa" | "bb":" (aa|bb))
|
||||
end
|
||||
end
|
||||
|
||||
say
|
||||
say "implies operation (&&)"
|
||||
loop aa over tritValues
|
||||
loop bb over tritValues
|
||||
say (aa" && "bb":" (aa&&bb))
|
||||
end
|
||||
end
|
||||
|
||||
say
|
||||
say "equals operation (=)"
|
||||
loop aa over tritValues
|
||||
loop bb over tritValues
|
||||
say (aa" = "bb":" (aa=bb))
|
||||
end
|
||||
end
|
||||
|
||||
::class trit
|
||||
-- making this a private method so we can control the creation
|
||||
-- of these. We only allow 3 instances to exist
|
||||
::method new class private
|
||||
forward class(super)
|
||||
|
||||
::method init class
|
||||
expose true false maybe
|
||||
-- delayed creation
|
||||
true = .nil
|
||||
false = .nil
|
||||
maybe = .nil
|
||||
|
||||
-- read only attribute access to the instances.
|
||||
-- these methods create the appropriate singleton on the first call
|
||||
::attribute true class get
|
||||
expose true
|
||||
if true == .nil then true = self~new("True")
|
||||
return true
|
||||
|
||||
::attribute false class get
|
||||
expose false
|
||||
if false == .nil then false = self~new("False")
|
||||
return false
|
||||
|
||||
::attribute maybe class get
|
||||
expose maybe
|
||||
if maybe == .nil then maybe = self~new("Maybe")
|
||||
return maybe
|
||||
|
||||
-- create an instance
|
||||
::method init
|
||||
expose value
|
||||
use arg value
|
||||
|
||||
-- string method to return the value of the instance
|
||||
::method string
|
||||
expose value
|
||||
return value
|
||||
|
||||
-- "and" method using the operator overload
|
||||
::method "&"
|
||||
use strict arg other
|
||||
if self == .trit~true then return other
|
||||
else if self == .trit~maybe then do
|
||||
if other == .trit~false then return .trit~false
|
||||
else return .trit~maybe
|
||||
end
|
||||
else return .trit~false
|
||||
|
||||
-- "or" method using the operator overload
|
||||
::method "|"
|
||||
use strict arg other
|
||||
if self == .trit~true then return .trit~true
|
||||
else if self == .trit~maybe then do
|
||||
if other == .trit~true then return .trit~true
|
||||
else return .trit~maybe
|
||||
end
|
||||
else return other
|
||||
|
||||
-- implies method...using the XOR operator for this
|
||||
::method "&&"
|
||||
use strict arg other
|
||||
if self == .trit~true then return other
|
||||
else if self == .trit~maybe then do
|
||||
if other == .trit~true then return .trit~true
|
||||
else return .trit~maybe
|
||||
end
|
||||
else return .trit~true
|
||||
|
||||
-- "not" method using the operator overload
|
||||
::method "\"
|
||||
if self == .trit~true then return .trit~false
|
||||
else if self == .trit~maybe then return .trit~maybe
|
||||
else return .trit~true
|
||||
|
||||
-- "equals" using the "=" override. This makes a distinction between
|
||||
-- the "==" operator, which is real equality and the "=" operator, which
|
||||
-- is trinary equality.
|
||||
::method "="
|
||||
use strict arg other
|
||||
if self == .trit~true then return other
|
||||
else if self == .trit~maybe then return .trit~maybe
|
||||
else return \other
|
||||
Loading…
Add table
Add a link
Reference in a new issue