Turbo Prolog using Harbour

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Turbo Prolog using Harbour

Post by Antonio Linares »

Reviewing some Turbo Prolog examples I found this one:

Code: Select all

domains
   person, activity = symbol

predicates
   likes(person,activity)

clauses
   likes(ellen,tennis).
   likes(john,football).
   likes(tom,baseball).
   likes(eric,swimming).
   likes(mark,tennis).
   likes(bill,X) if likes(tom,X)
 
Then I thought, could we do that using Harbour ?

Code: Select all

#define likes(x,y) If(ValType(likes[x])=="B", Eval(likes[x],y), likes[x]==y)

function Main()

   local likes := {=>}

   likes[ "ellen" ] = "tennis"
   likes[ "john" ] = "football"
   likes[ "tom" ] = "baseball"
   likes[ "eric" ] = "swimming"
   likes[ "marc" ] = "tennis"
   likes[ "bill" ] = { | x | likes( "tom", x ) }

   ? likes( "bill", "tennis" )
   ? likes( "bill", "baseball" )

return nil
How far can we go ? ;-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Turbo Prolog using Harbour

Post by Antonio Linares »

A step further

prolog.prg

Code: Select all

#define likes(x,y) SetGet(likes,x,y)

function Main()

   local likes := {=>}

   likes( "ellen", "tennis" )
   likes( "john",  "football" )
   likes( "tom",   "baseball" )
   likes( "eric",  "swimming" )
   likes( "marc",  "tennis" )
   likes( "bill", { | x | likes( "tom", x ) } )

   MsgInfo( likes( "bill", "tennis" ) )
   MsgInfo( likes( "bill", "baseball" ) )
   MsgInfo( likes( "marc", "swinmming" ) )

return nil

function SetGet( ... )

   local aParams  := HB_AParams()

   if pcount() == 2
      return aParams[ 1 ][ aParams[ 2 ] ]
   endif

   if pcount() == 3
      if ! hb_HHasKey( aParams[ 1 ], aParams[ 2 ] )
         aParams[ 1 ][ aParams[ 2 ] ] = aParams[ 3 ]
      else 
         if ValType( aParams[ 1 ][ aParams[ 2 ] ] ) == "B"
            return Eval( aParams[ 1 ][ aParams[ 2 ] ], aParams[ 3 ] )
         else 
            return aParams[ 1 ][ aParams[ 2 ] ] == aParams[ 3 ]
         endif
      endif
   endif

return nil
 
   
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Turbo Prolog using Harbour

Post by Antonio Linares »

Code: Select all

domains
   person symbol

predicates
   male(person)
   smoker(person)
   vegetarian(person)
   sophie_could_date(person)

goal
   sophie_could_date(X) and
   write("a possible date for sophie is ",X) and nl.

clauses
   male(joshua) . 
   male(bill) . 
   male(tom) . 
   smoker(guiseppe).
   smoker(tom).
   vegetarian(joshua).
   vegetarian(tom).
   sophie_could_date(X) if male(X) and not(smoker(X)). 
   sophie_could_date(X) if male(X) and vegetarlan(X).

Code: Select all

#include "FiveWin.ch"

#define male(x) male[x] := nil
#define smoker(x) smoker[x] := nil
#define vegetarian(x) vegetarian[x] := nil
#define Sophie_could_date(x) HEval( x, { | cKey, uValue | If( HHasKey( male, cKey ) .and. ! HHasKey( smoker, cKey ) ;
                                         .and. HHasKey( vegetarian, cKey ), MsgInfo( cKey ),) } ) 

function Main()

   local male := {=>}, smoker := {=>}, vegetarian := {=>}

   male( "joshua" ) 
   male( "bill" )
   male( "tom" )
   male( "guiseppe" )
   smoker( "guiseppe" )
   smoker( "tom" )
   vegetarian( "joshua" )

   Sophie_could_date( male )

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Turbo Prolog using Harbour

Post by Antonio Linares »

Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Turbo Prolog using Harbour

Post by Antonio Linares »

Code: Select all

#include "FiveWin.ch"

#define male(x)        male[x] := nil
#define IsMale(x)      HHasKey( male, x )
#define female(x)      female[x] := nil
#define IsFemale(x)    HHasKey( female, x )
#define father(x,y)    father[x] := y
#define GetFather(x)   If( HHasKey( father, x ), father[x],)
#define mother(x,y)    mother[x] := y
#define GetMother(x)   If( HHasKey( mother, x ), mother[x],)
#define IsBrother(x,y) IsMale(y) .and. ( GetFather(x) == GetFather(y) .or. GetMother(x) == GetMother(y) ) .and. x != y
#define IsSister(x,y)  IsFemale(y) .and. ( GetFather(x) == GetFather(y) .or. GetMother(x) == GetMother(y) ) .and. x != y

function Main()

   local male := {=>}, female := {=>}, father := {=>}, mother := {=>}

   male( "alan" )
   male( "charles" )
   male( "bob" )
   male( "ivan" )
   
   female( "beverly" )
   female( "fay" )
   female( "marilyn" )
   female( "sally" )    

   mother( "marilyn", "beverly" )
   mother( "alan", "sally" )
   mother( "ivan", "sally" )
   
   father( "alan", "bob" )
   father( "beverly", "charles" )
   father( "fay", "bob" )
   father( "marilyn", "alan" )   
   
   MsgInfo( IsBrother( "alan", "ivan" ) )
   MsgInfo( IsSister( "alan", "marilyn" ) )

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply