sllp file to print

Upload: manish-bansal

Post on 03-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Sllp File to Print

    1/43

    Submitted To: Submitted By:Er. Vishal Paika Kulwinder Kaur

    (Lecturer CSE) L-42103011

    1

  • 7/28/2019 Sllp File to Print

    2/43

    INDEX

    1. Study of proposition logic

    2. Study of predicate logic

    3. Introduction to prolog programming by family program

    4. Write a program to check whether given number is positive or negative

    5. Program to check whether given person is member of club or not

    6. Program showing mapping i.e constructing new structure

    7. Program explaining the use or recursion with Factorial Program

    8. Program to find length of the list using recursion

    a. Program to find length of the list using accumulator

    9. Program to illustrate cut query

    10. Program to generate Fibonacci series

    11. Program to concatenate two lists

    12. Program to delete elements from list.

    13. Program to reverse a list

    14. Program to illustrate bubble sort

    15. Program to illustrate insertion sort

    16. Program to find union of two lists

    17. Program to find intersection of two lists

    18. To find the GCD of a two numbers

    19. Develop rules to perform following arithmetic in PROLOG.

    a. Square of a number.

    b. Check whether number is +ve or ve.

    2

  • 7/28/2019 Sllp File to Print

    3/43

    c. Check whether two numbers are equal.

    20. Check whether number lies in given range

    21. To find the sum of N numbers.

    22. Trace steps for resolving Towers Of Hanoi problems for N = 5.

    23. Trace the stepwise behavior of procedure to find maximum number in

    the

    Following list:-

    [88, 66, 99, 7, 2, 1, 78, 97]

    24. Trace the stepwise behaviour of procedure to find the sum of the list of

    following numbers

    [88, 66, 99, 7, 2, 1, 78, 97]

    25. Check whether a number is even or odd.

    3

  • 7/28/2019 Sllp File to Print

    4/43

    Experiment 1STUDY OF PROPOSITIONAL LOGIC TERMS

    AND SYMBOLS

    A simple statement is one that does not contain any otherstatement as a part. We will use the lower-case letters, p, q, r, ...,as symbols for simple statements.

    A compound statement is one with two or more simplestatements as parts or what we will call components. Acomponent of a compound is any whole statement that is part ofa larger statement; components may themselves be compounds.

    An operator (or connective) joins simple statements intocompounds, and joins compounds into larger compounds. We willuse the symbols, , , , and to designate the sententialconnectives. They are called sententialconnectives because they

    join sentences (or what we are calling statements). The symbol,~, is the only operator that is not a connective; it affects singlestatements only, and does not join statements into compounds.

    The symbols for statements and for operators comprise ournotation or symbolic language. Parentheses serve aspunctuation.

    Simple statements

    4

  • 7/28/2019 Sllp File to Print

    5/43

    p "p is true" assertion

    ~p "p is false" negation

    Compounds and connectives

    p q "either p is true, or q is true, or both" disjunction

    p q "both p and q are true" conjunction

    p q "if p is true, then q is true" implication

    p q "p and q are either both true or both false" equivalence

    Implication statements (p q) are sometimes calledconditionals, and equivalence statements (p q) aresometimes called biconditionals.

    To indicate that a compound is to be taken as a who0le or singlestatement, we put it in parentheses. For example, p q is acompound. Its negation is ~(p q). When the negation sign isoutside the parentheses, it affects the entire compound, not justthe first component, p. Its conjunction with the compound, q r,would be expressed, (p q)(q r). If we wanted to say that thewhole latter statement was false, we would write, ~[(p q)(q r)].If we wanted to say that either the whole latter statement wastrue, or that s t was true, we would write, ~[(p q)(q r)] (s

    t). And so on.

    The truth value of a statement is its truth or falsity. Allmeaningful statements have truth values, whether they aresimple or compound, asserted or negated. That is, p is either trueor false, ~p is either true or false, p q is either true or false, andso on.

    A compound statement is truth-functional if its truth value as awhole can be figured out solely on the basis of the truth values of

    its parts or components. A connective is truth- functional if itmakes only compounds that are truth-functional. For example, ifwe knew the truth values of p and of q, then we could figure outthe truth value of the compound, p q. Therefore the compound,p q, is a truth-functional compound and disjunction is a truth-functional connective.

    5

  • 7/28/2019 Sllp File to Print

    6/43

    All four of the connectives we are studying (disjunction,conjunction, implication, and equivalence) are truth-functional.Negation is a truth-functional operator. With these fourconnectives and negation we can express allthe truth-functionalrelations among statements. (Can you imagine how we wouldprove this?)

    A truth table is a complete list of the possible truth values of astatement. We use "T" to mean "true", and "F" to mean "false"(though it may be clearer and quicker to use "1" and "0"respectively).

    For example, p is either true or false. So its truth table has just 2rows:

    p

    T

    F

    But the compound, p q, has 2 components, each of which canbe true or false. So there are 4 possible combinations of truthvalues. The disjunction of p with q will be true as a compound

    whenever p is true, or q is true, or both:

    p Q p q

    T T T

    T F T

    F T T

    F F F

    If a compound has n distinct simple components, then it will have2n rows in its truth table.

    6

  • 7/28/2019 Sllp File to Print

    7/43

    The truth table columns that define the basic connectives are asfollows:

    p q ~p ~q p q p q p q p q

    T T F F T T T T

    T F F T T F F F

    F T T F T F T F

    F F T T F F T T

    Experiment 2

    STUDY OF FIRST ORDER PREDICATELOGIC

    Apredicate is a feature of language which you can use to make astatement about something, e.g. to attribute a property to thatthing. If you say "Peter is tall", then you have applied to Peter thepredicate "is tall". We also might say that you have predicatedtallness of Peter or attributed tallness to Peter.

    A predicate may be thought of as a kind of function which applies

    to individuals (which would not usually themselves bepropositions) and yields a proposition. They are thereforesometimes known aspropositional functions.

    Analysing the predicate structure of sentences permits us tomake use of the internal structure of atomic sentences, and to

    7

    http://www.rbjones.com/rbjpub/philos/glossary/p.htm#predicatehttp://www.rbjones.com/rbjpub/philos/glossary/p.htm#PropositionalFunctionhttp://www.rbjones.com/rbjpub/philos/glossary/p.htm#predicatehttp://www.rbjones.com/rbjpub/philos/glossary/p.htm#PropositionalFunction
  • 7/28/2019 Sllp File to Print

    8/43

    understand the structure of arguments which cannot beaccounted for by propositional logic alone.

    Predicates (orrelations): Are operators which yield atomic sentences. Operate on things other than sentences. Are therefore not truth functionaloperators. Yield atomic sentences whose truth can be determined

    knowing only the identity of the things to which thepredicate is applied (i.e. they are extensional).

    Predicate Logics:--- Where a logic is concerned not only withsentential connectives but also with the internal structure of

    atomic propositions it is usually called apredicate logic.The most well known, and probably the simplest of these logics isknown as classical or boolean, first-order, predicate logic or,perhaps more appropriate but not so often used, quantifiertheory.

    The Universal Quantifier

    The expression: x P(x), denotes the universal quantification ofthe atomic formula P(x). Translated into the English language, theexpression 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 universalquantifier, and xmeans all the objects x in the universe. If thisis followed by P(x) then the meaning is that P(x) is true for everyobject 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:xhas wheels, and the universe of discourse is only populated by cars.

    Universal Quantifierand Connective AND

    If all the elements in the universe of discourse can be listed thenthe universal quantification x P(x) is equivalent to the

    8

    http://www.rbjones.com/rbjpub/logic/log003.htmhttp://www.rbjones.com/rbjpub/philos/glossary/r.htm#Relationhttp://www.rbjones.com/rbjpub/philos/glossary/e.htm#extensionalhttp://www.rbjones.com/rbjpub/logic/log003.htmhttp://www.rbjones.com/rbjpub/philos/glossary/r.htm#Relationhttp://www.rbjones.com/rbjpub/philos/glossary/e.htm#extensional
  • 7/28/2019 Sllp File to Print

    9/43

    conjunction: P(x1)) P(x2) P(x3) ... P(xn) .

    For example, in the above example of x P(x), if we knew thatthere were only 4 cars in our universe of discourse (c1, c2, c3and c4) then we could also translate the statement as: P(c1)P(c2) P(c3) P(c4)

    The Existential Quantifier

    The expression: xP(x), denotes the existential quantification ofP(x). Translated into the English language, the expression couldalso be understood as: "There exists an x such that P(x)" or"There is at least one xsuch that P(x)" is called the existential

    quantifier, and xmeans at least one objectxin the universe. Ifthis is followed by P(x) then the meaning is that P(x) is true for atleast one objectxof the universe. For example, "Someone lovesyou" could be transformed into the propositional form, x P(x),where:

    P(x) is the predicate meaning:xloves you, The universe of discourse contains (but is not limited to) all

    living creatures.

    Existential Quantifierand Connective OR

    If all the elements in the universe of discourse can be listed, thenthe existential quantification xP(x) is equivalent to thedisjunction: P(x1) P(x2) P(x3) ... P(xn).

    For example, in the above example of x P(x), if we knew thatthere were only 5 living creatures in our universe of discourse

    (say: me, he, she, rex and fluff), then we could also write thestatement as: P(me) P(he) P(she) P(rex) P(fluff)

    An appearance of a variable in a wffis said to be bound if eithera specific value is assigned to it or it is quantified. If anappearance of a variable is not bound, it is called free. The extent

    9

    http://www.cs.odu.edu/~toida/nerzic/content/logic/pred_logic/construction/wff_intro.htmlhttp://www.cs.odu.edu/~toida/nerzic/content/logic/pred_logic/construction/wff_intro.html
  • 7/28/2019 Sllp File to Print

    10/43

    of the application(effect) of a quantifier, called the scope of thequantifier, is indicated by square brackets [ ]. If there are nosquare brackets, then the scope is understood to be the smallestwfffollowing the quantification.For example, in x P(x, y), the variablexis bound while yisfree. In x[ y P(x, y) Q(x, y) ] , xand the yin P(x, y) arebound, while yin Q(x, y) is free, because the scope of yis P(x,y). The scope of xis [ y P(x, y) Q(x, y)].

    Quantifier axioms

    The following four logical axioms characterize a predicatecalculus:

    PRED-1:

    PRED-2:

    PRED-3:

    PRED-4:

    Experiment No. -3

    Aim : Introduction to prolog programming by family program.

    Domains

    person = string

    predicates

    10

    http://www.cs.odu.edu/~toida/nerzic/content/logic/pred_logic/construction/wff_intro.htmlhttp://www.cs.odu.edu/~toida/nerzic/content/logic/pred_logic/construction/wff_intro.html
  • 7/28/2019 Sllp File to Print

    11/43

    male(person)

    female(person)

    mother(person,person)

    father(person,person)

    brother(person,person)

    sister(person,person)

    parents(person,person,person)

    gparents(person,person,person)

    aunty(person,person)

    uncle(person,person)

    clauses

    male(kartarsingh).

    male(ratansingh).

    male(manjitsingh).

    male(harpreetsingh).

    male(upkarsingh

    female(kartarkaur).

    female(amarkaur).

    female(surinderkaur).

    female(jaspreetkaur).

    female(surjitkaur).

    mother(kartarkaur,manjitsingh).

    mother(kartarkaur,surjitkaur).

    11

  • 7/28/2019 Sllp File to Print

    12/43

    mother(kartarkaur,upkarsingh).

    mother(amarkaur,surinderkaur).

    mother(surinderkaur,harpreetsingh).

    mother(surinderkaur,jaspreetkaur).

    father(kartarsingh,manjitsingh).

    father(kartarsingh,surjitkaur).

    father(kartarsingh,upkarsingh).

    father(ratansingh,surinderkaur).

    father(manjitsingh,harpreetsingh).

    father(manjitsingh,jaspreetkaur).

    brother(X,Y) :- father(A,X),father(A,Y),male(X),XY.

    sister(X,Y) :- father(A,X),father(A,Y),female(X),XY.

    parents(X,Y,Z) :- father(X,Z), mother(Y,Z).

    gparents(P,Q,Z) :- parents(P,Q,A), father(A,Z).

    gparents(P,Q,Z) :- parents(P,Q,A), mother(A,Z).

    aunty(X,Y) :- father(A,Y),sister(X,A).

    aunty(X,Y) :- mother(A,Y),sister(X,A).

    uncle(X,Y) :- father(A,Y),brother(X,A).

    uncle(X,Y) :- mother(A,Y),brother(X,A).

    OUTPUT

    12

  • 7/28/2019 Sllp File to Print

    13/43

  • 7/28/2019 Sllp File to Print

    14/43

    Domains

    n=integer

    Predicates

    input(n)

    Clauses

    input(N):-N>0,write("No. is positive"),nl.

    input(N):-N

  • 7/28/2019 Sllp File to Print

    15/43

    Aim : Program to check whether given person is member ofclub or not

    Domains

    element = string

    list = element*

    Predicates

    member(element,list)

    Clauses

    member(X,[X|_]).

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

    OUTPUT

    Goal : member(david,[john,david])

    True

    Experiment No. -6

    15

  • 7/28/2019 Sllp File to Print

    16/43

    Aim : Program showing mapping i.e constructing new structure

    Domains

    element = string

    list = element*

    Predicates

    change(element,element)

    alter(list,list)

    Clauses

    change(you,i).

    change(are,am).

    change(my,your).

    change(X,X).

    alter([],[]).

    alter([H|T],[X|Y]) :- change(H,X),alter(T,Y).

    OUTPUT

    Goal : alter([you,are,my,friend],X)

    X=[I,am,your,friend]

    I solution

    16

  • 7/28/2019 Sllp File to Print

    17/43

    Experiment No. -7

    Aim : Program explaining the use or recursion with Factorial Program

    FACTORIAL

    domains

    element =integer

    predicates

    fact(element,element)

    clauses

    fact(0,1).

    fact(1,1).

    fact(N,R):- N>1,N1=N-1,fact(N1,Z),

    R=Z*N.

    OUTPUT

    Goal : fact(4,X)

    X=12

    1 solution

    Goal :

    17

  • 7/28/2019 Sllp File to Print

    18/43

  • 7/28/2019 Sllp File to Print

    19/43

  • 7/28/2019 Sllp File to Print

    20/43

    Experiment No. - 10

    Aim : Program to illustrate cut query

    domains

    element = integer

    list = element*

    predicates

    intersect(list,list,list)

    member(element,list)

    clauses

    intersect([],_,[]).

    intersect([H|T1],L,[H|T2]):-member(H,L),!,intersect(T1,L,T2).

    intersect([_|T1],L,T2):-intersect(T1,L,T2).

    member(H,[H|_]).

    member(H,[_|T]):-member(H,T).

    OUTPUT

    Goal : intersect([1,2,3,4],[3,4,5,6],X)

    X=[3,4]

    20

  • 7/28/2019 Sllp File to Print

    21/43

    1 solution

    Experiment No. -11

    Aim : Program to generate Fibonacci series

    domains

    n =integer

    predicates

    fib(n,n)

    clauses

    fib(0,0).

    fib(1,1).

    fib(N,R):- N>1,P=N-1,fib(P,Y),R=Y+N.

    OUTPUT

    Goal : fib(3,X)

    X=6

    1 solution

    Goal :

    21

  • 7/28/2019 Sllp File to Print

    22/43

    Experiment No. -12

    Aim : Program to concatenate two lists

    domains

    element = integer

    list = element*

    predicates

    conc(list,list,list)

    clauses

    conc([],L,L).

    conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).

    OUTPUT

    Goal : conc([1,2,3],[4,5,],X)

    X=[1,2,3,4,5]

    1 solution

    Goal :

    22

  • 7/28/2019 Sllp File to Print

    23/43

    Experiment No. -13

    Aim : Program to delete elements from list.

    domains

    element = integer

    list = element*

    predicates

    delete(element,list,list)

    clauses

    delete(X,[X|T],T).

    delete(X,[H|T],[H|T1]):-delete(X,T,T1).

    OUTPUT

    Goal : delete(2,[1,2,3,4,5],X)

    X=[1,3,4,5]

    1 solution

    23

  • 7/28/2019 Sllp File to Print

    24/43

    Experiment No. -14

    Aim : Program to reverse a list

    Domains

    element = integer

    list = element*

    predicates

    reverse(list,list)

    append(list,list,list)

    clauses

    reverse([],[]).

    reverse([H|T],L):-reverse(T,Z),append(Z,[H],L).

    append([],L,L).

    append([X|L1],L2,[X|L3]):-append(L1,L2,L3).

    24

  • 7/28/2019 Sllp File to Print

    25/43

    OUTPUT

    Goal :reverse([1,2,3,4],X)

    X=[4,3,2,1]

    1 solution

    Goal :

    Experiment No. -15

    Aim : Program to illustrate bubble sort

    Domains

    element = integer

    list = element*

    predicates

    bsort(list,list)

    swap(list,list)

    greater(element,element)

    clauses

    bsort(L,S):-swap(L,L1),!,bsort(L1,S).

    bsort(S,S).

    swap([X,Y|T],[Y,X|T]):-greater(X,Y).

    25

  • 7/28/2019 Sllp File to Print

    26/43

    swap([Z|T],[Z|T1]):-swap(T,T1).

    greater(X,Y):-X>Y.

    OUTPUT

    Goal : bsort([2,5,1,8,3],X)

    X=[1,2,3,5,8]

    1 Solution

    Experiment No. -16

    Aim : Program to illustrate insertion sort

    Domains

    element=integer

    list = element*

    Predicates

    insort(list,list)

    insorth(element,list,list)

    order(element,element)

    Clauses

    insort([],[]).

    insort([H|T],M) :- insort(T,N),insorth(H,N,M).

    26

  • 7/28/2019 Sllp File to Print

    27/43

    insorth(H,[A|T],[A|M]) :- order(A,H),!,insorth(H,T,M).

    insorth(H,T,[H|T]).

    order(P,Q):- P

  • 7/28/2019 Sllp File to Print

    28/43

  • 7/28/2019 Sllp File to Print

    29/43

  • 7/28/2019 Sllp File to Print

    30/43

    30

  • 7/28/2019 Sllp File to Print

    31/43

    31

  • 7/28/2019 Sllp File to Print

    32/43

    Experiment No. -20

    Develop rules to perform following arithmetic in PROLOG.

    1. Square of a number.

    2. Check whether number is +ve or ve.

    3. Check whether two numbers are equal.

    4. Check whether number lies in given range.

    Contents for above statement

    32

  • 7/28/2019 Sllp File to Print

    33/43

    33

  • 7/28/2019 Sllp File to Print

    34/43

    Experiment No. -21

    To find the sum of N numbers.

    Contents for above statement

    34

  • 7/28/2019 Sllp File to Print

    35/43

  • 7/28/2019 Sllp File to Print

    36/43

    Experiment No. -22

    Trace steps for resolving Towers Of Hanoi problems for N = 5.

    Contents for above statement

    36

  • 7/28/2019 Sllp File to Print

    37/43

    37

  • 7/28/2019 Sllp File to Print

    38/43

    Experiment No. -23

    Trace the stepwise behaviour of procedure to find maximum number in

    the following list:-

    [88, 66, 99, 7, 2, 1, 78, 97]

    SWI-PROLOG window for above statement

    38

  • 7/28/2019 Sllp File to Print

    39/43

  • 7/28/2019 Sllp File to Print

    40/43

  • 7/28/2019 Sllp File to Print

    41/43

    41

  • 7/28/2019 Sllp File to Print

    42/43

    Experiment No. -25

    Check whether a number is even or odd.

    Contents for above problem

    42

  • 7/28/2019 Sllp File to Print

    43/43