User:Ric Sherlock/J Dictionary/XML Structure

From J Wiki
Jump to navigation Jump to search

While working on a CSS layout for the J Dictionary, I thought it might be even more flexible/useful to describe the definition pages of J primitives using a more formal XML structure. Below is my attempt.

I'm not very experienced at creating XML structures, and have only had minimal experience using XSLT to format such structures (I haven't even attempted to create XSLT code to format the primitives below!) so I'm not sure how difficult it would be to work with this structure.

I'd be interested in thoughts on whether an XML structure for J primitives would be a desirable/sensible approach? Would it complicate things too much for too little reward? Would other descriptive formats (e.g. wiki markup, JSON) be better? [{{#file: "primitive.xml"}} Download script: primitive.xml ]

<?xml version="1.0" encoding="utf-8"?>
<dictionary>

  <primitive type="verb">
    <symbol>^</symbol>
    <form>
      <syntax></syntax>
	    <definition valence="monad">
	      <name>Exponential</name>
	      <rank>0</rank>
			  <description>
			    <p><tt>^y </tt>is equivalent to<tt> e^y</tt>,<tt> </tt>where
			    <tt> e </tt>is <i>Euler's number</i><tt> ^1 </tt>
			    (approximately 2.71828). The <i>natural logarithm</i><tt> </tt>
			    (<tt>^.</tt>)<tt> </tt>is inverse to<tt> ^ </tt>(that is,<tt> y=^.^y </tt>
			    and <tt>y=^^.y</tt>).</p>
			    <p>The monad<tt> x&^ </tt>is inverse to the monad <tt>x&^.</tt> .<tt> </tt>
			    For example:</p>
			    <pre>
			       10&^ 10&^. 1 2 3 4 5
			    1 2 3 4 5
			
			       10&^. 10&^ 1 2 3 4 5
			    1 2 3 4 5
			    

</description> </definition> <definition valence="dyad"> <name>Power</name> <rank>0 0</rank> <description>

x^2 and x^3 and x^0.5 are the square, cube, and square root of x .

In general, x^y is ^y*^.x, applying for complex numbers as well as real.

For a non-negative integer y, the phrasex ^ y is equivalent to */y # x; in particular, */ on an empty list is 1, and x^0 is 1 for any x, including 0 .

The fit conjunction applies to ^ to yield a stope defined as follows: x^!.k n is */x + k*i. n . In particular, ^!._1 is the falling factorial function.

</description> </definition> <examples>

