logic library

Joshua Shinavier edited this page May 28, 2011 · 5 revisions

The logic: library contains a small set of primitives for Boolean logic. They make use of the xsd:boolean datatype, with which there are two possible values: true and false. All of these primitives produce values of type xsd:boolean, but they consume values of any type: any argument not explicitly equal to true is considered to be equal to false.

and

This primitive represents the boolean "and" operation. It expects two arguments at the top of the stack, which are the logical operands. It pops both arguments from the stack and pushes the boolean value of their logical conjunction.

The inverse of and expects a single value at the top of the stack, pops it from the stack and pushes each combination of two boolean values whose conjunction is equal to that value.

Examples:

1)  true true and.

  [1]  true

2)  true false and.

  [1]  false

3)  true 42 and.

  [1]  false

4)  false and~.

  [1]  true false
  [2]  false true
  [3]  false false

5)  true and~.

  [1]  true true

not

This primitive represents boolean negation. It expects one argument at the top of the stack. It pops the argument from the stack and pushes the boolean value of its logical complement.

The not primitive is its own inverse.

Examples:

1)  false not.

  [1]  true

2)  true not~.

  [1]  false

or

This primitive represents the boolean "or" operation. It expects two arguments at the top of the stack, which are the logical operands. It pops both arguments from the stack and pushes the boolean value of their (inclusive) logical disjunction.

The inverse of or expects a single value at the top of the stack, pops it from the stack and pushes each combination of two boolean values whose disjunction is equal to that value.

Examples:

1)  true false or.

  [1]  true

2)  true true or.

  [1]  true

3)  false or~.

  [1]  false false

xor

This primitive represents the boolean "exclusive or" operation. It expects two arguments at the top of the stack, which are the logical operands. It pops both arguments from the stack and pushes the boolean value of their exclusive logical disjunction.

The inverse of xor expects a single value at the top of the stack, pops it from the stack and pushes each combination of two boolean values whose exclusive disjunction is equal to that value.

Examples:

1)  true "foo" xor.

  [1]  true

2)  true true xor.

  [1]  false

3)  true xor~.

  [1]  true false
  [2]  false true