sllp practical

29
PRACTICAL :-1 AIM :- STUDY OF PROPOSITIONAL LOGIC. THEORY :- Propositional logic, also known as sentential logic, is the branch of logic that studies ways of joining and/or modifying entire propositions, statements or sentences to form more complicated propositions, statements or sentences, as well as the logical relationships and properties that are derived from these methods of combining or altering statements. Propositional logic is logic at the sentential level. The smallest unit we deal with in propositional logic is a sentence. We do not go inside 1

Upload: samshr

Post on 22-Nov-2014

110 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Sllp Practical

PRACTICAL:-1

AIM:- STUDY OF PROPOSITIONAL LOGIC.

THEORY:-

Propositional logic, also known as sentential logic, is the branch of

logic that studies ways of joining and/or modifying entire propositions,

statements or sentences to form more complicated propositions,

statements or sentences, as well as the logical relationships and properties

that are derived from these methods of combining or altering statements.

Propositional logic is logic at the sentential level. The smallest unit we

deal with in propositional logic is a sentence. We do not go inside

individualsentences and analyze or discuss their meanings. We are going

to be interested only in true or false of sentences, and major concern is

whether or not the truth or falsehood of a certain sentence follows from

those of a set of sentences, and if so, how. Thus sentences considered in

this logic are not arbitrary sentences but are the ones that are true or false.

This kind of sentences is called propositions.

1

Page 2: Sllp Practical

Propositional logic can be thought of as primarily the study of logical

operators. A logical operator is any word or phrase used either to modify

one statement to make a different statement, or join multiple statements

together to form a more complicated statement. In the propositional

calculus the language consists of propositional variables (or placeholders)

and sentential operators (or connectives). The signs '&', 'v', '→', '↔', and '¬',

correspond, respectively, to the truth-functions of conjunction, disjunction,

material implication, material equivalence, and negation. Variables

represent propositions, and there are no relations, functions, or quantifiers

A proposition is a statement that can be true or false. Propositional

logic uses true statements to form or prove other true statements.

Representation:

A proposition is defined as:

1. A propositional symbol: p, q, r…

2. The negation of a proposition: !p (traditionally a '¬' symbol)

3. The disjunction of two propositions: pvq (traditionally a 'v'

symbol)

2

Page 3: Sllp Practical

4. The conjunction of two propositions: p^q (traditionally a '^'

symbol)

5. An implication: p→q

6. A logical equivalence: p↔q

7. An ordering operation: (p)

8. T (true)

9. F (false)

Elements of Propositional Logic:

Conjunction(^):

The conjunction of two statements α and β, written in PL as (α ^ β), is

true if both α and β are true, and is false if either α is false or β is false or

both are false.

Disjunction(v):

The disjunction of two statements α and β, written in PL as (α v β), is

true if either α is true or β is true, or both α or β are true, and is false only if

both α and β are false.

Negation(~):

The negation of statement α, simply written ¬α in language PL, is

regarded as true if α is false, and false if α is true.

3

Page 4: Sllp Practical

Material Implication (If-then)(=>):

This truth-function is represented in language PL with the sign '→'. A

statement of the form (α → β), is false if α is true and β is false, and is true

if either α is false or β is true (or both).

Double Implication (Material Equivalence or if and only if)(<=>):

This truth-function is represented in language PL with the sign '↔'. A

statement of the form (α ↔ β) is regarded as true if α and β are either both

are true or both are false, and is regarded as false if they have different

truth-values.

Types of Propositions:

1. Tautology :

Some propositions are always true regardless of the truth value

of its component propositions. For example (PV¬P) is always true

regardless of the value of the proposition P. A proposition that is

always true called a tautology.

2. Contradiction :

There are also propositions that are always false such as

(P^¬P). Such a proposition is called a contradiction.

4

Page 5: Sllp Practical

PRACTICAL:-2

AIM:- STUDY OF 1ST ORDER PREDICATE LOGIC.

THEORY:-

First-order predicate calculus or first-order logic (FOL) is a system of

mathematical logic, extending propositional logic. The propositional logic is

not powerful enough to represent all types of assertions that are used to

express certain types of relationship between propositions such as

equivalence. The most important knowledge representation language is

arguably predicate logic. Where a logic is concerned not only with

sentential connectives but also with the internal structure of atomic

propositions it is usually called a predicate logic. A predicate is a verb

phrase template that describes a property of objects, or a relationship

among objects represented by the variables. A predicate is a boolean

function whose value may be true or false, depending on the arguments to

the predicate.

Predicates are a generalization of propositional variables.

A propositional variable is a predicate with no arguments.

The atomic sentences of first-order predicate logic have the form

(a predicate with one or more "subjects"). The new ingredient

of first-order logic not found in propositional logic is quantification: where φ

5

