Explore reasoning with uncertain or conflicting information and rules with exceptions. Facts and rules may carry confidence values; the solver reports support for and against each answer and shows the proofs behind it.
Choose an example to load a problem and a short explanation. Edit it if needed, then press Solve to see the answer and its proofs.
Learn more: How it works · Reading results · Confidence and support · Defaults · Sampling checks · Help
Each statement ends with a period. Lowercase names such as
pingu are constants and predicates; uppercase names such as
X are variables. bird(X) :- penguin(X) is a rule:
every penguin is a bird. A minus sign states a negation, so the next rule
says a penguin does not fly.
The last rule is a default: a bird flies unless
-flies(X) is established — the classical "birds normally
fly" with its exception. An unless may also carry a number, the
rank of the default, as the later default examples do; without a number the
rank is 0. The query statement asks the question. It contains
the variable X, so gk answers for each value of X
that it finds.
A percent sign % starts a line comment; /* ... */
comments work as well. The order of statements does not matter.
% Penguins are birds, penguins do not fly, birds normally fly.
bird(tweety).
penguin(pingu).
bird(X) :- penguin(X).
-flies(X) :- penguin(X).
flies(X) :- bird(X), unless(-flies(X)).
query(flies(X)).
gk lists two answers. The first number of each row, the signed confidence, is the answer's main number: the support for it minus the support against it. Then come the four shares that it is made of, which sum to 1: for, against, conflict and ignorance.
flies(tweety) is (1, 0, 0, 0): the default applies, and
nothing speaks against flying. flies(pingu) is (0, 1, 0, 0): the
strict penguin rule establishes -flies(pingu). The default says
"unless the contrary is established", so it yields: the answer is against,
with no conflict.
Under the table are the proofs of each answer. The proof for
flies(pingu) ends with the open exception
unless(-flies(pingu)): it holds only where
-flies(pingu) is not established. The proof against
establishes exactly that, so the value is (0, 1, 0, 0) although both
proofs are listed.
gk starts from a proof and then checks it. The search line counts
the nested searches: for opposition to each conclusion, for further support
and opposition at each condition, and for each exception. The raw
output shows what gk printed: one line {"recursive": ...},
then a dump of the evidence. See how gk works and the support semantics for
what the four shares mean.
A number between 0 and 1 before :: is an input confidence. Facts
and rules can both carry confidences. A statement without the prefix has
confidence 1.
Both facts and rules can be uncertain. gk computes support for the answer from the confidences of the inputs used in the proof.
0.8::dark_clouds(today).
0.7::rain(X) :- dark_clouds(X).
query(rain(today)).
% There is uncertain evidence for dark clouds and an uncertain
% rule connecting dark clouds with rain.
The answer is (0.56, 0, 0, 0.44). For is 0.8 * 0.7 = 0.56:
the rule applies where the fact is established, and then supports rain with
strength 0.7. Nothing speaks against rain, so the rest, 0.44, is
ignorance.
A confidence is a strength of support. For a chain without opposing evidence it behaves like a probability, which is why the numbers multiply (the support semantics, Section 2).
Two independent sources support the same forecast with different confidences. Their combined support is greater than either confidence alone.
0.5::rain(today).
0.6::rain(today).
query(rain(today)).
% Two independent forecasts both predict rain today,
% one with confidence 0.5 and the other with 0.6.
% What is the combined support?
The answer is (0.8, 0, 0, 0.2). Two sources give two routes to the same
statement, and the routes combine by noisy-or:
1 - (1 - 0.5) * (1 - 0.6) = 0.8. Rain fails to be established
only where both sources fail. This is the rule of probabilistic logic
programming too.
gk finds both proofs and keeps their sources apart. A source that two proofs share counts once: see the "shared evidence" example.
The minus sign is explicit negation: -flies(tweety) is
evidence that Tweety does not fly, not an absence of evidence. Here both the
statement and its negation have direct support, and gk keeps both.
0.7::flies(tweety).
0.4::-flies(tweety).
query(flies(tweety)).
% Input confidences for a statement and its explicit negation:
% confidence 0.7 for flying, 0.4 for not flying.
The answer is (0.3, 0, 0.4, 0.3): for 0.3, conflict 0.4, ignorance 0.3. The signed confidence is 0.3.
The support semantics draws one number between 0 and 1 for the atom
flies(tweety). The support for flying passes when the number is
below 0.7, the support against when it is below 0.4. Below 0.4 both pass:
conflict, 0.4 of the worlds. Between 0.4 and 0.7 only flying passes: for,
0.3. Above 0.7 neither passes: ignorance, 0.3 (the support semantics, Section
4).
Conflict and ignorance both give a signed confidence near 0, but they mean different things: conflict is evidence on both sides, ignorance is no evidence.
Uncertain rules with variables act as weak general tendencies: the base
smoking rule has confidence 0.3, the friendship rule has confidence 0.1,
friendship is roughly symmetric, and the smoking-influence rule has confidence
0.2. On top of these there are two
certain facts about chris and sam.
The question is how strongly the rules and facts together support
smokes(sam).
% Social smoking, following a classical example from
% probabilistic logic programming.
0.3::smokes(X).
0.1::friends(X, Y).
0.9::friends(Y, X) :- friends(X, Y).
0.6::susceptible(X).
0.2::smokes(X) :- susceptible(X), friends(X, Y), smokes(Y).
friends(chris, sam).
smokes(chris).
query(smokes(sam)).
gk gives (0.3804, 0, 0, 0.6196).
The support semantics gives 0.3764 for smokes(sam). One route
is the base tendency, 0.3. The other route goes through chris: sam is
susceptible (0.6), sam is a friend of chris (0.91), and chris smokes (1).
The friendship pools two sources: the certain fact read through the symmetry
rule (0.9) and the general 0.1 tendency,
1 - (1 - 0.9) * (1 - 0.1) = 0.91. The influence rule then gives
0.6 * 0.91 * 0.2 = 0.1092, and noisy-or with the base tendency
gives 1 - (1 - 0.3) * (1 - 0.1092) = 0.3764.
gk's value is 0.004 higher, and gk records no reading for it. The
difference comes from the instance of the influence rule with
Y = sam, whose condition smokes(sam) is the
question itself. When the rule's condition names a separate predicate for
chris, gk gives 0.3764 exactly. Press Sample under Advanced: with
20,000 worlds the reference tool gives 0.372, within its sampling error
of 0.3764.
Two rules predict rain: one from dark clouds and falling pressure
(support 0.72), another from dark clouds and the radar (support 0.63).
Combining the two proofs naively as independent supports would give
1 - 0.28 * 0.37 = 0.896.
But that would be wrong: both proofs stand on the same uncertain
observation dark_clouds(today). If the cloud observation
fails, both proofs fail together, so the two are not independent.
0.9::dark_clouds(today).
0.8::falling_pressure(today).
0.7::rain_on_radar(today).
rain_likely(Day) :- dark_clouds(Day), falling_pressure(Day).
rain_likely(Day) :- dark_clouds(Day), rain_on_radar(Day).
query(rain_likely(today)).
% Two rules predict rain, but both depend on the
% same uncertain observation dark_clouds(today).
The answer is (0.846, 0, 0, 0.154), not 0.896. Both rules need the same
fact dark_clouds(today), and a fact used twice is one piece of
evidence. Where the clouds are established (0.9 of the worlds), rain is
likely through either of the other two observations:
0.9 * (1 - (1 - 0.8) * (1 - 0.7)) = 0.9 * 0.94 = 0.846.
gk computes this case split itself: it evaluates all the evidence in one
calculation and does not combine the two proofs as if they were
independent. Replace dark_clouds(Day) in the second rule by a
separate observation with confidence 0.9: the overlap goes, and the result
becomes 0.8964.
Here the conflict is not about the queried statement itself but about a
premise used to derive it: bird(tweety) has support 0.5 and
evidence against 0.2.
0.5::bird(tweety).
0.2::-bird(tweety).
0.9::flies(X) :- bird(X).
query(flies(tweety)).
% The premise bird(tweety) is itself contested:
% how does that affect the derived conclusion?
The answer is (0.27, 0, 0, 0.73). The premise bird(tweety)
has the shares (0.3, 0, 0.2, 0.5), by the rule of example 4: conflict 0.2,
for 0.3. The flying rule can use the premise only where it is established
without conflict, 0.3 of the worlds, and there it supports flying with
strength 0.9: 0.3 * 0.9 = 0.27. The evidence against the bird
premise is no evidence against flying, so against stays 0.
gk finds the opposing premise evidence by itself: after the proof of flying, it searches for opposition to each condition of the proof. The proof under the table has the confidence 0.45, 0.5 * 0.9: a proof does not see the evidence against its premise. The value does, so it is 0.27.
This example shows two quantities apart. Press Sample under
Advanced. The default, the support semantics, gives about 0.27. Switch the
sampling menu to clause activation: each world keeps every uncertain
statement with its confidence and asks whether the answer is provable. That
count gives about 0.45: where the bird fact is kept, flying is provable,
whether or not -bird(tweety) is kept too.
The unless(-flies(X), 2) condition makes this a default
rule: derive flies(X) from bird(X) unless
-flies(X) is established. The number 2 is the rank of the
default, used when defaults compete; see the later examples.
This states that birds normally fly without claiming that every bird flies.
bird(tweety).
bird(robin).
flies(X) :- bird(X), unless(-flies(X), 2).
query(flies(X)).
% Birds fly by default: the rule applies unless
% it can be shown that the bird does not fly.
Both flies(tweety) and flies(robin) are
(1, 0, 0, 0). The default applies to both birds. gk's nested searches looked
for -flies(tweety) and -flies(robin), the
exceptions, and found nothing.
Add the fact -flies(tweety). and solve again:
flies(tweety) becomes (0, 1, 0, 0). The default yields to the
established contrary, so there is no conflict. flies(robin)
stays (1, 0, 0, 0).
The new fact 0.9::-flies(tweety) supports the default's
exception, the negation of its conclusion, with confidence 0.9 rather
than 1.
bird(tweety).
bird(robin).
0.9::-flies(tweety).
flies(X) :- bird(X), unless(-flies(X), 2).
query(flies(X)).
% As before, but now evidence with confidence 0.9 says
% that Tweety does not fly.
flies(robin) is still (1, 0, 0, 0).
flies(tweety) is (0.1, 0.9, 0, 0): where the evidence against
flying passes (0.9 of the worlds), the default yields and the answer is
against. Where it fails (0.1), the default applies and supports flying. The
signed confidence is 0.1 - 0.9 = -0.8. There is no conflict: a
default yields to its exception instead of fighting it (the support semantics,
Section 6).
Change 0.9 to 1: with a certain exception the
answer is (0, 1, 0, 0).
Two default rules compete: birds fly (rank 3) and objects do not fly (rank 2). Every bird is an object, so both defaults apply to every bird. The higher rank wins, so for ordinary birds the flying default defeats the non-flying one.
The penguin rule -flies(X) :- penguin(X) has no
unless: it is strict, and a default yields to what a strict rule
establishes.
bird(tweety).
penguin(pingu).
bird(X) :- penguin(X).
object(X) :- bird(X).
-flies(X) :- penguin(X).
flies(X) :- bird(X), unless(-flies(X), 3).
-flies(X) :- object(X), unless(flies(X), 2).
query(flies(X)).
% Birds fly, penguins are birds, penguins
% do not fly. Also: objects in general do not fly.
flies(tweety) is (1, 0, 0, 0): both defaults apply to
tweety, and the rank-3 flying default wins against the rank-2 object
default.
flies(pingu) is (0, 1, 0, 0): the strict penguin rule
establishes -flies(pingu), and the flying default yields to it,
whatever its rank.
Two defaults of equal rank support opposite conclusions about the same person: the quaker rule supports pacifism and the republican rule supports its negation.
The query asks about a further consequence of pacifism.
quaker(nixon).
republican(nixon).
pacifist(X) :- quaker(X), unless(-pacifist(X), 2).
-pacifist(X) :- republican(X), unless(pacifist(X), 2).
dislikeswar(X) :- pacifist(X).
query(dislikeswar(X)).
% Quakers are pacifists; republicans are not.
% Nixon is both. Does he dislike war?
dislikeswar(nixon) is (0, 0, 0, 1): complete ignorance.
Both defaults apply, and neither has the higher rank, so neither wins:
pacifist(nixon) is not established either way, and its
consequence has no support (the support semantics, Section 6).
The proofs show both defaults. The proof for
dislikeswar(nixon) goes through the quaker default and leaves
its exception unless(-pacifist(nixon), 2) open. The
guard proof below it is a proof of that exception: the republican
default establishes -pacifist(nixon) with the same rank, so
the two block each other.
Raise the quaker default to rank 3: pacifism wins, and
dislikeswar(nixon) becomes (1, 0, 0, 0).
Three movements happen one after another: John to the hallway, Mary to the bathroom, John to the kitchen. Where is John at the end?
The point is what happens between the movements: nobody restates where John is while Mary moves, so his location must persist on its own. The last rule states this as a default with confidence 0.99: a location carries over to the next situation unless that person moved. Defeat of persistence by known events is the classical frame problem handled with defaults.
% John travelled to the hallway.
next(s0, s1).
move(john, hallway, s0, s1).
% Mary journeyed to the bathroom.
next(s1, s2).
move(mary, bathroom, s1, s2).
% John then travelled to the kitchen.
next(s2, s3).
move(john, kitchen, s2, s3).
% A movement establishes the traveller's new location.
at(Person, Place, New) :-
move(Person, Place, Old, New).
% Record who moves during each transition.
moved(Person, Old) :-
move(Person, Place, Old, New).
% Locations normally persist unless that person moves.
0.99::at(Person, Place, New) :-
at(Person, Place, Old),
next(Old, New),
unless(moved(Person, Old), 1).
% Where is John after his second movement?
query(at(john, Place, s3)).
at(john, kitchen, s3) is (1, 0, 0, 0), straight from the
movement rule.
at(john, hallway, s3) is (0, 0, 0, 1). John's hallway
location persists through Mary's movement into s2, because
nobody named John moved then. But John's own move from s2 to
s3 establishes moved(john, s2), the exception of
the persistence default for that step. So nothing supports the hallway at
s3, and gk lists the candidate with ignorance 1.
The search line reports some proof steps that gk did not turn into applications, and one proof application: gk keeps such a proof with its confidence as one piece of evidence (the output, Section 4).
Delete only the line move(john, kitchen, s2, s3)., keeping
next(s2, s3).: then at(john, hallway, s3) becomes
(0.9801, 0, 0, 0.0199), the persistence default applied twice,
0.99 * 0.99 = 0.9801.
Ground arithmetic (3 + 4 < 10 etc) is evaluated during
proof search. To find a value for a variable in an arithmetic condition, gk
uses bounded enumeration of candidate values. Automatic selection enables
this when required; the preset strategy shows the corresponding
arith_instantiation setting.
books_per_box(6).
books_to_pack(42).
0.8::boxes_needed(Boxes) :-
books_per_box(PerBox),
books_to_pack(Total),
Boxes * PerBox = Total.
query(boxes_needed(Boxes)).
% How many boxes of exactly 6 books each contain the
% 42 books? Note the strategy field under Advanced: it
% shows the arithmetic setting used to find a value for Boxes.
The answer boxes_needed(7) is (0.8, 0, 0, 0.2): the rule's
own confidence. gk's proof uses an arithmetic instance, Boxes =
7, which makes 7 * 6 = 42 true. gk keeps a proof step of
this kind as one proof application with the proof's confidence: the search
line counts it.
This is bounded search for satisfying values, not equation solving: it
handles one unknown conservatively (arith_instantiation: 1), and
mode 2 also tries selected two-variable cases. The rule encodes exact
equality, not capacity: with 43 books no value of Boxes
satisfies it, and there is no answer.
The reference tool of the Sample button declines this input: it does not read equality.
The first fact defines a function value: the capital of Estonia is Tallinn. The second states that Alice lives in the capital of Estonia. The question uses the other name of the same city.
gk handles equality with dedicated inference rules (paramodulation), the
same machinery used by classical theorem provers: the term
capital(estonia) is rewritten to tallinn wherever
it helps a proof.
capital(estonia) = tallinn.
lives_in(alice, capital(estonia)).
query(lives_in(alice, tallinn)).
% The two names denote the same city, so the
% question follows by equality rewriting.
The answer is (1, 0, 0, 0). gk's proof rewrites
capital(estonia) inside the second fact to tallinn,
after which the question matches directly.
The reference tool of the Sample button declines this input: it does not read equality.
Several sources give evidence about whether tweety is a bird:
two positive sightings and one negative report. One clause supports flying
from birdness; the other supports not flying from evidence that something is
not a bird. Both clauses are written as disjunctions, so gk may use them in
either direction. Input confidences can also be written as percentages:
90 means 0.9.
The detailed report (in Advanced) is switched on for this example: it lists the evidence gk collected.
[
{"@name":"sighting1","@confidence":90,"@logic":["bird","tweety"]},
{"@name":"sighting2","@confidence":80,"@logic":["bird","tweety"]},
{"@name":"doubt","@confidence":70,"@logic":["-bird","tweety"]},
{"@name":"birds_fly","@confidence":90,"@logic":[["-bird","?:X"],["flies","?:X"]]},
{"@name":"only_birds_fly","@confidence":90,"@logic":[["bird","?:X"],["-flies","?:X"]]},
{"@name":"q","@question":["flies","?:X"]}
]
The answer is (0.252, 0, 0, 0.748). The two sightings pool to
1 - (1 - 0.9) * (1 - 0.8) = 0.98 for
bird(tweety), and the doubt gives 0.7 against. By the rule of
example 4 the premise is in conflict in 0.7 of the worlds and established
in 0.28. The flying clause gives 0.28 * 0.9 = 0.252. Not flying
would need -bird(tweety) established without conflict, which
never happens, so against is 0. The four proofs for flying have higher
confidences (0.81 and less): each is one route, and none sees the doubt
about the premise, which only the value counts.
The table of evidence lists the applications: one use of one input
clause each. A row $src_e5(tweety) is a source event:
the clause only_birds_fly is used in two directions,
bird(tweety) from flies(tweety) and
-flies(tweety) from -bird(tweety), and both uses
share the one event that the clause is in force.
The two directions make a loop across signs between
bird(tweety) and flies(tweety). gk evaluates such
a loop with a cycle cut, which the readings list names. Here the cut
leaves the semantics' value: Sample gives 0.255 with 20,000
worlds, within its sampling error of 0.252.
The weather example is written in gk's
JSON-LD-LOGIC notation.
The input is a JSON list, variables start with ?:, and keys such
as @confidence and @question attach metadata.
This notation is intended for logic generated or consumed by programs.
[
{"@logic": ["dark_clouds", "today"], "@confidence": 0.8},
{"@logic": [["-dark_clouds", "?:X"], ["rain", "?:X"]],
"@confidence": 0.7},
{"@question": ["rain", "today"]}
]
The answer is (0.56, 0, 0, 0.44), as in example 2: the two inputs state the same statements. gk converts every input notation to JSON-LD-LOGIC before it searches.
The third input notation: rules are written premises-first with
& and =>, closer to textbook logic than the
Prolog-style :- form. Everything else — confidences,
queries, unless — is the same.
gk detects the notation automatically from the input text, so all the notations work in this same text box.
% Weather example in premise-to-consequence GKS notation.
0.8 :: dark_clouds(today).
0.7 :: dark_clouds(X) => rain(X).
query(rain(today)).
The answer is (0.56, 0, 0, 0.44), as in examples 2 and 16. All three inputs are translated to the same internal logic before the search.
gk also reads clause-normal-form problems in the
TPTP notation used by automated theorem
provers, extended with a confidence(...) annotation in the
optional useful-info list of the cnf(...) form (the field after
the source position). This example asks which object does not fly:
extracting a value for the open variable is a gk convention layered on top
of TPTP clause form, not part of TPTP semantics itself.
% The basic birds example in GKTPTP: standard TPTP CNF, with gk's metadata
% (confidence, blockers) in the optional useful_info field. This is a 1:1
% translation of bird_penguin.js and bird_penguin.gkp: gk converts all three
% to the same JSON-LD-LOGIC before proving. See Doc/input_languages.md.
%
% The source field is left empty here (",,"), which gk's reader allows; a
% file that must also parse under strict TPTP tools would write the filler
% source "unknown" instead.
cnf(b_is_a_bird, axiom,
( bird(b) ) ).
cnf(p_is_a_penguin, axiom,
( penguin(p) ),,
[confidence(0.8)] ).
cnf(penguins_are_birds, axiom,
( ~ penguin(X) | bird(X) ) ).
cnf(penguins_do_not_fly, axiom,
( ~ penguin(X) | ~ flies(X) ),,
[confidence(0.9)] ).
cnf(birds_fly, axiom,
( ~ bird(X) | flies(X) ),,
[unless(~ flies(X), 2)] ).
cnf(who_does_not_fly, conjecture,
( ~ flies(X) ) ).
gk gives -flies(p) (0.72, 0, 0, 0.28), with the reading
cycle cut. For is 0.8 * 0.9 = 0.72: the penguin
fact and the no-fly clause.
The support semantics gives (0.722, 0.077, 0, 0.201). Where
p is a penguin but the no-fly clause is not in force (about
0.08 of the worlds), p is a bird, the flying default applies,
and flying counts against the answer. gk does not see this part. The
flying default's exception is -flies(p), the question atom
itself, and the no-fly clause read backwards turns flying into evidence
against the penguin fact. So the evidence forms loops through the default
and across signs. gk evaluates such loops with a cycle cut: walking
from the question, it removes the application that leads back. Here the cut
removes the flying default's support, and against becomes 0. The proof
against #1 under the table is that route: it derives
flies(p) from the penguin fact (0.8) through the flying
default and leaves the default's exception
unless(-flies(p), 2) open, the question itself. It is listed,
but after the cut it adds nothing to the value.
Press Sample under Advanced to see the difference: the reference tool computes the support semantics by sampling worlds and shows both values side by side (the reference tool).
The reading self exception names a use of the flying clause
whose exception is its own condition, -bird(p) from
-flies(p) unless -flies(p): such a use never
applies, in gk and in the semantics.
The birds-and-penguins default in JSON notation. What GKP writes as
unless(-flies(X), 3) appears here as an explicit
$block literal inside the rule clause:
["$block", 3, ["$not", ["flies", "?:X"]]]. The GKP and GKS
unless forms are translated to exactly this.
// A bird flies by default; penguins are explicitly non-flying birds.
[
["bird", "b1"],
["penguin", "p1"],
[["penguin", "?:X"], "=>", ["bird", "?:X"]],
[["-bird", "?:X"], ["flies", "?:X"],
["$block", 3, ["$not", ["flies", "?:X"]]]],
[["penguin", "?:X"], "=>", ["-flies", "?:X"]],
{"@question": ["flies", "b1"]}
]
/*
The same knowledge in the GKP notation:
bird(b1).
penguin(p1).
bird(X) :- penguin(X).
flies(X) :- bird(X), unless(-flies(X), 3).
-flies(X) :- penguin(X).
query(flies(b1)).
*/
flies(b1) is (1, 0, 0, 0): b1 is a bird, and
nothing establishes that it does not fly. Programs that generate
JSON-LD-LOGIC write defaults in this form directly.
A classical planning problem: blocks on a table, a robot arm, and a goal
configuration. The plan is found as the answer term: a nested sequence of
pickup/putdown actions transforming the initial
situation.
The time limit is preset to 30 seconds and max answers to 1 (one plan). gk's first proof search gets a quarter of the budget, and finding the plan takes several seconds of it; with a shorter limit the plan is not found. The search runs off the page's main thread, so the page stays responsive; press Stop to abort it.
% Blocks-world planning with situation terms and frame axioms.
% Adapted from GKC's Examples/blocks.txt: goal(State) replaces the reserved
% $ans(State) literal, and query(goal(State)) asks gk for a plan.
% ----- initial situation s0 ------
%
% c
% a b d
% ------------------------------
holds(on(a,table),s0).
holds(on(b,table),s0).
holds(on(c,d),s0).
holds(on(d,table),s0).
holds(clear(a),s0).
holds(clear(b),s0).
holds(clear(c),s0).
holds(empty,s0).
holds(clear(table),State).
% ---- difference of objects -----
differ(X,Y) | -differ(Y,X).
differ(a,b).
differ(a,c).
differ(a,d).
differ(a,table).
differ(b,c).
differ(b,d).
differ(b,table).
differ(c,d).
differ(c,table).
differ(d,table).
% ----- pickup rules ------
holds(holding(X),do(pickup(X),State))
| -holds(empty,State)
| -holds(clear(X),State)
| -differ(X,table).
holds(clear(Y),do(pickup(X),State))
| -holds(on(X,Y),State)
| -holds(clear(X),State)
| -holds(empty,State).
% ---- frame axioms for pickup ----
holds(on(X,Y),do(pickup(Z),State))
| -holds(on(X,Y),State)
| -differ(X,Z).
holds(clear(X),do(pickup(Z),State))
| -holds(clear(X),State)
| -differ(X,Z).
% ----- putdown rules -----
holds(empty,do(putdown(X,Y),State))
| -holds(holding(X),State)
| -holds(clear(Y),State).
holds(on(X,Y),do(putdown(X,Y),State))
| -holds(holding(X),State)
| -holds(clear(Y),State).
holds(clear(X),do(putdown(X,Y),State))
| -holds(holding(X),State)
| -holds(clear(Y),State).
% ---- frame axioms for putdown ----
holds(on(X,Y),do(putdown(Z,W),State))
| -holds(on(X,Y),State).
holds(clear(Z),do(putdown(X,Y),State))
| -holds(clear(Z),State)
| -differ(Z,Y).
% Build the tower c on b on a. A goal answer is the situation term that
% records the action sequence.
-holds(on(b,a),State) | -holds(on(c,b),State) | goal(State).
query(goal(State)).
The answer is a situation term, with (1, 0, 0, 0): every clause is
certain. Read the term inside-out, starting from s0, as the
sequence of actions of the plan:
bb on acc on bThe whole run takes about 20 seconds. After the plan, gk checks it: the nested searches look for opposition to the steps of the proof until the budget is spent, and the search line reports the ones cut by their time allowance. With certain clauses and no negative facts they find nothing, so the value stays (1, 0, 0, 0).
The input was machine-generated by the
llmpipe
pipeline, which uses a large language model to translate English to gk logic:
the story is "Penguins are birds. Penguins cannot fly. Birds can fly. John
is a penguin. John flies?". Each clause carries its source sentence in the
@nl field. The pipeline numbers each named entity, so
#:John 1 is the generated identifier for the first entity named
John (the #: prefix marks a unique object) — not a typo in
the input.
The encoding uses events with actors and types rather than a simple
flies(john) atom, so that the same scheme covers arbitrary verbs,
and a shared background knowledge base (loaded automatically for this
example, not visible in the text box) supplies general axioms for taxonomy,
part-whole relations, degrees, events and space. The displayed clauses are
the generated ones; only the background axioms are added behind the
scenes.
The $ctxt terms carry the context an atom is asserted in
— tense, world, location and knowledge source — so that the
background axioms can reason about when and where a statement holds. They
are part of the llmpipe encoding scheme, not something gk requires.
// Penguins are birds. Penguins do not fly. Birds fly. John is a penguin. John flies?
// Expected answer: False
// Uses the shared background knowledge base axioms_std.js.
// Run from the repository root:
// ./bin/gk Examples/language/axioms_std.js Examples/language/penguin_default.js \
// -strategytext '{"strategy": ["negative_pref", "posunitpara"], "query_preference": 1}' \
// -seconds 3 -confidence 0.1 -keepconfidence 0.1
[
{"@name": "sent_S1", "@logic": [["-isa", "penguin", "?:X"], ["isa", "bird", "?:X"]], "@nl": "Penguins are birds."},
{"@name": "sent_S2", "@logic": [["-isa", "penguin", "?:X"], ["-isa", "activity", "?:E"], ["-has type", "?:E", "fly", ["$ctxt", "?:Fv8", "?:Fv7", "?:Fv5", "?:Fv6"]], ["-has actor", "?:E", "?:X", ["$ctxt", "?:Fv8", "?:Fv7", "?:Fv5", "?:Fv6"]], ["-capability", "?:E"]], "@nl": "Penguins cannot fly."},
{"@name": "sent_S3", "@logic": [["-isa", "bird", "?:X"], ["isa", "activity", ["sk0", "?:X"]]], "@nl": "Birds can fly."},
{"@name": "sent_S3", "@logic": [["-isa", "bird", "?:X"], ["has type", ["sk0", "?:X"], "fly", ["$ctxt", "?:Fv12", "?:Fv11", "?:Fv9", "?:Fv10"]]], "@nl": "Birds can fly."},
{"@name": "sent_S3", "@logic": [["-isa", "bird", "?:X"], ["has actor", ["sk0", "?:X"], "?:X", ["$ctxt", "?:Fv12", "?:Fv11", "?:Fv9", "?:Fv10"]]], "@nl": "Birds can fly."},
{"@name": "sent_S3", "@logic": [["-isa", "bird", "?:X"], ["capability", ["sk0", "?:X"]], ["$block", ["$", "bird", 1], ["$not", ["capability", ["sk0", "?:X"]]]]], "@confidence": 0.9, "@nl": "Birds can fly."},
{"@name": "sent_S4", "@logic": ["isa", "penguin", "#:John 1"], "@nl": "John 1 is a penguin."},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-$defq0"], ["isa", "activity", "sk1_activity"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-$defq0"], ["has type", "sk1_activity", "fly", ["$ctxt", "?:Fv22", "?:Fv21", "?:Fv15", "?:Fv16"]]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-$defq0"], ["has actor", "sk1_activity", "#:John 1", ["$ctxt", "?:Fv24", "?:Fv23", "?:Fv15", "?:Fv16"]]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-$defq0"], ["capability", "sk1_activity"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-isa", "activity", "?:E"], ["-has type", "?:E", "fly", ["$ctxt", "?:Fv28", "?:Fv27", "?:Fv15", "?:Fv16"]], ["-has actor", "?:E", "#:John 1", ["$ctxt", "?:Fv30", "?:Fv29", "?:Fv15", "?:Fv16"]], ["-capability", "?:E"], ["$defq0"]]},
{"@name": "sent_S5", "@question": ["$defq0"]},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["-isa", "penguin", "$some_not_penguin"], "@nl": "[population: from input]"},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["isa", "bird", "$some_bird"], "@nl": "[population: from input]"},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["-isa", "bird", "$some_not_bird"], "@nl": "[population: from input]"},
{"@name": "sent_S2", "@sourcetype": "populate", "@logic": ["isa", "activity", "$some_activity"], "@nl": "[population: from input]"},
{"@name": "sent_S2", "@sourcetype": "populate", "@logic": ["-isa", "activity", "$some_not_activity"], "@nl": "[population: from input]"},
{"@name": "frm_syn", "@logic": [["-has type", "?:E", "fly", "?:Fv31"], ["has type", "?:E", "go", "?:Fv31"]], "@confidence": 0.52, "@nl": "[generated: frm_syn]"}
]
The answer is "the question" with (0, 1, 0, 0): against 1. John, a
penguin, does not fly. The "birds can fly" sentence became a default (note
the $block in its clause and its 0.9 confidence), and the
strict "penguins cannot fly" sentence establishes the contrary, so the
default yields: the penguin pattern of example 10, now produced
automatically from English text.
The readings list cycle cut: the strict penguin sentence,
read backwards, connects the default's atoms in loops across signs, and gk
evaluates such loops with a cut. The search line reports nested searches cut
by their allowance in the large background knowledge base; the value comes
from the evidence found within the budget.
A wh-question produced by the same
llmpipe
English-to-logic pipeline; the displayed clauses are the generated ones,
with the shared background axioms loaded behind the scenes. The question is
defined by the $defq0 clauses, so the answer comes back as
$defq0(...) with its value: a name like #:John 1
is a specific individual mentioned in the text, while a value like
$some_fox is an existential one — "a fox", introduced by
the text without a name. The $ctxt terms carry the context an
atom is asserted in (tense, world, location, knowledge source), as in the
previous example.
The time limit is preset to 10 seconds: in the browser, gk's first proof search needs more than a quarter of a 5-second budget to find the answer.
// Squirrels can fly. Foxes cannot fly. Squirrels and foxes are animals. Which animal cannot fly?
// Expected answer: A fox
// Uses the shared background knowledge base axioms_std.js.
// Run from the repository root:
// ./bin/gk Examples/language/axioms_std.js Examples/language/which_cannot_fly.js \
// -strategytext '{"strategy": ["negative_pref", "posunitpara"], "query_preference": 1}' \
// -seconds 3 -confidence 0.1 -keepconfidence 0.1
[
{"@name": "sent_S1", "@logic": [["-isa", "squirrel", "?:X"], ["isa", "activity", ["sk0", "?:X"]]], "@nl": "Squirrels can fly."},
{"@name": "sent_S1", "@logic": [["-isa", "squirrel", "?:X"], ["has type", ["sk0", "?:X"], "fly", ["$ctxt", "?:Fv4", "?:Fv3", "?:Fv1", "?:Fv2"]]], "@nl": "Squirrels can fly."},
{"@name": "sent_S1", "@logic": [["-isa", "squirrel", "?:X"], ["has actor", ["sk0", "?:X"], "?:X", ["$ctxt", "?:Fv4", "?:Fv3", "?:Fv1", "?:Fv2"]]], "@nl": "Squirrels can fly."},
{"@name": "sent_S1", "@logic": [["-isa", "squirrel", "?:X"], ["capability", ["sk0", "?:X"]], ["$block", ["$", "squirrel", 1], ["$not", ["capability", ["sk0", "?:X"]]]]], "@nl": "Squirrels can fly."},
{"@name": "sent_S2", "@logic": [["-isa", "fox", "?:X"], ["-isa", "activity", "?:E"], ["-has type", "?:E", "fly", ["$ctxt", "?:Fv8", "?:Fv7", "?:Fv5", "?:Fv6"]], ["-has actor", "?:E", "?:X", ["$ctxt", "?:Fv8", "?:Fv7", "?:Fv5", "?:Fv6"]], ["-capability", "?:E"]], "@nl": "Foxes cannot fly."},
{"@name": "sent_S3", "@logic": [["-isa", "squirrel", "?:X"], ["isa", "animal", "?:X"]], "@nl": "Squirrels are animals."},
{"@name": "sent_S4", "@logic": [["-isa", "fox", "?:X"], ["isa", "animal", "?:X"]], "@nl": "Foxes are animals."},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-$defq0", "?:X"], ["isa", "animal", "?:X"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-$defq0", "?:X"], ["-isa", "activity", "?:E"], ["-has type", "?:E", "fly", ["$ctxt", "?:Fv26", "?:Fv25", "?:Fv17", "?:Fv18"]], ["-has actor", "?:E", "?:X", ["$ctxt", "?:Fv28", "?:Fv27", "?:Fv17", "?:Fv18"]], ["-capability", "?:E"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-isa", "animal", "?:X"], ["isa", "activity", ["sk1", "?:X"]], ["$defq0", "?:X"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-isa", "animal", "?:X"], ["has type", ["sk1", "?:X"], "fly", ["$ctxt", "?:Fv36", "?:Fv35", "?:Fv17", "?:Fv18"]], ["$defq0", "?:X"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-isa", "animal", "?:X"], ["has actor", ["sk1", "?:X"], "?:X", ["$ctxt", "?:Fv40", "?:Fv39", "?:Fv17", "?:Fv18"]], ["$defq0", "?:X"]]},
{"@name": "sent_S5", "@sourcetype": "question", "@logic": [["-isa", "animal", "?:X"], ["capability", ["sk1", "?:X"]], ["$defq0", "?:X"]]},
{"@name": "sent_S5", "@question": ["$defq0", "?:X"]},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["isa", "squirrel", "$some_squirrel"], "@nl": "[population: from input]"},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["-isa", "squirrel", "$some_not_squirrel"], "@nl": "[population: from input]"},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["isa", "activity", "$some_activity"], "@nl": "[population: from input]"},
{"@name": "sent_S1", "@sourcetype": "populate", "@logic": ["-isa", "activity", "$some_not_activity"], "@nl": "[population: from input]"},
{"@name": "sent_S2", "@sourcetype": "populate", "@logic": ["isa", "fox", "$some_fox"], "@nl": "[population: from input]"},
{"@name": "sent_S2", "@sourcetype": "populate", "@logic": ["-isa", "fox", "$some_not_fox"], "@nl": "[population: from input]"},
{"@name": "sent_S3", "@sourcetype": "populate", "@logic": ["isa", "animal", "$some_animal"], "@nl": "[population: from input]"},
{"@name": "sent_S3", "@sourcetype": "populate", "@logic": ["-isa", "animal", "$some_not_animal"], "@nl": "[population: from input]"},
{"@name": "frm_syn", "@logic": [["-has type", "?:E", "fly", "?:Fv43"], ["has type", "?:E", "go", "?:Fv43"]], "@confidence": 0.52, "@nl": "[generated: frm_syn]"}
]
The answer is $defq0($some_fox) with (1, 0, 0, 0): an
unnamed fox, introduced as a representative member of its class. Population
clauses (marked @sourcetype: "populate") supply such typical
members for the classes mentioned in the text, so that questions about "a
fox" or "some bird" have witnesses to find. The run takes about 7
seconds.
Three objects with known parts: h1 has a leg, b1
has a leg, a wing and feathers, a1 has an engine and a wing.
Uncertain default rules classify by parts; the question asks which objects
are organisms.
The defaults here use tax(class) instead of a numeric rank:
the rank is looked up from a WordNet-derived taxonomy, in which a more
specific class defeats a more general one. The taxonomy comes from two data
files (a mapping of names to class numbers and the packed class hierarchy),
fetched gzipped (about 0.9 MB) and loaded automatically for this example;
gk then runs with its -defaults option.
% Classify three objects: h1 (likely human), b1 (likely bird),
% a1 (likely airplane). Question: which objects are organisms?
has_part(h1, leg).
has_part(b1, leg). has_part(b1, wing). has_part(b1, feathers).
has_part(a1, engine). has_part(a1, wing).
% Default classification rules with confidences; each applies unless the
% opposite classification is provable, taxonomy-strength blockers.
0.3::isa(X, human) :-
has_part(X, leg), unless(-isa(X, human), tax(human)).
0.2::isa(X, bird) :-
has_part(X, leg), unless(-isa(X, bird), tax(bird)).
0.6::isa(X, bird) :-
has_part(X, wing), unless(-isa(X, bird), tax(bird)).
0.8::isa(X, bird) :-
has_part(X, feathers), unless(-isa(X, bird), tax(bird)).
0.4::isa(X, airplane) :-
has_part(X, wing), unless(-isa(X, airplane), tax(airplane)).
% Strict rules and the taxonomy.
-isa(X, organism) :- has_part(X, engine).
isa(X, organism) :- isa(X, human).
isa(X, organism) :- isa(X, bird).
-isa(X, organism) :- isa(X, airplane).
query(isa(X, organism)).
gk lists two answers. isa(b1, organism) is (0.5731, 0.0179,
0.3821, 0.0269). The support for organism is 0.9552: the bird evidence from
leg, wing and feathers pools to
1 - 0.8 * 0.4 * 0.2 = 0.936, and the human evidence from the
leg joins it, 1 - (1 - 0.936) * (1 - 0.3) = 0.9552. The airplane
default (0.4, from the wing) and the strict rule "an airplane is not an
organism" give 0.4 against. For plus conflict is 0.9552, against plus
conflict is 0.4.
isa(a1, organism) is (0, 0.4, 0.6, 0): the strict engine
rule gives 1 against, the bird default from the wing gives 0.6 for, and
where both pass the result is conflict, 0.6.
Three remarks. First, the certain rules are read in both directions: "a
bird is an organism" also says that what is not an organism is not a bird.
This makes the defaults' exceptions depend on the organism question itself,
a loop through exceptions, and gk evaluates it with a cycle cut.
With equal numeric ranks in place of the tax(...) ranks, the
reference tool gives b1 and h1 within its sampling error of gk's values, but
a1 (0, 1, 0, 0): the engine makes a1 not an organism, hence not a bird, and
the bird default yields. The cut drops that exception. Second, gk's first
proof search with the default strategy does not find h1; with
the strategy {"strategy":["query_focus"],"query_preference":1}
gk lists isa(h1, organism) (0.44, 0, 0, 0.56) and b1, but not
a1. Third, the recursive method reads a taxonomy rank as rank 0; here no two
defaults of opposite signs compete on one atom, so the ranks do not change
the value. The reference tool declines tax(...) ranks.
Examples 24 to 27 build one question in layers: does Tweety fly? Tweety is a bird. Birds normally fly, with confidence 0.9. Penguins normally do not, and the penguin default has the higher rank, 3. Tweety is a seabird with confidence 0.8, and a rule supports that a seabird is a penguin with confidence 0.7.
bird(tweety).
0.9::flies(X) :- bird(X), unless(-flies(X), 2).
-flies(X) :- penguin(X), unless(flies(X), 3).
0.8::seabird(tweety).
0.7::penguin(X) :- seabird(X).
query(flies(tweety)).
The answer is (0.396, 0.56, 0, 0.044). Penguin has support
0.8 * 0.7 = 0.56. Where it is established, the penguin default
wins by rank, and the answer is against. In the remaining 0.44 the flying
default applies: 0.44 * 0.9 = 0.396 for. The rest, 0.044, is
ignorance (the support semantics, Section 10).
The 0.7 penguin rule is replaced by a classical disjunction, "a seabird is a gull or a penguin", and by evidence against gull with confidence 0.7. The disjunction alone establishes neither species.
bird(tweety).
0.9::flies(X) :- bird(X), unless(-flies(X), 2).
-flies(X) :- penguin(X), unless(flies(X), 3).
gull(X) ; penguin(X) :- seabird(X).
0.8::seabird(tweety).
0.7::-gull(tweety).
query(flies(tweety)).
The answer is again (0.396, 0.56, 0, 0.044). The disjunction and the
evidence against gull give a route to penguin(tweety):
0.8 * 0.7 = 0.56, as in layer 1. gk's proof uses the
disjunction in the direction "a seabird that is not a gull is a
penguin".
A report of a yellow bill (confidence 0.6) and a rule that supports gull for seabirds with yellow bills (confidence 0.9) are added.
bird(tweety).
0.9::flies(X) :- bird(X), unless(-flies(X), 2).
-flies(X) :- penguin(X), unless(flies(X), 3).
gull(X) ; penguin(X) :- seabird(X).
0.8::seabird(tweety).
0.7::-gull(tweety).
0.9::gull(X) :- seabird(X), yellow_bill(X).
0.6::yellow_bill(tweety).
query(flies(tweety)).
The answer is (0.6984, 0.224, 0, 0.0776). The new evidence speaks for
gull, so it contests the premise -gull(tweety) inside the
penguin proof. Where seabird and yellow bill are established, gull has
support 0.9 for and 0.7 against: below 0.7 both pass (conflict), between
0.7 and 0.9 gull is established. In those worlds -gull(tweety)
is no usable premise. Penguin now needs the seabird and the negative gull
evidence without a usable yellow bill:
0.8 * 0.7 * (1 - 0.6) = 0.224, and flying gets
(1 - 0.224) * 0.9 = 0.6984.
gk reaches this in three steps. The first proof gives (0.9, 0, 0, 0.1).
The search for opposition to flying finds the penguin default: (0.396, 0.56,
0, 0.044). The search for opposition to the premise
-gull(tweety) finds the yellow-bill rule: (0.6984, 0.224, 0,
0.0776). The last discovery is not another proof of flying: it changes the
state of a premise inside the opposing proof (how gk works, Section
5).
The proofs show this as well. The default proof against flying has the
confidence 0.56 (seabird 0.8 and not gull 0.7), although the against share
is only 0.224: the proof gives the main structure of the reasoning, not the
sub-proofs of the nested searches. Check full proofs under Advanced
and solve again: the proof of gull(tweety) from the yellow bill
(confidence 0.432) appears inside the proof against, under the step whose
search found it.
As layer 3, but the gull rule is certain. A certain rule without exceptions is classical: gk may use it in every direction. With not-gull and seabird it supports not-yellow-bill, and with not-gull and yellow bill it supports not-seabird.
bird(tweety).
0.9::flies(X) :- bird(X), unless(-flies(X), 2).
-flies(X) :- penguin(X), unless(flies(X), 3).
gull(X) ; penguin(X) :- seabird(X).
0.8::seabird(tweety).
0.7::-gull(tweety).
gull(X) :- seabird(X), yellow_bill(X).
0.6::yellow_bill(tweety).
query(flies(tweety)).
The answer is again (0.6984, 0.224, 0, 0.0776), with two readings
cycle cut. The three atoms seabird,
yellow_bill and gull now form a loop across
signs. The support semantics evaluates the loop by narrowing: where the three
input facts pass, all three atoms end in conflict, none supplies a usable
premise, and the penguin proof fails there. gk cuts the loop instead; on
this input the cut leaves the semantics' value. Sample shows the same
numbers within its sampling error.