torchmodal.kripke¶
torchmodal.kripke ¶
torchmodal.kripke ~~~~~~~~~~~~~~~~~
Kripke model and formula graph for Modal Logic Neural Networks.
A Kripke model M = ⟨W, R, V⟩ consists of: - W: a finite set of possible worlds - R: a binary accessibility relation on W - V: a valuation function assigning truth values to propositions in worlds
This module provides :class:KripkeModel, the central data structure
that manages worlds, propositions, accessibility, and formula evaluation.
Proposition ¶
Bases: Module
A named atomic proposition with truth bounds across worlds.
Each proposition stores [L, U] bounds per world in [0, 1].
For learnable propositions, use :meth:set_bounds_value to temporarily
set bounds in-place (e.g. in adversarial or minimax setups).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable name for the proposition. |
required |
num_worlds
|
int
|
Number of worlds |W|. |
required |
learnable
|
bool
|
If |
True
|
init
|
float
|
Initial value for both L and U bounds. Default 0.5 (maximum uncertainty). |
0.5
|
Source code in torchmodal/kripke.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
set_bounds ¶
Set bounds externally (only for non-learnable propositions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tensor
|
Tensor of shape |
required |
Source code in torchmodal/kripke.py
set_bounds_value ¶
Set current bounds in-place.
For learnable propositions, updates internal logits so that the next
:attr:bounds read returns (approximately) bounds. Use this to
temporarily inject bounds (e.g. adversary values in minimax) without
removing learnability. For non-learnable propositions, equivalent to
:meth:set_bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tensor
|
Tensor of shape |
required |
Source code in torchmodal/kripke.py
set_world ¶
Set truth bounds for a single world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_idx
|
int
|
Index of the world. |
required |
lower
|
float
|
Lower truth bound. |
required |
upper
|
float
|
Upper truth bound. |
required |
Source code in torchmodal/kripke.py
KripkeModel ¶
Bases: Module
Differentiable Kripke model M = ⟨W, R, V⟩.
Central data structure for MLNN computation. Manages: - Possible worlds and their propositions (valuation V) - Accessibility relation R (fixed or learnable) - Modal operator evaluation (□, ♢) - Contradiction loss computation
The model supports two learning modes:
- Deductive (fixed R, learnable V): Enforces known axioms by updating proposition truth values through gradient descent.
- Inductive (fixed V, learnable R): Discovers relational structure by learning the accessibility relation from data.
Use :meth:get_proposition to get a proposition by name, :meth:get_bounds
for its truth bounds, and :meth:all_bounds for a dict of all bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_worlds
|
int
|
Number of possible worlds |W|. |
required |
accessibility
|
Union[FixedAccessibility, LearnableAccessibility, MetricAccessibility]
|
Accessibility relation module. One of
:class: |
required |
tau
|
float
|
Temperature for modal operators. Default 0.1. |
0.1
|
world_names
|
Optional[List[str]]
|
Optional list of human-readable world names. |
None
|
top_k
|
Optional[int]
|
Top-k aggregation for the model's □ / ♢ operators (see
:class: |
None
|
Example::
>>> from torchmodal import KripkeModel
>>> from torchmodal.nn import LearnableAccessibility
>>> model = KripkeModel(
... num_worlds=3,
... accessibility=LearnableAccessibility(3),
... )
>>> model.add_proposition("p", learnable=True)
>>> model.add_proposition("q", learnable=False)
Source code in torchmodal/kripke.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
add_proposition ¶
Add an atomic proposition to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Proposition name (must be unique). |
required |
learnable
|
bool
|
Whether bounds are learnable. Default |
True
|
init
|
float
|
Initial truth value. Default 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
Proposition
|
The created :class: |
Source code in torchmodal/kripke.py
get_proposition ¶
Retrieve a proposition by name.
nn.ModuleDict is typed as returning a bare Module, so the
cast records what the container actually holds — every entry is put
there by :meth:add_proposition.
Source code in torchmodal/kripke.py
get_bounds ¶
get_accessibility ¶
Compute the current accessibility matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Optional[Tensor]
|
Optional features for :class: |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Accessibility matrix |
Source code in torchmodal/kripke.py
necessity ¶
Evaluate □ϕ (necessity) for a proposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_name
|
str
|
Name of the proposition. |
required |
accessibility
|
Optional[Tensor]
|
Pre-computed accessibility matrix. If |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Truth bounds |
Source code in torchmodal/kripke.py
possibility ¶
Evaluate ♢ϕ (possibility) for a proposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_name
|
str
|
Name of the proposition. |
required |
accessibility
|
Optional[Tensor]
|
Pre-computed accessibility matrix. If |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Truth bounds |
Source code in torchmodal/kripke.py
contradiction_loss ¶
Compute the total contradiction loss across all propositions.
.. math:: \mathcal{L}{\text{contra}} = \sum{w \in W} \sum_\phi \max(0,\; L_{\phi,w} - U_{\phi,w})
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar contradiction loss. |
Source code in torchmodal/kripke.py
all_bounds ¶
Return a dict mapping proposition names to their bounds.
forward ¶
Compute accessibility and return all proposition bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Optional[Tensor]
|
Optional features for MetricAccessibility. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dictionary of proposition name → bounds |