Page 6: Sllp Practical

is any sentence, the new constructions and , read "for all x, φ"

and "for some x, φ", are introduced.

Quantifiers tell us of how many objects the predicate is asserted. If

we want to assert a predicate of all objects, we use the universal quantifier.

If we want to assert a predicate of some objects (at least one), we use the

existential quantifier. Variables inside the scope of a quantifier are bound

by that quantifier; otherwise they are free. Predicate logic in which

predicates takes only individuals as arguments and quantifiers only bind

individual variables.

Representation:-

A proposition is defined as:

1. A set of predicate variables (or relations) each with some valence ≥1,

which are often denoted by uppercase letters P, Q, R,..

2. A set of constants, often denoted by lowercase letters a, b, c,...

3. A set of functions, each of some valence ≥ 1, which are often denoted

by lowercase letters f, g, h,...

4. An infinite set of variables, often denoted by lowercase letters x, y,

z,...

5. Symbols denoting logical operators: ¬ (logical not), (logical and),

(logical or), → (logical conditional), ↔ (logical biconditional).

6

Page 7: Sllp Practical

6. Symbols denoting quantifiers: (universal quantification),

(existential quantification)

7. Left and right parenthesis.

The Universal Quantifier

The expression: x P(x), denotes the universal quantification of the

atomic formula P(x). Translated into the English language, the expression

is understood as: "For all x, P(x) holds", "for each x, P(x) holds" or "for

every x, P(x) holds". is called the universal quantifier, and x means all

the objects x in the universe. If this is followed by P(x) then the meaning is

that P(x) is true for every object x in the universe. For example, "All cars

have wheels" could be transformed into the propositional form, x P(x),

where: P(x) is the predicate denoting: x has wheels.

The Existential Quantifier:-

The expression: xP(x), denotes the existential quantification of P(x).

Translated into the English language, the expression could also be

understood as: "There exists an x such that P(x)" or "There is at least one x

such that P(x)" is called the existential quantifier, and x means at least

one object x in the universe. If this is followed by P(x) then the meaning is

that P(x) is true for at least one object x of the universe. For example,

"Someone loves you" could be transformed into the propositional form, x

P(x), where: P(x) is the predicate meaning: x loves you,

7

Page 8: Sllp Practical

PRACTICAL:-3

AIM:- INTODUCTION TO PROLOG.

THEORY:-

Prolog (Programming in Logic) is one of the most widely used

programming languages in artificial intelligence research. As opposed to

imperative languages such as C or Java, it is a declarative programming

language. That means, when implementing the solution to a problem,

instead of specifying how to achieve a certain goal in a certain situation, we

specify what the situation (rules and facts) and the goal (query) are and let

the prolog interpreter derive the solution for us. Prolog is very useful in

some problem areas such as artificial intelligence, natural language

processing, and databases….but pretty useless in others, such as graphics

or numerical algorithms.

Logic plays an important role as it is used to study computer

science foundations. It forms the basis for logic programming. Logic

programming is done by using the language prolog. Logic programming

consists of two kinds of semantics:

1> DECLARATIVE SEMANTICS : This deals with the logical consequences or logical results of logical programs.

8

Page 9: Sllp Practical

2> OPERATIONAL SEMANTICS : This deals with the computation of answer substitution.

Prolog Syntax

This consists of terms, clauses, program and queries:-

Terms:-

The central data structure in prolog is that of term. There are terms of

four kinds – atoms, variables, numbers, and compound terms.

1. Atoms –

Atoms are usually strings made up of lower and upper case

letters, digits and the underscore starting with lowercase letter. The

following are all valid prolog atoms. Elephant, b, abcXYZ, x_123

1. Numbers –

All prolog implementations have an integer type: a sequence of

digits, optionally preceded by – (minus).some also support floats.

2. Variables –

Variables are the strings of letters, digits and the underscore,

starting with a capital letter or an underscore. X, Elephant, _4711, X_1_2, MyVariable,_

9

Page 10: Sllp Practical

3. Compound Terms –

These are made up of a prolog atoms and a number of arguments (prolog terms, i.e. atoms, numbers, variables or other compound terms enclosed in parentheses and separated by commas).

is_bigger (horse), f (g(X), 7)

Clauses, Programs and Queries:-

Prolog programs are made up of facts and rules. Facts and

rules are also called clauses.

A fact is a predicate followed by a dot.

For Example: life_is_beautiful.

A rule consists of a head (a predicate) and a body. Head and

body are separated by the sign: - and, like every prolog expression, a

rule has to be terminated by a dot.

For Example, is_smaller(X, Y):- is bigger(Y, X).

Aunt (Aunt, Child):- sister (Aunt, Parent), parent (Parent, Child).

QUERIES:

After compilation a prolog program is run by submitting queries

