User:PascalJasmin/JON assignment free pure functional DSL

From J Wiki
Jump to navigation Jump to search

JSON is a text encoded data exchange format.

JON achieves the same capabilities with the enhancements of supporting any valid J noun, no matter hoe deeply nested and boxed, and in any dimension. The J limitation on not being able to box sparse arrays (and join them to other data) is the only limiting deficiency. JON encoding is very efficient in J. It is simply:

JON=: lr=: 3 : '5!:5 <''y'''

In addition to a data exchange protocol, JON (JOFF decoding) also serves as a purely assignment free functional language that is suitable for transmitting computations for safe execution on remote computers. J's sandboxing features (time and space execution limits) provides additional security enhancements to prevent non terminating or otherwise offensively large/compute intensive computations from affecting a client.

Although JOFF ensures that only a noun results, a computation can sometimes be more space efficient than the resulting data, and so JOFF has important (and unlimited) data compression applications.

The implementation is a simple white list of allowable J operators. It is possible to extend the white list to longer than 2 character operators and include some user defined words, though the reference implementation does not do so:

cocurrent 'jon'
safe1 =: '''_0123456789+*-<>|;,#{}()]~'
safe2 =: 'u:';'x:';'}.';'}:';'" ';'":';'! ';'$ ';'= ';'^ ';'^.';'a.';'a:';'L.';'L:';'j.';'i.';'i:';'$.'
isSafe =: *./@:(issafe2d +. issafe1d)@:;:
noevoke =: 3 : '*./ 2 ((''''''''  -: {.@:>@:{.) *: (<,''~'') -: {: )\ ;: y'
JOFF_z_ =: doSafe =: 0 0&$`(0 0&$`".@.isSafe)@. noevoke
JON_z_ =: lr_z_ =: 3 : '5!:5 <''y'''

NB. non essential debug tools
issafe1d =: safe1 e.~ {. &>
issafe2d =: safe2 e.~ 2&{. each

doSafed =: 3 : 0
o =. doSafe y
if. o = i.0 0 do. w=. ;: y
o =. 'terms not safe1' ; (] #~ -.@:issafe1d) w
o , 'terms not safe2' ; (] #~ -.@:issafe2d) w return. end.
o
)

In terms of compression applications, some database structures are the most amenable to JON compression. Sparse arrays can transmit differences between data sets.

  lr  $. 0 0 5 0 3 , 1e6 $ 0

5 3 (2 4)}1$.(,1000005);(,0);0+-~2

 JOFF  lr  $. 0 0 5 0 3 , 1e6 $ 0
2 │ 5
4 │ 3

   lr (2 | i.6);1+ 5* i.10
0 1 0 1 0 1;1+5*i.10          NB. note that lr keeps some computations compressed, but where it fails, can be manually replaced with computation.

   JOFF '(2 | i.6);1+ 5* i.10'
┌───────────┬───────────────────────────┐
│0 1 0 1 0 1│1 6 11 16 21 26 31 36 41 46│
└───────────┴───────────────────────────┘

While noun only replacements has a data transfer focus, it can also be leveraged by client server aplications where data can be safely sent to server processes and results returned. A workaround for sending functions to be executed by a server safely occurs if the server expects to see the function, and knows what data to apply it to. The server can then safely apply the function to the data.

   lr '+:' ; 2
'+:';2

 JOFF lr '+:' ; 2
┌──┬─┐
│+:│2│
└──┴─┘

   JOFF ;: inv ": each JOFF lr '+:' ; 2
4

   JOFF ;: inv ": each JOFF lr ' ". +:' ; 2
NB. no output since ". is not safe

While no assignments are allowed inside expressions and JOFF sublanguage, data to be assigned can be transmitted. The acceptor can sandbox the assignments into a client's locale and evaluate and return functions based on those assignments. Web Cookies is an example application. But so is JSON-like data records where field and record values are transmitted as boxed structures. J's box display facilities makes JON more readable/manipulable than JSON within J.

Something like a SGML DOM tree is storable within J box trees a bit more directly than pure text which requires terminating elements:

   'html' ;< 'body' ; < 'a'; < ('href';'www.jsoftware.com'), ('' ; 'click here')
┌────┬───────────────────────────────────────────────┐
│html│┌────┬────────────────────────────────────────┐│
│    ││body│┌─┬────────────────────────────────────┐││
│    ││    ││a│┌────┬─────────────────┬┬──────────┐│││
│    ││    ││ ││href│www.jsoftware.com││click here││││
│    ││    ││ │└────┴─────────────────┴┴──────────┘│││
│    ││    │└─┴────────────────────────────────────┘││
│    │└────┴────────────────────────────────────────┘│
└────┴───────────────────────────────────────────────┘