The last result in the first example below illustrates the falling factorial function, formed by the fit conjunction. See <a href="ref.htm#14">Chapter 5 of [14]</a> for the use of stope functions, stope polynomials, and Stirling numbers in the difference calculus:

				   e=: ^ 1 [ x=: 4 [ y=: 0 1 2 3
				   ,.&.> x (e"_ ; e&^@] ; ^ ; ^@(] * ^.@]) ; (]^]) ; ^!._1) y
				+-------+-------+--+--+--+--+
				|2.71828|      1| 1| _| 1| 1|
				|       |2.71828| 4| 1| 1| 4|
				|       |7.38906|16| 4| 4|12|
				|       |20.0855|64|27|27|24|
				+-------+-------+--+--+--+--+
				
				   S2=: %.@S1=: (^!._1/~ %. ^/~) @ i. @ x:
				   (S1;S2) 8
				+---------------------------+-------------------+
				|1 0  0  0  0   0    0     0|1 0 0 0 0  0  0   0|
				|0 1 _1  2 _6  24 _120   720|0 1 1 1 1  1  1   1|
				|0 0  1 _3 11 _50  274 _1764|0 0 1 3 7 15 31  63|
				|0 0  0  1 _6  35 _225  1624|0 0 0 1 6 25 90 301|
				|0 0  0  0  1 _10   85  _735|0 0 0 0 1 10 65 350|
				|0 0  0  0  0   1  _15   175|0 0 0 0 0  1 15 140|
				|0 0  0  0  0   0    1   _21|0 0 0 0 0  0  1  21|
				|0 0  0  0  0   0    0     1|0 0 0 0 0  0  0   1|
				+---------------------------+-------------------+
				
				

S1 gives (signed) Stirling numbers of the first kind and S2 gives Stirling numbers of the second kind. They can be used to transform between ordinary and stope polynomials. Note that <a href="dxco.htm">x:</a> gives <a href="dictg.htm">extended precision</a>.

</examples>

   </form>
 </primitive>
 <primitive type="conjunction">
   <symbol>^:</symbol>
   <form>

<syntax>u^:n</syntax> <definition valence="monad"> <name>Power</name> <rank>_</rank> <description>

n may be integer, boxed, or a gerund.

Integer. The verb u is applied n times. An infinite power n produces the limit of the application of u . For example, (2&o.^:_)1 is 0.73908 , the solution of the equation y=Cos y . If n is negative, the obverse u^:_1 (see below) is applied |n times. Finally, u^:n y for an array n is produced by assembling u^:a y (for all the atoms a in n) into an overall result.

The obverse is used in <a href="d631.htm">u&.v</a> and is produced by <a href="dbdotu.htm">v b. _1</a> . Repeated application of a verb is also provided by <a href="d630n.htm">Bond (&)</a>.

Boxed. If n is boxed it must be an atom, and u^:(<m)

u^:(i.m) y if m is a non-negative integer
u^:(i.k) y if m is _ or '' , where k is the smallest positive integer such that (u^:(k-1) y) -: u^:k y
u^:_1^:(<|m) y if m is negative

Gerund. See on the right.

</description> </definition> <definition valence="dyad"> <name>Power</name> <rank>_ _</rank> <description>

n may be integer, boxed, or a gerund.

Integer or Boxed. x u^:n y x&u^:n y

Gerund. (Compare with the gerund cases of the <a href="d530n.htm">merge adverb }</a>)
x u^:(v0`v1`v2)y (x v0 y)u^:(x v1 y) (x v2 y)
x u^:(   v1`v2)y x u^:([`v1`v2) y
  u^:(   v1`v2)y u^:(v1 y) (v2 y)

</description> </definition> <examples>

The obverse (which is normally the inverse) is specified for six cases:

  1. The self-inverse functions + - -. % %. |. |: /: [ ] C. p.
  2. The pairs in the following table:
    < >
    <: >:
    +. j./"1"_
    +: -:
    *. r./"1"_
    *: %:
    ^ ^.
    $. $.^:_1
    ,: {.
    ;: ;@(,&' '&.>"1)
    #. #:
    ! 3 : '(-(!-y"_)%1e_3&* !"0 D:1 ])^:_^.y'
    3!:1 3!:2
    3!:3 3!:2
    \: /:@|.
    ". ":
    j. %&0j1
    o. %&1p1
    p: π(n)
    q: */
    r. %&0j1@^.
    s:5&s:
    u:3&u:
    x:_1&x:
    +~ -:
    *~ %:
    ^~ 3 : '(- -&b@(*^.) % >:@^.)^:_ b=.^.y'"0
    ,~ <.@-:@# {. ]
    ,:~ {.
    ;~ >@{.
    j.~ %&1j1
  3. Obviously invertible bonded dyads such as -&3 and 10&^. and 1 0 2&|: and 3&|. and 1&o. and a.&i. as well as u@v and u&v if u and v are invertible.
  4. Monads of the form v/\ and v/\. where v is one of + * - % = ~:
  5. Obverses specified by the conjunction :.
  6. The following cases merit special mention:
    • p:^:_1 n gives the number of primes less than n, denoted by π(n) in math
    • q:^:_1 is */
    • b&#^:_1 where b is a boolean list is Expand (whose fill atom f can be specified by fit, b&#^:_1!.f or #^:_1!.f )
    • a&#.^:_1 produces the base-a representation
    • !^:_1 and !&n^:_1 and n&!^:_1 produce the appropriate results
    • {= and i."1&1 are inverses of each other; these convert between integer permutation vectors and boolean permutation matrices

Example 1:

				   (] ; +/\ ; +/\^:2 ; +/\^:0 1 2 3 _1 _2 _3 _4) 1 2 3 4 5
				+---------+-----------+------------+-------------+
				|1 2 3 4 5|1 3 6 10 15|1 4 10 20 35|1  2  3  4  5|
				|         |           |            |1  3  6 10 15|
				|         |           |            |1  4 10 20 35|
				|         |           |            |1  5 15 35 70|
				|         |           |            |1  1  1  1  1|
				|         |           |            |1  0  0  0  0|
				|         |           |            |1 _1  0  0  0|
				|         |           |            |1 _2  1  0  0|
				+---------+-----------+------------+-------------+
				

Example 2: Fibonacci Sequence

				   +/\@|.^:(i.10) 0 1
				 0  1
				 1  1
				 1  2
				 2  3
				 3  5
				 5  8
				 8 13
				13 21
				21 34
				34 55
				   {. +/\@|.^:n 0 1x [ n=:128       NB. n-th term of the Fibonacci sequence
				251728825683549488150424261
				   {.{: +/ .*~^:k 0 1,:1 1x [ k=:7  NB. (2^k)-th term of the Fibonacci sequence
				251728825683549488150424261
				

Example 3: Newton Iteration

				   -:@(+2&%)^:(0 1 2 3) 1
				1 1.5 1.41667 1.41422
				   -:@(+2&%)^:(_) 1
				1.41421
				   -:@(+2&%)^:a: 1
				1 1.5 1.41667 1.41422 1.41421 1.41421
				   %: 2
				1.41421
				

Example 4: Subgroup Generated by a Set of Permutations

				   sg=: ~. @ (,/) @ ({"1/~) ^: _ @ (i.@{:@$ , ])
				   sg ,: 1 2 3 0 4
				0 1 2 3 4
				1 2 3 0 4
				2 3 0 1 4
				3 0 1 2 4
				   # sg 1 2 3 4 5 0 ,: 1 0 2 3 4 5
				720
				

Example 5: Transitive Closure

				   x=: (#x)<. (#x),~x=: (i.20)+1+20 ?.@# 3
				   (i.#x) ,: x
				0 1 2 3 4 5 6 7  8  9 10 11 12 13 14 15 16 17 18 19 20
				1 4 5 5 7 6 9 9 10 12 11 14 14 15 16 18 18 18 20 20 20
				   {&x^:(<15) 0
				0 1 4 7 9 12 14 16 18 20 20 20 20 20 20
				   {&x^:a: 0
				0 1 4 7 9 12 14 16 18 20
				   x {~^:a: 0
				0 1 4 7 9 12 14 16 18 20
				
				

Interpretation: x specifies a directed graph with nodes numbered i.#x and links from i to i{x . For example, the links are: 0 1 , 1 4 , 2 5 , 3 5 and so on. Then {&x^:a:0 or x{~^:a:0 computes all the nodes reachable from node 0.

Example 6: Transitive Closure

Each record of a file begins with a byte indicating the record length (excluding the record length byte itself), followed by the record contents. Given a file, the verb rec below produces the list of boxed records.

				rec=: 3 : 0
				 n=. #y
				 d=. _1 ,~ n<.1+(i.n)+a.i.y
				 m=. d {~^:a: 0
				 ((i.n) e. m) <;._1 y
				)
				
				randomfile=: 3 : 0
				 c  =. 1+y ?@$ 255           NB. record lengths
				 rec=. {&a.&.> c ?@$&.> 256  NB. record contents
				 (c{a.),&.> rec              NB. records with lengths
				)
				
				   boxed_rec=: randomfile 1000
				   $ boxed_rec
				1000
				
				   file=: ; boxed_rec
				   $ file
				132045
				
				   r=: rec file
				   $r
				1000
				
				   r -: }.&.> boxed_rec
				1
				
				

The last phrase verifies that the result of rec are the records without the leading length bytes.

</examples> </form> <form> <syntax>u^:v</syntax> <definition valence="monad"> <name>Power</name> <rank>_</rank> <description>

The case of ^: with a verb right argument is defined in terms of the noun right argument case (<a href="d202n.htm">u ^: n</a>) as follows:

  u ^: v y   u^:(  v y) y

</description> </definition> <definition valence="dyad"> <name>Power</name> <rank>_ _</rank> <description>

The case of ^: with a verb right argument is defined in terms of the noun right argument case (<a href="d202n.htm">u ^: n</a>) as follows:

x u ^: v y x u^:(x v y) y

</description> </definition> <examples>

For example:

				   x=: 1 3 3 1
				   y=: 0 1 2 3 4 5 6
				   x p. y
				1 8 27 64 125 216 343
				
				   x p. ^: (]>3:)"1 0 y
				0 1 2 3 125 216 343
				
				   a=: _3 _2 _1 0 1 2 3
				   %: a
				0j1.73205 0j1.41421 0j1 0 1 1.41421 1.73205
				
				   * a
				_1 _1 _1 0 1 1 1
				
				   %: ^: * " 0 a
				9 4 1 0 1 1.41421 1.73205
				
				   *: a
				9 4 1 0 1 4 9
				
				

The following monads are equivalent. (See the example of ^ T. _ in the definition of <a href="dtcapdot.htm">Taylor Approximation (T.)</a> .)

				
				   g=: u ^: p ^: _
				   h=: 3 : 't=. y while. p t do. t=. u t end.'
				
				   u=: -&3
				   p=: 0&<
				   (g"0 ; h"0) i. 10
				+-------------------------+-------------------------+
				|0 _2 _1 0 _2 _1 0 _2 _1 0|0 _2 _1 0 _2 _1 0 _2 _1 0|
				+-------------------------+-------------------------+
				

All partitions of an integer, based on an algorithm by R.E. Boss on 2005-07-05:

				partition=: [: final ] (,new)@]^:[ (<i.1 0)"_
				final    =: 0 <@-."1~ >@{:
				new      =: [: <@; (-i.)@# cat&.> ]
				cat      =: [ ,. (>: {."1) # ]
				
				   partition 5
				+-+---+---+-----+-----+-------+---------+
				|5|4 1|3 2|3 1 1|2 2 1|2 1 1 1|1 1 1 1 1|
				+-+---+---+-----+-----+-------+---------+
				
				

</examples>

   </form>
 </primitive>
 <primitive type="adverb">
   <symbol>~</symbol>
   <form>

<syntax>u~</syntax> <definition valence="monad"> <name>Reflexive</name> <rank>_</rank> <description>

u~ y y u y . For example, ^~ 3 is 27, and +/~ i. n is an addition table.

</description> </definition> <definition valence="dyad"> <name>Passive</name> <rank>ru lu</rank> <description>

~ commutes or crosses connections to arguments: x u~ y y u x .

</description> </definition> <examples>

Certain uses of the reflexive and passive are illustrated below:

				   x=: 1 2 3 4 [ y=: 4 5 6
				   x (,.@[ ; ^/ ; ^/~ ; ^/~@[ ; ]) y
				+-+-------------+-------------+-----------+-----+
				|1|  1    1    1|4 16  64  256|1  1  1   1|4 5 6|
				|2| 16   32   64|5 25 125  625|2  4  8  16|     |
				|3| 81  243  729|6 36 216 1296|3  9 27  81|     |
				|4|256 1024 4096|             |4 16 64 256|     |
				+-+-------------+-------------+-----------+-----+
				
				   into=: %~
				   (i. 6) % 5
				0 0.2 0.4 0.6 0.8 1
				
				   5 into i. 6
				0 0.2 0.4 0.6 0.8 1
				
				   from=: -~
				   (i.6) - 5
				_5 _4 _3 _2 _1 0
				
				   5 from i.6
				_5 _4 _3 _2 _1 0
				
				   (x %/ y);(x %~/ y);(x %/~ y)
				+-----------------+-------------------+------------------+
				|0.25 0.2 0.166667|      4       5   6|4   2 1.33333    1|
				| 0.5 0.4 0.333333|      2     2.5   3|5 2.5 1.66667 1.25|
				|0.75 0.6      0.5|1.33333 1.66667   2|6   3       2  1.5|
				|   1 0.8 0.666667|      1    1.25 1.5|                  |
				+-----------------+-------------------+------------------+
				

</examples> </form> <form> <syntax>m~</syntax> <definition valence="monad"> <name>Evoke</name> <rank>_</rank> <description>

If m is a name, then 'm'~ is equivalent to m . For example:

					   m=: 2 3 4
					   'm'~
					2 3 4
					
					   m=: +/
					   'm'~ 2 3 5 7
					17
					
					   m=: /
					   + 'm'~ 2 3 5 7
					17
					

</description> </definition> </form>

 </primitive>

</dictionary>


I get the following error attempting to display the above XML in MSIE7:

The XML page cannot be displayed

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

The operation completed successfully. Error processing resource 'file:///C:/amisc/J/NYCJUG/200911/primitiveEG.xml'. Line 1...

                                         <td><big>↔</big><tt> u^:(i.m) y</tt></td&...

-- Devon McCormick <<DateTime(2009-10-20T18:03:006Z)>>

I suspect this is because the HTML fragments are not correctly embedded in the XML file.

The document was intended as an idea of how a dictionary page could be stored as an XML structure, I didn't take this or the CSS formatting ideas any further because Roger didn't see any value in it. -- Ric Sherlock <<DateTime(2009-10-21T13:46:24+1300)>>


  • It doesn't have to be XML: can be HTML or XHTML with well-defined elements, classes and attributes throughout so that it renderes in the browser, but also is susceptible to automated parsing.
  • The leading spaces should be minimized or eliminated, especially in
     elements.

-- Oleg Kobchenko <<DateTime(2009-10-21T02:48:05-0500)>>