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

Ch6 - remove concrete test Action Multiply Int #334

Conversation

gomain
Copy link

@gomain gomain commented May 13, 2021

There exists an alternative solution

import Data.Int (pow)

instance actionMultiplyInt :: Action Multiply Int where
  act (Multiply m) i = pow i m

The concrete test is not significant here. We only need to test that the laws are upheld.

@milesfrain
Copy link
Member

Related to #198 (cc @dvail).

Does this generalized test catch the incorrect solution noted in that PR?

@gomain
Copy link
Author

gomain commented May 14, 2021

Related to #198 (cc @dvail).

Does this generalized test catch the incorrect solution noted in that PR?

Strictly speaking, the cautious solution act _ a = a is "correct". I see some ways to resolve this

  1. Specify the expected behavior of act :: Multiply -> Int -> Int. This just takes away the fun though.
  2. Include in the text that this particular implementation is prohibited and include tests to reject it. This would require some creative way to put together.
  3. Include in the text that not all implementations will pass the test. Maybe incorporate tests so that any one-of accepted implementations pass. Or not. This is straight TDD — the test is the spec.
  4. Accept this solution.

Any thoughts?

@milesfrain
Copy link
Member

In the next exercise we specify the behavior of act :: Multiply -> String -> String :

(Medium) Write an Action instance which repeats an input string some number of times

So I think it's in the spirit of this series of exercises to also specify the behavior for Int (your first proposal).

I agree that this is not as interesting as allowing any lawful behavior, but I figure the concrete instructions are helpful for beginners.

I still believe this section is a bit too confusing for non-Haskellers. We could come up with even clearer explanations, and perhaps even more approachable exercises, but that requires a more involved rewrite (related to #238). I think the book could use a bit of restructuring overall to better cater to beginners with no FP background (nicely explained in #212 (comment)).

@gomain
Copy link
Author

gomain commented May 15, 2021

How about, modify the test to also accept act (Multiply m) i = pow i m. And place some comments in Test.NoPeeking.Solutions on how act _ m = a is sadly not accepted. This will kick the can until someone comes up with another law abiding solution. I can't think of one. Can you? I feel this will be a good balance between fun and helpful.

Up to this chapter, I think the book is just-about-right confusing. It gets worse in chapter7. I somewhat like the get dirty and push through approach. I think it's ok to confuse the reader aka. leave things unexplained as long as they get resolved after doing exercises. But that's for another topic/PR.

@gomain gomain closed this May 15, 2021
@gomain gomain deleted the remove-test-concreat-action-multiply-int branch May 15, 2021 09:56
@milesfrain
Copy link
Member

This will kick the can until someone comes up with another law abiding solution. I can't think of one. Can you?

Does this also satisfy the laws?:

act (Multiply n) m = m / n

Could we also cater to more advanced readers by adding an additional exercise such as:

(Difficult) Can you think of any other implementations of act that also satisfy the laws? We can think of 3 so far. Feel free to try out some alternative solutions with the "concrete" tests disabled.

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

Successfully merging this pull request may close these issues.

2 participants