RE: Type families and classes

2017-05-18 Thread Simon Peyton Jones via ghc-devs
Yes that looks right.  A class instance can’t dispatch on a type family 
application.  In haskell we don’t allow

reverse (a ++ b) = reverse a ++ reverse b

and it’s the same for type families and class instances.  You should do the 
type-family reduction yourself, as you do below.

Simon

From: ghc-devs [mailto:ghc-devs-boun...@haskell.org] On Behalf Of Alan & Kim 
Zimmerman
Sent: 18 May 2017 08:15
To: ghc-devs@haskell.org
Subject: Re: Type families and classes

And to answer my own question, it seems

instance HasSourceText SourceText where
  noSourceText= NoSourceText
  sourceText s= SourceText s
 getSourceText a = a

And then applying the appropriate constraint at the use-site does the job.
So

noSyntaxExpr :: (HasSourceText (XHsString x)) => SyntaxExpr x id
Alan



On 18 May 2017 at 09:04, Alan & Kim Zimmerman 
<alan.z...@gmail.com<mailto:alan.z...@gmail.com>> wrote:
Hi all
I am experimenting with Trees that Grow [1] in the context of the GHC HsSyn 
AST, and wanting to express that a given extension point needs to have certain 
properties.
The specific case is to be able to contain a SourceText, in the context of HsLit
So I have (stripped down)

data GHCX

type family XHsString x

type instance XHsString GHCX = SourceText

class HasSourceText a where
  -- Provide setters to mimic existing constructors
  noSourceText  :: a
  sourceText:: String -> a

  getSourceText :: a -> SourceText

instance HasSourceText (XHsString GHCX) where
  noSourceText= NoSourceText
  sourceText s= SourceText s
 getSourceText a = a

But this gives an error

compiler/hsSyn/HsExtension.hs:80:10: error:
• Illegal type synonym family application in instance:
XHsString GHCX
• In the instance declaration for ‘HasSourceText (XHsString GHCX)’
Is there some way to achieve this, or is it simply impossible?
The full work-in-progress source is here[2]

Regards
  Alan


[1] 
https://arxiv.org/abs/1610.04799<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Farxiv.org%2Fabs%2F1610.04799=02%7C01%7Csimonpj%40microsoft.com%7Cec7c24b0b3aa4d0b0af608d49dbdc4c5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636306885764359008=ojtHcyFU%2BooW%2FdxDMaPpsDSUH8kfEF6y0NF4Encv1WE%3D=0>
[2] 
https://github.com/alanz/ghc/blob/df1c3b3d42284dd121086e6c571793f19e758977/compiler/hsSyn/HsExtension.hs#L73<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Falanz%2Fghc%2Fblob%2Fdf1c3b3d42284dd121086e6c571793f19e758977%2Fcompiler%2FhsSyn%2FHsExtension.hs%23L73=02%7C01%7Csimonpj%40microsoft.com%7Cec7c24b0b3aa4d0b0af608d49dbdc4c5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636306885764359008=SSvg%2FOyBUAJoZ3HMAEpaUYuYdXROVNGeyo7bXTnUe2k%3D=0>

___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Type families and classes

2017-05-18 Thread Alan & Kim Zimmerman
And to answer my own question, it seems

instance HasSourceText SourceText where
  noSourceText= NoSourceText
  sourceText s= SourceText s
 getSourceText a = a


And then applying the appropriate constraint at the use-site does the job.

So

noSyntaxExpr :: (HasSourceText (XHsString x)) => SyntaxExpr x id

Alan



On 18 May 2017 at 09:04, Alan & Kim Zimmerman  wrote:

> Hi all
>
> I am experimenting with Trees that Grow [1] in the context of the GHC
> HsSyn AST, and wanting to express that a given extension point needs to
> have certain properties.
>
> The specific case is to be able to contain a SourceText, in the context of
> HsLit
>
> So I have (stripped down)
>
> data GHCX
>
> type family XHsString x
>
> type instance XHsString GHCX = SourceText
>
> class HasSourceText a where
>   -- Provide setters to mimic existing constructors
>   noSourceText  :: a
>   sourceText:: String -> a
>
>   getSourceText :: a -> SourceText
>
> instance HasSourceText (XHsString GHCX) where
>   noSourceText= NoSourceText
>   sourceText s= SourceText s
>  getSourceText a = a
>
>
> But this gives an error
>
> compiler/hsSyn/HsExtension.hs:80:10: error:
> • Illegal type synonym family application in instance:
> XHsString GHCX
> • In the instance declaration for ‘HasSourceText (XHsString GHCX)’
>
> Is there some way to achieve this, or is it simply impossible?
>
> The full work-in-progress source is here[2]
>
> Regards
>   Alan
>
>
> [1] https://arxiv.org/abs/1610.04799
> [2] https://github.com/alanz/ghc/blob/df1c3b3d42284dd121086e6c571793
> f19e758977/compiler/hsSyn/HsExtension.hs#L73
>
>
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Type families and classes

2017-05-18 Thread Alan & Kim Zimmerman
Hi all

I am experimenting with Trees that Grow [1] in the context of the GHC HsSyn
AST, and wanting to express that a given extension point needs to have
certain properties.

The specific case is to be able to contain a SourceText, in the context of
HsLit

So I have (stripped down)

data GHCX

type family XHsString x

type instance XHsString GHCX = SourceText

class HasSourceText a where
  -- Provide setters to mimic existing constructors
  noSourceText  :: a
  sourceText:: String -> a

  getSourceText :: a -> SourceText

instance HasSourceText (XHsString GHCX) where
  noSourceText= NoSourceText
  sourceText s= SourceText s
 getSourceText a = a


But this gives an error

compiler/hsSyn/HsExtension.hs:80:10: error:
• Illegal type synonym family application in instance:
XHsString GHCX
• In the instance declaration for ‘HasSourceText (XHsString GHCX)’

Is there some way to achieve this, or is it simply impossible?

The full work-in-progress source is here[2]

Regards
  Alan


[1] https://arxiv.org/abs/1610.04799
[2]
https://github.com/alanz/ghc/blob/df1c3b3d42284dd121086e6c571793f19e758977/compiler/hsSyn/HsExtension.hs#L73
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs