Well-Founded Semantics Solver
The WellFoundedSolver computes the well-founded model
of a normal logic program (definite rules plus negation-as-failure). Unlike
Answer Set Programming, which enumerates zero or more two-valued stable models,
the well-founded semantics always yields exactly one three-valued model, in
which every ground atom is true, false, or undefined.
It ships three interchangeable backends, selected with the backend field:
native(default) — a dependency-free pure-Python implementation of the Van Gelder alternating fixpoint. Returns the full three-valued model. Best for teaching and modestly-sized programs; it grounds naively and is not built for large programs (see Scaling the native well-founded solver for the plan to fix that).problog— delegates to ProbLog, which evaluates the two-valued restriction of WFS. It agrees withnativeon stratified programs and raisesNegativeCycleErroron programs that are genuinely three-valued.xsb— experimental, unverified. Drives XSB Prolog, the reference SLG/tabling engine, as an external subprocess (requires thexsbexecutable onPATH). Intended for large or deeply-recursive programs, but it has not yet been validated against a live XSB install and is not exercised in CI; it emits a warning when used. Prefernativeuntil it is validated.
For a worked comparison of the well-founded semantics against closed-world Datalog and Answer Set Programming, see the semantics notebook.
Installation
The native backend has no dependencies beyond typedlogic itself. The optional
backends reuse existing extras:
pip install typedlogic # native backend only
pip install 'typedlogic[problog]' # adds the problog backend
The xsb backend additionally requires the external
XSB executable on PATH.
Usage
Reading the three-valued model
Load a theory (here in TLog syntax) and ask
for its model. The returned WellFoundedModel (documented below)
exposes the atoms that are true as ground_terms (so iter_retrieve works as
with any solver) and the atoms that are undefined as undefined_terms. Any
atom in neither list is false.
from typedlogic.parsers.tlog_parser import TLogParser
from typedlogic.integrations.solvers.wellfounded import WellFoundedSolver
program = """
pred Bird(name: str).
pred Penguin(name: str).
pred Abnormal(name: str).
pred Flies(name: str).
Bird("tweety").
Bird("opus").
Penguin("opus").
Abnormal(x) :- Penguin(x).
Flies(x) :- Bird(x), not Abnormal(x). # `not` is negation-as-failure
"""
solver = WellFoundedSolver() # backend="native" is the default
solver.add(TLogParser().parse(program))
model = solver.model()
print(sorted(str(t) for t in model.ground_terms)) # true atoms
print(sorted(str(t) for t in model.undefined_terms)) # undefined atoms
print([str(t) for t in model.iter_retrieve("Flies")]) # -> ['Flies(tweety)']
tweety flies (nothing proves it abnormal), opus does not (it is a penguin),
and this stratified program has no undefined atoms.
Undefined atoms
Where a program loops through negation, the well-founded model marks the offending
atoms undefined instead of yielding several models (as ASP would) or none:
loop = """
pred p().
pred q().
p() :- not q().
q() :- not p().
"""
solver = WellFoundedSolver()
solver.add(TLogParser().parse(loop))
model = solver.model()
print([str(t) for t in model.ground_terms]) # [] -- nothing is true
print(sorted(str(t) for t in model.undefined_terms)) # ['p', 'q']
Building a theory programmatically
You can also assert predicate definitions, rules, and facts directly, using
NegationAsFailure for NAF body literals:
from typedlogic import Term, NegationAsFailure, Variable, PredicateDefinition
from typedlogic.integrations.solvers.wellfounded import WellFoundedSolver
solver = WellFoundedSolver()
solver.add(PredicateDefinition(predicate="Bird", arguments={"name": "str"}))
solver.add(PredicateDefinition(predicate="Abnormal", arguments={"name": "str"}))
solver.add(PredicateDefinition(predicate="Flies", arguments={"name": "str"}))
x = Variable("x")
solver.add((Term("Bird", x) & NegationAsFailure(Term("Abnormal", x))) >> Term("Flies", x))
solver.add(Term("Bird", "tweety"))
print([str(t) for t in solver.model().iter_retrieve("Flies")]) # -> ['Flies(tweety)']
Choosing a backend
WellFoundedSolver() # native (default): full three-valued model
WellFoundedSolver(backend="problog") # two-valued; raises NegativeCycleError on loops
WellFoundedSolver(backend="xsb") # experimental; needs the xsb binary
check() always reports satisfiable=True (a well-founded model always exists),
and models() yields the single model.
Bases: Solver
A solver that computes the well-founded model of a normal logic program.
>>> from typedlogic import NegationAsFailure, PredicateDefinition, Variable
>>> from typedlogic.integrations.solvers.wellfounded import WellFoundedSolver
>>> solver = WellFoundedSolver()
>>> solver.add_predicate_definition(PredicateDefinition(predicate="Bird", arguments={'name': 'str'}))
>>> solver.add_predicate_definition(PredicateDefinition(predicate="Abnormal", arguments={'name': 'str'}))
>>> solver.add_predicate_definition(PredicateDefinition(predicate="Flies", arguments={'name': 'str'}))
A bird flies unless it can be shown abnormal (negation as failure, expressed
programmatically with :class:~typedlogic.datamodel.NegationAsFailure):
>>> x = Variable("x")
>>> solver.add((Term("Bird", x) & NegationAsFailure(Term("Abnormal", x))) >> Term("Flies", x))
>>> solver.add(Term("Bird", "tweety"))
>>> model = solver.model()
>>> [str(t) for t in model.iter_retrieve("Flies")]
['Flies(tweety)']
This solver implements the closed-world assumption but not the open-world one:
>>> from typedlogic.profiles import OpenWorld, ClosedWorld
>>> solver.profile.impl(ClosedWorld)
True
>>> solver.profile.impl(OpenWorld)
False
Unlike ASP, an ambiguous negative loop does not produce multiple models (or none); the offending atoms become undefined:
>>> solver = WellFoundedSolver()
>>> solver.add_predicate_definition(PredicateDefinition(predicate="p", arguments={}))
>>> solver.add_predicate_definition(PredicateDefinition(predicate="q", arguments={}))
>>> solver.add(NegationAsFailure(Term("q")) >> Term("p"))
>>> solver.add(NegationAsFailure(Term("p")) >> Term("q"))
>>> model = solver.model()
>>> model.ground_terms
[]
>>> sorted(str(t) for t in model.undefined_terms)
['p', 'q']
Source code in src/typedlogic/integrations/solvers/wellfounded/wellfounded_solver.py
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 161 162 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
check()
Report satisfiability; a well-founded model always exists, so this is always true.
Source code in src/typedlogic/integrations/solvers/wellfounded/wellfounded_solver.py
153 154 155 156 | |
models()
Yield the single well-founded model.
Source code in src/typedlogic/integrations/solvers/wellfounded/wellfounded_solver.py
158 159 160 | |
model()
Return the single well-founded model (with three-valued information).
Source code in src/typedlogic/integrations/solvers/wellfounded/wellfounded_solver.py
162 163 164 165 166 167 168 169 170 | |
Bases: Model
A three-valued model.
:attr:ground_terms holds the atoms that are true in the well-founded
model (mirroring the two-valued :class:~typedlogic.solver.Model contract, so
iter_retrieve and friends behave as usual). The atoms that are neither
true nor false -- i.e. paradoxical or mutually-dependent under negation -- are
exposed separately via :attr:undefined_terms. Atoms in neither list are
false.
Source code in src/typedlogic/integrations/solvers/wellfounded/wellfounded_solver.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
Bases: NotInProfileError
Raised by two-valued backends when the well-founded model is not total.
Backends such as problog only support programs whose well-founded model
assigns every atom true or false. A negative recursive cycle (e.g.
p :- not q. q :- not p.) makes atoms undefined, which these backends
cannot represent; use the native (or xsb) backend for such programs.
Source code in src/typedlogic/integrations/solvers/wellfounded/wellfounded_solver.py
67 68 69 70 71 72 73 74 75 | |