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

Add more CycleInDeclaration examples #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions errors/CycleInDeclaration.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,45 @@ Note that cycles can also spring up in much less obvious ways, e.g. if you defin
## Notes

- If you are trying to implement a recursive parser in PureScript, then you might find this post on [recursive parsing in PureScript](https://github.com/Thimoteus/SandScript/wiki/2.-Parsing-recursively) to be helpful.

### Additional Examples

Mutually recursive functions:
Comment on lines +43 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move the generic deriving example to be a link then we can change this header to be ### Mutual recursion and just focus on this mutual recursion example. I think that fits better with how other errors use the Notes section.

Also, what do you think of trimming the example a little bit to something like this?

isEven :: Int -> Boolean
isEven n 
  | n == 0 = true
  | otherwise = again (n - 1)

again :: Int -> Boolean
again = isEven

which also fails, producing this error:

  The value of isEven is undefined here, so this reference is not allowed.

while checking that expression isEven
  has type Int -> Boolean
in binding group isEven, again

```purs
{-
A very inneficient way to write:
zeroOrNine n = if n >= 9 then 9 else 0
-}
zeroOrNine :: Int -> Int
zeroOrNine 9 = 9
zeroOrNine n | n < 0 = 0
zeroOrNine n = again (n - 1)

again :: Int -> Int
again = zeroOrNine
{-
Fix compiler error by replacing the above line with either:
again n = zeroOrNine n
again = \n -> zeroOrNine n
-}
```
https://try.purescript.org/?gist=3e013405988ab9a58521412fddadca08

Type class instance for recursive data type:
```purs
data Chain a
= End a
| Link a (Chain a)

derive instance genericChain :: Generic (Chain a) _

-- This builds, but blows-up call stack and triggers a
-- "too much recursion error".
instance showChain :: Show a => Show (Chain a) where
show = genericShow
{-
Fix the issue by replacing the above line with:
show c = genericShow c
-}
```
https://try.purescript.org/?gist=32b78ce065d60427620cdd8d40777a1f
Comment on lines +66 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could just be a link to the contents being added in #338?