kevinlawler / kona

Open-source implementation of the K programming language
ISC License
1.36k stars 139 forks source link

d:.(); d #543

Closed tavmem closed 4 years ago

tavmem commented 5 years ago

in k2.8

 rlwrap -n ./k
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems 
\ for help. \\ to exit.

  d:.(); d
.()

in Kona

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  d:.(); d
.[]
tavmem commented 5 years ago

Also, in k2.8

$ rlwrap -n ./k
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems 
\ for help. \\ to exit.

  a:.[]; a
.[]

in Kona

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  a:.[]; a
.[;]
tavmem commented 5 years ago

The first case is a recent regression. In commit 50ea6d501b0fe12a4e8ce64d8476ca37f92351ac made on 3/30/19 titled: fix #538: the root cause the first case works fine:

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  d:.(); d
.()
  a:.[]; a
.[;]

It was broken by the next commit 4466f8671dac81d7773cc95764741bcae4df6667 made on 4/4/19 titled: adjustments to sd() ... show details by the following change:

  -  printAtDepth(0,a,0,0,0,0);
  +  printAtDepth(0,a,0,0,0,1);
tavmem commented 5 years ago

The case 1 regression is fixed:

  d:.(); d
.()

Case 2 is not a relevant construct in k2.8:

  a:.[]; a
.[;]

But, like k2.8, the Kona interpreter should return the phrase unchanged.

Case 2 is not a high priority.

tavmem commented 4 years ago

Some other "odd" results for case 2:

K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems
\ for help. \\ to exit.

  .[]
.[]
  .[;]
.[;]
  .[;;]
.[;;]
  .[;;;]
.[;;;]
  .[;;;;]
.[;;;;]
kona      \ for help. \\ to exit.

  .[]
.[;]
  .[;]
.[;]
  .[;;]
.[;]
  .[;;;]
.[;;;]
  .[;;;;]
valence error
>
tavmem commented 4 years ago

To recap status on this issue:

  1. It's not a regression. We get the same result from a commit from Jun 23, 2011.
  2. There are 3 primary areas to look at: -- The parser might provide an incorrect input to the executor. -- The executor might get a correct input, but produce an erroneous result. -- K show(K a) may take a correct result and display it incorrectly.
tavmem commented 4 years ago

Start with the original cause of this issue .() Make this code change to display more result

$ git diff
diff --git a/src/kc.c b/src/kc.c
index 1e2e00f..4e78abe 100644
--- a/src/kc.c
+++ b/src/kc.c
@@ -26,6 +26,7 @@ extern void win_usleep(unsigned int); //buggy TDMGCC64 usleep()
 ;    // Need semicolon, probably missing from <pthread.h>.
 #endif

+K sd_(K x,I f);
 K KONA_GSET=0,KONA_IDX=0;

 volatile sig_atomic_t interrupted=0;
@@ -278,7 +279,7 @@ I line(FILE*f, S*a, I*n, PDA*p)     //just starting or just executed: *a=*n=*p=0
     if(o&&k)O("Elapsed: %.7f\n",d);
   #endif
  next:
-  if(o && fam && !feci)show(k);
+  if(o && fam && !feci){ O("\nresult: %p",&k);sd_(k,2);O("\n"); }
   cd(k);
  cleanup:
   if(fCheck && (strlen(s)==0 || s[strlen(s)-1]<0)) exit(0)

then, we get

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  .()

result: 0x7ffc33568ff8     0x7f608f265880 0x7f608f265898            1-6 5 0   
.()

The result is type5-0, a dictionary, with 0 elements.

Originally, we were getting .[] as a result. Reversing the correcting code change to K show(K a)

diff --git a/src/k.c b/src/k.c
index ffacbd9..c5769f4 100644
--- a/src/k.c
+++ b/src/k.c
@@ -293,7 +293,7 @@ void printAtDepth(V u, K a, I d, I x, I vdep, I b) //u {0=stdout or K* charvec }

 K show(K a)
 {
-  printAtDepth(0,a,0,0,0,0);
+  printAtDepth(0,a,0,0,0,1);
   if(a && a->t!=6)O("\n");
   R a;
 }

we get

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  .()

result: 0x7ffdac258708     0x7f242227a880 0x7f242227a898            1-6 5 0   
.[]

Again, type 5-0, a dictionary with no elements.

Putting the correcting code change back, and trying again we get

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  .[]

result: 0x7fff6b7e00a8     0x7f8cd6d65200 0x7f8cd6d65218            1-7 7 1   .[;]
     a0:    0x7f8cd6d65218     .k
     a1:    0x7f8cd6d65220     (nil)
     a2:    0x7f8cd6d65228     0x7f8cd6d8fa40 0x7f8cd6d8fa58            1-6 -4 2   0x3a 
(nil)  
     a3:    0x7f8cd6d65230     0x7f8cd6d8f9c0 0x7f8cd6d8f9d8            1-6 5 0   
.()
     a4:    0x7f8cd6d65238     0x7f8cd6d8fa00 0x7f8cd6d8fa18            1-6 5 0   
