kevinlawler / kona

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

Scan noun left argument #140

Closed isawdrones closed 11 years ago

isawdrones commented 13 years ago

Scan allows noun arguments on its left, these lead to an infinite loop:

1\1 2 3 4
2.3\2 3 4
(1;2)\1 2 3 4
"a"\1 2 3

Shouldn't these be type errors?

silentbicycle commented 13 years ago

IMHO they should be parse errors.

kevinlawler commented 13 years ago

There are legitimate uses of scan with noun left arguments:

  4 2 0 1 3 \ 2    /pointer chasing (trace)
2 0 4 3 1

The infinite loops look suspect. If they are not bubbling up errors that's definitely a problem.

tavmem commented 11 years ago

For a case that is not legitimate (such as any of the examples listed above by isawdrones), what error message should be displayed?

tavmem commented 11 years ago

In the legitimate use of scan with both left and right arguments (for pointer chasing): 4 2 0 1 3 \ 2 Z K scanMonad(K a, V p, K b) is executed, calling Z K dv_ex(K a, V p, K b) which calls K vf_ex(V q, K g)

scanMonad is called once. dv_ex and vf_ex are called 5 times.

scanDyad is not executed at all.

Is that what we want?

kevinlawler commented 11 years ago

scanMonad is called once. dv_ex and vf_ex are called 5 times. scanDyad is not executed at all. Is that what we want?

Yes. Scan Monad is correct. In this case the monad is "4 2 0 1 3\" and it's being repeatedly applied to 2 (and the consecutive results).

The original examples @isawdrones gave should be Rank Errors or Index Errors. Rank is for when the depth is exceeded. Index is for when the length of the list is exceeded.

I think you can rewrite the first iteration of

  1\1 2 3 4

as

  1 . 1 2 3 4 

which makes it easier to think about what's going on. In this case dot is indexing at depth.

  {x^y} . 2 3
8.0
  {x^y}[2;3]
8.0

Putting two nouns side-by-side causes the second noun to index into the first. (The application of noun scan to a noun without a verb causes indexing, which is the extension of the standard result of noun juxtaposition in K.)

  "abcd" 2
"c"

Compare also indexing without indexing at depth.

  {x+1 2 3} @ 4 5 6
5 7 9
  "abcd" @ 2 3 2
"cdc"
  "abcd" 2 3 2
"cdc"
  "abcd" . ,2 3 2
"cdc"

The "@" verb operates at a more shallow level than the "." verb. The "@" verb is basically a special case of the "." verb. (I forget whether there are corner cases that break this.) See the discussion at "@"/"." triad and tetrad:

https://github.com/kevinlawler/kona/wiki/Amend

Unfortunately, dyadic "@" and "." haven't been documented quite as well.

Some more cases:


  ("abcd";"efgh")/1 2
"g"
  ("abcd";"efgh")\1 2
(1;"g")