hackworthltd / primer

A pedagogical functional programming language.
GNU Affero General Public License v3.0
15 stars 1 forks source link

Scaffolding: build lambda expressions automatically from type #35

Open dhess opened 3 years ago

dhess commented 3 years ago

I've been pushing for awhile for some editor smarts that would automatically build a function's expression lambdas as the student creates the function's type. For example, as the student fills out type A -> B -> C, the editor could be filling in the expression hole with λa -> λb -> ?.

I still think this is a good idea. However, it's something we should only enable at levels well above Beginner level. The reason is that I think it's quite important for students to build their function expressions by hand for quite awhile before we automate this for them. It should arguably not be enabled until the student begins to feel a bit frustrated/bored by the robotic nature of building the expression lambda spines. (When this point occurs could be left to the discretion of the instructor.)

georgefst commented 3 years ago

To state the obvious drawback (which I raised when we discussed this), the body of a function needn't necessarily contain a lambda.

-- ignore polymorphism and our need for explicit type applications here - they aren't relevant

f :: a -> a
f = id

f :: [Int] -> [Int]
f = map succ

The former is trivial, and fairly useless. But the latter less so.

So if we were to introduce the feature discussed above, we may wish to disable it again once the student grasps partial application.

georgefst commented 3 years ago

we may wish to disable it again once the student grasps partial application

Alternatively, we could continue to insert the lambda, allow them to construct f = \xs -> map succ xs, then expect them to recognise the opportunity to simplify it (or even point it out to them, as HLint would). This would benefit from a new "eta-contract" action (which would be a refactoring action like those discussed in hackworthltd/primer#19, but not one we'd see in Eval mode).

brprice commented 3 years ago

we may wish to disable it again once the student grasps partial application.

The two other options which spring to mind are

dhess commented 3 years ago

Those are all good points. Perhaps this feature isn't as useful as I thought it might be. Ironically, it's probably the most convenient in the beginning stages when students are just writing pattern matches on inputs, rather than composing functions, but that is precisely when I think it's important that they build the lambdas by hand, so that they understand what "make a function with an input" means.

I'll keep this open for now, in any case, but I don't see any reason to prioritize it for the time being.

georgefst commented 3 years ago
* don't automatically do anything, but have an action "insert the canonical construction here", which will do this.

This is a nice compromise.