jmoenig / Snap

a visual programming language inspired by Scratch
http://snap.berkeley.edu
GNU Affero General Public License v3.0
1.51k stars 745 forks source link

Code rings comparison #2353

Open DavidGasku opened 5 years ago

DavidGasku commented 5 years ago

Shouldn't this comparison give true? I'm trying to make autocorrected lessons for my students, and I need to compare code given as a parameter, but it doesnt work.

SMILEYS SIMETRIAS script pic

brianharvey commented 5 years ago

For blocks that are ringified in the same place, as in your picture, I suppose it could work. But in general it's really hard to compare functions; even Scheme punts on this.

DavidGasku commented 5 years ago

Well, then I'll use this issue as a suggestion. I think it would open many possibilities:

Some custom block could turn this: circleArt_start2 script pic (1)

into this: circleArt_start2 script pic (2)

And be able recursively to travel the program tree until the parameters:

untitled script pic (1)

circleArt_start2 script pic (3)

brianharvey commented 5 years ago

Oh! Yes! The ability to convert back and forth between blocks and abstract syntax trees is at the very top of my list (not to be confused with Jens's list :-/ ). That's what we need to implement Scheme macros.

But it won't entirely solve the problem. The text equivalence of two blocks is both too broad and too narrow a criterion. Too broad because the same block text reified in two different contexts are not the same function: Untitled script pic Untitled script pic Plus3 and plus4 are derived from the same (in the eq? sense) ringed expression, but they implement different functions.

Too narrow because different texts can create the same function: Untitled script pic (1) Untitled script pic (2) F and g have different texts, but they implement the same function.

This is why Scheme just throws up its hands. It'll tell you reliably if two functions are EQ?, meaning that they're the very same memory location, but not if they're EQV?, equivalent values.

DavidGasku commented 5 years ago

Well, I would just compare the specs. It's not necessary to go down to the semantic level.

Another user case that this feature could enable: In one of my lessons about coordinates and symetries, students have several painting primitives like SMILEYS SIMETRIAS script pic (1)

I wanted to have a C-shaped block to draw the symetric drawing, so SMILEYS SIMETRIAS script pic (3)

would also draw SMILEYS SIMETRIAS script pic (2)

Unless I'm missing something, this is not possible at the moment.

brianharvey commented 5 years ago

You can fake it with an ugly global variable hack. Have the LINE block set LAST ACTION to a list with the word LINE and the four coordinates. Then your X SYMMETRY block can run its input, and then review LAST ACTION to decide how to flip it. (If you want multi-command scripts flipped, make LAST ACTION a list to which LINE adds its list of coordinates.)

I agree that this is hideous. We should definitely fix it.