Research Paper · January 2025

The Realistic Type

Implementing Three-Valued Logic in the Matyos Programming Language

""
Ahmed Hafdi  ·  Matyos Language Project  ·  January 2025
Abstract

This paper introduces realistic, a novel type in the Matyos programming language that implements Kleene's three-valued logic (K3). The type extends classical Boolean logic by adding a third truth value \(\odot\) (realistic), representing uncertain, unknown, or partially-true states. We present the formal model based on the truth domain \(\mathbb{B}_3 = \{\top, \bot, \odot\}\), define truth tables for all three-valued logical operations, and describe the implementation architecture within Matyos — including the lexer extension, three-valued operator semantics, and runtime evaluation. Benchmarks show an average overhead of 38% over classical Boolean operations. Advanced application domains including paradox resolution, weather modelling, quantum computing simulation, and AI uncertainty handling are discussed.

§ 01

Introduction

Classical Boolean logic, with its binary truth domain \(\mathbb{B}_2 = \{\top, \bot\}\), has been the foundation of programming languages since their inception. Yet real-world reasoning is rarely so clean: uncertainty, partial information, and genuine unknowns permeate every domain from medicine to finance to scientific modelling.

Three-valued logic systems — most notably Kleene's K3 — extend the classical framework by introducing a third truth value to represent exactly this indeterminacy. Proposed independently by Łukasiewicz and Kleene in the mid-20th century, these systems have found application in database null semantics (SQL's UNKNOWN), hardware circuit design, and formal verification.

This paper presents realistic, the implementation of Kleene K3 as a native type in the Matyos programming language. The key contributions are:

Contribution 1 — Formal Model

A rigorous formal model of \(\mathbb{B}_3 = \{\top, \bot, \odot\}\) with operational semantics for AND, OR, and NOT.

Contribution 2 — Language Implementation

A complete implementation within Matyos: lexer tokens, type system extension, and runtime operator evaluation.

Contribution 3 — Empirical Evaluation

Benchmark measurements of overhead vs. classical Boolean operations: AND +33%, OR +35%, NOT +47%.

Contribution 4 — Applications

Case studies demonstrating realistic in paradox resolution, weather prediction, quantum simulation, and AI reasoning.

§ 02

Formal Model of Three-Valued Logic

The foundation of the realistic type is Kleene's strong three-valued logic, which extends the classical Boolean domain to three truth values.

2.1 The Truth Domain

Definition 1 — Three-Valued Truth Domain
\[ \mathbb{B}_3 \;=\; \{\,\top,\; \bot,\; \odot\,\} \] where \(\top\) denotes true, \(\bot\) denotes false, and \(\odot\) denotes realistic — an indeterminate, unknown, or partially-true value.
TRUE REALISTIC FALSE

2.2 Three-Valued Operations

We define three logical connectives over \(\mathbb{B}_3\). The key property of Kleene's strong logic is that \(\odot\) "propagates" uncertainty: the result is indeterminate whenever the output depends on the unknown input.

\(a\) \(b\) \(a \wedge b\) \(a \vee b\) \(\neg a\)
Formal Definition — AND
\[ a \wedge b = \begin{cases} \top & \text{if } a = \top \text{ and } b = \top \\ \bot & \text{if } a = \bot \text{ or } b = \bot \\ \odot & \text{otherwise} \end{cases} \]
Formal Definition — OR
\[ a \vee b = \begin{cases} \bot & \text{if } a = \bot \text{ and } b = \bot \\ \top & \text{if } a = \top \text{ or } b = \top \\ \odot & \text{otherwise} \end{cases} \]
Formal Definition — NOT
\[ \neg a = \begin{cases} \bot & \text{if } a = \top \\ \top & \text{if } a = \bot \\ \odot & \text{if } a = \odot \end{cases} \]

§ 03

Implementation in Matyos

The Matyos language is extended with the realistic keyword and type. The implementation touches three layers: the lexer, the type system, and the runtime evaluator.

3.1 Lexer Extension

Three new tokens are added to the Matyos lexer: realistic (type keyword), yes (\(\top\)), no (\(\bot\)), and maybe (\(\odot\)).

