an application of prolog logical inference with an interface

31
06-25433 – Logic Programming An application of Prolog Logical inference with an interface This lecture will cover:. making logical inferences from new facts; using CHR to implement an inference system; providing an interface to the inference system using a DCG.

Upload: others

Post on 06-Apr-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

06-25433 – Logic Programming

An application of Prolog

Logical inference with an interface

This lecture will cover:.

– making logical inferences from new facts;

– using CHR to implement an inference system;

– providing an interface to the inference system using a DCG.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 1

Last time: DCGs

DCGs are a powerful and convenient implementation of CFG in Prolog.

They allow rules which specify

– structure building

– arbitrary embedded Prolog code

Because DCGs use Prolog’s depth-first search, they have problems with left-recursive rules – but this can be eliminated.

DCGs are more than a language parsing tool – they have uses in a wide variety of programs.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 2

DCGs: a reminder - 1

Consider the problem of finding the sublists of a list:

| ?- sub_string([a,b,c,d,e], Sub_String).

Sub_String = [a,b,c,d,e] ? ;

Sub_String = [b,c,d,e] ? ;

Sub_String = [c,d,e] ? ;

Sub_String = [d,e] ? ;

Sub_String = [e] ? ;

Sub_String = [] ? ;

no

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 3

DCGs: a reminder - 2

This can be written in “normal” Prolog:

% 1 – a list is a sub-list of itself

sub_string(List, List).

% 2 – a sub-list is in the tail

sub_string([_|List], Sub_List) :-

sub_string(List, Sub_List).

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 4

DCGs: a reminder - 3

Or it can be written in DCG notation:

% 1 – a list is a sub-list of itself

sub_string -->

[].

% 2 – a sub-list is in the tail

sub_string -->

[_],

sub_string.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 5

Simple inference

The simplest rule of inference: modus ponens.

The truth table:

P Q P → Q

t t t

t f f

f t t

f f t

if X is_father_of Y then Y is_child_of X

X is_father_of Y

Y is_child_of

X

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 6

Simple inference

The simplest rule of inference: modus ponens.

The truth table:

P Q P → Q

t t t

t f f

f t t

f f t

if X is_father_of Y then Y is_child_of X

Henry is_father_of

Mary

Mary is_child_of

Henry

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 7

Simple inference in Prolog is not simple

This looks easy to write in Prolog:

if X is_father_of Y then Y is_child_of X

is_child_of(Child, Parent) :-

is_father_of(Parent, Child).

with this fact in the database: is_father_of(henry, mary).

and the query: | ?- is_child_of(Child, Father).

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 8

Simple inference in Prolog is not simple

There are two ways of running this rule:

Backward chaining:

• start with the goal and then look for evidence …

• work from the consequent (i.e. the conclusion) to the antecedents (i.e. the evidence).

• this is Prolog’s model of reasoning.

Good if you have the evidence and an hypothesis.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 9

Simple inference in Prolog is not simple

Forward chaining:

• start with the evidence and then look for conclusion(s) …

• work from the antecedents (i.e. the evidence) to the consequent (i.e. the conclusion).

• this is the opposite of Prolog’s model of reasoning.

Good if you have the evidence but no hypothesis.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 10

Simple inference in Prolog is not simple

We often want to use forward chaining – we combine new information with old information to produce new ideas.

It is possible to write a forward chaining inference engine in Prolog – see the module WWW page for a link.

But there is an easier way …

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 11

Constraint Handling Rules (CHR) - 1

It is easy to use Prolog as an implementation language for other languages – and then you can run them within Prolog without any difficulty…

CHR is a programming language for writing constraint processors.

A constraint is only a relationship that should hold between arguments: parent(Child, Mother, Father).

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 12

Constraint Handling Rules (CHR) - 2

In CHR, constraints are Prolog-like structures.

Constraints are held in the central constraint store.

The store is a (logically) unordered heap.

Constraints are posted to the global store.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 13

Constraint Handling Rules (CHR) - 3

CHR is a concurrent programming language.

Concurrent means that it can have one or more processes running at the same time.

The (rough) idea is that each process runs in parallel on their own processor.

Individual processes communicate and share information through shared logical variables.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 14

Constraint Handling Rules (CHR) - 4

In practice, we write rules that specify how to process constraints.

These rules act like processes that are run as soon as their constraints are satisfied.

Each rule runs as soon as it can – so if something happens to satisfy two rules – both are fired concurrently.

The programmer doesn’t have to tell CHR when to fire a rule.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 15

