<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">There are two things at play here: 1) Safe Haskell, and 2) levity-polymorphic classes.<div class=""><br class=""></div><div class="">I think (1) is straightforward. I can't think of any lack of safety or loss of abstraction from levity polymorphism, and now that the issue has been raised, I think the lack of Safe Haskell support for levity polymorphism is a bug. Would you want to file a ticket?</div><div class=""><br class=""></div><div class="">I think (2) is quite possible, but with design issues (of course). This has actually been discussed before: <a href="https://github.com/ghc-proposals/ghc-proposals/pull/30" class="">https://github.com/ghc-proposals/ghc-proposals/pull/30</a> Note that the proposal withered on the vine due to lack of love, but it had some support -- it was not rejected. As I've posted previously, I think the right way to get this is to make an alternative, levity-plymorphic prelude, just to see how it all works out. But I do like this direction of travel.</div><div class=""><br class=""></div><div class="">Richard</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 27, 2019, at 9:47 PM, chessai . <<a href="mailto:chessai1996@gmail.com" class="">chessai1996@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="auto" class="">Regarding UnliftedNewtypes: apparently GND can solve this issue for you. There are som cases where, because of the desugaring, a GND clause will fail, but moving the clause out to a StandaloneDeriving one can fix the issue. <div dir="auto" class=""><br class=""></div><div dir="auto" class="">But defaults still become annoying for things like Foldable, where most users rely pretty heavily on defaults.</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Oct 27, 2019, 4:22 PM chessai . <<a href="mailto:chessai1996@gmail.com" class="">chessai1996@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class="">Just realised i hit reply and not reply all. Here was my email:<div dir="auto" class=""><br class=""></div><div dir="auto" class=""><div style="font-family:sans-serif;font-size:12.8px" dir="auto" class="">See <a href="https://github.com/chessai/levity" style="text-decoration-line:none;color:rgb(66,133,244)" target="_blank" rel="noreferrer" class="">https://github.com/chessai/levity</a> and <a href="http://hackage.haskell.org/package/unlifted-list" style="text-decoration-line:none;color:rgb(66,133,244)" target="_blank" rel="noreferrer" class="">http://hackage.haskell.org/package/unlifted-list</a>.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">There are a few annoyances due to binder restrictions (see [1]).</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">For example, it is not possible to write polymorphic `bindUnliftedToLifted`:</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">bindUnliftedToLifted :: forall (a :: TYPE r) (b :: TYPE 'LiftedRep). ST s a -> (a -> ST s b) -> ST s b</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">This function is frequently useful when working with monads which have levity-polymorphic parameters, but you cannot write it when a is levity-polymorphic, since it will occur in a binding position. What ends up happening is that you write monomorphic versions of this function for each one you need. Clearly not desirable. </div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">The other thing is that levity-polymorphic kinds are (almost?) never inferred. For example, if I have:</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div style="color:rgb(80,0,80);font-family:sans-serif;font-size:12.8px" dir="auto" class=""><div dir="auto" class="">class Show (a :: TYPE r) where</div><div dir="auto" class=""> show :: a -> String</div><div dir="auto" class=""><br class=""></div></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">addNewline :: Show a => a -> String</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">addNewline x = show x ++ "\n"</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">GHC will infer the kind of `a` in `addNewline` to be `TYPE 'LiftedRep`, even though it very well could be `forall (r :: RuntimeRep). TYPE r`. In other words, users will have to constantly kind-annotate because of (over-?)restrictive inference. This becomes annoying rather quickly, and the type errors don't always immediately make it clear what's happening when you miss an annotation.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">Another thing which is annoying, you can't write things like Monoid or Bounded in the same way! (See also [1])</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">class Monoid (a :: TYPE r) where</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""> mempty :: a</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">GHC will complain about mempty here. You have to instead make it</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">class Monoid (a :: TYPE r) where</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""> mempty :: () -> a</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">```</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">which just becomes cluttering, your code gets filled with a lot of `mempty ()`.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">Another thing is that default implementations will not work. You state that it's fine because the number of inhabitants of unlifted kinds is small and finite. This will not be the case in GHC 8.10, when UnliftedNewtypes lands. Then the number of inhabitants becomes non-finite.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">The ways to use levity-polymorhism which result in the best UX are: 1) CPS, 2) backpack, and 3) resolving [1]. (1) is the easiest to most users right now (see [2] for an example)</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class=""></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">With all of these drawbacks I'm against having the API of base or any core library really be a place for levity-polymorphic code, especially when talking about core typeclasses/types. Probably best for this to be in userspace.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class=""><br class="">[1]: <a href="https://gitlab.haskell.org/ghc/ghc/issues/14917" style="text-decoration-line:none;color:rgb(66,133,244)" target="_blank" rel="noreferrer" class="">https://gitlab.haskell.org/ghc/ghc/issues/14917</a></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px" class="">[2]: <a href="http://hackage.haskell.org/package/bytesmith-0.3.0.0/docs/src/Data.Bytes.Parser.Internal.html#Parser" target="_blank" rel="noreferrer" class="">http://hackage.haskell.org/package/bytesmith-0.3.0.0/docs/src/Data.Bytes.Parser.Internal.html#Parser</a></div></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Oct 27, 2019, 9:47 AM Zemyla <<a href="mailto:zemyla@gmail.com" target="_blank" rel="noreferrer" class="">zemyla@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr" class="">I'm wondering if there would be a benefit, if not to the average programmer, then to the ones working on deeper/faster code, to allow some of the # kinded types (mostly Int#, Word#, Char#, Float#, Double#) to be used in Safe code, and to have typeclasses able to work with them.</p><p dir="ltr" class="">For instance, the definition of Show would become:</p><p dir="ltr" class="">class Show (a :: TYPE r) where<br class="">
show :: a -> String<br class="">
default show :: (r ~ 'LiftedRep) => a -> String<br class="">
show x = showsPrec 0 x ""</p><p dir="ltr" class=""> showsPrec :: Int -> a -> ShowS<br class="">
default showsPrec :: (r ~ 'LiftedRep) => Int -> a -> ShowS<br class="">
showsPrec _ x s = show x ++ s</p><p dir="ltr" class=""> showList :: (r ~ 'LiftedRep) => [a] -> ShowS<br class="">
showList ls s = showList__ shows ls s</p><p dir="ltr" class="">The fact that the defaults only work when the type is a LiftedRep is a nonissue, because there's only a finite number of non-lifted types we'd be defining it for.</p><p dir="ltr" class="">You could do the same with Eq, Ord, Num, Real, Integral, Fractional, Floating, RealFrac, RealFloat, Semigroup, Monoid, Bits, FiniteBits, and probably several others I can't think of right now. However, with the functions that return pairs, you'd need a version that returns an unboxed pair instead. Assuming you changed ReadPrec, you could even do the same with Read:</p><p dir="ltr" class="">newtype ReadP (a :: RuntimeRep r) = ReadP (forall b. (a -> R b) -> R b)<br class="">
newtype ReadPrec (a :: RuntimeRep r) = ReadPrec (Int -> ReadP a)</p><p dir="ltr" class="">IO, ST, and STM could be made polykinded the same way, and would open up Storable. However, how to do a definition for Monad that works for polykinded monads is an issue. I do know that RebindableSyntax handles it easily when there's just one monad that can operate on multiple kinds, though.</p><p dir="ltr" class="">As for which # types could be exposed, I'm thinking that Char#, Int#, Word#, Float#, Double#, and Proxy# wouldn't be able to break out of Safe code. Int64# and Word64# would work as well, and for 64-bit machines would just be type aliases for Int# and Word# respectively. For types which have functions with undefined behavior for some arguments, you can just make wrappers that check the arguments and error out for the bad values. MutVar#, MVar#, TVar#, and StableName# don't open up any functions that would be unsuitable for safe code, either. I'm pretty sure that Array# and MutableArray# would also be safe, as long as all functions were length-checked and threw errors instead of having undefined behavior.</p><p dir="ltr" class="">As for why this would be a desirable thing? Mostly for the sake of convenience and generality, I think. I find myself working with unboxed values from time to time, and it's a pain to always remember to use (+#) for Int# and plusWord# for Word#. In addition, having typeclasses that can return unboxed values (like a hypothetical sized# :: Sized a => a -> Int# vs sized :: Sized a => a -> Int) can improve the generated code when the code using the typeclass doesn't get specialized.</p><p dir="ltr" class="">The module to import these would have to explain the differences between # kinded types and * kinded ones: # values aren't lazy; they can't be top-level definitions; you can't use unboxed tuples or sums with GHCi; and with a few exceptions, you can't place them in containers (you can't have an [Int#], for instance).</p>
_______________________________________________<br class="">
Libraries mailing list<br class="">
<a href="mailto:Libraries@haskell.org" rel="noreferrer noreferrer" target="_blank" class="">Libraries@haskell.org</a><br class="">
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer noreferrer" target="_blank" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br class="">
</blockquote></div>
</blockquote></div>
_______________________________________________<br class="">Libraries mailing list<br class=""><a href="mailto:Libraries@haskell.org" class="">Libraries@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries<br class=""></div></blockquote></div><br class=""></div></body></html>