.()
     a5:    0x7f8cd6d65240     0x7f8cd6d8fa80 0x7f8cd6d8fa98            1-6 0 2   
(;)
 0x7f8cd6d8faa0     0x7f8cd6d8f040 0x7f8cd6d8f058            7-6 6 0   
 0x7f8cd6d8fa98     
     a6:    0x7f8cd6d65248     
     a7:    0x7f8cd6d65250     

Now we get type 7-1, a verb or derived verb. We probably should have gotten type 5-0.

Looks like there is a good chance the problem starts in the parser.

tavmem commented 4 years ago

Make the following code changes to show the details of the input to the parser

$ git diff
diff --git a/src/kx.c b/src/kx.c
index f39bfaf..ea24cf7 100644
--- a/src/kx.c
+++ b/src/kx.c
@@ -63,7 +63,7 @@ K sd_(K x,I f)
            calf--; )
     CS(-4, if(1)       //(f>-1)
            { V* v=(kV(x));
-             if((v[0]>(V)0x10) & (v[0]<(V)0x5000000)) R 0; //stop, if have string of interned symbols
+             //if((v[0]>(V)0x10) & (v[0]<(V)0x5000000)) R 0; //stop, if have string of interned symbols
              I ii; for(ii=0;v[ii];ii++)
                    { O("     .2%c[%lld]: %p",alf[calf],ii,v[ii]);
                      if(v[ii]>(V)DT_SIZE){ if(calf<1)sd_(*(K*)v[ii],2); else sd_(*(K*)v[ii],1); }
@@ -591,6 +591,7 @@ Z V ex_(V a, I r)   //Expand wd()->7-0 types, expand and evaluate brackets.   Co

 K ex(K a)   //Input is (usually, but not always) 7-0 type from wd()
 { U(a);
+  O("sd_(a,2):");sd_(a,2);O("\n");
   if(a->t==7 && kVC(a)>(K)DT_SIZE && 7==kVC(a)->t && 6==kVC(a)->n) fwh=1;
   if(a->t==7)
   { if(prnt==0)
$ 

Then we get

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  .()
sd_(a,2):     0x7f9d385f5000 0x7f9d385f5018            1-7 7 0   
     a0:    0x7f9d385f5018     .k
     a1:    0x7f9d385f5020     (nil)
     a2:    0x7f9d385f5028     0x7f9d38623700 0x7f9d38623718            1-6 -4 3   0x39 `"�P_8�" (nil)  
     .2a[0]: 0x39
     .2a[1]: 0x7f9d38623820     0x7f9d385f5080 0x7f9d385f5098            1-7 7 0   
     b0:    0x7f9d385f5098     .k
     b1:    0x7f9d385f50a0     (nil)
     b2:    0x7f9d385f50a8     0x7f9d38623840 0x7f9d38623858            1-6 -4 1   ,(nil)  
     b3:    0x7f9d385f50b0     0x7f9d38623780 0x7f9d38623798            1-6 5 0   
.()
     b4:    0x7f9d385f50b8     0x7f9d386237c0 0x7f9d386237d8            1-6 5 0   
.()
     b5:    0x7f9d385f50c0     
     b6:    0x7f9d385f50c8     
     b7:    0x7f9d385f50d0     
     a3:    0x7f9d385f5030     0x7f9d38623640 0x7f9d38623658            1-6 5 1   
.,(`;;)
 0x7f9d38623658     0x7f9d38623800 0x7f9d38623818            1-6 0 3   
(`;;)
 0x7f9d38623828     0x7f9d38623740 0x7f9d38623758 0x1e4c290  1-6 4 1   `
 0x7f9d38623820     0x7f9d385f5080 0x7f9d385f5098            1-7 7 0   
     b0:    0x7f9d385f5098     .k
     b1:    0x7f9d385f50a0     (nil)
     b2:    0x7f9d385f50a8     0x7f9d38623840 0x7f9d38623858            1-6 -4 1   ,(nil)  
     b3:    0x7f9d385f50b0     0x7f9d38623780 0x7f9d38623798            1-6 5 0   
.()
     b4:    0x7f9d385f50b8     0x7f9d386237c0 0x7f9d386237d8            1-6 5 0   
.()
     b5:    0x7f9d385f50c0     
     b6:    0x7f9d385f50c8     
     b7:    0x7f9d385f50d0     
 0x7f9d38623818     0x7f9d38623040 0x7f9d38623058            7-6 6 0   
     a4:    0x7f9d385f5038     0x7f9d38623680 0x7f9d38623698            1-6 5 0   
.()
     a5:    0x7f9d385f5040     
     a6:    0x7f9d385f5048     
     a7:    0x7f9d385f5050     

.()

Note the entry at .2a It is 0x39 Looking that up in the Dispatch Table in src/k.c it is {0, 1, dot_monadic,".",{0}},

Now try

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  .[]
sd_(a,2):     0x7f333f5d4000 0x7f333f5d4018            1-7 7 0   
     a0:    0x7f333f5d4018     .k
     a1:    0x7f333f5d4020     (nil)
     a2:    0x7f333f5d4028     0x7f333f5fe700 0x7f333f5fe718            1-6 -4 2   `"�@]?3" (nil)  
     .2a[0]: 0x7f333f5fe8e0     0x7f333f5d4080 0x7f333f5d4098            1-7 7 0   
     b0:    0x7f333f5d4098     .k
     b1:    0x7f333f5d40a0     (nil)
     b2:    0x7f333f5d40a8     0x7f333f5fe7c0 0x7f333f5fe7d8            1-6 -4 2   0x3a 
(nil)  
     .2b[0]: 0x3a
     b3:    0x7f333f5d40b0     0x7f333f5fe740 0x7f333f5fe758            1-6 5 0   
.()
     b4:    0x7f333f5d40b8     0x7f333f5fe780 0x7f333f5fe798            1-6 5 0   
.()
     b5:    0x7f333f5d40c0     0x7f333f5d4100 0x7f333f5d4118            1-7 7 0   
     c0:    0x7f333f5d4118     .k
     c1:    0x7f333f5d4120     (nil)
     c2:    0x7f333f5d4128     0x7f333f5fe900 0x7f333f5fe918            1-6 -4 1   ,(nil)  
     c3:    0x7f333f5d4130     0x7f333f5fe840 0x7f333f5fe858            1-6 5 0   
.()
     c4:    0x7f333f5d4138     0x7f333f5fe880 0x7f333f5fe898            1-6 5 0   
.()
     c5:    0x7f333f5d4140     
     c6:    0x7f333f5d4148     
     c7:    0x7f333f5d4150     
     b6:    0x7f333f5d40c8     
     b7:    0x7f333f5d40d0     
     a3:    0x7f333f5d4030     0x7f333f5fe640 0x7f333f5fe658            1-6 5 1   
.,(`;;)
 0x7f333f5fe658     0x7f333f5fe8c0 0x7f333f5fe8d8            1-6 0 3   
(`;;)
 0x7f333f5fe8e8     0x7f333f5fe800 0x7f333f5fe818 0xdcd290  1-6 4 1   `
 0x7f333f5fe8e0     0x7f333f5d4080 0x7f333f5d4098            1-7 7 0   
     b0:    0x7f333f5d4098     .k
     b1:    0x7f333f5d40a0     (nil)
     b2:    0x7f333f5d40a8     0x7f333f5fe7c0 0x7f333f5fe7d8            1-6 -4 2   0x3a 
(nil)  
     .2b[0]: 0x3a
     b3:    0x7f333f5d40b0     0x7f333f5fe740 0x7f333f5fe758            1-6 5 0   
.()
     b4:    0x7f333f5d40b8     0x7f333f5fe780 0x7f333f5fe798            1-6 5 0   
.()
     b5:    0x7f333f5d40c0     0x7f333f5d4100 0x7f333f5d4118            1-7 7 0   
     c0:    0x7f333f5d4118     .k
     c1:    0x7f333f5d4120     (nil)
     c2:    0x7f333f5d4128     0x7f333f5fe900 0x7f333f5fe918            1-6 -4 1   ,(nil)  
     c3:    0x7f333f5d4130     0x7f333f5fe840 0x7f333f5fe858            1-6 5 0   
.()
     c4:    0x7f333f5d4138     0x7f333f5fe880 0x7f333f5fe898            1-6 5 0   
.()
     c5:    0x7f333f5d4140     
     c6:    0x7f333f5d4148     
     c7:    0x7f333f5d4150     
     b6:    0x7f333f5d40c8     
     b7:    0x7f333f5d40d0     
 0x7f333f5fe8d8     0x7f333f5fe040 0x7f333f5fe058            7-6 6 0   
     a4:    0x7f333f5d4038     0x7f333f5fe680 0x7f333f5fe698            1-6 5 0   
.()
     a5:    0x7f333f5d4040     
     a6:    0x7f333f5d4048     
     a7:    0x7f333f5d4050     

.[;]
  \\

The entry in .2a is type 7-0, an unexecuted type. The relevant detail is in b2: which is 0x3a Looking that up in the dispatch table we find {0, 2, dot,".",{0}},

It should have been parsed as dot_monadic.

tavmem commented 4 years ago

.[] is not valid syntax for a dictionary So, .[] is (probably) a derived function. The behavior is similar in k2.8 and in kona:

$ rlwrap -n ./k
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems 
\ for help. \\ to exit.

  f: .()
  f 99
type error
f 99
^
>  \
  f `a

  f: .[]
  f 99
99
  f `a
 rlwrap -n ./k
kona      \ for help. \\ to exit.

  f: .()
  f 99
type error
>  \
  f `a

  f: .[]
  f 99
99
  f `a

The problems are (firstly) that the notational definition changes in kona

$ rlwrap -n ./k
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems 
\ for help. \\ to exit.

  f: .[]
  f
.[] 
$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  f: .[]
  f
.[;]

and (secondly)

$ rlwrap -n ./k
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems 
\ for help. \\ to exit.

  .[;;;;]
.[;;;;]
$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  .[;;;;]
valence error
>