Skip to content

Propositions

Greg Swindle edited this page May 2, 2019 · 2 revisions

In Boolean logic (and archetypes-rules), anything that has a truth-value is called a Proposition.

How Propositions are normally used in source code

We use Propositions all the time in source code. A variable with a Boolean value is a Proposition:

const socratesIsHuman = true

Because socratesIsHuman has a Boolean value, it's a Proposition.

How to use Propositions with archetype-rules

We explicitly declare Propositions in archetypes-rules:

const { Proposition } = require('archetypes-rules')

const proposition = new Proposition(
  'socrates-is-human',
  true)

console.log(proposition.toString())
// =>
// (Proposition statement = 'socrates-is-human', value = true)

console.log(JSON.stringify(proposition, null, '  '))
// =>
// {
//   "name": "socrates-is-human",
//   "type": "archetypes.rules.Proposition",
//   "value": true
// }

How to evaluate Propositions

Logical AND

archetypes-rules would express the following source code...

const humansAreMortal = true
const socratesIsHuman = true

const result = (humansAreMortal && socratesIsHuman)

...like this:

const { Proposition } = require('archetypes-rules')

const humansAreMortal = new Proposition('humans-are-mortal', true)
const socratesIsHuman = new Proposition('socrates-is-human', true)

const proposition = humansAreMortal.and(socratesIsHuman)

console.log(proposition.toString())
// =>
// Proposition name = (humans-are-mortal AND socrates-is-human), value = true

Logical OR

const p = new Proposition('p', true)
const q = new Proposition('q', false)

const proposition = p.or(q)

console.log(proposition.toString())
// => Proposition name = (p OR q), value = true

Logical NOT

const p = new Proposition('p', true)

const proposition = p.not()

console.log(proposition.toString())
// => Proposition name = (NOT p), value = false

Logical XOR

const p = new Proposition('p', true)
const q = new Proposition('q', false)

const proposition = p.xor(q)

console.log(proposition.toString())
// => Proposition name = (p XOR q), value = true