Scripts/Prolog
This is a simplistic Prolog-like example creating and querying relations between subjects and properties.
The following clause is recognized:
<subject> is <property>
The following special subjects act as query words and execute a query
- what queries subjects about properties
- who queries properties about subjects
For example,
Clause Response john is tall none - sets a relation john is old none what is john john is tall and old who is tall john is tall
[{{#file: "prolog.ijs"}} Download script: 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. [{{#file: "state"}} Download script: 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. [{{#file: "text-utilities"}} Download script: 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 [{{#file: "query-words"}} Download script: 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. [{{#file: "creating-relation"}} Download script: 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. [{{#file: "execution"}} Download script: 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. [{{#file: "example"}} Download script: 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
- The Very Logo Way article on the www.elica.net web site
- J Forum message JForum:chat/2009-August/002255 by Boyko Bantchev
- Essays/AnimalsGame
Contributed by Oleg Kobchenko