Vocabulary/Valence

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Valence

Monad and dyad

A Verb can have either a single argument (to the right of the verb) or two arguments (to the left and right).

  • A verb with only a right argument is called a monad.
  • A verb with both right and left arguments is called a dyad.

Whether a verb is a monad or a dyad is its Valence.

A given verb's valence depends on the context it's being used in. It is not a property the verb keeps for life. When a given sentence containing the verb is executed, J decides from the syntax alone whether the verb is a monad or a dyad.

Often the same verb v behaves differently depending on whether it is called with one or two arguments.

  • called with just a y-argument we talk of the monadic form of v, or that v is called monadically.
  • called with both an x-argument and a y-argument we talk of the dyadic form of v, or that v is called dyadically.

Do all verbs have monadic and dyadic forms?

No.

A verb with both monadic and dyadic forms is called "dual-valence" or ambivalent.

All tacit verbs are ambivalent.

But it's possible to write a tacit verb having separate phrases to define the monadic form and the dyadic form of the verb. See Monad-Dyad (:) .

Example: the standard library verb: sort

sort is a

  • Standard Library word(s) residing in the 'z'-locale
  • Defined in the factory script stdlib.ijs which is located in  ~system/main/stdlib.ijs
  • View the definition(s) in a JQt session by entering:  open '~system/main/stdlib.ijs'

   sort
/:~ :/:

The two phrases of sort lie either side of (:). They define respectively

  • the monadic form:  /:~
  • the dyadic form:  /:

An explicit verb has separate code blocks to define the monadic form and the dyadic form of the verb. Either code block can be absent. See Monad-Dyad (:) .

  • If both code blocks are present, the verb is ambivalent.
  • If the 1st code block is absent, then the verb is dyadic-only, behaving as if defined using  dyad define . See Explicit (:).
  • If the 2nd code block is absent, then the verb is monadic-only, behaving as if defined using  monad define . See Explicit (:).

Can monad and dyad have different names?

Yes. If v is a primitive, its monadic and dyadic forms may be known by different names.

The NuVoc portal shows different names for the same primitive in many cases.

Example: Let v be the primitive verb (<)

  • called monadically - it is called Box
  • called dyadically - it is called Less Than.

y-argument and x-argument

In the body of an explicit verb definition, the name y always refers to the verb's right argument, and x always refers to the verb's left argument.

What's more, the names y and x are conventionally used for nouns which are intended to become the right and left arguments respectively of some given verb. NuVoc employs this convention throughout.

   % y              NB. monadic form of %
   x % y            NB. dyadic form of %

Example: Let v be the primitive verb %

  • called monadically - it returns the reciprocal of its right argument
  • called dyadically - it divides the left argument by the right argument
   % 4              NB. monadic %
0.25
   4 % 2            NB. dyadic %
2

Can a verb be called with no arguments?

Some verbs don't need arguments.

Some languages, e.g. APL (the ancestor of J), allow a function to be executed with no arguments (niladic form). But J has no niladic form. If you type the name of a verb without any arguments into the session window, J does not execute the verb. Instead it displays its definition.

As we said above, J decides from the syntax alone whether the verb is a monad or a dyad. It can't do this without finding a noun to serve as the y-argument.

A verb that ignores its arguments is conventionally executed by giving it an empty y-argument. Such verbs include

  • Several Foreigns (!:), e.g.  9!:14 (returns the J version)
  • User-written verbs that get their data from global nouns or the machine environment.

Example: The Constant Function (0:) ignores its arguments and always returns the value 0 . If you call  0: monadically, it needs no y-argument and ignores whatever noun you give it. Nevertheless a y-argument must be given, so we choose to give it the empty string: '' .

   0: ''
0

But if you give it no arguments at all, J computes a verb value, which it returns. The effect is to show you the verb's definition, i.e. itself, in this case.

   0:
0:

Example: The Foreign (9!:14) returns the J version. It needs no argument to be able to do this. Nevertheless it needs a y-argument for the sake of sentence syntax, otherwise J won't execute it.

   jver=: 9!:14
   jver ''
j602/2008-03-03/16:45

In this case the verb is so touchy that J signals an error if you give it any argument except an empty one.

   jver 0
|rank error: jver
|       jver 0
   jver 'the quick brown fox'
|length error: jver
|       jver'the quick brown fox'

But if you give jver no arguments at all, J doesn't execute it. Instead J computes a verb value, which it displays in the session. The effect is to show you the verb's definition

   jver
9!:14

Does your verb need more than 2 arguments?

Often we want to call a verb with more than 2 arguments.

There are a number of Strategies for dealing with Multiple Verb Arguments.