This is a simplistic Prolog-like example creating and querying relations between subjects and properties.

The following clause is recognized:

The following special subjects act as query words and execute a query

For example,

«prolog.ijs»=
NB. prolog - creating and querying relations between subject and properties

«state»

«text-utilities»

«query-words»

«creating-relation»

«execution»

«example»

The state is defined with three arrays of subject, properties and the map of their relations.

«state»=
NB. ---------------------------------------------------------
NB. state

WHO=: ''      NB. subjects
WHAT=: ''     NB. properties
MAP=: 0 0$0   NB. ($MAP) -: (#WHO),(#WHAT)

Text utilities help format the query output.

«text-utilities»=
NB. ---------------------------------------------------------
NB. text utilities

copula=: 3 : 0
  ' ',~' ',> {&(;:'is are') 1 < #y
)

enlist=: 13 : ';}:,(<'', ''),.~y'

say=: 3 : 0
  if. 0=#y do. 'none' return. end.
  if. 1=#y do. ;y return. end.
  (enlist }:y),' and ',(>_1{y)
)

Query words serve as special subjects

«query-words»=
NB. ---------------------------------------------------------
NB. query words

what=: 3 : 0
  y,(copula 1),say ((WHO i.<y){MAP)#WHAT
)

who=: 3 : 0
  r=. ((WHAT i.<y){"1 MAP)#WHO
  (say r),(copula r),y
)

Relations are created by looking up existing subjects and properties-- growing their lists if necessary--and marking the Boolean relation map.

«creating-relation»=
NB. ---------------------------------------------------------
NB. creating relation

index=: 4 : 0
  if. (#w) = i=. (w=. x~) i.<y do. (x)=: w,<y end. i
)

is=: 4 : 0
  xy=. <('WHO'index x),('WHAT'index y)
  MAP=: 1 xy } ((#WHO),#WHAT){.MAP
)

Parsing a clause identifies the subject and property parts, and then applies either one of defined routines as a special subject, or a generic is routine.

A group of clauses is executed each on a separate line, where the line is printed first and then passed for parsing, which prints a results if present.

«execution»=
NB. ---------------------------------------------------------
NB. execution

parse=: 3 : 0
  's IS o'=. ;:y
  if. 3=nc<s do. smoutput s~ o
           else. s is o end.
  empty''
)

run=: (parse [ smoutput@('   '&,)) ;. _2

The following example shows a typical session with a number of creation and querying clauses are used.

«example»=
NB. ---------------------------------------------------------
NB. example

TEST=: 0 : 0
mike is tall
john is tall
peter is old
john is old
what is john
john is smart
what is john
who is smart
who is old
mike is old
who is old
what is mike
)

Note 'Test'
  run TEST
)

The corresponding output looks like this:

     run TEST
   mike is tall
   john is tall
   peter is old
   john is old
   what is john
john is tall and old
   john is smart
   what is john
john is tall, old and smart
   who is smart
john is smart
   who is old
john and peter are old
   mike is old
   who is old
mike, john and peter are old
   what is mike
mike is tall and old

The state at this point is

   ('';WHAT),WHO,.<"0 MAP
+-----+----+---+-----+
|     |tall|old|smart|
+-----+----+---+-----+
|mike |1   |1  |0    |
+-----+----+---+-----+
|john |1   |1  |1    |
+-----+----+---+-----+
|peter|0   |1  |0    |
+-----+----+---+-----+

See also


Contributed by OlegKobchenko


CategoryLiterate

Scripts/Prolog (last edited 2009-09-28 08:27:32 by OlegKobchenko)