This example demonstrates the integration of a Causal State Machine (CSM) with an Effect Ethos to add a layer of deontic (normative) reasoning to a reactive system.
It models a simple temperature monitoring system that triggers an alert when the temperature exceeds a threshold.
However, the action of sending the alert is subject to approval by the EffectEthos, which evaluates it against a set
of predefined norms.
The example is structured to highlight the difference between a purely reactive system and one governed by deontic rules.
CausalState: A state that becomes active when a specific condition is met. Here, it checks if thetemperaturedata value is greater than50.0.CausalAction: An action that is triggered when the corresponding state is active. In this case, it's a simple function that prints an alert to the console.CausalStateMachine (CSM): Manages the link between states and actions. When a state evaluates totrue, the CSM triggers its associated action.EffectEthos: A deontic reasoning engine that contains a set of norms (Teloids). It evaluates aProposedActionto determine if it is permissible, obligatory, or impermissible.Teloid(Norm): A single rule within the ethos. In this example, we define a norm that makes theHigh temp alertImpermissible. This simulates a scenario where, for instance, alerts might be suppressed to prevent spamming or if a manual override is in effect.
The main function executes the same logic twice to provide a clear comparison:
A. Execution without EffectEthos
- The
CSMis initialized with the high-temperature state and the alert action, but without theEffectEthos. - The
CSMis evaluated with a temperature of60.0, which exceeds the threshold. - The
CausalStatebecomes active. - The
CSMimmediately fires theCausalAction, and the alert "Alert! High temperature detected!" is printed. - The result is
Ok(()), as the action was executed without restriction.
B. Execution with EffectEthos
- The
CSMis initialized with the state, the action, and theEffectEthoscontaining the impermissibility norm. ATeloidTag(High temp alert) is provided to link the evaluation to the correct norm. - The
CSMis evaluated again with a temperature of60.0. - The
CausalStatebecomes active. - Instead of firing the action directly, the
CSMcreates aProposedActionand submits it to theEffectEthosfor evaluation. - The
EffectEthosfinds the activeHigh temp alertnorm, which renders the action Impermissible. - Because the action is forbidden, the
CSMdoes not fire the action. Instead, it returns aCsmError::Forbiddencontaining a detailed explanation of the verdict.
From the root of the deep_causality project, run:
cargo run -p csm_examples --example csm_effect_ethos_example--- Running CSM without EffectEthos ---
Alert! High temperature detected!
Result without ethos: Ok(())
--- Running CSM with EffectEthos ---
Result with ethos: Err(Forbidden("The final verdict is Impermissible....
The outcome is Impermissible because at least one impermissible norm was active and undefeated, which has the highest precedence."))
This output clearly illustrates the core concept: the EffectEthos successfully intercepted and blocked an action that
would have otherwise been executed, providing a powerful mechanism for building safer and more robust systems.