Prolog help

Posted on 2006-04-24
Last Modified: 2012-06-27
Hi ,

I have been given an assignment to write a prolog program to implement a Context Free Grammar (CFG) using an english lanquage interface.

The CFG can be extremely simple. Apparently, showing the feasibility of the idea is the core requirement.
More information will be forthcoming from the instructor in a couple of days but this is all the info I have at the moment. However, since this must be completed in less than two weeks, I figure I need to start on it now.

The course I am taking is an overview of Expert Systems. We spent one class going over some prolog syntax but that is about the extent of our coverage. I have programmed enough in other lanquages to be confident of my general skill level with languages but prolog seems to be a lot different than any other lanquage I have used. However, I don't have the time to really learn the language as I have other course work that needs to be done. Personally, I am a bit perplexed as to how the instructor really expects this project to be done on such short notice though I suspect that she is really looking for someone to show her how to do it herself  (I know she is just now starting to learn the lanquage herself). Regardless, though, she holds my grade in her hands so I have to do the best I can.

I downloaded swi-prolog for windows and have played around with the demo program that was included. However, I really do not understand what I am doing yet. Prolog has an entirely different way of doing things (and terminology) from procedural and object oriented lanquages I have used.

My basic questions now are:

Are there any well written Tutorials available that hit the high points quickly and perhaps contrast prolog with C ?
Something that has samples of interactive prolog programs?

How do I get input from the user via screen inputs (ie.. "Please enter something here:   " )

How do I clear the screen?

How do I output to the screen? To file?

Is there a way to debug that is simple? Anything similar to printf() statements dropped where they are needed?

Does anyone have a suggestion on how to even begin this project?

I do not want you to do my project for me (ok..I really do but I don't really expect it to happen) but any examples of anything that might be in any way similar will be greatly appreciated.



Question by:clays_dna
    LVL 24

    Accepted Solution

    a bit to many questions for my taste.
    A google search for prolog tutorial yields around a million hits. Have you checked just one of it?

    Gettin input: read or readstring
    as in C write 25 new lines and that's it.
    Writing wheter to standard output or a file write, opening a file can be done via see or seeing

    example for a round using read (must be a Prolog term) so be cautious:
    write('I''m a prompt '), read(SomeVar), write('you gave me '), write(SomeVar).
    I'm a prompt 'Friedrich'.
    you gave me Friedrich

    The 'Friedrich'. must be typed in by you.

    There is surely a debugger for stepping through the evaluation. So check your user manuals.

    Well the base line you should learn it that you do not formulat how things are done you describe the problem and Prolog will find it's way through it.

    How to begin? I would start by trying out some example to get a feeling for how things are done in Prolog.



    Author Comment

    Answer helped with input/output a bit but not with understanding prolog in a very limited time frame. I was really looking for some examples of code written in C and then in Prolog to contrast how things were done. However, I blame only myself for not following up on this topic to get more input. I was just swamped.

    After digging into prolog (very,very basic knowledge), I really think a tutorial that contrasted how to do things in prolog compared to some other procedural or object oriented language would be really useful. If I ever learn enough about it, I may start one.

    So far, prolog has not impressed me.
    Everything I read has folk going on and on about how great it is but I never see where they actually DO anything much with it. I suppose it is cool to be able to use predicate logic to figure things out but, so far, it seems so cumbersome and data is so prone to errors and difficult to track done that I really don't see much use in it so far. Of course, I am into some AI courses now and I think LISP and PROLOG are the only languages the professors know so I guess I will eventually have to bear down. Sort of amazes me that computer science professors can suck at programming but so far, most of mine can talk all day about finite state machines or how to get a syntax diagram or bs about writing a design document that has so much crap in it that no one ever reads it but acutally programming? Nahh....that is not what they do. Funny as hell. I doubt I would hire any of mine for anything that required an actual product to be developed.

    Anyway, I appreciate your help.

    LVL 24

    Expert Comment

    Well I suggest you have a look at a decent book about prolog and then you use it in the area it was meant to be used in. Reasoning, then try to write that in C and despair. A side by side comparison is unfair if you just see it from the C side. So look up a decent Prolog program where the reasoning is used and then write that in C also.

    LVL 24

    Expert Comment

    See this example from  Handbooks of Programming Languages Vol IV.
    father(elizabeth_i, henry_vii).
    father(henry_viii, henry_vii).
    father(margarete_tudor, henry_vii).
    father(mary_queen_of_scots, james_v_of_scotland).
    father(james_v_of_scotland, james_iv_from_scotland).

    mother(james_v_of_scotland, margaret_tudor).
    mother(james_vi_of_scotland, mary_queen_of_scots).

    parent(X,Y):- father(X,Y).
    parent(X,Y):- mother(X,Y).

    Now you can read it as henry_vii is father of eliszabeth_i
    You know can ask Questions.

    parent(james_v_of_scotland, X)

    parent(james_v_of_scotland, X).

    X = james_iv_of_scotland ;

    X = margaret_tudor

    But you see I have not stated how to  start searching all the entities. I just stated some facts, who's the parent from whom and who's the mother from whom. And I declared a rule that a parent is either a father or a mother

    Now Prolog start exploring all the possibilites till it finds a match.

    Now write that in C


    Author Comment

    It could definitely be done in C though the more relationships, the more difficult it could get. If the relations got too hairy, you would eventually end up writtng a prolog interpreter instead. I wonder if that is how prolog was originally developed?

    An object oriented language would make it easier.

    A relational database system would make it simplier yet but the code could still get twisted if the database is not thought out all the way.

    An object relational database system would be the easiest of all.

    I know a bit about all of the above. I know little about Prolog. There is an old saying that everything looks like a nail if the only tool you have is a hammer. So I readily agree that there may be really good stuff that has been written in prolog or which is much better for being written in prolog. I just have not seen em yet.

    Question though. Why has prolog not become more popular? Twenty years ago I was hearing about it all the time but now I hear little about it at all. I have only seen one commercial program that was written in prolog (a data analysis program) that was OK but difficult to work with. Why is that??


    LVL 24

    Expert Comment

    Well I can not see how a database can help you with this. It's not the data which are the problem but implementing the relationships. And whatever database you use for that with any combination of programmign langauge this is a weak point in other approaches.

    Why havn't it be more popular? Maybe because the most programming stuff is done in the Business area. and there the tool to choose are some sort of Database. You do not need rules you do not persistent storage of data. Well that's not fully true of course because just thing of triggers, stored procedures and the like.

    However the training is usually with some procedural language and therfor most of the time problems are tackled with "imperative" approaches.

    First do this, then do that, store that there etc....


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now