[comp.lang.apl] Forks, Hooks and With in J

kjell@ygdrasil.ucsc.edu (Kjell E Post) (06/03/91)

I've ordered the Tangible Math book and started to program in J, but find
the use of & (with) confusing, especially in conjunction with forks and hooks.

For instance, (0&,) and (,&0) seems to represent the trees

	  ,			,
	 / \		       / \
	0			  0

respectively.  Is there any reason why this notation was chosen, i.e.
what is the underlying functionality of '&'?  

I'm also curious about the expressibility of '&', fork, and hook.  
In particular, I'm interested in representing the following dag as a verb,
using only '&', '~', fork/hook, and the items in the dag.

           #
          / \
	+:   \
       /  \   \
      /     ,  \
     |     / \  \
     |   }.   1  |
     |    |      |
     `----=  	 |
	 / \	 |
	/   \	 |
       ,     \	 |
      /	\     \	/
     0	 +     ,
	/ \   / \
           1     0		<--- Note: the empty leaves represents
					   argument positions.

--
  |   |  |\  /| |\  |\   | Kjell Post               | Vi i Sverige har blivit
  |  /|\ | \/ | | \ | \  | CS Grad Student, UCSC    | trygghetsnarkomaner.
 /|\ \|/ |    | |   |    | email: kjell@cs.ucsc.edu | Livet best}r inte bara av
/ | \ |  |    | |   |    `--------------------------' trygghet.  --Curt Nicolin

rockwell@socrates.umd.edu (Raul Rockwell) (06/03/91)

Kjell E Post:
   I've ordered the Tangible Math book and started to program in J,
   but find the use of & (with) confusing, especially in conjunction
   with forks and hooks.

   For instance, (0&,) and (,&0) seems to represent the trees
	     ,			,
	    / \		       / \
	   0			  0
   respectively.  Is there any reason why this notation was chosen, i.e.
   what is the underlying functionality of '&'?  

Forks and hooks are types of verb phrases.  Given a verb phrase of the
form  v1 v2 v3  a fork will be constructed, and given a verb phrase of
the form  v2 v3  a hook will be constructed.  "Trees" involving  0  or
&  are neither forks nor hooks (0  is a noun and  &  is an adverb).

The "underlying functionality" of  &  is, roughly, that of binding an
object to a function.  If you bind a noun to a verb, as above, you
create an intransitive verb derived from the transitive form of the
original verb.  If you bind a verb to a verb  v1&v2  then the
intransitive form of v2 is used as a preprocessor for v1 (the
distinction between the transitive and intransitive form for v1 is
determined from context).

Raul Rockwell

wdr@wang.com (William Ricker) (06/06/91)

rockwell@socrates.umd.edu (Raul Rockwell) writes:
[in answer to a question on representing a DAG with fork, hook, and &]

>Forks and hooks are types of verb phrases.  ... [expressions like]
> v1 v2 v3  [is] a fork [and]  v2 v3 [is] a hook [.]
>&  are neither forks nor hooks (0  is a noun and  &  is an adverb).
    I note that unless the fork or hook is the entire expression,
to indicate that it is a fork/hook parenthesis will be required, else it
will just execute left to right.

>The "underlying functionality" of  &  is, roughly, that of binding an
>object to a function.  If you bind a noun to a verb, as above, you
>create an intransitive verb derived from the transitive form of the
>original verb.  If you bind a verb to a verb  v1&v2  then the
>intransitive form of v2 is used as a preprocessor for v1 (the
>distinction between the transitive and intransitive form for v1 is
>determined from context).

In the fork and hook, it seems that the distinction between dyadic and monadic
(which I think is the same as you mean by transitive & instransitive) also
seems to be determined by context.  However, sometimes I want to express
   (v1 n1) v2 (v3 n2)
 as 
    n1 (v1 v2 v3) n2
but if both v1 and v3 have dyadic interpretations, the parser seems to assume
I meant :
    (n1 v1 n2) v2 (n1 v2 n2)
which is the other fork.

This can be solved by & "with" and sufficient use of ()'s, or by the identity
combinator ] to make hook forcing the verb v1 to monadic case : (]v1).  the &
tree constructor is the natural translation, but the ] option is the only
method I've found so far to mark a verb usage as monadic case.
   Example: if I try to re-code a remove-blanks-from-string:
    trim=. '(-. '' ''=y.) # y. ' : ''
 or equivalently
    trim=. 'y. #~ -. '' ''=y. ' : ''
as an implicit or functional defintion, the "obvious" hook
    ftrim=. #~ (-. ' '&=)             
doesn't work, because although I'm thinking of monadic -. as NOT, the parser
thinks dyadic set difference; and thus I get a double-hook not a hook
and composition of two monadics.  So I can do:
    ftrim=. #~ (-.  &(=&' '))
or, if I really want to do it with hooks,
    ftrim=. #~ ((]-.)' '&=)
-- 
/s/ Bill Ricker                wdr@wang.wang.com 
"The Freedom of the Press belongs to those who own one."
*** Warning: This account is not authorized to express opinions. ***

rockwell@socrates.umd.edu (Raul Rockwell) (06/06/91)

William Ricker:
..     I note that unless the fork or hook is the entire expression,
.. to indicate that it is a fork/hook parenthesis will be required,
.. else it will just execute left to right.

Yeah, there are several different ways of combining functions.

.. In the fork and hook, it seems that the distinction between dyadic
.. and monadic (which I think is the same as you mean by transitive &
.. intransitive) also seems to be determined by context.

yeah, that's what I meant by transitive/intransitive.  And I don't
have a really good feel for cases like:
  n (f:'' g h) m

I think the rules on this got changed on me with one of the J version
changes, but I haven't played with it enough to pin it down.

.. However, sometimes I want to express (v1 n1) v2 (v3 n2) as n1 (v1
.. v2 v3) n2 but if both v1 and v3 have dyadic interpretations, the
.. parser seems to assume I meant: (n1 v1 n2) v2 (n1 v2 n2) which is
.. the other fork.

Actually,  (v1 n1) v2 (v3 n2)  isn't a fork, because n1 is a different
object than n2.  If you mean you want to construct a function  f
where  n1 f n2  computes the same result as  (v1 n1) v2 (v3 n2), I'd use
   f =. v1@[ v2 v3@]

.. Example: if I try to re-code a remove-blanks-from-string:
..     trim=. '(-. '' ''=y.) # y. ' : ''
..  or equivalently
..     trim=. 'y. #~ -. '' ''=y. ' : ''
.. as an implicit or functional defintion, the "obvious" hook
..     ftrim=. #~ (-. ' '&=)
.. doesn't work, because although I'm thinking of monadic -. as NOT,
.. the parser thinks dyadic set difference; and thus I get a
.. double-hook not a hook and composition of two monadics.

try
   ftrim=. #~  -.@(' '&=)

Note that  v@u  always uses the monadic definition of  v

   [two other working solutions by Bill Ricker elided]

Raul Rockwell

hui@yrloc.ipsa.reuter.COM (Roger Hui) (06/11/91)

Comments on recent postings by Kjell Post, Raul Rockwell, and Bill Wicker.
 
Expressions in J are better represented as trees than as DAGs.
For example, try representing  (a+b)*(b+c)  as a DAG.
 
&  , like all other conjunctions, accepts noun or verb arguments,
thereby giving rise to four possible cases of derivations.  In principle,
the four cases need not be related, nor do the monadic and dyadic defns.
   m & n    undefined
   m & v    with,     m&v y  is  m v y  ;  x m&v y  has empty domain
   u & n    with,     u&n y  is  y u n  ;  x u&n y  has empty domain
   u & v    compose,  u&v y  is  u v y  ;  x u&v y  is  (v x) u (v y)
 
Many useful verbs can be defined as  m&v  or  u&n  :
  0&-  negation     ^&0.5 square root    0&<     positive
  _1&* negation     10&^. base-10 log    a.&i.   ASCII code
  +&1  increment    0&{   first          -.&' '  remove blanks
  -&1  decrement    _1&{  last           2&(-~\) first difference
 
All verbs are ambivalent, although some verbs have empty monadic
or dyadic domains.  Whether the monadic or the dyadic definition of
a verb is applied, is determined by context, as defined by the first
three parsing rules in Table 2 of the dictionary.  These parsing rules
apply to all verbs (whatever their derivation).
 
In the fork (f g h), f and g are ambivalent, and whether f or h
have empty monadic or dyadic domains would not be "discovered"
until the derived verb is applied.  For  u=.f@[ g h@]  ,
  x u y                    u y
  x (f@[ g h@]) y          (f@[ g h@]) y          defn of u
  (x f@[ y) g (x h@] y)    (f@[ y) g (h@] y)      defn of fork
  (f x[y) g (h x]y)        (f [ y) g (h ] y)      defn of @
  (f x) g (h y)            (f y) g (h y)          defn of [ and ]
 
As in the above example, and as specified in the dictionary, a fork
(hook) is an isolated sequence of 3 verbs (2 verbs).  Parentheses may
be required to effect that isolation.
 
The explicit verb  trim=.'(-.'' ''=y.)#y.' : ''  can be stated tacitly
as  -.&' '  .  ("Explicit" because the argument is explicitly mentioned;
"tacit" because not.)  Translations more faithful to the original:
   -.@(' '&=) # [
   #~ -.@(' '&=)
 
As noted in Bill Ricker's msg,  #~ (-. ' '&=)  is not an equivalent
computation.  (-. ' '&=)  is a hook not because  -.  is dyadic
(it is ambivalent), but because the parsing rules specify that an
isolated sequence of 2 verbs is a hook.  This derives a verb v, whence
#~ v  is an isolated sequence of 2 verbs, and is another hook.
 
J 3.1 has an explicit-to-tacit translator:
   'x.+y.' : 11
+-+-+-+
|[|+|]|
+-+-+-+
   '(x.+y.)*(x.-y.)' : 11
+-------+-+-------+
|+-+-+-+|*|+-+-+-+|
||[|+|]|| ||[|-|]||
|+-+-+-+| |+-+-+-+|
+-------+-+-------+
   '+/y.' : 11
+-----+-+-+
|+-+-+|@|]|
||+|/|| | |
|+-+-+| | |
+-----+-+-+
   '(-.'' ''=y.)#y.' : 11
+--------------------+-+-+
|+--+-+-------------+|#|]|
||-.|@|+-------+-+-+|| | |
||  | ||+-+-+-+|@|]||| | |
||  | ||| |&|=|| | ||| | |
||  | ||+-+-+-+| | ||| | |
||  | |+-------+-+-+|| | |
|+--+-+-------------+| | |
+--------------------+-+-+
 
The last result is the verb  -.@((' '&=)@]) # ]  (a fork).  Not all
explicit verbs can be so translated.

-----------------------------------------------------------------
Roger Hui
Iverson Software Inc., 33 Major Street, Toronto, Ontario  M5S 2K9
(416) 925 6096