// Matyos lexer — new tokens for the realistic type
token REALISTIC_TYPE  "realistic"
token REALISTIC_TRUE  "yes"     // ⊤
token REALISTIC_FALSE "no"      // ⊥
token REALISTIC_UNK   "maybe"   // ⊙

3.2 Type Declaration

A realistic variable is declared analogously to a bool:

// Classical Boolean
bool is_ready = true;

// Three-valued realistic
realistic is_ready = maybe;   // unknown at this point
realistic has_data = yes;
realistic is_error = no;

3.3 Operator Evaluation

The runtime evaluates three-valued operators via a dispatch table. For AND, the short-circuit rule is: if either operand is \(\bot\), return \(\bot\) immediately (regardless of the other). For OR: if either is \(\top\), return \(\top\).

// AND with realistic short-circuit
realistic a = maybe;
realistic b = no;
realistic result = a && b;   // → no  (short-circuit on ⊥)

// OR propagates uncertainty
realistic x = maybe;
realistic y = maybe;
realistic z = x || y;       // → maybe (⊙ ∨ ⊙ = ⊙)

// Conditional on realistic value
if (result == yes) { … }
else if (result == maybe) { … }   // handle uncertainty
else { … }

§ 04

Evaluation & Performance

We benchmark realistic operations against their classical Boolean counterparts across 10,000 iterations per operation. All tests run on a single core at fixed clock speed to isolate operator overhead.

4.1 Overhead Results

AND
+33%
OR
+35%
NOT
+47%
Average
+38%
Analysis

The overhead stems from the extended dispatch table (3-value lookup vs. 2-value branch) and the extra comparison needed to detect \(\odot\). NOT has the highest overhead (+47%) because it cannot benefit from short-circuit evaluation. The average overhead of ~38% is acceptable for domains where correctness under uncertainty outweighs raw speed.

§ 05

Advanced Applications

The realistic type enables natural expression of reasoning under uncertainty across several important domains.

Paradox Resolution
Classical logic collapses under statements like "This sentence is false" (the Liar paradox). With realistic, such statements evaluate to \(\odot\) — genuinely indeterminate — rather than causing inconsistency.
Weather Forecasting
Probabilistic conditions ("will it rain tomorrow?") map naturally to maybe, allowing systems to track uncertainty without collapsing to arbitrary binary decisions before sufficient evidence arrives.
ψ
Quantum Computing
Before measurement, quantum bits are in superposition — neither definitively 0 nor 1. The realistic type captures this pre-measurement state, making quantum circuit simulation more semantically faithful.
AI Uncertainty
Inference systems that cannot confidently classify an input can return maybe rather than a forced binary decision. This explicit uncertainty signal allows downstream components to request more evidence or defer to a human.

§ 06

Discussion

The realistic type demonstrates that a modest extension to a language's type system — three values instead of two — can dramatically improve expressiveness in uncertainty-heavy domains. The 38% average overhead is a fixed cost of the richer semantics, not an implementation artifact; any correct implementation of K3 requires at least this additional branching.

A key design decision was using Kleene's strong rather than weak three-valued logic. In weak K3, \(\odot \wedge \bot\) would yield \(\odot\) (uncertainty dominates), but in strong K3 it yields \(\bot\) — because \(\bot \wedge x = \bot\) for all \(x\). This reflects the intuition that a definite false is enough to falsify a conjunction, regardless of uncertainty in the other operand.

Limitation

The current implementation does not support probabilistic semantics (i.e., \(\odot\) carries no weight or confidence score). Future work could extend \(\mathbb{B}_3\) to a continuous \([0,1]\) interval, recovering fuzzy logic as a generalization.

§ 07

Related Work

Three-valued and multi-valued logics have a long history. Łukasiewicz (1920) was the first to systematically study three-valued logic \(L_3\), motivated by future contingents. Kleene (1938) independently developed K3 in the context of partial recursive functions — where \(\odot\) represents "undefined."

