Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions manuals/1.0/en/06-semantic-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,29 @@ Semantic Variables make **impossible states impossible**. Invalid email addresse

The type system itself becomes a **domain language**, where each type speaks of what can exist in your business domain.

Looking at function signatures, they become specifications:
## Design by Contract

Constructor arguments reveal preconditions. Properties reveal postconditions:

```php
function processOrder(ProductCode $product, PaymentAmount $amount, CustomerAge $age)
final class ProcessedOrder
{
// The signature IS the specification
public function __construct(
#[Input] #[Verified] string $productCode, // Precondition: verified product code
#[Input] int $paymentAmount, // Precondition: payment amount
#[Input] #[Adult] int $age // Precondition: adult age
) {
// Can only exist when preconditions are satisfied
$this->orderNumber = $this->generateOrderNumber();
$this->processedAt = new DateTime();
}

public readonly string $orderNumber; // Postcondition: order number always exists
public readonly DateTime $processedAt; // Postcondition: processed time always exists
}
```

This function accepts only valid product codes, positive amounts, and valid ages. No need to read documentation—the types tell the whole story.
Constructor arguments express **preconditions** (conditions that must be satisfied for this object to exist), while `public readonly` properties express **postconditions** (states this object guarantees).

Defensive programming becomes unnecessary. Argument validation, null checks, range verification, inventory confirmation, geographic constraints—semantic variables guarantee all of these. Code can focus on its true purpose: implementing business logic.

Expand Down
160 changes: 75 additions & 85 deletions manuals/1.0/en/07-type-driven-metamorphosis.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,144 +7,134 @@ permalink: /manuals/1.0/en/07-type-driven-metamorphosis.html

# Type-Driven Metamorphosis

> "The object knows its own nature. We merely create the conditions for its becoming."
> "The Tao gives birth to one, one gives birth to two, two gives birth to three, three gives birth to all things"
>
>   —Laozi, *Tao Te Ching*, Chapter 42 (6th century BCE)

Type-Driven Metamorphosis represents the deepest transformation—where objects **discover their own destiny** through their nature.

## Beyond Fixed Paths

Traditional metamorphosis follows predetermined routes:
Type-driven metamorphosis enables objects to choose from multiple possible types based on conditions. Multiple classes are declared as arrays using the `#[Be()]` attribute, with the selection expressed through the being property:

```php
#[Be(UserProfile::class)] // Single destiny
final class UserInput
#[Be([Success::class, Failure::class])]
final class PaymentAttempt
{
// Always becomes UserProfile, regardless of content
}
```

## Self-Determining Beings

Type-Driven Metamorphosis allows objects to **choose their own becoming**:

```php
#[Be([ActiveUser::class, InactiveUser::class])]
final class UserValidation
{
public readonly ActiveUser|InactiveUser $being; // Being Property
public readonly Success|Failure $being;

public function __construct(
#[Input] string $email,
#[Input] DateTime $lastLogin,
#[Inject] UserRepository $repository
#[Input] Money $amount,
#[Input] CreditCard $card,
#[Inject] PaymentGateway $gateway
) {
$daysSinceLogin = $lastLogin->diff(new DateTime())->days;
$result = $gateway->process($amount, $card);

// Self-determination of destiny
$this->being = $daysSinceLogin < 30
? new ActiveUser($email, $lastLogin)
: new InactiveUser($email, $lastLogin);
// Branching based on results
$this->being = $result->isSuccessful()
? new Success($result)
: new Failure($result->getError());
}
}
```

## The Being Property

The **Being Property** is where self-determination manifests:
The `$being` property indicates the next transformation destination:

```php
public readonly Success|Failure|Pending $being;
```

- Must be a **union type** of all possible destinations
- Must be named exactly `being`
- Contains the object's chosen destiny
Classes are chosen when this type signature matches the constructor of the next class.
For example, if `$being` is of type `Success`, a class with the following constructor would be selected:

```php
public readonly SuccessfulPayment|FailedPayment $being;
class NextStep {
public function __construct(#[Input] Success $being) {
```

## Natural Branching
`#[Be]` completely expresses all possibilities of an object. Types become the specification for workflows and use cases.

Objects choose their path based on **inner nature**:
## Multiple Type Selection Example

