Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need an Ivy discussion forum to ask user questions - e.g. where Alpha and Omega commands? #28

Open
benjamin-rood opened this issue Jun 17, 2016 · 12 comments

Comments

@benjamin-rood
Copy link

Hi Rob, sorry to bother you here, but I don't see how in Ivy you can use the APL α and ω commands, e.g. if I wanted to create a gcd operator like so:

screen shot 2016-06-17 at 3 08 20 pm

src: http://dfns.dyalog.com/n_gcd.htm

@robpike
Copy link
Owner

robpike commented Jun 17, 2016

You can't, at least not yet. Ivy is far from a complete mapping of APL's features, even APL\360. I believe the more powerful versions α and ω came much later, perhaps in APL2. I'm not sure.

Of course you can always define a unary or binary operator and name the arguments yourself; there are examples in the ivy help text. But looping and conditionals are not available.

I may do something about that one day but have no plans to do so.

@benjamin-rood
Copy link
Author

Thanks for the response. I wonder how one could do a recursive gcd operator then without conditionals? Ultimately I wanted to try to implement a binary coprime operator where op x coprime n produces a vector with all natural numbers ≤ n which are coprime to x.

I do appreciate that as you say, this is a plaything. I very much enjoyed the implementation in Go presentation and I learnt a lot from it, so thank you.

robpike added a commit that referenced this issue Jul 7, 2021
Based on a suggestion by Alan Davies (glxxyz on GitHub), and borrowing
heavily from Dyalog's guards, implement conditional evaluation. The
new construct looks like a binary operator ":", but is of lower
precedence than any other operator. It is enabled only inside
user-defined functions, and has the property that if the left-hand
operand is non-zero, the function returns the right-hand operand.
Otherwise, the right-hand operand is not evaluated and execution
continues.

It is an error if the left-hand operand is not a scalar.

This has been a long time coming, so thanks to Alan for planting
the seed, life for giving me a free day to think about how to do
it, and especially to GitHub user jojomoore2007, who found a way
to write a recursive function that is so amazing and yet abusive
it pushed me over the edge.

Example from testdata/function.ivy:

	# Recursive factorial.
	op fac n =
		n <= 1 : 1
		n * fac n - 1

	# Recursivd gcd.
	op a gcd b =
		a == b: a
		a > b: b gcd a-b
		a gcd b-a

	fac 10
		3628800
	1562 gcd fac 11
		22

Fixes #65
Update #28
@rsc
Copy link
Contributor

rsc commented Dec 9, 2021

I noticed this is still open, presumably because of the 'need an Ivy discussion forum' part since the conditional execution now exists. One possibility would be to enable GitHub Discussions on this repo. We've used them for Go discussions that are expected to be too much for the issue tracker, and the single level of threading is really helpful.

We tend to only use them for big things in Go, but projects often use them now for Q&A etc.
golang/go#47141 is one of our smaller discussions.

@robpike
Copy link
Owner

robpike commented Dec 9, 2021

Could do that. It hasn't really had enough users to be worth doing until you started posting all these power-user videos :)

@fumin
Copy link

fumin commented May 9, 2022

Could we not panic in value.Errorf?
In particular, value.index already does bounds checking, so repanicking seems wierd.

By the way, sincere gratitude for creating this wonderful piece of software!!

A bit of background why I prefer not panicking

I actually want to use ivy as a script language embedded in a Go programme.
The Go programme is doing image manipulation on Gerber images.
In particular, it is trying to match parts of an image to a golden image.
Due to the fragmentation of the PCB industry, there are myriad ways how our suppliers
describe their manufactured parts.
I can't handle all these different ways, so I am thinking of letting operators figure out the image mapping with their own eyeballs, and script it out to the programme.
You may ask why not simply let operators input three numbers denoting the X, Y translations and theta for rotation.
This is because in order to increase mechanical efficiency, some parts may contain multiple heads and work on many units simulatenously.
In other words, the image mapping is something like:

op panel p = 
  p[0] > 0 && p[1] > 0: p[0]*cos(theta) + p[1]*sin(theta)
  p[0] < 0 && p[1] > 0: ...
  ... 

I was initially thinking of using https://github.com/pkujhd/goloader to allow operators to write in Go, but hacking Go's linker seems to fragile.
Then I thought of GOARCH=wasm, but moving slices in and out of wasm is a pain in the ass.
Moreover, GOARCH=wasm in it's current state is not much faster than an interpreter.
In the end, I found ivy, which seems suited for these kinds of mathematical tasks.
However, if ivy's value.Errorf panics, I would need to recover which feels like I am commiting a sin.

@fumin
Copy link

fumin commented May 9, 2022

Why does (5 5 rho iota 25)[3 2; 1 2 3] return the following?

11 12
13  6
 7  8

I was expecting

11 12 13 
6 7  8

which is also what's returned by https://tryapl.org/ with the expression (5 5 ⍴ ⍳ 25)[3 2; 1 2 3].

Below is my investigation
(5 5 rho iota 25)[3 2; 1]
11 6

(5 5 rho iota 25)[3 2; 1 2]
11 12
 6  7

(5 5 rho iota 25)[3 2; 1 2 3]
11 12
13  6
 7  8

@robpike
Copy link
Owner

robpike commented May 9, 2022

value.Errorf panics because that is how the interpreter does error recovery. it's an easy way to do exception handling. Otherwise the control flow from deep in the interpreter during a calculation would require threading errors through a vast collection of code.

It's common for language implementations and related systems to work like this.

@robpike
Copy link
Owner

robpike commented May 9, 2022

Regarding the indexing example, that's the usual APL way, as you can see by trying the example on https://tryapl.org/. The vector left of the semicolon selects the first axis, while the vector on the right selects the second.

@fumin
Copy link

fumin commented May 9, 2022

@robpike thanks for your explanation and for sharing your experience in language and systems implementation. I have few experience in language and systems implementation, so it's enlightening to learn that panic and recover can be useful in these circumstances.

I tried the expression (5 5 ⍴ ⍳ 25)[3 2; 1 2 3] in https://tryapl.org/ , and it returned

11 12 13
 6  7  8

I guess it makes sense for ivy to return this, too?

@robpike
Copy link
Owner

robpike commented May 9, 2022

Oh, fair point. I misread your issue.

@rsc did the rewrite that got us here so over to him.

@rsc
Copy link
Contributor

rsc commented May 9, 2022

@fumin @robpike I opened #111 for the indexing question.

@kpym
Copy link

kpym commented Dec 22, 2022

I noticed this is still open, presumably because of the 'need an Ivy discussion forum' part since the conditional execution now exists. One possibility would be to enable GitHub Discussions on this repo. We've used them for Go discussions that are expected to be too much for the issue tracker, and the single level of threading is really helpful.

We tend to only use them for big things in Go, but projects often use them now for Q&A etc. golang/go#47141 is one of our smaller discussions.

@rsc this can be moved to disucssions now ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants