Page 1 of 1

Turbo Prolog using Harbour

Posted: Wed May 08, 2019 9:02 pm
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 ? ;-)

Re: Turbo Prolog using Harbour

Posted: Thu May 09, 2019 6:54 am
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
 
   

Re: Turbo Prolog using Harbour

Posted: Thu May 09, 2019 9:16 am
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

Re: Turbo Prolog using Harbour

Posted: Thu May 09, 2019 9:53 am
by Antonio Linares
Image

Re: Turbo Prolog using Harbour

Posted: Thu May 09, 2019 8:58 pm
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