Prolog Tutorial

 
 
 

Prolog Basics


File and Database Programs

Spiral game Relation max(X,Y,Max) Display each digit with respective frequencie Family hierarchy Person hobbies Predicate order (List) is true if List is ordered String in increasing order of their length in db Rel last(item,list) so item is last el of list Convert decimal number to roman number Find animals leaving in water and land Defines predicate maxlist (L,Max) Add element in last position in given list Predicate logic representation and prove proof Search student data - Name or phone no is i/p

Game

Game Predicates evenlength(List) and oddlength(List) Multi-List Program Run marriage burro Find names of student obtained marks greater Find length of given list DB of teacher - show no of lectures require Relation translate L1, L2 to num b/w 0 and 9 Read file and create list of all the lines of it Add and multiply numbers Find grade Concate , member in list, reverse, delete list Read number Parent Tree Search company, car name, price, class, color Relation dividelist (L,L1,L2) Find factorial of given number Create window i/p string, remove the window & text Reverse list using concatenate Check number is member of given list or not 10 int in list,10 from database - find 2nd max

Search Programs

Game LT Find grade Find relations Insert an element anywhere in the list Display item name, code, quantity, price,grade Find easy courses person likes Logic repres - converting to prove the proof Define relation shift L1,L2- L1 shift by one left Read file display line by line - go to output file Delete an element from list using concatenate Read 10 strings into a list & display similar char Parent and predecessor Check given word is a palindrome or not Delete an element from given list Copy String length equal to number using file Sort list of integers using Insertion Sort Find maximum number from inputted number Monkey and banana Find nth element of a list Generate first 10 prime num store them in list Blocks problem using hill climbing Find last item of the list Check whether given list is palindrome or not Store cricket players details in database Relation last(item,list) so last el w/o concate Ask question Buy and sale cars Merge two ordered list Print number from 1 to 25 Murder mystery 8puzzle - heuristic function % - best first search

Problem Solving

Game 5*5 matrix -moving in spiral way- dice value Fibonacci Series Read two strings concatenation of the both Production system - Missionary cannibal problem Print zodiac sign of a given birth date Use of list DB Employees - Print emp children of given age Add an element to the begining of list Print message Find relations in family Login page Add element in head position in given list Water Jug Problem start with state(0,0) end (2,0) Search record by name and phone number

Sort Programs

Game EX Find length of a list is even or odd String token Generate facts of family - take different relation Relation split(L,P,N) Find sum of all numbers in given list Show no of user entered book - 2 diff database Program to print user entered number Marcus Check year is a leap year or not Compare character and string Medical Diagnostic system Rotate list n places to the left Define relation shift L1,L2- L1 shift by one right Read sentence from file copy each of token in list Delete el from given list w/o using concatenate Display strings with closest length of int entered Find murderer Find permutation of a given list Find last element in a given list Read file and create a list of all words of it

Basic Programs

Sort list using Bubble Sort Find length of names Teacher and lecture Generate sublist of a given list Compare characters, strings, reverse string 4-3 Gallon Water Jug Problem Split list in negative & positive list Reverse given list Read and write file Relation count(A,L,N) num of occurrences Find summation of list of numbers Buy car and truck Write elements of the list line by line Concatenate two lists in giving third list Copy in database - string length equal to number Sort list using Quick Sort
 
 
domains
    value, row, col, gval, hval, pval, sval, parent, nodeno = integer
    nodevalue=ndval(value,row,col)
    nodelist=nodevalue*
    loclist=value*
    poslist=posval(loclist,value)
    nodestruct=ndstruct(nodelist,value,value,value,value)
    hvallist=nodestruct*

database
    opennodeinfo(nodelist,value,value,value,value).
    closenodeinfo(nodelist,value,value,value,value).
    bestnodeinfo(nodelist,value,value,value,value).
    nvalue(value,row,col).
    rowcolCounter(value).
    nodeNo(value).
    currParent(value).

predicates
    displayPuzzle.
    displayNodeList(nodelist).
    calHvalue(nodelist,value).
    calPvalue(nodelist,value).
    getNodeInfo(nodevalue,value,row,col).
    finalnode(nodelist,hval).
    pvalue(value, poslist).
    findStepsFar(value,poslist,value).
    memberOfLocList(value,loclist).
    moveLeft.
    moveRight.
    moveUp.
    moveDown.
    findValue(row,col,value).
    findBlank(row,col).
    setNewPos(value,row,col).
    setCurrNode(nodelist).
    emptyCurrNode.
    genNodeList(nodelist,nodelist). 
    genHvalList(hvallist,hvallist).
    input.
    performSearch.
    processCurrNode.
    checkInOpen(nodelist,value).
    checkInClose(nodelist,value).
    bubblesort(hvallist,hvallist).
    swap(hvallist,hvallist).
    insertSortedList(hvallist).

clauses

    finalnode([ndval(1,0,0),ndval(2,0,1),ndval(3,0,2),ndval(8,1,0),
        ndval(0,1,1),ndval(4,1,2),ndval(7,2,0),ndval(6,2,1),
        ndval(5,2,2)],0).

    pvalue(1,posval([0],0)).
    pvalue(1,posval([1,3],1)).
    pvalue(1,posval([2,6,4],2)).
    pvalue(1,posval([7,5],3)).
    pvalue(1,posval([8],4)).

    pvalue(2,posval([1],0)).
    pvalue(2,posval([0,2,4],1)).
    pvalue(2,posval([3,5,7],2)).
    pvalue(2,posval([6,8],3)).

    pvalue(3,posval([2],0)).
    pvalue(3,posval([1,5],1)).
    pvalue(3,posval([0,4,8],2)).
    pvalue(3,posval([3,7],3)).
    pvalue(3,posval([6],4)).

    pvalue(4,posval([5],0)).
    pvalue(4,posval([2,4,8],1)).
    pvalue(4,posval([1,3,7],2)).
    pvalue(4,posval([0,6],3)).

    pvalue(5,posval([8],0)).
    pvalue(5,posval([5,7],1)).
    pvalue(5,posval([2,4,6],2)).
    pvalue(5,posval([1,3],3)).
    pvalue(5,posval([0],4)).

    pvalue(6,posval([7],0)).
    pvalue(6,posval([4,6,8],1)).
    pvalue(6,posval([1,3,5],2)).
    pvalue(6,posval([0,2],3)).

    pvalue(7,posval([6],0)).
    pvalue(7,posval([3,7],1)).
    pvalue(7,posval([0,4,8],2)).
    pvalue(7,posval([1,5],3)).
    pvalue(7,posval([2],4)).

    pvalue(8,posval([3],0)).
    pvalue(8,posval([0,4,6],1)).
    pvalue(8,posval([1,5,7],2)).
    pvalue(8,posval([2,8],3)).

    pvalue(0,posval([4],0)).
    pvalue(0,posval([1,3,5,7],1)).
    pvalue(0,posval([0,2,6,8],2)).

    input:-
        assert(opennodeinfo([ndval(2,0,0),ndval(1,0,1),
            ndval(6,0,2),ndval(4,1,0),ndval(0,1,1),
            ndval(8,1,2),ndval(7,2,0),ndval(5,2,1),
            ndval(3,2,2)],0,0,0,0)),
        assert(rowcolCounter(8)),
        assert(nodeNo(1)),
        assert(currParent(0)).

    displayPuzzle:-
        genNodeList([],Nodelist),
        bestnodeinfo(Nodelist,Hval,Gval,Parent,Nodeno),
        displayNodeList(Nodelist),
        write(\"h(n) : \",Hval),nl,
        write(\"g(n) : \",Gval),nl,
        write(\"parent(n) : \",Parent),nl,
        write(\"nodno(n) : \",Nodeno).

    displayNodeList([]).
    displayNodeList([ndval(Value1,_,_),ndval(Value2,_,_),ndval(Value3,_,_)|Tail]):-
        write(Value1,\" \"), write(Value2,\" \"), write(Value3,\" \"),nl,
        displayNodeList(Tail).

    getNodeInfo(ndval(Value,Row,Col),Value,Row,Col).

    emptyCurrNode:-
        retractall(nvalue(_,_,_)).

    setCurrNode([]):- !.
    setCurrNode([ndval(Value,Row,Col)|Tail]):-
        assert(nvalue(Value,Row,Col)),
        setCurrNode(Tail).

    calHvalue(NodeList,Hval):-
        calPvalue(NodeList,Pval),
        Hval=Pval.

    calPvalue([],0):- !.
    calPvalue([Head|Tail],Pval):-
        getNodeInfo(Head,Value,Row,Col),
        CurrPos=(Row*3)+Col,
        pvalue(Value,PosList),
        findStepsFar(CurrPos,PosList,Steps),
        calPvalue(Tail,NewPval),
        Pval=Steps+NewPval.

    findStepsFar(CurrPos,posval(LocList,Steps),Steps):-
        memberOfLocList(CurrPos,LocList).

    memberOfLocList(CurrPos,[CurrPos|_]) :- !.
    memberOfLocList(CurrPos,[_|Rest]):-
        memberOfLocList(CurrPos,Rest).

    moveLeft:-
        findBlank(Row,Col),
        Col>0,
        NewCol=Col-1,
        findValue(Row,NewCol,Value),
        setNewPos(Value,Row,Col),
        setNewPos(0,Row,NewCol),
        write(\"\\nleft successor\"),
        processCurrNode,
        setNewPos(0,Row,Col),
        setNewPos(Value,Row,NewCol),!.
    moveLeft.

    moveRight:-
        findBlank(Row,Col),
        Col<2,
        NewCol=Col+1,
        findValue(Row,NewCol,Value),!,
        setNewPos(Value,Row,Col),
        setNewPos(0,Row,NewCol),
        write(\"\\nright successor\"),
        processCurrNode,
        setNewPos(0,Row,Col),
        setNewPos(Value,Row,NewCol),!.
    moveRight.

    moveUp:-
        findBlank(Row,Col),
        Row>0,
        NewRow=Row-1,
        findValue(NewRow,Col,Value),
        setNewPos(Value,Row,Col),
        setNewPos(0,NewRow,Col),
        write(\"\\nup successor\"),
        processCurrNode,
        setNewPos(0,Row,Col),
        setNewPos(Value,NewRow,Col),!.
    moveUp.

    moveDown:-
        findBlank(Row,Col),
        Row<2,
        NewRow=Row+1,
        findValue(NewRow,Col,Value),
        setNewPos(Value,Row,Col),
        SetNewPos(0,NewRow,Col),
        write(\"\\ndown successor\"),
        processCurrNode,
        setNewPos(0,Row,Col),
        setNewPos(Value,NewRow,Col),!.
    moveDown.

    processCurrNode:-
        genNodeList([],Nodelist),
        checkInOpen(Nodelist,Oval),
        Oval=1,
        checkInClose(Nodelist,Cval),
        Cval=1,
        calHvalue(Nodelist,Hval),
        currParent(Parent),
        Gval=Parent+1,
        nodeNo(Nodeno),
        NewNodeno=Nodeno+1,
        retract(nodeNo(Nodeno)),
        assert(nodeNo(NewNodeno)),
        assert(opennodeinfo(Nodelist,Hval,Gval,Parent,Nodeno)),
        nl,displayNodeList(Nodelist), write(\"Parent : \",Parent,\" Nodeno : \",Nodeno),
        readchar(_),
        genHvalList([],HvalList),
        bubblesort(HvalList,SortedList),
        insertSortedList(SortedList).
    processCurrNode.

    checkInOpen(Nodelist,Oval):-
        Oval=1,
        not(opennodeinfo(Nodelist,_,_,_,_)),!.
    checkInOpen(Nodelist,Oval):-
        opennodeinfo(Nodelist,_,_,_,_),
        Oval=0.

    checkInClose(Nodelist,Cval):-
        Cval=1,
        not(closenodeinfo(Nodelist,_,_,_,_)),!.
    checkInClose(Nodelist,Cval):-
        closenodeinfo(Nodelist,_,_,_,_),
        Cval=0.

    findValue(Row,Col,Value):-
        nvalue(Value,Row,Col).

    findBlank(Row,Col):-
        nvalue(0,Row,Col).

    setNewPos(Value,Row,Col):-
        retract(nvalue(_,Row,Col)),
        assert(nvalue(Value,Row,Col)).

    genNodeList(L,NodeList):- 
        rowcolCounter(Cnt), 
        Cnt>=0, 
        NewCnt=Cnt-1, 
        retract(rowcolCounter(Cnt)), 
        assert(rowcolCounter(NewCnt)),
        Row=Cnt div 3, 
        Col=Cnt mod 3, 
        nvalue(Value,Row,Col), 
        NewList=[ ndval(Value,Row,Col) | L ],!,
        genNodeList(NewList,NodeList).
    genNodeList(NodeList,NodeList):-
        rowcolCounter(Cnt),
        retract(rowcolCounter(Cnt)), 
        assert(rowcolCounter(8)),!.

    genHvalList(L,Hvallist):-
        retract(opennodeinfo(Nodelist,Hval,Gval,Parent,Nodeno)),
        Newlist=[ndstruct(Nodelist,Hval,Gval,Parent,Nodeno)|L],
        genHvalList(Newlist,Hvallist).
    genHvalList(Hvallist,Hvallist):- !.

    insertSortedList([ndstruct(Nodelist,Hval,Gval,Parent,Nodeno)|Tail]):-
        assert(opennodeinfo(Nodelist,Hval,Gval,Parent,Nodeno)),
        insertSortedList(Tail).
    insertSortedList([]) :- !.

    performSearch:-
        retract(bestnodeinfo(_,_,_,_,_)),
        opennodeinfo(Nodelist,Hval,Gval,Parent,Nodeno),
        Hval<> 0,
        retract(currParent(_)),
        assert(currParent(Nodeno)),
        assert(bestnodeinfo(Nodelist,Hval,Gval,Parent,Nodeno)),
        emptyCurrNode,
        setCurrNode(Nodelist),
        write(\"\\ncurrent best node\\n\"),
        displayNodeList(Nodelist),
        write(\"h(n) : \",Hval),nl,
        write(\"g(n) : \",Gval),nl,
        write(\"parent(n) : \",Parent),nl,
        write(\"nodno(n) : \",Nodeno),
        readchar(_),
        assert(closenodeinfo(Nodelist,Hval,Gval,Parent,Nodeno)),
        retract(opennodeinfo(Nodelist,Hval,Gval,Parent,Nodeno)),
        moveLeft,
        moveUp, 
        moveRight, 
        moveDown,
        performSearch.
    performSearch.

bubblesort(List, Sorted) :- 
     swap(List, List1), !, 
     bubblesort(List1, Sorted). 

bubblesort(Sorted, Sorted). 

swap([ndstruct(A,X,B,C,D),ndstruct(E,Y,F,G,H)|Rest], [ndstruct(E,Y,F,G,H),ndstruct(A,X,B,C,D)|Rest]) :- X > Y.

swap([ndstruct(A,Z,B,C,D)|Rest], [ndstruct(A,Z,B,C,D)|Rest1]) :- swap(Rest, Rest1).

goal
    makewindow(1,2,3,\"8-Puzzle Problem\",0,0,25,80),
    input,
    opennodeinfo(Nodelist,_,_,_,_),
    calHvalue(Nodelist,Hval),
    nodeNo(Nodeno),
    NewNodeno=Nodeno+1,
    retract(nodeNo(Nodeno)),
    assert(nodeNo(NewNodeno)),
    retract(opennodeinfo(Nodelist,_,_,_,_)),
    assert(opennodeinfo(Nodelist,Hval,0,0,Nodeno)),
    assert(bestnodeinfo(Nodelist,Hval,0,0,Nodeno)),
    setCurrNode(Nodelist),
    emptyCurrNode,
    performSearch.

    Related Post:
  1. Prolog program to merge two ordered list generating an ordered list

  2. Prolog program to define the relation shift(L1,L2) so that L2 is L1 shifted rotationally by one element to the left

  3. Prolog program for solving the blocks problem using hill climbing

  4. Prolog program to define the relation shift(List1, List2) so that List1 shifts rotationally by one element to the right

  5. ProLog Program to search record by name and phone number

  6. Program to compare character and string

  7. Prolog program to read details about your classmates and display pairs of classmates with highest number of hobbies matching earlier then the rest

  8. ProLog Program to generate facts of the family and take different relations

  9. Prolog program to delete an element from a given list without using concatenate

  10. ProLog Program to find relations in family

  11. ProLog Program to find relations

  12. ProLog Program of Backtracking through fail

  13. ProLog Program to buy car and truck

  14. Prolog program to read file and create list of all the lines of it. Each list in turn should be list of word.

  15. Prolog program to define a relation dividelist (L,L1,L2) so that L is partitioned into L1 and L2 where length of L1 and L2 are almost same

  16. Prolog program to create a database of Employees containing EmpNo, EmpName, Emp Spouse Name, children and print Employee children having a given age

  17. Prolog program to define the relation shift(List1, List2) so that List1 shifts rotationally by one element to the left

  18. Prolog program to read a sentence from a file and then copy each of the token one by one into a list

  19. Design a game based on 5*5 matrix and two players moving in spiral way to the center given the dice value

  20. ProLog Program of parent and predecessor