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

Improve documentation for IsSymbol #224

Open
ursi opened this issue Sep 4, 2020 · 1 comment
Open

Improve documentation for IsSymbol #224

ursi opened this issue Sep 4, 2020 · 1 comment
Labels
type: documentation Improvements or additions to documentation.

Comments

@ursi
Copy link

ursi commented Sep 4, 2020

-- | A class for known symbols
class IsSymbol (sym :: Symbol) where
reflectSymbol :: SProxy sym -> String

As a newcomer who is currently trying to learn this, I don't understand what this is supposed to mean. What is a "known symbol"? I think better documentation would explicitly mention how the compiler will create these instances for you.

@hdgarrood
Copy link
Contributor

I think this will need some workshopping before being ready to put in the module docs, but here's a potential explanation:

A known symbol is a symbol which is "known" to the compiler, in the sense that the compiler can tell you what the sequence of characters in that symbol are. There's a distinction between "known" and "unknown" symbols because having a type variable whose kind is Symbol doesn't guarantee that you can reflect it to the term level via reflectSymbol; you need to have IsSymbol instances for carrying around the actual string data at runtime.

Perhaps simplest example of a case where the compiler can't solve IsSymbol and a Symbol would be unknown is this:

foreign import data Unknown :: Symbol

and so if you write eg

x = reflectSymbol (SProxy :: SProxy Unknown)

you'll get an error that "No type class instance was found for Data.Symbol.IsSymbol Unknown". However that's a slightly weird example as you could argue that you shouldn't even be allowed to write foreign import data Unknown :: Symbol in the first place.

Perhaps a better example might be

test :: forall sym. SProxy sym -> String
test = reflectSymbol

which also doesn't compile because "No type class instance was found for Data.Symbol.IsSymbol sym0". The reason for this is that, with the code as written here, you're trying to return information you don't have. If the above were to compile, then to call test, I'd only have to supply an SProxy. However, by the time I run this code, any information about the phantom type argument of the SProxy has been lost; an SProxy doesn't contain any information at runtime. The purpose of the IsSymbol instance is to carry around the actual string data is carried at runtime, because an SProxy can't do that on its own.

@JordanMartinez JordanMartinez added the type: documentation Improvements or additions to documentation. label Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation Improvements or additions to documentation.
Projects
None yet
Development

No branches or pull requests

3 participants