```php
#[Be([ChildAccount::class, AdultAccount::class, SeniorAccount::class])]
final class AgeBasedAccount
#[Be([VIPUser::class, RegularUser::class, SuspendedUser::class])]
final class UserClassification
{
public readonly ChildAccount|AdultAccount|SeniorAccount $being;
public readonly VIPUser|RegularUser|SuspendedUser $being;

public function __construct(
#[Input] int $age,
#[Input] string $name,
#[Inject] AccountFactory $factory
#[Input] UserActivity $activity,
#[Input] array $violations,
#[Inject] UserPolicy $policy
) {
$this->being = match (true) {
$age < 18 => $factory->createChild($name, $age),
$age < 65 => $factory->createAdult($name, $age),
default => $factory->createSenior($name, $age)
$policy->shouldSuspend($violations) => new SuspendedUser($violations),
$activity->qualifiesForVIP() => new VIPUser($activity),
default => new RegularUser($activity)
};
}
}
```

## Error States as Valid Beings
## Continuation Processing Mechanism

Failure is not an exception—it's a **valid form of existence**:
The advantage of type-driven processing lies in automatic continuation processing:

```php
#[Be([SuccessfulRegistration::class, FailedRegistration::class])]
final class UserRegistration
{
public readonly SuccessfulRegistration|FailedRegistration $being;

public function __construct(
#[Input] string $email,
#[Input] string $password,
#[Inject] UserService $userService
) {
try {
$user = $userService->register($email, $password);
$this->being = new SuccessfulRegistration($user);
} catch (RegistrationException $e) {
$this->being = new FailedRegistration($e->getErrors());
}
}
}
$evaluation = $becoming(new UserInput($data));
$notification = $becoming($evaluation); // $evaluation->being is automatically selected
```

Both success and failure are **equally valid beings**.
The framework detects the `$being` property and performs the next processing based on its type. External conditional branching becomes unnecessary.

## Extended Decision-Making Prospects

## Metamorphosis Continuation
⚠️ **Note**: AMD (Advanced Decision-Making) is currently an unimplemented future concept.

The framework automatically extracts the Being Property for continued transformation:
Beyond deterministic judgment, a new paradigm that embraces uncertainty is being prepared:

```php
$classification = $becoming(new OrderInput($data));
$processedOrder = $becoming($classification); // Uses being property automatically
// Future concept
#[Accept] // Unimplemented: delegation to experts
#[Be([Approved::class, Rejected::class, Undetermined::class])]
final class ComplexDecision
{
public readonly Approved|Rejected|Undetermined $being;

// Extended decision-making through AI-human collaboration
}
```

## Philosophical Implications
A decision system where determinable things are decided by types, and indeterminate things are delegated to experts.

### Objects as Conscious Entities
## Elimination of Control Structures

Type-Driven Metamorphosis treats objects as **conscious beings** that understand their own nature.
Be Framework eliminates traditional "complex control structures within methods". The framework follows flows declared with `#[Be]` and selects the next class through type matching.

### Wu Wei in Code
Traditional complex conditional branching:

The programmer doesn't **force** transformation—they create conditions where objects naturally become what they are meant to be.

### Elimination of Control Flow

No `if-else` chains in business logic. The object's nature **is** the logic.
```php
if ($score > 800) {
return new Approved($amount);
} elseif ($score < 400) {
return new Rejected("Low score");
} else {
return new Review($amount);
}
```

## The Revolution
In type-driven metamorphosis, these are expressed as union types:

Being Property signatures become **documentation**:
```php
public readonly Success|Warning|Error $being; // All possibilities visible
public readonly Approved|Rejected|Review $being;
```

Objects **self-determine** their destiny based on their essential nature, not external control.
## Integration with Type System

Type-driven metamorphosis integrates complex decision logic into the type system. Union types make possible results explicit, while constructors handle the actual branching. This makes decision logic understandable and maintainable code.

From the simple principle "if types match, proceed to the next", a rich workflow system that handles real-world complexity is constructed.

---

**Next**: Learn about [Reason Layer: Ontological Capabilities](07-reason-layer.md) where contextual capabilities shape transformation.
**Next**: Learn about the philosophy of dependency injection that supports metamorphosis in [Reason Layer: Ontological Capabilities](08-reason-layer.html).

*"We don't decide what objects become—we discover what they already are, in their deepest nature."*
*"Type-driven metamorphosis is a technique that integrates complex control flow into the type system itself."*
Loading