Thread: I hope some of you know Prolog.

  1. #1
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    I hope some of you know Prolog.

    CSC 240 : Intro do different programming languages.

    Just introduced to prolog today. Assignment was to make a program with 10 facts and 3 rules. I've done that. So this isn't a do my home work question, its a curiosity--I want to be better question.

    My question is:
    How can I display all the associations (facts) known about a subject?

    Ie:
    Code:
    descrip(car, big).
    descrip(car, yellow).
    descrip(car, ford).
    .. etc.
    In the interaction i want to see:
    Code:
    ?what_do_we_know_about_the(car).
    The car is big.
    The car is yellow.
    The car is ford.
    I'm sure it has to do with a recursive call.
    I've also got a problem with "what_is(Property)". It will return only the first fact it finds that matches the property. However, I want it to display all the subjects that are Property.


    Heres the stuff I've got so far:
    Code:
    % Author:   Jeremy Giberson
    % Date: 4/22/2004
    
    %uncontestable facts, for associations.
    %color associations
    axioms(color, red).
    axioms(color, blue).
    axioms(color, black).
    axioms(color, white).
    axioms(color, green).
    %size associations
    axioms(size, small).
    axioms(size, medium).
    axioms(size, large).
    axioms(size, very_large).
    %weight associations
    axioms(weight, heavy).
    axioms(weight, light).
    
    
    object_color(box, red).
    object_color(car, blue).
    object_color(watch, black).
    object_color(mouse, white).
    
    object_size(box, medium).
    object_size(car, large).
    object_size(watch, small).
    object_size(mouse, small).
    
    object_description(box, is_square).
    object_description(car, provides_transport).
    object_description(watch, keeps_time).
    object_description(mouse, furry_creature).
    
    %query the fact list to see if the Object is a Property.
    %Object = car, box, animal, watch, etc.
    %Property = big, small, blue, red, heavy, etc.
    is_the(Object, Property) :- object_color(Object, Property);
                                    object_size(Object, Property);
                                    object_description(Object, Property).
                                    
    %querry axioms list to dertermine what a property might be.
    %Value is a property, not an object. Red, big, small, furry etc. NOT car, box, watch etc.
    what_is_a(Value) :-     axioms(color, Value), write(Value), write(' is a color.'), nl; % a color?
                            axioms(size, Value), write(Value), write(' is a size.'), nl;   % a size?
                            %.....                                                         % insert more axiom tests here
                            write('it is unknown what '), write(Value), write(' is.'), nl. % unknown, not define axiom
    
    %See if there is an Object in the list of facts, that has the attribute of Property, display that object if it exists.
    what_is(Property) :- is_the(Object, Property), write('The '), write(Object), write(' is '), write(Property).
    
    %See if there is an Object in the list of facts, that has ANY '_' attribute, display so if there is.
    is_there_a(Object) :- is_the(Object, _), write('There is a '), write(Object).

    Hopefully Thanks in Advance
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    >I hope some of you know Prolog.

    Well, you are in luck.

    Really, you are, I just learned it this past semester. Kind of...I actually didn't do any programming in it, but I made my own interpreter for it in my CS class (Digital Logic and Discrete Math), and so I know all about it.

    >How can I display all the associations (facts) known about a subject?

    I suppose you would have to make a new rule that could handle that. Just put the queries in your rule and output them, I suppose.

    >I've also got a problem with "what_is(Property)". It will return only the first fact it finds that matches the property.

    It would really help if you knew relational algebra. Do you know relational algebra? It helps out with Prolog a lot. By the looks of it, with that rule, the is_the part of the rule should return a list of all Objects that match that property, and then each of those objects is outputed using your write command later on. I don't know what is wrong with that. Well, it all depends what property you are giving it. Some of those properties would only return one fact anyways, but others would return more.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Isn't he related to Epilog?


  4. #4
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    I've been tinkering with the problem since I posted, and I managed to solve it.
    It occured to me, that it would search through all the facts and retrun when a match was found. What I needed was to make it keep going as if the match hadnt been found at all. So I made use of Prologs different evaluation of IF, AND and OR and false logic.
    I said:
    If there is a match with ANY OBJECT that is PROPERTY, write display the object, AND if 1 < 0 return yes.

    Since one is NOT smaller then zero, the method is forced to search through the whole fact list-display all the results and always return No. I wrote a wrapper function as explained in the comments to write this artifact. Anyway, heres the new code for any one that cares.

    Code:
    % Author:   Jeremy Giberson
    % Date: 4/22/2004
    
    %uncontestable facts, for associations.
    %color associations
    axioms(color, red).
    axioms(color, blue).
    axioms(color, black).
    axioms(color, white).
    axioms(color, green).
    %size associations
    axioms(size, small).
    axioms(size, medium).
    axioms(size, large).
    axioms(size, very_large).
    %weight associations
    axioms(weight, heavy).
    axioms(weight, light).
    
    
    object_color(box, red).
    object_color(car, blue).
    object_color(watch, black).
    object_color(mouse, white).
    
    object_size(box, medium).
    object_size(car, large).
    object_size(watch, small).
    object_size(mouse, small).
    
    object_description(box, is_square).
    object_description(car, provides_transport).
    object_description(watch, keeps_time).
    object_description(mouse, furry_creature).
    
    %query the fact list to see if the Object is a Property.
    %Object = car, box, animal, watch, etc.
    %Property = big, small, blue, red, heavy, etc.
    is_the(Object, Property) :- object_color(Object, Property);
                                    object_size(Object, Property);
                                    object_description(Object, Property).
                                    
    %querry axioms list to dertermine what a property might be.
    %Value is a property, not an object. Red, big, small, furry etc. NOT car, box, watch etc.
    what_is_a(Value) :-     axioms(color, Value), write(Value), write(' is a color.'), nl; % a color?
                            axioms(size, Value), write(Value), write(' is a size.'), nl;   % a size?
                            %.....                                                         % insert more axiom tests here
                            write('it is unknown what '), write(Value), write(' is.'), nl. % unknown, not define axiom
    
    %See if there is an Object in the list of facts, that has the attribute of Property, display that object if it exists.
    what_is(Property) :- is_the(Object, Property), write('The '), write(Object), write(' is '), write(Property).
    
    %See if there is an Object in the list of facts, that has ANY '_' attribute, display so if there is.
    is_there_a(Object) :- is_the(Object, _), write('There is a '), write(Object).
    
    %Find and list all the objects in the fact list thave a certain property. The logic here is tricky.
    %The help_what_are() function is what actually searches for, and displays all the objects in the fact list,
    %it relies on a false statement (1 < 0) at its tail to force it to keep searching the facts. It will always
    %return No--regardless of if it finds matching objects. To correct this, I add an wrapper function what_are()
    %that nots the result of help_what_are() (now always Yes.). To correctly print yes or no on the result of the search
    %i tag the matching query of the helper function as an AND to the call. If there is a match the method will print Yes.
    %if there is not, it will print No.
    what_are(Property) :- not(help_what_are(Property)), is_the(_, Property).
    help_what_are(Property) :- is_the(Object, Property), write(Object), write(' is '), write(Property), nl, 1 < 0.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I learn C++ or Prolog?
    By coreyt1111 in forum C++ Programming
    Replies: 10
    Last Post: 11-14-2006, 09:35 AM
  2. Prolog
    By YankeePride13 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-15-2005, 03:50 PM
  3. Learned optimism
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-25-2003, 03:06 PM
  4. Bob Hope, dead at 100 of being really old
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-28-2003, 08:04 AM
  5. Prolog
    By bob20 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-03-2002, 07:02 PM