SQL databases have implemented a form of three-valued logic since SQL-86, with NULL producing UNKNOWN in comparisons. However, SQL's NULL semantics are notoriously inconsistent — NULL = NULL yields UNKNOWN rather than TRUE — a departure from classical K3.

In type theory, optional types (Maybe in Haskell, Option in Rust) address absence of a value but do not natively express logical indeterminacy as a first-class Boolean value. The realistic type fills precisely this gap.

§ 08

Conclusion

We have presented the realistic type: a native implementation of Kleene's strong three-valued logic in the Matyos programming language. The type provides a clean, first-class representation of uncertainty through the truth domain \(\mathbb{B}_3 = \{\top, \bot, \odot\}\), with well-defined semantics for AND, OR, and NOT.

The implementation is practical: the overhead of ~38% over classical Boolean operations is acceptable for the expressiveness gained, and the yes / no / maybe keywords integrate naturally into Matyos syntax.

Future directions include extending to weighted realistic values (fuzzy logic integration), implementing realistic short-circuit evaluation in all contexts, and exploring type inference for propagating uncertainty through complex expressions.

PAPER EXPLORER — CKX 003 Open PDF ↗
— / —
§0 — Abstract
The Realistic Type

A novel type in the Matyos language that implements Kleene's three-valued logic (K3).

Truth domain: 𝔹₃ = {⊤, ⊥, ⊙}

realistic is_ready = maybe;

Average overhead: +38% over classical Boolean — a reasonable cost for first-class uncertainty.

§1 — Introduction
Beyond True & False

Classical Boolean logic: 𝔹₂ = {⊤, ⊥}

Real-world reasoning involves uncertainty, partial information, and genuine unknowns that cannot be forced into a binary.

Three-valued logics were independently developed by Łukasiewicz (1920) and Kleene (1938).

The realistic type brings K3 into a modern programming language as a first-class citizen.

§2 — Formal Model
Three Values, Three Laws

Truth domain: 𝔹₃ = {⊤, ⊥, ⊙}

aba∧ba∨b¬a

Key insight: ⊙ propagates uncertainty except when a definite value dominates (⊥ in AND, ⊤ in OR).

§3 — Implementation
Matyos Language Extension

Three new lexer tokens:

realistic — type keyword

yes / no / maybe — ⊤ / ⊥ / ⊙ literals

Example:

realistic x = maybe;

realistic y = yes;

realistic z = x && y; // → maybe

§4 — Evaluation
Performance Overhead

Overhead vs. classical Boolean (10,000 iterations):

AND
+33%
OR
+35%
NOT
+47%

Average: +38% — acceptable for uncertainty-aware systems.

§5 — Applications
Where Uncertainty Lives

∞ Paradoxes — Liar paradox → ⊙ (genuinely indeterminate)

☁ Weather — "will it rain?" → maybe until evidence arrives

ψ Quantum — pre-measurement qubit superposition → maybe

◎ AI — low-confidence classification → maybe defers to human

§6 — Discussion
Strong vs. Weak K3

Kleene strong K3 was chosen over weak K3.

In strong K3: ⊙ ∧ ⊥ = ⊥
In weak K3: ⊙ ∧ ⊥ = ⊙

Intuition: a definite false is enough to falsify a conjunction — uncertainty in the other operand is irrelevant.

Limitation: ⊙ carries no weight or confidence score. Future work: extend to continuous [0,1] interval (fuzzy logic).

§7 — Related Work
Historical Context

Łukasiewicz (1920) — first systematic three-valued logic L₃

Kleene (1938) — K3 in context of partial recursive functions

SQL (1986) — NULL/UNKNOWN in comparisons (inconsistent K3)

Haskell Maybe / Rust Option — absence of value, not logical indeterminacy

The realistic type is the first native K3 implementation as a first-class Boolean type in a programming language.

§8 — Conclusion
A Third Way

The realistic type provides:

Clean semantics — Kleene strong K3, rigorously defined

Natural syntaxyes / no / maybe keywords

Practical overhead — ~38% above classical Boolean

Broad applicability — paradoxes, weather, quantum, AI

"Not everything is true or false. Some things are simply realistic."