Representation
An Implementation of J

Atomic Representation
Boxed Representation
Tree Representation
Linear Representation



Atomic Representation

5!:1 is a verb that applies to a boxed name, and produces the atomic representation of the named object. Gerunds (results of the ` conjunction) are arrays of atomic representations. The adverb 5!:0 defines an object from its representation.

The atomic representation is a boxed list of two boxes:
   noun symbol value
verb symbol arguments
adverb symbol arguments
conjunction        symbol arguments

The symbol is a string computed by function
spellout in file ws.c. For a primitive with an assigned symbol (for example + or /.), the symbol is simply that word; for those without, the symbol is one of the following:

   '0'           noun
   '2'           hook
   '3'           fork
   '4'           bonded conjunction
   '5'           2-element a-train or c-train
   '6'           3-element a-train or c-train
The "value" in the representation of a noun is just the noun itself; arguments in the representation of a verb, adverb, or conjunction are themselves atomic representations. If an object is uniquely identified by the symbol alone, then the second box is elided, and the representation is the boxed symbol alone.

The following examples illustrate atomic representation:

   ar=: 5!:1

   plus=: +          sum=: +/          mean=: +/ % #
   ar <'plus'        ar <'sum'         ar <'mean'
┌─┐               ┌───────┐         ┌─────────────────┐
│+│               │┌─┬───┐│         │┌─┬─────────────┐│
└─┘               ││/│┌─┐││         ││3│┌───────┬─┬─┐││
                  ││ ││+│││         ││ ││┌─┬───┐│%│#│││
                  ││ │└─┘││         ││ │││/│┌─┐││ │ │││
                  │└─┴───┘│         ││ │││ ││+│││ │ │││
                  └───────┘         ││ │││ │└─┘││ │ │││
                                    ││ ││└─┴───┘│ │ │││
                                    ││ │└───────┴─┴─┘││
                                    │└─┴─────────────┘│
                                    └─────────────────┘
   +`(+/)`(+/ % #)
┌─┬───────┬─────────────────┐
│+│┌─┬───┐│┌─┬─────────────┐│
│ ││/│┌─┐│││3│┌───────┬─┬─┐││
│ ││ ││+││││ ││┌─┬───┐│%│#│││
│ ││ │└─┘│││ │││/│┌─┐││ │ │││
│ │└─┴───┘││ │││ ││+│││ │ │││
│ │       ││ │││ │└─┘││ │ │││
│ │       ││ ││└─┴───┘│ │ │││
│ │       ││ │└───────┴─┴─┘││
│ │       │└─┴─────────────┘│
└─┴───────┴─────────────────┘
   a=: 5             xenos=: !:
   ar <'a'           ar <'xenos'       ar <'ar'
┌─────┐           ┌──┐              ┌──────────────────┐
│┌─┬─┐│           │!:│              │┌──┬─────────────┐│
││0│5││           └──┘              ││!:│┌─────┬─────┐││
│└─┴─┘│                             ││  ││┌─┬─┐│┌─┬─┐│││
└─────┘                             ││  │││0│5│││0│1││││
                                    ││  ││└─┴─┘│└─┴─┘│││
                                    ││  │└─────┴─────┘││
                                    │└──┴─────────────┘│
                                    └──────────────────┘
   lgamma=: ^.@!@<:
   ar <'lgamma'
┌───────────────────┐
│┌─┬───────────────┐│
││@│┌──────────┬──┐││
││ ││┌─┬──────┐│<:│││
││ │││@│┌──┬─┐││  │││
││ │││ ││^.│!│││  │││
││ │││ │└──┴─┘││  │││
││ ││└─┴──────┘│  │││
││ │└──────────┴──┘││
│└─┴───────────────┘│
└───────────────────┘

Boxed Representation

5!:2 is a verb that applies to a boxed name, and produces the boxed representation of the named object. The representation can be modelled as follows:

ar     =: 5!:1
boxed  =: 32&=@(3!:0)
oarg   =: >@(1&{)

bxroot =: (<1 0)&C.@,`] @. (e.&(,&.>'0123456789')@[)

bxx    =: {. bxroot bx&.>@oarg
bxgl   =: {. bxroot (bxx&.>@{. , bx &.>@}.)@oarg
bxgr   =: {. bxroot (bx &.>@{. , bxx&.>@}.)@oarg
bxg    =: bxgr`bxgl`bxx @. (i.&(<,'`')@oarg)
bxtil  =: bxx`(oarg@>@{.@oarg) @. ((<,'0')&=@{.@>@{.@oarg)
bxcase =: oarg`bxgl`bxgl`bxg`bxtil`bxx @. ((;:'0@.`:4~')&i.@{.)
bx     =: ]`bxcase @. boxed

brep   =: ,@<`[ @. boxed @ bx @ > @ ar

   brep <'brep'
┌───────────────────────────────────┬─┬──┐
│┌─────────────────────────────┬─┬─┐│@│ar│
││┌──────────────────────┬─┬──┐│@│>││ │  │
│││┌───────────┬──┬─────┐│@│bx││ │ ││ │  │
││││┌───────┬─┐│@.│boxed││ │  ││ │ ││ │  │
│││││┌─┬─┬─┐│[││  │     ││ │  ││ │ ││ │  │
││││││,│@│<││ ││  │     ││ │  ││ │ ││ │  │
│││││└─┴─┴─┘│ ││  │     ││ │  ││ │ ││ │  │
││││└───────┴─┘│  │     ││ │  ││ │ ││ │  │
│││└───────────┴──┴─────┘│ │  ││ │ ││ │  │
││└──────────────────────┴─┴──┘│ │ ││ │  │
│└─────────────────────────────┴─┴─┘│ │  │
└───────────────────────────────────┴─┴──┘
The model is divided into groups of verbs. The first group are utilities:
     ar              atomic representation
boxed 1 if boxed
oarg open the second element of the list argument

bxroot produces an infix representation from a root r and its list of arguments a. If r is a digit, it denotes a primitive without an assigned word (e.g. '3' denotes a fork; see Atomic Representation), and the result of bxroot is a; otherwise, r bxroot a produces:
     a,r if one argument
({.a),r,(}.a)   if two arguments
r if no arguments (primitive)

The other verbs named with the bx prefix apply to the opened atomic representation, and embody logic to effect "nice" displays for various special cases. The agenda items in bxcase are:

    ID      Agenda
0       oarg    noun (leaf)
@. bxgl gerundial left subtree
`: bxgl gerundial left subtree
4 bxg bonded conjunction; gerundial left or right subtree
~ bxtil possible instance of evoke
other bxx none of the above

brep is a model of 5!:2.


Tree Representation

5!:4 is a verb that applies to a boxed name, and produces a literal table of the tree representation of the named object. Thus:

   tree=: connect @ > @ (,.&.>/) @ ('0'&root ; ]) @ (tr@>@ar)
   5!:4 <'tree'
                        ┌─ connect     
                  ┌─ @ ─┴─ >           
            ┌─ @ ─┤               ┌─ ,.
            │     └─ / ─── &. ────┴─ > 
            │                          
      ┌─ @ ─┤           ┌─ '0'         
      │     │     ┌─ & ─┴─ root        
      │     └─────┼─ ;                 
      │           └─ ]                 
── @ ─┤                                
      │           ┌─ tr                
      │     ┌─ @ ─┴─ >                 
      └─ @ ─┴─ ar 
The tree representation can be modelled as follows:

ar      =: 5!:1
lr      =: 3 : '5!:5 <''y.'''
boxed   =: 32&= @ (3!:0)
mt      =: 0&e.@$
oarg    =: >@(1&{)
shr     =: |.!.''
shl     =: 1&(|.!.'')
mat     =: (1 1&}.)@(_1 _1&}.)@":@<
boxc    =: {. 9!:6 ''
dash    =: 10{boxc

extent  =: (+./\ *. +./\.) @ (' '&~:) @: ({."1)
limb1   =: 1&|.@$ 1&~: }. (10 6 0{boxc)&,@($&(9{boxc))
limb    =: -@(i.&1)@[ |. #@[ {. limb1@]
pfx     =: (limb +/)@extent ,. ]
pad     =: [ {. ] ,. dash&=@({:"1)@] {  ' '&,:@($&dash)@(-&{: $)
take    =: pad`($&' '@[) @. (mt@])
rc      =: #@>@{."1 ; >./@:({:@$@>)
kernt   =: (0{boxc)&=@shl@[ *. ' '&~:@]
kernb   =: (6{boxc)&=@] *. ' '&~:@shl@[
kern    =: (<0 0)&{&>"2 (kernt +./"1@:+. kernb) (<_1 0)&{&>"2
gap     =: ,&.>"_1 {&((0 1$' ');1 1$' ')@kern
graft   =: (pfx&.>@{. 0} ]) @ (,&.>/) @ gap @ ({@rc take&.> ])

lab     =: ,: @ (2&|.) @ ((' ',dash,dash,' ')&,)
label   =: lab`((,.dash)&[) @. (e.&'0123456789'@{.)
center  =: ((i.&1) -@+ <.@-:@(+/))@] |. #@] {. [
root    =: label@[ center extent@>@{.@]

leaf    =: ,@<@(((,:dash,' ')&[ center $&1@#) ,. ])@mat@":

trx     =: >@{. (root ; ]) graft@:(tr@>)@oarg
trgl    =: >@{. (root ; ]) graft@:(trx@>@{. , tr @>@}.)@oarg
trgr    =: >@{. (root ; ]) graft@:(tr @>@{. , trx@>@}.)@oarg
trg     =: trgr`trgl`trx @. (i.&(<,'`')@oarg)
trtil   =: trx`(leaf@oarg@>@{.@oarg) @. ((<,'0')&=@{.@>@{.@oarg)
trnoun  =: leaf @ lr @ oarg
trcase  =: trnoun`trgl`trgl`trg`trtil`trx @. ((;:'0@.`:4~')&i.@{.)
tr      =: leaf`trcase @. boxed

rep     =: [. & (((# i.@#)@,@) (@])})
right   =: (5{boxc) rep (e.&(9{boxc) *. shr"1@(e.&dash))
cross   =: (4{boxc) rep (e.&(5{boxc) *. shl"1@(e.&dash))
left    =: (3{boxc) rep (e.&(9{boxc) *. shl"1@(e.&dash))
bot     =: (7{boxc) rep (e.&(6{boxc) *. shr"1@(e.&dash))
connect =: bot @ left @ cross @ right

tree    =: connect @ > @ (,.&.>/) @ ('0'&root ; ]) @ (tr@>@ar)
The model is divided into groups of definitions (which are verbs unless indicated otherwise). The first group are utilities:
     ar         atomic representation
boxed 1 if boxed
mt 1 if empty
oarg open the second element of the list argument
shr shift right
shl shift left
mat a literal matrix image of the argument
boxc (noun) box drawing characters
dash (noun) the "dash" in the set of box drawing characters

A "generational tree" (GT) is a list of boxed literal tables having the same number of rows, such that nodes at the same depth are in the same box. For example, the GT for tree is:
   ┌──────┬──────┬──────┬──────┬──────────┬─────┐
   │      │      │      │      │┌─ connect│     │
   │      │      │      │┌─ @ ─│└─ >      │     │
   │      │      │┌─ @ ─││     │          │┌─ ,.│
   │      │      ││     │└─ / ─│── &. ────│└─ > │
   │      │      ││     │      │          │     │
   │      │┌─ @ ─││     │      │┌─ '0'    │     │
   │      ││     ││     │┌─ & ─│└─ root   │     │
   │      ││     │└─────││─ ;  │          │     │
   │      ││     │      │└─ ]  │          │     │
   │── @ ─││     │      │      │          │     │
   │      ││     │      │┌─ tr │          │     │
   │      ││     │┌─ @ ─│└─ >  │          │     │
   │      │└─ @ ─│└─ ar │      │          │     │
   └──────┴──────┴──────┴──────┴──────────┴─────┘
graft is the main verb in the next group. The argument is a table whose rows are GTs for the nodes at the same depth. The result is a GT.

root accepts a string left argument and a GT right argument. The result is a literal matrix with the string centered relative to the GT.

leaf computes a unitary (single-element) GT from its argument.

tr applies to the opened atomic representation of an object and produces a GT. The verbs named with the tr prefix embody logic to effect "nice" displays for various special cases. The agenda items in trcase are:
    
ID             Agenda
0         leaf@oarg    noun (leaf)
@. trgl gerundial left subtree
`: trgl gerundial left subtree
4 trg bonded conjunction; gerund left or right
~ trtil possible instance of evoke
other trx none of the above

rep is a conjunction whose left argument is a single literal c and whose right argument is a proposition p, deriving a verb such that the phrase c rep p y replaces with c the positions in y marked by p y. connect substitutes ┴ (bot), ├ (left), ┼ (cross), and ┤ (right) at nexuses of the tree.

tree is a model of 5!:4.


Linear Representation

5!:5 is a verb that applies to a boxed name, and produces a literal list of the linear representation of the named object. Thus:

   lrep=: lr @ > @ ar

   5!:5 <'lrep'
lr@>@ar
   $ 5!:5 <'lrep'
7
The linear representation can be modelled as follows:

ar     =: 5!:1
boxed  =: 32&= @ (3!:0)
oarg   =: >@(1&{)
mtv    =: i.@0:
paren  =: ('('&,)@(,&')')
symb   =: $&' '@(e.&'.:')@{. , ]
quote  =: ''''
alp    =: (,65 97+/i.26){a.
dig    =: '0123456789'

slist  =: $&','@(1&=)
shape  =: mtv`slist`(,&'$'@":)@.(2&<.@#)`('i.'&,@":) @. (0&e.) @ $
vchar  =: >:@(quote&=)@, quote&,@(,"e)@# ,
vbox   =: }. @ ; @: (','&,@paren@('<'&,)@lnoun&.>)
value  =: vchar`vbox`(":!.18@,) @. (2 32&i.@(type * *@(*/)@$))
lnoun  =: shape , value

dotco  =: 2&=@# *. e.&'.:'@{:
name   =: e.&alp@{. *. *./@(e.&(alp,dig,'_'))@}: *. e.&(alp,dig,'_.:')@{:
num    =: e.&(dig,'_')@{. *. *./@(e.&(dig,'_ .ejdr'))
qstr   =: mtv -: -.@(~:/\)@(e."e) -."e@# ]
pstr   =: -.@(0&e.)@}:@(+/\)@({&1 _1 0)@('()'&i.)
nopar  =: 1&=@# +. dotco +. name +. num +. qstr +. pstr
cp     =: paren`] @. nopar

bp     =: ]`cp@.(' '&e.)
hfork  =: }.@;@:(' '&,@bp&.>)@]
left   =: bp@>@{.
right  =: mtv`(cp@>@{:)@.(1&<@#)
ins    =: left@] , symb@>@[ , right@]
act    =: ;@:(cp&.>)@]
insert =: hfork`hfork`act`act`act`ins @. ('23456'&i.@{.@>@[)

lx     =: {. insert lr&.>@oarg
ltie   =: lr`(}.@;@:('`'&,@cp@lr&.>)@oarg) @. ((<,'0')&=@{.)
lgl    =: {. insert (ltie&.>@{. , lr  &.>@}.)@oarg
lgr    =: {. insert (lr  &.>@{. , ltie&.>@}.)@oarg
lg     =: lgr`lgl`lx @. (i.&(<,'`')@oarg)
ltil   =: lx`(oarg@>@{.@oarg) @. ((<,'0')&=@{.@>@{.@oarg)
lcase  =: (cp@lnoun@oarg)`lgl`lgl`lg`ltil`lx @. ((;:'0@.`:4~')&i.@{.)
lr     =: symb`lcase@.boxed

lrep   =: lr @ > @ ar


NextPreviousIndexTable of Contents