Turbo Prolog using Harbour

Turbo Prolog using Harbour

Postby Antonio Linares » Wed May 08, 2019 9:02 pm

Reviewing some Turbo Prolog examples I found this one:

Code: Select all  Expand view
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  Expand view
#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: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Turbo Prolog using Harbour

Postby Antonio Linares » Thu May 09, 2019 6:54 am

A step further

prolog.prg
Code: Select all  Expand view
#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: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Turbo Prolog using Harbour

Postby Antonio Linares » Thu May 09, 2019 9:16 am

Code: Select all  Expand view
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  Expand view
#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: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Turbo Prolog using Harbour

Postby Antonio Linares » Thu May 09, 2019 9:53 am

Image
regards, saludos

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

Re: Turbo Prolog using Harbour

Postby Antonio Linares » Thu May 09, 2019 8:58 pm

Code: Select all  Expand view
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to AI Introduction (Harbour code and samples)

Who is online

Users browsing this forum: No registered users and 1 guest