AI Applications

Extensions: explanations ("why?", "how?" via meta-interpreters), certainty factors, forward chaining, rule editors.

35.1 Expert systems

Extensions: explanations ("why?", "how?" via meta-interpreters), certainty factors, forward chaining, rule editors.

:- dynamic known/3.

% Knowledge base
animal(cheetah) :- mammal, carnivore, verify(has_tawny_color), verify(has_dark_spots).
animal(tiger)   :- mammal, carnivore, verify(has_tawny_color), verify(has_black_stripes).
animal(penguin) :- bird, verify(does_not_fly), verify(swims).
mammal    :- verify(has_hair).
carnivore :- verify(eats_meat).
bird      :- verify(has_feathers).

% Interactive verification with memory
verify(Q) :- known(yes, Q, _), !.
verify(Q) :- known(no, Q, _), !, fail.
verify(Q) :-
    format("Does the animal have the property: ~w? (yes/no) ", [Q]),
    read(Answer),
    assertz(known(Answer, Q, asked)),
    Answer == yes.

identify :- retractall(known(_, _, _)), ( animal(A) -> format("It is a ~w.~n", [A]) ; writeln("Unknown.") ).

35.2 Forward chaining

:- dynamic fact/1.
rule([fever, cough], flu).
rule([flu, high_risk], see_doctor).

forward :-
    rule(Conds, Concl), \+ fact(Concl),
    forall(member(C, Conds), fact(C)), !,
    assertz(fact(Concl)), format("Derived: ~w~n", [Concl]),
    forward.
forward.

35.3 Planning (STRIPS-style)

% Blocks world: action(Name, Preconditions, AddList, DeleteList)
action(move(B, From, To),
       [clear(B), on(B, From), clear(To)],
       [on(B, To), clear(From)],
       [on(B, From), clear(To)]) :- B \== To, From \== To, B \== From.

plan(State, Goals, []) :- subset(Goals, State).
plan(State, Goals, [A|Plan]) :-
    action(A, Pre, Add, Del),
    subset(Pre, State),
    subtract(State, Del, S1), union(Add, S1, S2),
    plan(S2, Goals, Plan).

% Use iterative deepening: length(Plan, _), plan(Init, Goal, Plan).

35.4 Natural language processing

DCGs with features (agreement, semantics):

Logical forms, question answering over a knowledge base, chatbots (ELIZA), parsing with semantic attachments (lambda calculus).

sentence(S) --> noun_phrase(Num, X), verb_phrase(Num, X, S).
noun_phrase(sg, john) --> [john].
noun_phrase(pl, people) --> [people].
verb_phrase(sg, X, sleeps(X)) --> [sleeps].
verb_phrase(pl, X, sleep(X)) --> [sleep].

?- phrase(sentence(M), [john, sleeps]).      % M = sleeps(john)
?- phrase(sentence(M), [john, sleep]).       % false (agreement)

35.5 Theorem proving & logic

Propositional SAT/tautology checking (CLP(B)).

First-order theorem provers: leanTAP (tableau prover in a few lines), PTTP.

Type inference (Hindley-Milner in ~30 lines using unification).

Program verification and abstract interpretation (Ciao).

35.6 Inductive Logic Programming (ILP)

Learning rules from examples: Aleph, Metagol, Popper — Prolog as a machine-learning hypothesis language.

35.7 Prolog + LLMs

Neuro-symbolic systems use LLMs to translate natural language into Prolog facts/queries, then let Prolog do exact reasoning (e.g., via Janus Python bridge).