TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ivy – An interpreter for an APL-like language

135 pointsby georgia_peachalmost 3 years ago

8 comments

Saseralmost 3 years ago
Russ Cox (who worked together with Rob Pike on Go for a long time) solved several of last year&#x27;s Advent of Code puzzles using Ivy. Here&#x27;s a YouTube playlist where he has recorded himself doing so: <a href="https:&#x2F;&#x2F;youtube.com&#x2F;playlist?list=PLrwpzH1_9ufMLOB6BAdzO08Qx-9jHGfGg" rel="nofollow">https:&#x2F;&#x2F;youtube.com&#x2F;playlist?list=PLrwpzH1_9ufMLOB6BAdzO08Qx...</a>. I don&#x27;t think he&#x27;s solving the problems for the first time in these videos, instead merely demonstrating how they can be solved using Ivy.
henrydarkalmost 3 years ago
It took me a while to understand the syntax in general and the primes example in particular. For those facing the same obstacle, here is what I understood:<p>1. Read single lines from right to left.<p>2. Imagine a stack of one variable that you keep replacing the top as you go from right to left, kind of like concatenative languages (e.g. FORTH, kitten, factor), but right to left instead of left to right.<p>3. Here is a step by step translation of the primes example to imepartive pseudo code:<p><pre><code> op primes N = (not T in T o.\* T) sel T = 1 drop iota N </code></pre> First of all this is a function definition, so can be put to<p><pre><code> function primes(N) return (not T in T o.\* T) sel T = 1 drop iota N </code></pre> Now we apply the two rules above:<p><pre><code> function primes(N) x = iota(N) &#x2F;&#x2F; 1..N x = drop(1, x) &#x2F;&#x2F; 2..N T = x ToT = outerproduct(T, T) &#x2F;&#x2F; a*b for all a, b in T, so 4,6,...,N^2 return [x in T not in Tot] </code></pre> So we get a O(N^2) (or maybe O(N^3), depending on how `in` is implemented) algorithm for primes until N.<p>Here is a similar algorithm in python that might help further:<p><pre><code> def primes(N): T = range(2, N) ToT = [a*b for a in T for b in T] return [x for x in T if x not in ToT]</code></pre>
评论 #32377332 未加载
评论 #32378887 未加载
评论 #32379395 未加载
frankjralmost 3 years ago
For those curious to see the language in action, Russ Cox recorded solutions to many of the AoC 2021 challenges in Ivy.<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;user&#x2F;rscgolang&#x2F;videos" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;user&#x2F;rscgolang&#x2F;videos</a>
cpachalmost 3 years ago
Out of curiosity:<p>I noticed that Pike chose the, uh, “username”, <i>robpike.io</i> for this project. (It’s probably called something else than username in this context.)<p>What are the benefits of using a domain name as username&#x2F;organization name in this context?
评论 #32375997 未加载
评论 #32375898 未加载
评论 #32376068 未加载
评论 #32375978 未加载
kevingaddalmost 3 years ago
The precedence rules are terrifying. I get it, but:<p>&gt; 3&#x2F;6*4 is 2 while 3 &#x2F; 6*4 is 1&#x2F;8 since the spacing turns the &#x2F; into a division operator. Use parentheses or spaces to disambiguate: 3&#x2F;(6*4) or 3 &#x2F;6*4.<p>having to be up-front as a warning feels like a sign that your approach to precedence is fundamentally broken and you should <i>require</i> parentheses. (Also the idea that 1&#x2F;8 and 1 &#x2F; 8 are different things feels bonkers to me in general, but maybe that&#x27;s just taste.)
评论 #32374996 未加载
评论 #32375380 未加载
评论 #32376731 未加载
评论 #32375386 未加载
评论 #32375917 未加载
评论 #32375021 未加载
评论 #32374879 未加载
评论 #32375236 未加载
meltedcapacitoralmost 3 years ago
Can&#x27;t have an APL family post without mentioning Klong!<p><a href="http:&#x2F;&#x2F;t3x.org&#x2F;klong&#x2F;" rel="nofollow">http:&#x2F;&#x2F;t3x.org&#x2F;klong&#x2F;</a>
ianthehenryalmost 3 years ago
I really like Ivy as a simple, friendly introduction to APL. There is a surprising lack of APL-derived languages that use words to name things -- most stick with the original symbols; J and friends choose equally-cryptic symbols composed of ASCII characters.<p>Earlier this year I decided to solve AoC 2021 in Ivy, then watch Russ Cox&#x27;s videos to see how he did it and use that to learn something about array programming -- a topic I knew absolutely nothing about going into this.<p>Unfortunately, Ivy really is, as Rob Pike says, a plaything. It is buggy -- if you ever write a function that returns a vector or a higher-rank array, you are entering bizarre undefined behavior territory. The array-language equivalent of &quot;concat_map&quot; or &quot;flat_map&quot; or &quot;mapcat&quot; or whatever you want to call it just produces garbage values, which is <i>very confusing</i> when you&#x27;re learning about array programming for the first time (&quot;Wait, this vector says its length is 25, but it contains 50 elements...?&quot; or &quot;The transpose of this array is just the first column repeated over and over??&quot;).<p>Beyond that, a very cool thing about array languages is that, you know, functions can implicitly act on entire arrays. You can multiple a vector by 2 and it will know to multiply every element in the vector by 2, because multiplication is defined for scalars.<p>But in Ivy, this is only true for the built-in functions. There is no way to write user-defined functions that have this implicit act-on-every-element behavior. Which is basically <i>the</i> looping primitive in array languages -- so to do anything nontrivial, you have to write it out with explicit recursion (still with the caveat that your functions can only return scalars, or you enter undefined behavior town) or rewrite your operations as binary operations with an ignored right-hand side and use &quot;fold&quot; to &quot;map&quot; them. It&#x27;s bad.<p>The latter is crippling enough that Russ Cox eventually forks Ivy to add support for it, but it is not currently part of the language. <a href="https:&#x2F;&#x2F;github.com&#x2F;robpike&#x2F;ivy&#x2F;pull&#x2F;83" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;robpike&#x2F;ivy&#x2F;pull&#x2F;83</a><p>Anyway that&#x27;s a long comment to say: Ivy is a good, friendly introduction to APL syntax (stranding, ambivalent functions, precedence, etc) and <i>some</i> array language concepts, but it is far more of a calculator than a programming language.<p>But it&#x27;s a good arbitrary-precision calculator! And if you&#x27;re still interested in trying it, maybe check out this thing I made. It&#x27;s an... Ivy programming environment?... that lets you run Ivy scripts and see the results inline. (Ivy&#x27;s repl is... very primitive, and has to be wrapped by something like readline. Russ Cox uses 9term to get around this; self-modifying programs are my preferred approach.)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ianthehenry&#x2F;privy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ianthehenry&#x2F;privy</a><p>My frustration with Ivy led me to look into other array languages, trying to find one that 1) used English words instead of cryptic symbols and 2) worked. And I really couldn&#x27;t find any! Someone should do something about that. :)
评论 #32380869 未加载
评论 #32377760 未加载
评论 #32378624 未加载
titzeralmost 3 years ago
&gt; Unlike in most other languages, operators always have the same precedence and expressions are evaluated in right-associative order. That is, unary operators apply to everything to the right, and binary operators apply to the operand immediately to the left and to everything to the right. Thus, 3<i>4+5 is 27 (it groups as 3</i>(4+5))<p>This seems like a blatant unforced error.
评论 #32376150 未加载
评论 #32377303 未加载
评论 #32376181 未加载