Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!spool.mu.edu!uwm.edu!src.honeywell.com!The-Star.honeywell.com!umn.edu!csus.edu!sfsuvax1.sfsu.edu!emclean
From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: Re: Parsing and Tracing J functions
Message-ID: <1993Feb18.075947.28645@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
References: <1993Feb17.221313.2507@Princeton.EDU>
Distribution: W
Date: Thu, 18 Feb 1993 07:59:47 GMT
Lines: 192

(Ira H. Fuchs) writes:

>I am looking for are any tools that can be used to help me figure out what  
>various sentences are doing. A tool which parsed a sentence down to its  
>constituent parts and labelled them (verb,adverb, etc) would be very nice.  
>In other words, I would like to hand a name to a function and have as a  
>result a printout (tree) of the "parts of speech" and in turn each part  
>would be broken down where applicable.
>
>I would also like a tool which traced each line of a function and  
>(optionally) changes in the values of other given names. In short, I would  
>like some of the capabilites which APL has for tracing functions. 

 There currently are not many primitive tools for debugging
 in J. Erik Iverson is hopeful that the version of  J
 introduced in October will have a primitive debugger. I have
 no idea who is implementing it. Or if anyone is actually
 working on it. (Eugene, do you know ?)

 But the lack of tools encourages programmers to use tacit 
 definition. When you look at Roger Hui's and much of Raul Rockwell's 
 code you will see that it is common for a tacit verb to be constructed
 from 10 or 20 "component" verbs. They partly do this because it 
 is the J style, but mostly it is because each of the "component" 
 verbs can be tested interactively, so there is less need for a
 debugger. You'll know you've arrived as a J programmer when you
 think of the style of tracing explicit functions, as in APL,
 inefficient. (Well, OK, I admit I haven't had a chance to check out 
 Manugistics APL debugger lately.) So becomming better at J means
 you are practicing utilizing the modularity of functional 
 programming, reusing verbs, and not writing large*fun*beautiful 
 one liners.

 Now, Your question.

 I usually debug my explicit definition programs by inserting
 print statments. For example :

  b =. i. 0 0        
  b =. b, 'i =. 0'    
  b =. b, 'n =. 10'    
  b =. b, 'loop) '      
  b =. b, 'y. =. y. + 2' 
  b =. b, 'i =. >: i'     
  b =. b, '$. =. > (i = 10) { loop ; end ' 
  b =. b, 'end) y.'   
  t =. b : '' "0       
                         
  b =. i. 0 0             
  b =. b, '1!:2&2 i =. 0'    
  b =. b, '1!:2&2 n =. 10'    
  b =. b, '       loop) '      
  b =. b, '1!:2&2 y. =. y. + 2' 
  b =. b, '1!:2&2 i =. >: i'   
  b =. b, '       $. =. > (i = n) { loop ; end '
  b =. b, '       end) y.'
  tTrace =. b : '' "0      

 Now. Raul Rockwell has cooked up some really neat verbs,
 one for showing functional dependence of tacit verbs, and
 one for simple debugging. (I posted recently for some help
 on writting an adverb which returns a function which prints
 its arguments transparently to write a better
 debugger and basically translate some Scheme code into J.
 But know one helped me so RR's debugger is all we have 
 for now.) I find these programs more interesting as models
 of functional programming rather than as programming tools.
 In any case, these programs are RR at his ingenious best.
 I am very impressed by this stuff and have encouraged him to 
 post similar cool things. His next offering will be a 
 solution to the 7 queens problem. ... Ah, see, where
 was I.  Oh yes.  Here's RR's cools stuff I've been telling
 you about.  

 Also you should check out Mike Powell's "keyed" file utilities
 for J in the 1992 archive. *Really Great* .
 (sent to you under separate cover)

 Emmett (I've been having problems with my system lately and
         hope this isn't a repost.)

   9!:3 (5)
   NB. Raul Rockwells debugger go to the 1992 #948 archive for explanations.
   NB. RR's original used a read_block verb which is obsolete.
   NB. This should save some time for anyone wishing to study
   NB. this program.  It provides a great example of the power
   NB. one gets from a functional programming language. (I just
   NB. wish I *really* understood it -:)
   
   print   =. 1!:2&2
   resolve =. '(5!:1<x.)5!:0':1
   enquote =. ''''"], ], ''''"] 
   undocify =. ('o=:prefob x.' ; '(<x.)=. o resolve') :1
   types =. (;:'boolean literal integer floating complex boxed')
   d_type =. ''&,@>@{&types@((2 ^ i. 6)&i.)@(3!:0)@]
   p =. i. 0 0
   p =.p,' print@[ print @ ('' ''&,:@(x. & [ , d_type , '' $ '' & [ , ": @ $))'
   p_label =.p : 1
   
   p =. i. 0 0
   p =. p,' o  =. y. , ''_orig_'' '
   p =. p,' $. =. do }.~  0 < 4!:0 < o'
   p =. p,' do) (<o) =: y. resolve'
   p =. p,' o'
   prefob =. p : ''
   
   q =. i. 0 0
   q =. q,'print $.'
   q =. q,'print s1'
   q =. q,'$.=.s1 }.~ 0 < y.'
   q =. q,'s1) y. =. 3'
   q =. q,'y. =.y., 2'
   q =. q : ''
   
   p =. i. 0 0
   p =. p,'o=.prefob x.'
   p =. p,'(<x.) =: (enquote x.) p_label @ (o resolve) @ ((''entering '',enquote x.) p_label) : (((enquote x.),'' x. '') p_label @ [ (enquote x.) p_label @ (o resolve) ((enquote x.),'' y. '') p_label @ ]) " (o resolve) '
   docify =. p : 1
   
   NB. Useage is:    
   test =. +/
   test =. 'test' docify
   test 1 2 3
                          
entering 'test'integer $ 3
1 2 3
                
'test'integer $ 
6
6
   test =. 'test' undocify 
   test 1 2 3
6
   
   NB. RR's program demonstrating tacit functional dependencies

   9!:3 (5)
   boxedType       =. (32&=)@:(3!:0)
   characterType   =. (2&=)@:(3!:0)
   functionClass   =. (3&=)@:(4!:0)
   list            =. 1: = #@:$
   flatten         =. ;@:( <`flatten@.boxedType &. > )
   selectFnNames   =. #~  0:`functionClass @.((list *. characterType)@>)
   children        =. selectFnNames@:(~.)@:flatten@:(5!:2)@:]
   leaf            =. '] , x.&[@:] ' : 1
   parentNode	    =.  ] , <@:(, dependTree  children)
   buildTree       =.  parentNode ` ((<'')  leaf) @. ((0&=)@:#@:children)
   dependTree      =.  buildTree  ` ((<'*') leaf) @. (] e. [) " 1 0

   buildTree ;:'buildTree'
