pro log file

Upload: gursimran-bedi

Post on 10-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Pro Log File

    1/36

    Mrinalini Rana CSE7th sem 685034228

    (Submitted in the partial fulfillment of B.TECH)S.B.B.S.I.E.T

    (Affiliated to PTU)

    Submitted To: - Submitted By:-Lect.Gurpreet Singh Mrinalini Rana

    685034228CSE (7th sem)

    S.B.B.S.I.E.T 1

  • 8/8/2019 Pro Log File

    2/36

    Mrinalini Rana CSE7th sem 685034228

    PRACTICAL NO.1:

    Aim: Study of propositional logic.

    In logic and mathematics, a propositional calculus or logic (also a sentential calculus) is aformal system in which formulae representingpropositions can be formed by combining

    atomic propositions using logical connectives, and a system of formal proof rules allows

    certain formulae to be established as theorems.

    Propositional logic, also known as sentential logic and statement logic, is the branch oflogic 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 oraltering statements. In propositional logic, the simplest statements are considered as

    indivisible units, and hence, propositional logic does not study those logical properties

    and relations that depend upon parts of statements that are not themselves statements ontheir own, such as the subject and predicate of a statement. The most thoroughly

    researched branch of propositional logic is classical truth-functional propositional logic,

    which studies logical operators and connectives that are used to produce complex

    statements whose truth-value depends entirely on the truth-values of the simplerstatements making them up, and in which it is assumed that every statement is either true

    or false and not both. However, there are other forms of propositional logic in which

    other truth-values are considered, or in which there is consideration of connectives thatare used to produce statements whose truth-values depend not simply on the truth-values

    of the parts, but additional things such as their necessity, possibility or relatedness to one

    another.

    Introduction:

    A statement can be defined as a declarative sentence, or part of a sentence, that is capable

    of having a truth-value, such as being true or false. So, for example, the following are

    statements:

    George W. Bush is the 43rd President of the United States.

    Paris is the capital of France.

    Everyone born on Monday has purple hair.

    Sometimes, a statement can contain one or more other statements as parts. Consider for

    example, the following statement:

    Either Ganymede is a moon of Jupiter or Ganymede is a moon of Saturn.

    S.B.B.S.I.E.T 2

    http://en.wikipedia.org/wiki/Logichttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Formal_systemhttp://en.wikipedia.org/wiki/Propositional_formulahttp://en.wikipedia.org/wiki/Atomic_formulahttp://en.wikipedia.org/wiki/Logical_connectivehttp://en.wikipedia.org/wiki/Theoremhttp://en.wikipedia.org/wiki/Logichttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Formal_systemhttp://en.wikipedia.org/wiki/Propositional_formulahttp://en.wikipedia.org/wiki/Atomic_formulahttp://en.wikipedia.org/wiki/Logical_connectivehttp://en.wikipedia.org/wiki/Theorem
  • 8/8/2019 Pro Log File

    3/36

    Mrinalini Rana CSE7th sem 685034228

    Joining two simpler propositions with the word and is one common way of combining

    statements. When two statements are joined together with and, the complex statementformed by them is true if and only if both the component statements are true. Because of

    this, an argument of the following form is logically valid:

    Paris is the capital of France and Paris has a population of over twomillion.Therefore, Paris has a population of over two million.

    The Language of Propositional Logic:

    The basic rules and principles of classical truth-functional propositional logic are, among

    contemporary logicians, almost entirely agreed upon, and capable of being stated in adefinitive way. This is most easily done if we utilize a simplified logical language that

    deals only with simple statements considered as indivisible units as well as complex

    statements joined together by means of truth-functional connectives. We first consider a

    language called PL for Propositional Logic. Later we shall consider two even simplerlanguages, PL and PL.

    a. Syntax and Formation Rules of PL

    In any ordinary language, a statement would never consist of a single word, but would

    always at the very least consist of a noun or pronoun along with a verb. However, because

    propositional logic does not consider smaller parts of statements, and treats simple

    statements as indivisible wholes, the language PL uses uppercase letters A, B, C,etc., in place of complete statements. The logical signs &, v, , , and are

    used in place of the truth-functional operators, and, or, if then, if and only

    if, and not, respectively. So, consider again the following example argument,mentioned in Section I.

    Paris is the capital of France and Paris has a population of over two

    million.

    Therefore, Paris has a population of over two million.

    Definition: A well-formed formula (hereafter abbreviated as wff) of PL is defined

    recursively as follows:

    1. Any statement letter is a well-formed formula.

    2. If is a well-formed formula, then so is .3. If and are well-formed formulas, then so is ( & ) .

    4. If and are well-formed formulas, then so is ( v ) .

    5. If and are well-formed formulas, then so is ( ) .

    6. If and are well-formed formulas, then so is ( ) .7. Nothing that cannot be constructed by successive steps of (1)-(6) is a well-formed

    formula.

    S.B.B.S.I.E.T 3

    http://www.iep.utm.edu/prop-log/#H1http://www.iep.utm.edu/prop-log/#H1
  • 8/8/2019 Pro Log File

    4/36

    Mrinalini Rana CSE7th sem 685034228

    b. Truth Functions and Truth Tables

    So far we have in effect described the grammar of language PL. When setting up a

    language fully, however, it is necessary not only to establish rules of grammar, but also

    describe the meanings of the symbols used in the language. We have already suggested

    that uppercase letters are used as complete simple statements. Because truth-functionalpropositional logic does not analyze the parts of simple statements, and only considers

    those ways of combining them to form more complicated statements that make the truth

    or falsity of the whole dependent entirely on the truth or falsity of the parts, in effect, itdoes not matter what meaning we assign to the individual statement letters like P, Q

    and R, etc., provided that each is taken as either true or false (and not both).

    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. Ineffect, the meaning of the operator & can be displayed according to the following chart,

    which shows the truth-value of the conjunction depending on the four possibilities of the

    truth-values of the parts:

    ( & )

    TT

    F

    F

    TF

    T

    F

    TF

    F

    F

    Disjunction: The disjunction of two statements and , written in PL as ( v ) , is true

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

    false. A chart similar to that given above for conjunction, modified for to show themeaning of the disjunction sign v instead, would be drawn as follows:

    ( v )

    T

    TF

    F

    T

    FT

    F

    T

    TT

    F

    Material Implication: 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). This truth-function generates the following chart:

    ( )

    TT

    F

    TF

    T

    TF

    T

    S.B.B.S.I.E.T 4

  • 8/8/2019 Pro Log File

    5/36

    Mrinalini Rana CSE7th sem 685034228

    F F T

    Material Equivalence: 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 true

    or both false, and is regarded as false if they have different truth-values. Hence, we have

    the following chart:

    ( )

    T

    T

    F

    F

    T

    F

    T

    F

    T

    F

    F

    T

    Negation: The negation of statement , simply written in language PL, is regarded astrue if is false, and false if is true. Unlike the other operators we have considered,

    negation is applied to a single statement. The corresponding chart can therefore be drawnmore simply as follows:

    TF

    FT

    c. Rules of Inference

    Here we give a list of intuitively valid rules of inference. The rules are stated in schematic

    form. Any inference in which any wff of language PL is substituted unformly for theschematic letters in the forms below constitutes an instance of the rule.

    Modus ponens (MP):

    (Modus ponens is sometimes also called modus ponendo ponens, detachment or a

    form of -elimination.)

    Modus tollens (MT):

    S.B.B.S.I.E.T 5

  • 8/8/2019 Pro Log File

    6/36

    Mrinalini Rana CSE7th sem 685034228

    (Modus tollens is sometimes also called modus tollendo tollens or a form of -

    elimination.)

    d. Semantics

    An interpretation of a first-order language assigns a denotation to all non-logical

    constants in that language. It also determines a domain of discourse that specifies therange of the quantifiers. The result is that each term is assigned an object that it

    represents, and each sentence is assigned a truth value. In this way, an interpretation

    provides semantic meaning to the terms and formulas of the language. The study of theinterpretations of formal languages is called formal semantics.

    S.B.B.S.I.E.T 6

    http://en.wikipedia.org/wiki/Interpretation_(logic)http://en.wikipedia.org/wiki/Interpretation_(logic)http://en.wikipedia.org/wiki/Domain_of_discoursehttp://en.wikipedia.org/wiki/Formal_semanticshttp://en.wikipedia.org/wiki/Interpretation_(logic)http://en.wikipedia.org/wiki/Domain_of_discoursehttp://en.wikipedia.org/wiki/Formal_semantics
  • 8/8/2019 Pro Log File

    7/36

    Mrinalini Rana CSE7th sem 685034228

    PRACTICAL NO.2:

    Aim: Study of First Order Predicate Logic.

    First-order logic is a formal logic used in mathematics, philosophy, linguistics, and

    computer science. It goes by many names, including: first-order predicate calculus, the

    lower predicate calculus, and predicate logic. First-order logic is distinguished frompropositional logic by its use of quantifiers; each interpretation of first-order logic

    includes a domain of discourse over which the quantifiers range.There are many

    deductive systems for first-order logic that are sound (only deriving correct results) andcomplete (able to derive any logically valid implication). Although the logical

    consequence relation is only semidecidable, much progress has been made in automated

    theorem proving in first-order logic.

    Afirst-order theory consists of a set ofaxioms in a particular first-order signature. Theset of axioms is often finite or recursively enumerable, in which case the theory is called

    effective. Some authors require theories to also include all logical consequences of the

    axioms.

    A first-order structure that satisfies all sentences in a given theory is said to be a model ofthe theory. Anelementary class is the set of all structures satisfying a particular theory.

    These classes are a main subject of study in model theory.

    First-order logic permits reasoning about the propositional connectives (as in propositional logic) and also about quantification ("all" or "some"). A classic, if

    elementary, example of what can be done with the predicate logic is the inference from

    the premises:

    All men are mortal.

    Socrates is a man. to the conclusion

    Quantifiers:

    Universal quantification corresponds to conjunction ("and") in that (Ax)P(x) means that

    P holds for all values ofx in the domain associated with that variable.

    E.g., (Ax) dolphin(x) => mammal(x)

    S.B.B.S.I.E.T 7

    http://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Philosophyhttp://en.wikipedia.org/wiki/Linguisticshttp://en.wikipedia.org/wiki/Computer_sciencehttp://en.wikipedia.org/wiki/Predicate_logichttp://en.wikipedia.org/wiki/Propositional_logichttp://en.wikipedia.org/wiki/Domain_of_discoursehttp://en.wikipedia.org/wiki/Deductive_systemhttp://en.wikipedia.org/wiki/Logical_validityhttp://en.wikipedia.org/wiki/Semidecidabilityhttp://en.wikipedia.org/wiki/Automated_theorem_provinghttp://en.wikipedia.org/wiki/Automated_theorem_provinghttp://en.wikipedia.org/wiki/First-order_theoryhttp://en.wikipedia.org/wiki/First-order_theoryhttp://en.wikipedia.org/wiki/Axiomhttp://en.wikipedia.org/wiki/Recursively_enumerablehttp://en.wikipedia.org/wiki/Elementary_classhttp://en.wikipedia.org/wiki/Elementary_classhttp://en.wikipedia.org/wiki/Model_theoryhttp://www.rbjones.com/rbjpub/logic/log003.htmhttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Philosophyhttp://en.wikipedia.org/wiki/Linguisticshttp://en.wikipedia.org/wiki/Computer_sciencehttp://en.wikipedia.org/wiki/Predicate_logichttp://en.wikipedia.org/wiki/Propositional_logichttp://en.wikipedia.org/wiki/Domain_of_discoursehttp://en.wikipedia.org/wiki/Deductive_systemhttp://en.wikipedia.org/wiki/Logical_validityhttp://en.wikipedia.org/wiki/Semidecidabilityhttp://en.wikipedia.org/wiki/Automated_theorem_provinghttp://en.wikipedia.org/wiki/Automated_theorem_provinghttp://en.wikipedia.org/wiki/First-order_theoryhttp://en.wikipedia.org/wiki/Axiomhttp://en.wikipedia.org/wiki/Recursively_enumerablehttp://en.wikipedia.org/wiki/Elementary_classhttp://en.wikipedia.org/wiki/Model_theoryhttp://www.rbjones.com/rbjpub/logic/log003.htm
  • 8/8/2019 Pro Log File

    8/36

    Mrinalini Rana CSE7th sem 685034228

    Existential quantification corresponds to disjunction ("or") in that (Ex)P(x) means that Pholds for some value ofx in the domain associated with that variable.E.g., (Ex) mammal(x) ^ lays-eggs(x)

    Universal quantifiers are usually used with "implies" to form "if-then rules."

    E.g., (Ax) cs15-381-student(x) => smart(x) means "All cs15-381 students are smart." You rarely use universal quantification to make blanket statements about every

    individual in the world: (Ax)cs15-381-student(x) ^ smart(x) meaning that everyone in

    the world is a cs15-381 student and is smart.Existential quantifiers are usually used with "and" to specify a list of properties or facts

    about an individual.

    E.g., (Ex) cs15-381-student(x) ^ smart(x) means "there is a cs15-381 student who issmart."

    A common mistake is to represent this English sentence as the FOL sentence: (Ex) cs15-381-student(x) => smart(x)

    Switching the order of universal quantifiers does not change the meaning: (Ax)(Ay)P(x,y) is logically equivalent to (Ay)(Ax)P(x,y). Similarly, you can switch the order

    of existential quantifiers.

    Switching the order of universals and existentials does change meaning:

    Everyone likes someone: (Ax)(Ey)likes(x,y)Someone is liked by everyone: (Ey)(Ax)likes(x,y)

    First-Order Logic (FOL) Syntax:

    Sentences are built up of terms and atoms:A term (denoting a real-world object) is a constant symbol, a variable symbol, or a

    function e.g. left-leg-of ( ). For example, x andf(x1, ..., xn) are terms, where eachxi is aterm.

    An atom (which has value true or false) is either an n-place predicate of n terms, or, if Pand Q are atoms, then ~P, P V Q, P ^ Q, P => Q, P Q are atomsA sentence is an atom, or, ifP is a sentence and x is a variable, then (Ax)P and (Ex)Pare sentences

    A well-formed formula (wff) is a sentence containing no "free" variables. I.e., all

    variables are "bound" by universal or existential quantifiers.

    E.g., (Ax)P(x,y) has x bound as a universally quantified variable, but y is free.

    Translating English to FOL:

    Every gardener likes the sun. (Ax) gardener(x) => likes(x,Sun)

    S.B.B.S.I.E.T 8

  • 8/8/2019 Pro Log File

    9/36

    Mrinalini Rana CSE7th sem 685034228

    You can fool some of the people all of the time. (Ex)(At) (person(x) ^ time(t)) => can-

    fool(x,t)

    You can fool all of the people some of the time. (Ax)(Et) (person(x) ^ time(t) => can-

    fool(x,t)

    All purple mushrooms are poisonous. (Ax) (mushroom(x) ^ purple(x)) =>

    poisonous(x)

    No purple mushroom is poisonous. ~(Ex) purple(x) ^ mushroom(x) ^ poisonous(x)or, equivalently, (Ax) (mushroom(x) ^ purple(x)) => ~poisonous(x)

    There are exactly two purple mushrooms. (Ex)(Ey) mushroom(x) ^ purple(x) ^

    mushroom(y) ^ purple(y) ^ ~(x=y) ^ (Az) (mushroom(z) ^ purple(z)) => ((x=z) v

    (y=z))

    Deb is not tall. ~tall(Deb)

    X is above Y if X is on directly on top of Y or else there is a pile of one or more otherobjects directly on top of one another starting with X and ending with Y. (Ax)(Ay)above(x,y) (on(x,y) v (Ez) (on(x,z) ^ above(z,y)))

    Inference Rules for FOL:

    Inference rules for PL apply to FOL as well. For example, Modus Ponens, And-Introduction, And-Elimination, etc.

    New sound inference rules for use with quantifiers:

    UniversalEliminationIf (Ax)P(x) is true, then P(c) is true, where c is a constant in the domain of x. For

    example, from (Ax)eats(Ziggy, x) we can infereats(Ziggy, IceCream).The variable symbol can be replaced by any ground term, i.e., any constant symbol or

    function symbol applied to ground terms only.

    ExistentialIntroductionIfP(c) is true, then (Ex)P(x) is inferred.

    For example, from eats(Ziggy, IceCream) we can infer(Ex)eats(Ziggy, x).All instances of the given constant symbol are replaced by the new variable symbol.

    Note that the variable symbol cannot already exist anywhere in the expression.ExistentialEliminationFrom (Ex)P(x) inferP(c).

    For example, from (Ex)eats(Ziggy, x) infereats(Ziggy, Cheese).Note that the variable is replaced by a brand new constant that does not occur in this or

    any othersentence in the Knowledge Base. In other words, we don't want to accidentally

    S.B.B.S.I.E.T 9

  • 8/8/2019 Pro Log File

    10/36

    Mrinalini Rana CSE7th sem 685034228

    draw other inferences about it by introducing the constant. All we know is there must besome constant that makes this true, so we can introduce a brand new one to stand in forthat (unknown) constant.

    Semantics:

    An interpretation of a first-order language assigns a denotation to all non-logical

    constants in that language. It also determines a domain of discourse that specifies the

    range of the quantifiers. The result is that each term is assigned an object that itrepresents, and each sentence is assigned a truth value. In this way, an interpretation

    provides semantic meaning to the terms and formulas of the language. The study of the

    interpretations of formal languages is called formal semantics.

    The domain of discourse D is a nonempty set of "objects" of some kind. Intuitively, a

    first-order formula is a statement about these objects; for example, states theexistence of an objectx such that the predicatePis true where referred to it. The domain

    of discourse is the set of considered objects. For example, one can take D to be the set ofinteger numbers.

    Evaluation of truth values:

    A formula evaluates to true or false given an interpretation, and a variable assignment

    that associates an element of the domain of discourse with each variable. The reason that

    a variable assignment is required is to give meanings to formulas with free variables, such

    asy =x. The truth value of this formula changes depending on whetherx and y denotethe same individual.

    First, the variable assignment can be extended to all terms of the language, with theresult that each term maps to a single element of the domain of discourse. The following

    rules are used to make this assignment:

    1. Variables. Each variablex evaluates to (x)

    2. Functions. Given terms that have been evaluated to elements

    of the domain of discourse, and a n-ary function symbol f, the term

    evaluates to .

    Next, each formula is assigned a truth value. The inductive definition used to make this

    assignment is called the T-schema.

    1. Atomic formulas (1). A formula is associated the value true or

    false depending on whether , where are the

    evaluation of the terms and I(P) is the interpretation ofP, which by

    assumption is a subset ofDn.

    S.B.B.S.I.E.T 10

    http://en.wikipedia.org/wiki/Interpretation_(logic)http://en.wikipedia.org/wiki/Interpretation_(logic)http://en.wikipedia.org/wiki/Domain_of_discoursehttp://en.wikipedia.org/wiki/Formal_semanticshttp://en.wikipedia.org/wiki/T-schemahttp://en.wikipedia.org/wiki/Interpretation_(logic)http://en.wikipedia.org/wiki/Domain_of_discoursehttp://en.wikipedia.org/wiki/Formal_semanticshttp://en.wikipedia.org/wiki/T-schema
  • 8/8/2019 Pro Log File

    11/36

    Mrinalini Rana CSE7th sem 685034228

    2. Atomic formulas (2). A formula t1 = t2 is assigned true ift1 and t2 evaluate to the

    same object of the domain of discourse (see the section on equality below).

    3. Logical connectives. A formula in the form , , etc. is evaluated

    according to the truth table for the connective in question, as in propositional

    logic.

    4. Existential quantifiers. A formula is true according to Mand if thereexists an evaluation ' of the variables that only differs from regarding the

    evaluation ofx and such that is true according to the interpretation Mand the

    variable assignment '. This formal definition captures the idea that istrue if and only if there is a way to choose a value forx such that (x) is satisfied.

    5. Universal quantifiers. A formula is true according to Mand if (x) is

    true for every pair composed by the interpretation M and some variable

    assignment ' that differs from only on the value ofx. This captures the idea that

    is true if every possible choice of a value forx causes (x) to be true.

    If a formula does not contain free variables, and so is a sentence, then the initial variableassignment does not affect its truth value. In other words, a sentence is true according toMand if and only if is true according to Mand any other variable assignment '.

    There is a second common approach to defining truth values that does not rely on variable

    assignment functions. Instead, given an interpretation M, one first adds to the signature a

    collection of constant symbols, one for each element of the domain of discourse in M; say

    that for each d in the domain the constant symbol cd is fixed. The interpretation isextended so that each new constant symbol is assigned to its corresponding element of the

    domain. One now defines truth for quantified formulas syntactically, as follows:

    1. Existential quantifiers (alternate). A formula is true according to Mif

    there is some din the domain of discourse such that (cd) holds. Here (cd) is theresult of substituting cd for every free occurrence ofx in .

    2. Universal quantifiers (alternate). A formula is true according to M if,

    for every din the domain of discourse, (cd) is true according to M.

    This alternate approach gives exactly the same truth values to all sentences as theapproach via variable assignments.

    S.B.B.S.I.E.T 11

    http://en.wikipedia.org/wiki/Truth_tablehttp://en.wikipedia.org/wiki/Truth_table
  • 8/8/2019 Pro Log File

    12/36

    Mrinalini Rana CSE7th sem 685034228

    PRACTICAL NO.3:

    3.1 AIM: Introduction to PROLOG programming.

    Prolog is a logic programming general purpose fifth generation language associated with

    artificial intelligence and computational linguistics. It has a purely logical subset, called

    "pure Prolog", as well as a number of extralogical features.

    Prolog has its roots in formal logic, and unlike many other programming languages,Prolog is declarative: The program logic is expressed in terms of relations, and execution

    is triggered by running queries over these relations. Relations and queries are constructed

    using Prolog's single data type, the term. Relations are defined by clauses. Given a query,

    the Prolog engine attempts to find a resolution refutation of the negated query. If thenegated query can be refuted, i.e., an instantiation for all free variables is found that

    makes the union of clauses and the singleton set consisting of the negated query false, it

    follows that the original query, with the found instantiation applied, is a logicalconsequence of the program. This makes Prolog (and other logic programming

    languages) particularly useful for database, symbolic mathematics, and language parsing

    applications. Because Prolog allows impurepredicates, checking the truth value of certainspecial predicates may have some deliberate side effect, such as printing a value to the

    S.B.B.S.I.E.T 12

    http://en.wikipedia.org/wiki/Logic_programminghttp://en.wikipedia.org/wiki/Artificial_intelligencehttp://en.wikipedia.org/wiki/Computational_linguisticshttp://en.wikipedia.org/wiki/Formal_logichttp://en.wikipedia.org/wiki/Formal_logichttp://en.wikipedia.org/wiki/Declarative_programminghttp://en.wikipedia.org/wiki/Resolution_(logic)http://en.wikipedia.org/wiki/Refutationhttp://en.wikipedia.org/wiki/Predicateshttp://en.wikipedia.org/wiki/Logic_programminghttp://en.wikipedia.org/wiki/Artificial_intelligencehttp://en.wikipedia.org/wiki/Computational_linguisticshttp://en.wikipedia.org/wiki/Formal_logichttp://en.wikipedia.org/wiki/Declarative_programminghttp://en.wikipedia.org/wiki/Resolution_(logic)http://en.wikipedia.org/wiki/Refutationhttp://en.wikipedia.org/wiki/Predicates
  • 8/8/2019 Pro Log File

    13/36

    Mrinalini Rana CSE7th sem 685034228

    screen. This permits the programmer to use some amount of conventional imperative

    programming when the logical paradigm is inconvenient.

    What is a Prolog program?

    Programming in Prolog is very different from programming in a traditionalproceduralprocedural language like Pascal. In Prolog you don't say how the program will

    work.Prolog can be separated in two parts:

    The Program

    The program, sometimes called Database is a texte file (*.pl) that contain the facts and

    rules that will be used by the user of the program. It contains all the relations that make

    this program.

    The Query

    When you launch a program you are in query modequery mode. This mode is represented

    by the sign? - At the begining of the line. In query mode you ask questions about relations

    described in the program.

    Loading a program:

    Loading First you have to launch your Prolog compiler, for this report we used the SWI-Prolog wich is a freeware (if you want to know more about SWI-Prolog go to this page).

    When Prolog is launched the? - should appear meaning you are in query mode. The

    manier to launch a program depends of your compiler. For SWI-Prolog you can load aprogram by typing the cammand [file]. When the file of your program is file.pl. If you

    compiler is not SWI-Prolog you can also try the command reconsult(file). When you have

    done this you can use all the facts and rules that are contained in the program. Now let's

    begin to see what a fact...is

    S.B.B.S.I.E.T 13

    http://en.wikipedia.org/wiki/Imperative_programminghttp://en.wikipedia.org/wiki/Imperative_programminghttp://www.mars-attacks.org/~boklm/prolog/page_10.htmlhttp://en.wikipedia.org/wiki/Imperative_programminghttp://en.wikipedia.org/wiki/Imperative_programminghttp://www.mars-attacks.org/~boklm/prolog/page_10.html
  • 8/8/2019 Pro Log File

    14/36

    Mrinalini Rana CSE7th sem 685034228

    3.2 AIM: Introduction to Facts & Rules.

    Simple facts:

    In Prolog we can make some statements by using facts. Facts either consist of a particularitem or a relation between items. For example we can represent the fact that it is sunny by

    writing the program :

    sunny.We can now ask a query of Prolog by asking

    ?- sunny.

    ?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true because(from above) Prolog matches it in its database of facts.

    Facts have some simple rules of syntax. Facts should always begin with a lowercase letter

    and end with a full stop. The facts themselves can consist of any letter or number

    combination, as well as the underscore _ character. However, names containing thecharacters -,+,*,/, or other mathematical operators should be avoided.

    Facts with arguments:

    S.B.B.S.I.E.T 14

  • 8/8/2019 Pro Log File

    15/36

    Mrinalini Rana CSE7th sem 685034228

    More complicated facts consist of a relation and the items that this refers to. These items

    are called arguments. Facts can have arbitrary number of arguments from zero upwards.A general model is shown below:

    relation(,,...., ).

    The arguments can be any legal Prolog term. The basic Prolog terms are an integer, an

    atom, a variable or a structure. Various Prolog implementation enhance this basic list withother data types, such as floating point numbers, or strings.

    Exemple :

    likes(john,mary).In the above fact john and mary are two atomes. Atoms are usally made from letters and

    digits with lowercase characters. The underscore (_) can also be used to separe 2 words

    but is not allowed as the first charactere. Atoms can also be legally made from symbols.The followings are legal atoms :atoms

    hello

    zz42

    two_words

    ====>The followings are not legal atoms :

    Hello4hello

    _Hello

    two wordstwo-words

    You can use single quotes to make any character combination a legal atom.

    'two words''UpperCase'

    '12444'

    'etc...'

    The fact likes(john,mary). say that there is a relation between john and mary. It can beread as either john likes mary or mary likes john. This reversibility can be very useful to

    the programmer, however it can also be a source of mistakes. You have to be clear on

    how you intend to interpret the relation. The number of arguments is the arity of thepredicate. A predicate with 2 arguments will be called by predicate_name/2. You can

    have differents predicats with the same name if they have a different arity.

    Rules:

    Consider the following sentence : 'All men are mortal' We can express this thing in Prologby :

    mortal(X) :- human(X).

    The clause can be read as 'X is mortal if X is human'.

    To continue with this example, let us define the fact that Socrate is a human. Our programwill be :

    mortal(X) :- human(X).

    S.B.B.S.I.E.T 15

  • 8/8/2019 Pro Log File

    16/36

    Mrinalini Rana CSE7th sem 685034228

    human(socrate).Now if we ask to prolog :

    ?- mortal(socrate).

    Prolog will respond :

    yesIn order to solve the query -? mortal(socrates). Prolog will use the rule we have given. It

    says that in order to prove that someone is mortal we can prove that he is human. So from

    the goal mortal(socrate) Prolog generate the subgoal human(socrate).We can still use variables. For example we might want to know who is mortal :

    ?- mortal(X).

    Then Prolog should respond :P=socrate

    This means that Prolog was able to succed the goal by unifying the variable X to socrates.

    Again this was done by using the subgoal human(X).

    Sometimes we may wish to specify alternatives ways to provre something. We can do this

    by using differents rules ands facts with the same name. For exeample, we can representthe sentence 'Something is fun if it is a PC running UNIX or am old amiga or an ice

    cream' with the following program :fun(X) :- /* something is fun if */

    pc(X), /* it is a pc and */

    unix(X). /* it is running unix */fun(X) :- /* or it is fun if */

    old(X), /* it is old and */

    amiga(X). /* it is an amiga */fun(ice_cream). /* the ice_crean is also fun */

    This program says that there are three ways to know if an object is fun or not. Like for

    pure facts, Prolog will start from the first clause (a clause can be a rule or a fact) of funand try it. If that does not succed Prolog will try the next clause. If there is no more

    clauses then it fails and Prolog responds ' no '. We can also see in this example that the

    'and' is represented by a ',' and the 'or' by differents clause. If needed the 'or' can also berepresebted by ';'. In the previous examples when we was asking eats(fred,What) and

    pressing the key ';' to see the following results we was in fact asking 'or'.

    All identically-named variables in a rule (for example X in the last rule we've seem) areof course considered as one unic variable and must have the same instantiation for each

    solution in a particular query. Identical variables names in differents rules are

    considerated as differents variables and are totally independent, this is the same as if we

    had used differents names.The following program :

    fun(X) :-

    pc(X),unix(X).

    fun(X) :-old(X),

    amiga(X).

    S.B.B.S.I.E.T 16

  • 8/8/2019 Pro Log File

    17/36

    Mrinalini Rana CSE7th sem 685034228

    Will be seen by Prolog as :

    fun(X_1) :-pc(X_1),

    unix(X_1).

    fun(X_2) :-old(X_2),

    amiga(X_2).

    How to add a rule with a program?

    It is possible to add new rules or facts with the instruction Assert (fact1) which will addthe fact called fact1. The predicates added with this command are considereted like any

    other in the source of the program.

    The instructions

    The usefull instructions are :assert(c). Add the rule c in the database.assert()

    retract(c). Remove the c from the database.retract(c)

    asserta(c). Add c at the beginning of the database.asserta()assertz(c). Add c at the end of the database.assertz()

    Example

    ?- sunny.

    no.

    ?- assert(sunny).

    yes.

    ?- sunny.

    yes

    ?- retract(sunny).

    yes.

    ?- sunny.

    no.

    How to query:

    Once you have entered the facts in a program you can ask prolog about it. An exemple

    programm can be :

    eats(fred,oranges). /* 'Fred eats oranges' */

    S.B.B.S.I.E.T 17

  • 8/8/2019 Pro Log File

    18/36

    Mrinalini Rana CSE7th sem 685034228

    eats(tony,apple). /* 'Tony eats apple' */

    eats(john,apple). /* 'John eats apple' */

    If we now ask some queries we would get the followings things :

    ?- eats(fred,oranges).

    /* does this match anything in the database? */

    yes

    /* yes, that matchs the first clause */

    ?- eats(john,apple).

    yes

    ?- eats(mike,apple).

    no/* there is no relation between mike and apple */

    3.3 AIM: Introduction to some Built-in Predicates.

    This file is a list of the predicates that are built in to our prolog.

    1. Append:

    The built-in predicate append(?List1, ?List2, ?List1_then_List2) succeeds if List1

    followed by List2 = List1_then_List2. Examples:

    ?- append([a, b], [c, d, e], Result).

    Result = [a, b, c, d, e].true.

    ?- append([a, b], SecondBit, [a, b, c, d, e]).SecondBit = [c, d, e]

    true.

    ?- append(FirstBit, [c, d, e], [a, b, c, d, e]).FirstBit = [a, b]

    true.

    2. Atom_charsThe built-in Prolog predicate atom_chars can convert an atom into the list of its

    constituent letters, or vice-versa. A fairly broad concept of atom is used: this predicatewill glue together (or split up) any reasonable characters you give it. A possible list would

    be to put together a list of letters read, one character at a time, to make a word - that is, anatom whose name is the word. Examples:

    ?- atom_chars(pizza, List).

    List = [p, i, z, z, a]

    ?- atom_chars(Atom, [b, e, e, r]).

    S.B.B.S.I.E.T 18

    http://www.cse.unsw.edu.au/~billw/#atomhttp://www.cse.unsw.edu.au/~billw/#atom
  • 8/8/2019 Pro Log File

    19/36

    Mrinalini Rana CSE7th sem 685034228

    Atom = beer

    ?- atom_chars(2007, List).

    List = ['2', '0', '0', '7']

    ?- atom_chars(Atom, ['[', '3', ' ', ',', '4', ']']).Atom = '[3 ,4]'

    3. atom_codes :The built-in Prolog predicate atom_codes can convert an atom into the list of the numeric

    codes used internally to represent the characters in the atom, or vice-versa. Examples:

    ?- atom_codes(pizza, List).List = [112, 105, 122, 122, 97]

    ?- atom_codes(Atom, [98, 101, 101, 114]).Atom = beer

    4. Member:Here is the definition of the built-in Prolog predicate member:

    member(X, [X | Rest]). % X is a member if its the first element

    member(X, [Y | Rest]) :-

    member(X, Rest). % otherwise, check if X is in the RestYou may not think of member as a backtracking predicate, but backtracking is built into

    Prolog, so in suitable circumstances, member will backtrack:

    ?- member(X, [a, b, c]).

    X = a ;

    X = b ;X = c ;

    fail.

    Here member backtracks to find every possible solution to the query given to it. Consideralso:

    ?- member(X, [a, a, a]).

    X = a ;X = a ;

    X = a ;

    fail.

    Here member backtracks even though it keeps on finding the same answer. What about

    ?- member(a, [a, a, a]).

    true ;

    true ;

    true ;

    fail.

    S.B.B.S.I.E.T 19

    http://www.cse.unsw.edu.au/~billw/#atomhttp://www.cse.unsw.edu.au/~billw/#atom
  • 8/8/2019 Pro Log File

    20/36

    Mrinalini Rana CSE7th sem 685034228

    5. Fail :Built-in Prolog predicate with no arguments, which, as the name suggests, always fails.Useful for forcing backtracking and in various other contexts.

    6. Findall :

    The built-in predicate findall(+Template, +Goal, -List) is used to collect a list List of allthe items Template that satisfy some goal Goal.

    Example: assume

    likes(mary, pizza).likes(marco, pizza).

    likes(Human, pizza) :- italian(Human).

    italian(marco).

    Then

    ?- findall(Person, likes(Person, pizza), Bag).

    Person = _G180List = [mary, marco, marco]

    findall succeeds and binds List to the empty list, if Goal has no solutions. This can beconvenient if you don't want your goal to fail just because the collection of solutions is

    empty. (In other cases, you wouldwant the goal to fail if there are no solutions.)

    7. Functor :In Prolog, the word functor is used to refer to the atom at the start of a structure. Forexample, in likes (mary, pizza), likes is the functor. In a more complex structure, like

    persondata(name(smith, john), date(28, feb, 1963))

    the top-level functor is termed the principal functor- in this case persondata - There is

    also a built-in predicate called functor, used to extract the functor and arity of a structure.

    There is also a built-in predicate functor with three arguments: functor(Term, Functor,Arity), which succeeds if Term is a term with functor Functor and arity Arity. Examples:

    ?- functor(likes(mary, pizza), Functor, Arity).

    Functor = likes

    Arity = 2

    ?- functor(likes(X, Y), Functor, Arity).

    X = _G180Y = _G181

    Functor = likes

    Arity = 2

    ?- functor(likes, Functor, Arity).

    S.B.B.S.I.E.T 20

    http://www.cse.unsw.edu.au/~billw/#structurehttp://www.cse.unsw.edu.au/~billw/#arityhttp://www.cse.unsw.edu.au/~billw/#termhttp://www.cse.unsw.edu.au/~billw/#arityhttp://www.cse.unsw.edu.au/~billw/#structurehttp://www.cse.unsw.edu.au/~billw/#arityhttp://www.cse.unsw.edu.au/~billw/#termhttp://www.cse.unsw.edu.au/~billw/#arity
  • 8/8/2019 Pro Log File

    21/36

    Mrinalini Rana CSE7th sem 685034228

    Functor = likes

    Arity = 0

    ?- functor(X, likes, 2).

    X = likes(_G232, _G233)

    8. if-then-else, -> :The built-in infix predicate -> ; functions as an if then else facility.

    Example:

    min(A, B, Min) :- A < B -> Min = A ; Min = B.

    This version of min (which, like the one below, assumes that A and B are numbers) says"if A < B then unify Min with A otherwise unify Min with B". Possibly it is easier to

    understand a two-rule version of min:

    min(A, B, A) :- A

  • 8/8/2019 Pro Log File

    22/36

    Mrinalini Rana CSE7th sem 685034228

    We have expressed this here using variable, but this need not be so, for example, we

    could write [1, 2, and 3] as:

    [1 | [2, 3]]

    [1 | Rest], where Rest is bound to [2, 3]

    [First | [2, 3]], where First is bound to 1 [First | Rest], where First is bound to 1, and Rest is bound to [2, 3]

    [1, 2 | [3]]

    [1, 2, 3 | []]

    and many more possibilities.

    You should always write your Prolog list in the most compact reasonable format. So forexample, while [X | []] is the same list as [X], the second version is much easier to read,

    so you should use it.

    Lists can also be expressed using a normal term syntax, using the built-in predicate name. - that is, a full stop or period. In this case, the empty list atom ([]) must be used toterminate the list. However, this approach is more cumbersome, and in practice people

    use the [1, 2, 3]- style syntax. Example:

    ?- X = .(1, .(2, .(3, []))).

    X = [1, 2, 3]

    12. Once :The built-in Prolog extra-logical predicate once takes a single argument, which must be a"callable term" - one that makes sense as a goal - e.g. happy(X) makes sense as a goal, but

    23 does not - and calls the term in such a way as to produces just one solution. It isdefined as:once(P) :- P, !.

    13. op , infix, prefix, and postfix operators, precedence in Prolog :Syntax:

    : - op (+Precedence, +Type, Name).

    The Prolog built-in predicate op serves to define the Type and Precedence of infix andpostfix, and prefix operators in Prolog terms. Prolog terms normal begin with the functor

    (e.g. likes, in likes(mary, pizza)) but exceptions exist - for example, arithmetic

    expressions are written in the usual infix way (i.e. as X + 1, rather than +(X, 1)), andnegation can be written without parentheses, as a prefix operator: not P.

    The table below lists the predefined infix operators in SWI-Prolog. You may wish to add

    infix operators of your own. For example, you might wish to define an infix and. This canbe done as follows:

    : - op (700, xfy, and).

    S.B.B.S.I.E.T 22

  • 8/8/2019 Pro Log File

    23/36

    Mrinalini Rana CSE7th sem 685034228

    14. Repeat :The built-in predicate repeat behaves as if defined by:repeat.

    repeat :- repeat.

    Thus repeat succeeds when first called, thanks to the first clause. If the Prolog interpretersubsequently backtracks, the second clause (repeat :- repeat.) is tried. This initiates a new

    call to repeat, which succeeds via the first clause, and so on.

    15. Retract :Retract is a built-in meta-predicate used to remove facts or rules from the Prolog database

    while a program is executing. It is most often used in partnership withassert (or one of itsrelatives). For example, your program might have hypothesised that some fact or rule is

    correct, added it to the Prolog database using assert (or one of its relatives), then your

    program explores the consequences of that assumption, and concludes that the fact or rulewas wrong. So then the program retracts the fact or rule.

    More prosaically, you might simply have a query that runs repeatedly during a single

    prolog session, discovers some new facts in each run, but needs to get rid of them at the

    start of the next query. So again, retract can be used to clean the discovered facts out ofthe database.

    ?- assert(likes(mary, pizza)).

    true.

    ?- likes(mary, pizza).true.

    ?- retract(likes(mary, pizza)).

    true.?- likes(mary, pizza).

    fail.

    ?- assert((happy(X) :- rich(X), famous(X))).X = _G180

    true.

    ?- retract((happy(X) :- rich(X), famous(X))).

    X = _G180true.

    Don't worry about the X = _G180, that's just SWI Prolog renaming the variable X with a

    unique name so it doesn't get confused with the (different) variable X that you might have

    used in some other rule. Note also the extra pair of parentheses () around the rule, asopposed to the fact.

    16. Retractall:What a call to retract actually does is to remove the first fact or rule that matches the

    argument to retract. If you want to remove, say, all the facts relating to likes with twoarguments, it looks as though you might have to call retract repeatedly. Never fear,

    S.B.B.S.I.E.T 23

    http://www.cse.unsw.edu.au/~billw/#asserthttp://www.cse.unsw.edu.au/~billw/#asserthttp://www.cse.unsw.edu.au/~billw/#asserthttp://www.cse.unsw.edu.au/~billw/#asserthttp://www.cse.unsw.edu.au/~billw/#assert
  • 8/8/2019 Pro Log File

    24/36

    Mrinalini Rana CSE7th sem 685034228

    retractall is here! This meta-predicate, as its name suggests, retracts allfacts or rules that

    match its single argument. For example:?- assert(likes(mary, pizza)), assert(likes(john, beer)).

    true.

    ?- listing(likes).

    :- dynamic likes/2.likes(mary, pizza).

    likes(john, beer).

    The ":- dynamic/2" tells us that likes/2 is a built-in predicate that can be modified duringprogram execution (see dynamic). This is to stop the program modifying unauthorised

    parts of itself, and becoming totally un-debuggable. Example continues:

    ?- retractall(likes(X, Y)).X = _G180

    Y = _G181

    ?- listing(likes).

    :- dynamic likes/2.

    true.

    17. Setof :The built-in Prolog predicate setof(+Template, +Goal, -Set) binds Set to the list of allinstances of Template satisfying the goal Goal.

    For example, given the facts and rule:

    happy(fido).happy(harry).

    happy(X) :- rich(X).

    rich(harry).

    it follows that

    ?- setof(Y, happy(Y), Set).

    Y = _G180

    Set = [fido, harry] ;

    fail.

    18. True:Built-in Prolog predicate with no arguments, which, as the name suggests, alwayssucceeds

    19. Arithmetic:Many of the usual arithmetic operators are available in Prolog:

    Operator Meaning Example

    + addition 2 is 1 + 1.

    subtraction 1 is 2 1.

    unary minus Try the query X is 1, Y is - X.

    S.B.B.S.I.E.T 24

    http://www.cse.unsw.edu.au/~billw/#dynamichttp://www.cse.unsw.edu.au/~billw/#dynamic
  • 8/8/2019 Pro Log File

    25/36

    Mrinalini Rana CSE7th sem 685034228

    * multiplication 4 is 2 * 2.

    / division 2 is 6 / 3.// integer division 1 is 7 // 4.

    mod integer remainder 3 is 7 mod 4.

    ** exponentiation 1.21 is 1.1 ** 2.

    Except in the context of an arithmetic comparison operator, arithmetic expressions need

    to be explicitly evaluatedin Prolog, using the is built-in predicate.

    Mostly one uses these operators in a goal more like this:1. X = Y + 2

    where Y is a variable already bound to a numeric value, or perhaps a arithmetic

    comparison goal like this:2. X > Y * Z

    Here's another example: a rule to tell if a (whole) number is odd:

    3. odd(Num) :- 1 is Num mod 2.

    Another way to do this one is to use =:=:

    4. odd(Num) :- Num mod 2 =:= 1.If you wanted to be more cautious, you could first check that Num is in fact a whole

    number:5. odd(Num) :- integer(Num), 1 is Num mod 2.

    As usual in digital computations, with fractional numbers, it is necessary to be carefulwith approximation issues. Thus the final example in the table above, 1.21 is 1.1 ** 2.

    actually fails when typed into Prolog as a query. This is because 1.1 ** 2, as represented

    in computers, actually comes out to be something like 1.210000000001. See the sectionon comparison operators for a solution to this issue.

    20. Call:

    Call is a built-in meta-predicate that allows its single argument to be called/invoked as agoal. For example, a program might create a goal (perhaps using=..) on the fly, and then,presumably later in the program, need to test the goal. Here are queries that perform these

    roles - in a real program, both the assert and the call would be built in to Prolog

    procedures written by the programmer.

    ?- assert(likes(mary, pizza)).

    ?- call(likes(Person, pizza)).Person = mary

    ?- Goal =.. [likes, mary, What], call(Goal).

    Goal = likes(mary, pizza)

    What = pizza

    S.B.B.S.I.E.T 25

    http://www.cse.unsw.edu.au/~billw/#ishttp://www.cse.unsw.edu.au/~billw/#comparisonhttp://www.cse.unsw.edu.au/~billw/#termtypehttp://www.cse.unsw.edu.au/~billw/#comparisonhttp://www.cse.unsw.edu.au/~billw/#univhttp://www.cse.unsw.edu.au/~billw/#univhttp://www.cse.unsw.edu.au/~billw/#asserthttp://www.cse.unsw.edu.au/~billw/#ishttp://www.cse.unsw.edu.au/~billw/#comparisonhttp://www.cse.unsw.edu.au/~billw/#termtypehttp://www.cse.unsw.edu.au/~billw/#comparisonhttp://www.cse.unsw.edu.au/~billw/#univhttp://www.cse.unsw.edu.au/~billw/#assert
  • 8/8/2019 Pro Log File

    26/36

    Mrinalini Rana CSE7th sem 685034228

    3.4 AIM: Introduction to PROLOG programming by PROLOG

    programs.

    1. Write a simple program in PROLOG.

    Program:likes(john,mary).

    likes(john,flowers).likes(mary,flowers).

    ?-likes(john,mary).

    Output:

    S.B.B.S.I.E.T 26

  • 8/8/2019 Pro Log File

    27/36

    Mrinalini Rana CSE7th sem 685034228

    2. Write a program to print the value of variable.

    Program:likes(john,mary).

    likes(john,flowers).likes(mary,flowers).

    ?-likes(john,X),write(X).

    Output:

    S.B.B.S.I.E.T 27

  • 8/8/2019 Pro Log File

    28/36

    Mrinalini Rana CSE7th sem 685034228

    3. Write a program of Tower of Hanoi.

    Program:hanoi(N):-hanoi(N,a,b,c).hanoi(N,_,_,_).

    hanoi(N,fromPin,ToPin,UsingPin):-M is N-1,N>1.

    hanoi(M,FromPin,UsingPin,ToPin).move(FromPin,FromPin).hanoi(M,ToPin,FromPin,UsingPin).

    move(From,To):-write([move,disk,from,pin,from,to,pin,ToPin]).

    ?-hanoi(N,FromPin,ToPin,UsingPin).

    Output:

    S.B.B.S.I.E.T 28

  • 8/8/2019 Pro Log File

    29/36

    Mrinalini Rana CSE7th sem 685034228

    4. Write a program of factorial.

    Program:factorial(0,1).

    factorial(X,Y) :-X1 is X - 1,

    factorial(X1,Z),

    Y is Z*X,!.

    ?- factorial(15,X),write(X).

    Output:

    S.B.B.S.I.E.T 29

  • 8/8/2019 Pro Log File

    30/36

    Mrinalini Rana CSE7th sem 685034228

    5. Write a program to find member from a list.

    Program:member(X,[X|R]).

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

    ?- member(2,[1,2,3]).

    Output:

    S.B.B.S.I.E.T 30

  • 8/8/2019 Pro Log File

    31/36

    Mrinalini Rana CSE7th sem 685034228

    6. Write a program to find the fibonnacci series.

    Program:

    fib(1,1).fib(2,1).

    fib(N,R):- N >= 3,N1 is N-1,N2 is N-2,

    fib(N1,R1),fib(N2,R2),R is R1+R2.?- fib(7,R),write(R).

    Output:

    S.B.B.S.I.E.T 31

  • 8/8/2019 Pro Log File

    32/36

    Mrinalini Rana CSE7th sem 685034228

    7. Write a prolog program to print the sum of n numbers.

    PROGRAM:sum_to(1,1).

    sum_to(N,R):- N\=1,

    N1 is N-1,sum_to(N1,R1),

    R is N+R1.

    ?-sum_to(6,X),write(X).

    S.B.B.S.I.E.T 32

  • 8/8/2019 Pro Log File

    33/36

    Mrinalini Rana CSE7th sem 685034228

    Output:

    8. Write a program using aless predicate.

    Program:

    aless(advocate,clergyman).?-aless (advocate,clergyman).

    Output:

    S.B.B.S.I.E.T 33

  • 8/8/2019 Pro Log File

    34/36

    Mrinalini Rana CSE7th sem 685034228

    9. Write a program to get the possible pairs.

    Program:boy(rohan).

    boy(krishana).

    boy(paul).girl(ritika).

    girl(komal).

    girl(ridhima).

    S.B.B.S.I.E.T 34

  • 8/8/2019 Pro Log File

    35/36

    Mrinalini Rana CSE7th sem 685034228

    possible_pair(X,Y):-boy(X),girl(Y).

    ?-possible_pair(X,Y),write(X),write(Y).

    Output:

    10. Write a program to use change predicate.

    Program:change(you,i).

    change(are,[am,not]).

    change(frence,german).

    change(do,no).

    change(X,X).

    alter([],[]).

    S.B.B.S.I.E.T 35

  • 8/8/2019 Pro Log File

    36/36

    Mrinalini Rana CSE7th sem 685034228

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

    ?-alter([you,are,a,computer],Z),write(Z).

    Output: