Can we add new control structures to GP?

Extending or modifying the GP system itself (advanced!)

Moderator: MSandro

Post Reply
mguzdial
Posts: 70
Joined: Sep 15th, '15, 11:21

Can we add new control structures to GP?

Post by mguzdial » Sep 25th, '15, 16:41

This is a question about Extending GP, rather than describing an extension of GP. Would y'all prefer these under feature requests?

Can we add new control blocks to GP? How do we do it?

The control blocks defined in AuthoringSpecs.gp seem to all map to the underlying implementation -- the primitive structures like if and for and while. Is that the only way to define blocks?

Some of the blocks that I'd like to explore building include:
  • A for loop with an increment, e.g., increment by 2 or 3.
  • A repeat-until, which tends to be easier than while for novices.
  • Would love to be able to explore map, apply, and filter, but I think that also requires functions as first class objects which we don't have quite yet, right?
Thanks!

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Can we add new control structures to GP?

Post by JohnM » Sep 26th, '15, 11:15

You can use the "range" block with the "for" loop to iterate over a range of numbers, including using different step sizes.

In general, unlike Smalltalk, you can't create new control structures in GP that do everything the built-in ones do. Doing that would require some kind of closure, like Smalltalk's "block" objects. However, I'd consider adding a repeat-until control structure to the virtual machine if that would make GP better for beginners.

Functional programming is possible in GP. GP has first-class functions and a way to invoke them on arguments. There is even a way to combine a function with some pre-bound arguments using an Action object. We haven't yet created a set of blocks for doing functional programming at the authoring level, but probably will add those in the next year.

Note, however, that we've made an explicit design choice to NOT support closures in GP, just as we decided to omit inheritance from the language. This choice is controversial, even among the GP team, but I feel that having closures in GP would create a barrier to entry for beginners and "casual programmers".

mguzdial
Posts: 70
Joined: Sep 15th, '15, 11:21

Re: Can we add new control structures to GP?

Post by mguzdial » Sep 28th, '15, 01:14

I"m okay with no closures. I'm an old-time Logo programmer. I look forward to exploring the functions as first-class object features!

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Can we add new control structures to GP?

Post by JohnM » Sep 28th, '15, 14:23

What do think of the following idea for a flexible loop control structure to replace the current while loop. There would be three blocks:

loop <body>
exit loop
restart loop

The loop block would be equivalent to "while true <body>". By combining a test with an exit loop block, you can build either a while or a repeat loop (depending on whether you put the test at at the start or end of the loop body.

Do you think this would be easier or harder for students to understand than a while or repeat loop? Separating the two concepts, "loop" and "end test," might make it easier to talk about. On the other hand, this proposal would require more assembly and more choices (does the end test go at the start, at the end, or perhaps somewhere in the middle of the loop body?). Thus, there are more ways to make errors. In many programming languages, teaching a loop construct without a built-in termination condition would be inviting trouble, since infinite loops would lock up the program. But in GP, the program environment continues to work (although more slowly) while an infinite loop is running, giving students a chance to explore and debug the problem.

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Can we add new control structures to GP?

Post by JohnM » Sep 28th, '15, 14:33

By the way, do you have a theory about why "repeat-until" is easier for beginners when "while"? Do you think this result might be different in a blocks language?

I've always preferred "while" since it allows for running the loop body zero times, while repeat-until always runs the body at least once.

Do you think we should add a "repeat until" control structure to the GP virtual machine?

mguzdial
Posts: 70
Joined: Sep 15th, '15, 11:21

Re: Can we add new control structures to GP?

Post by mguzdial » Sep 28th, '15, 15:21

Elliot Soloway did a set of studies on looping structures in Pascal back at Yale in the 1980's. He found that students preferred a read/process strategy (rather than process/read) with an ability to exit the loop midway. See attached paper. I have a student who is doing some further experiments, trying to measure the cognitive complexity of NOT having an ability to exit the loop mid-way.
Attachments
Soloway-1983-Cognitive strategies.pdf
(594.95 KiB) Downloaded 142 times

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Can we add new control structures to GP?

Post by JohnM » Sep 28th, '15, 15:27

Here's an example of using the range block:
Screen Shot 2015-09-28 at 11.21.19 AM.png
The range, start, stop and step parameters can be integers or floats. If start is greater than end, the range values decrease. The sign of step doesn't matter, but it cannot be zero.

That "range" block actually creates a list with all the values, which is useful in many situations. For example, you can easily create a list of even numbers from 1 to 1000. On the other hand, creating large lists (say, over a million values) can be inefficient in performance-critical situations.

mguzdial
Posts: 70
Joined: Sep 15th, '15, 11:21

Re: Can we add new control structures to GP?

Post by mguzdial » Sep 28th, '15, 18:00

By the way, I really do like the three block solution you describe! It matches Elliot's theory and Briana's developing theory.

No, I don't really know why. But it makes me think that GoTo's are another thing that Dijkstra was wrong about. GoTo's make code far easier for novices to write. Your exit loop block would be hated by Dijkstra, I fear, but I think students would love it.

Post Reply