[Haskell-cafe] clonetype
Li-yao Xia
lysxia at gmail.com
Mon Sep 24 13:08:35 UTC 2018
Another issue with saying "derive all instances for this newtype" is
that not all instances are coercible.
type family F a where
F Char = Int
class C a where
f :: a -> F a
instance C Char where
f _ = 'a'
newtype D = D Char
There is no way to derive an instance C D.
A more explicit and flexible solution would be "deriving synonyms",
discussed recently here:
https://www.reddit.com/r/haskell/comments/9dx6s9/proposal_data_deriving_synonyms/
Li-yao
On 9/24/18 8:47 AM, Oleg Grenrus wrote:
> The problem is that "All instances" is hard to pin point. We have open
> world assumption, so instances can be added later (in the dependency
> tree). Should they be cloned too? And even of you restrict to "instances
> visible at clonetype definition", that's IMHO not a good idea either, as
> it's implicit and volatile set (editing imports changes may change the set).
>
> So use either tagged-trick/pattern or GND.
>
> Haskell's heavy type machinery exists so we can explicitly and exactly
> say what we need or want. Tagged/phantom-pattern and GND are different,
> they have different pros & cons. Having `clonetype-fits-all` seems
> non-trivial to me.
>
> Sent from my iPhone
>
> On 24 Sep 2018, at 15.25, Harendra Kumar <harendra.kumar at gmail.com
> <mailto:harendra.kumar at gmail.com>> wrote:
>
>> That comes close, but Haskell having such a heavy type machinery to do
>> all the esoteric type stuff in the world, but not allowing you to
>> express such a simple day to day programming thing in a simpler manner
>> is not very satisfying.
>>
>> -harendra
>>
>> On Mon, 24 Sep 2018 at 16:16, Oleg Grenrus <oleg.grenrus at iki.fi
>> <mailto:oleg.grenrus at iki.fi>> wrote:
>>
>> data MyStringTag
>> type MyString = Tagged MyStringTag String
>>
>> Sent from my iPhone
>>
>> On 24 Sep 2018, at 13.42, Harendra Kumar <harendra.kumar at gmail.com
>> <mailto:harendra.kumar at gmail.com>> wrote:
>>
>>> Often, we need to create a newtype that is equivalent to a given
>>> type for safety reasons. Using type synonym is useless from type
>>> safety perspective. With newtype, we have to add a "deriving"
>>> clause to it for deriving the required instances, to make it
>>> practically useful.
>>>
>>> Does it make sense, and is it possible to have something like a
>>> "clonetype" that creates a new type and derives all the instances
>>> of the parent type as well? It will be quite helpful in creating
>>> equivalent newtype synonyms quickly. Almost always, I do not use
>>> a newtype where I should just because of the inconvenience of
>>> deriving the instances. Ideally, we should just be able to say
>>> something like:
>>>
>>> clonetype MyString = String
>>>
>>> and we are good to go. What is the shortest possible way to
>>> achieve this with currently available mechanisms, if any?
>>>
>>> -harendra
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> To (un)subscribe, modify options or view archives go to:
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>> Only members subscribed via the mailman list are allowed to post.
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
More information about the Haskell-Cafe
mailing list