+---------+----------------------------------------------------------+
|buildTree|+----------+---------------------------------------------+|
|         ||parentNode|+----------+--------------------------------+||
|         ||          ||dependTree|+---------+-+                   |||
|         ||          ||          ||buildTree|*|                   |||
|         ||          ||          |+---------+-+                   |||
|         ||          |+----------+--------------------------------+||
|         ||          ||children  |+-------------+----------------+|||
|         ||          ||          ||selectFnNames|+-------------++||||
|         ||          ||          ||             ||functionClass||||||
|         ||          ||          ||             |+-------------++||||
|         ||          ||          ||             ||list         ||||||
|         ||          ||          ||             |+-------------++||||
|         ||          ||          ||             ||characterType||||||
|         ||          ||          ||             |+-------------++||||
|         ||          ||          |+-------------+----------------+|||
|         ||          ||          ||flatten      |+---------+-+   ||||
|         ||          ||          ||             ||flatten  |*|   ||||
|         ||          ||          ||             |+---------+-+   ||||
|         ||          ||          ||             ||boxedType| |   ||||
|         ||          ||          ||             |+---------+-+   ||||
|         ||          ||          |+-------------+----------------+|||
|         ||          |+----------+--------------------------------+||
|         |+----------+---------------------------------------------+|
|         ||children  |+-------------+----------------+             ||
|         ||          ||selectFnNames|+-------------++|             ||
|         ||          ||             ||functionClass|||             ||
|         ||          ||             |+-------------++|             ||
|         ||          ||             ||list         |||             ||
|         ||          ||             |+-------------++|             ||
|         ||          ||             ||characterType|||             ||
|         ||          ||             |+-------------++|             ||
|         ||          |+-------------+----------------+             ||
|         ||          ||flatten      |+---------+-+   |             ||
|         ||          ||             ||flatten  |*|   |             ||
|         ||          ||             |+---------+-+   |             ||
|         ||          ||             ||boxedType| |   |             ||
|         ||          ||             |+---------+-+   |             ||
|         ||          |+-------------+----------------+             ||
|         |+----------+---------------------------------------------+|
+---------+----------------------------------------------------------+
   