Inference in CHR

This is the rule for being a mother:

parent(Mother, Child, _),

female(Mother, _) ==>

mother(Mother, Child, system).

This reads:

if parent(M, C) and female(M) then infer

mother(M, C).

Note that CHR allows more than one constraint in the head of the rule.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 16

Coding in CHR

Some constraints are:

– child(mary, henry, system)

– father(henry, mary, system)

– female(mary, user)

– male(henry, user)

– parent(henry, mary, user)

system and

user shows

whether the

constraint has

been added by

user input or

inferred by the

system.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 17

Part of the Tudor family tree

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 18

Using inference rules

parent(Father,Child,_),male(Father,_) ==>

father(Father, Child, system).

parent(Parent, Child, _) ==>

child(Child, Parent, system).

female(Sister, _) ==>

sister(Sister, Sibling, system).

half_sibling(Sister, Sibling, _),

female(Sister, _) ==>

half_sister(Sister, Sibling,system).

Demo 1

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 19

What is this application?

The application is

– a natural language interface to a knowledge base

– with an inference system to infer new knowledge

The knowledge domain is family relationships, e.g.

– parent

– mother

– sibling

– step-brother, …

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 20

What could we use this for?

One use would be to learn information from texts so it can be used to answer questions …

For instance, IBM’s Watson system:

http://www.youtube.com/watch?v=WFR3lOm_xhE

Watson has about 50,000 lines of Prolog code.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 21

An example of the system in action - 1

The input is: “Henry VIII is male”

Input text: [henry8, is, male].

Syntax: s(np(propn(Henry VIII)),

vp(is,adj_p(adj(male))))

Semantics: male(Henry VIII,user)

Knowledge store

-----------------------------------------

male(Henry VIII,user)

-----------------------------------------

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 22

An example of the system in action - 2

The next input is: “Henry VIII is the parent of Mary Tudor”

Input text: [henry8, is, the, parent, of, mary].

Syntax: s(np(propn(Henry VIII)),

vp(is,

np(det(the),noun(parent)),

pp(prep(of),

np(propn(Mary Tudor)))))

Semantics: parent(Henry VIII,Mary,user)

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 23

An example of the system in action - 3

The knowledge store has facts added by the user and inferred by the system:

Knowledge store

-----------------------------------------

male(Henry VIII,user)

parent(Henry VIII,Mary,user)

child(Mary,Henry VIII,system)

father(Henry VIII,Mary,system)

-----------------------------------------

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 24

A natural language interface - 1

In the following, we’ll see how reasoning can be linked to a natural language interface.

We will use syntax to ensure that our sentences are well-formed and use a simple semantic structure to provide facts for the inference engine.

Our semantic structure has the basic form of:

– predicate

– one or more arguments

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 25

A natural language interface - 2

Examples of semantic structure

– male(‘Henry VIII’, user)

– parent(‘Henry VIII’, ‘Mary’, system)

In other words, the semantic structures are the CHR constraints.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 26

Adding semantics

We need to map from the input words to semantic representation.

Syntax guides this process:

• it picks out the “action/state” of the sentence to become the predicate of the semantic structure:

• Henry is male male(henry)

• it shows what fills which arguments:

• Jane hits John with the club hits(jane, john, club)

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 27

Building the semantic structures

Building semantic structures is not for beginners!

The standard technique:

• uses lambda calculus (Models of Computation) to put variables into the correct slots;

• pattern matching (because assignment won’t work here).

All will be revealed in Natural Language Processing 1

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 28

How do the interface

and inference engine communicate?

The answer is very simply.

The output of the interface is a semantic structure, e.g.: parent('Elizabeth of York',

'Henry VIII', system)

female('Elizabeth of York', system)

These are simply posted to the store.

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 29

How do the interface

and inference engine communicate?

Posting to store is simply done by parsing an input then “running” the semantics:

| ?- s(Tree, Sem, [henry8,is,male], []),

Sem.

Sem = male('Henry VIII',user),

Tree = s(propn('Henry VIII'),

vp(is,adj_p(adj(male)))),

male('Henry VIII',user) ? ;

no Demo 3

06-25433 – Logic Programming

13 - An application of Prolog: Logical inference with an interface 30

The significance of this system

Inference (in Prolog or CHR) is used in a wide range of applications - configuring telephone systems, reasoning about security access, controlling Java concurrency.

DCGs can be used for describing structured data - and writing simpler list processing programs.

The NL interface could be developed to answer questions, to accept new rule definitions, to understand questions about all, some, how many etc.