to the interpreter. A query has the same structure as the body of a

rule that is it is a sequence of predicates separated by commas and

terminated by a dot. They can be entered at the prolog prompt, which

10

Page 11: Sllp Practical

in most implementations look something like this:?-. When writing

about queries we often include the: ?-.

EXAMPLES:

?- is_bigger (elephant, donkey).

?- small(X), green(X), slimy(X).

SYSTEM PREDICATE CUT:

Prolog is non-deterministic in nature because even when a goal has

been achieved the interpreter backtracks and sees if there any other way to

achieve the goal. So prolog provides a system predicate cut which is

denoted by (!) sign and it affects the overall procedure behavior of a

program and the system predicate cut is also used to limit the non-

determinism by preventing interpreter from finding alternative solutions.

When interpreter comes across occurs then all the alternative

solutions of a goal waiting to be tried are abandoned or debarred with

which it reduces the size of the search tree. Cut shortens the path

traversed by prolog interpreter. And also it reduces the computation time.

IMPORTANT USES OF THE CUT:

1. By using cut, program operates faster.

2. Less memory space is used.

11

Page 12: Sllp Practical

3. It helps the prolog interpreter to find a right rule for particular goal.

4. If-Then-else statements are used.

TYPES OF CUT:

1. GREEN CUT.

2. RED CUT.

1. GREEN CUT :

It does not affect the solution but affects the efficiency of prolog

program. Removal of such cut does not change the meaning of

the program.

2. RED CUT:

Removal of red cut from the program changes the meaning of

the program.

12

Page 13: Sllp Practical

PRACTICAL:-4

AIM:-PROGRAM TO CHECK WHETHER INPUT IS ALPHABET OR NOT.

THEORY:-

check(X):-number(X),write("its not an alphabet").

check(X):-not(number(X)),write("its an alphabet").

?-check(s).

OUTPUT:-

13

Page 14: Sllp Practical

PRACTICAL:-5

AIM:-PROGRAM TO FIND WHETHER A GIVEN NUMBER IS +VE OR –VE.

THEORY:-

check(X):-X>0,write("positive").

check(X):-X<0,write("negative").

?-check(1).

OUTPUT:-

14

Page 15: Sllp Practical

PRACTICAL:-6

AIM:-PROGRAM TO CHECK WHETHER A GIVEN PERSON IS A MEMBER OF CLUB.

THEORY:-

member(X,[X|_]).

member(X,[_|Y]):-member(X,Y).

?-member(jasmeen,[neha,harpreet,jasmeen]).

OUTPUT:-

15

Page 16: Sllp Practical

PRACTICAL:-7

AIM:- PROGRAM ILLUSTRATING THE USE O F RECURSION THAT IS FINDING SUM OF N-INTEGERS.

THEORY:-

domains

n,r=integer

predicates

sum(n,r)

clauses

sum(0,0).

sum(N,R):-N>0,N1=N-1,sum(N1,Z),R=Z+N.

OUTPUT

Goal: sum(5,R).

R=15.

1 solution

Goal:

16

Page 17: Sllp Practical

PRACTICAL:-8

AIM:-PROGRAM TO FIND THE LENGTH OF LIST USING RECURSION AND ACCUMULATOR.

THEORY:-

lislen(L,N):-lenacc(L,0,N).

lenacc([],A,A).

lenacc([H|T],A,N):-A1=A+1,lenacc(T,A1,N).

?-lislen([1,2,3,4,5,6],N),write("length of list is:"),write(N).

OUTPUT:-

17

Page 18: Sllp Practical

PRACTICAL:-9

AIM:-PROGRAM TO FIND FACTORIAL OF A NUMBER.

THEORY:-

domains

number=integer

predicates

fact(number,number)

clauses

fact(0,1).

fact(1,1).

fact(N,R) if N>1 and

I=N-1 and fact(I,P) and R=P*N.

OUTPUT:-

Goal: fact(5,R).

R=120.

1 solution

Goal:

18

Page 19: Sllp Practical

PRACTICAL:-10

AIM:-PROGRAM TO GENERATE FIBONACCI SERIES.

THEORY:-domains

n,r=integer

predicates

fib(n,r)

clauses

fib(0,0).

fib(1,1).

fib(N,R):-N1=N-1,N2=N1-1,fib(N1,Z),fib(N2,Y),R=Z+Y.

OUTPUT:-

Goal: sum(2,R).

R=1.

1 solution

Goal:

19

Page 20: Sllp Practical

PRACTICAL:-11

AIM:-PROGRAM TO FIND LAST ELEMENT OF A LIST.

THEORY:-

last(X,[X]).

last(X,[_|Y]):-last(X,Y).

?-last(X,[1,3,5,2,4]),write("the last element of list is: "),write(X).

OUTPUT

20