<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Yes -- sweep it away!<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Feb 14, 2019, at 5:30 PM, Simon Peyton Jones via ghc-devs <<a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span class="">What do you (or anyone else) think about sweeping all that stuff away?  See my comments on<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""><a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fmerge_requests%2F361&data=02%7C01%7Csimonpj%40microsoft.com%7Cfc2d7f5498a34d68153008d692aaa9ae%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636857659024990967&sdata=H7euZSJmjiP%2BNE8KD9WD7TeeuHGFvuHzBAGLebPIMPM%3D&reserved=0" target="_blank" style="color: purple; text-decoration: underline;" class="">https://gitlab.haskell.org/ghc/ghc/merge_requests/361</a><o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">Simon</span><span class=""><o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span class=""><o:p class=""> </o:p></span></div><div style="border-style: none none none solid; border-left-width: 1.5pt; border-left-color: blue; padding: 0cm 0cm 0cm 4pt;" class=""><div class=""><div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(225, 225, 225); padding: 3pt 0cm 0cm;" class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><b class=""><span lang="EN-US" class="">From:</span></b><span lang="EN-US" class=""><span class="Apple-converted-space"> </span>Ryan Scott <<a href="mailto:ryan.gl.scott@gmail.com" class="">ryan.gl.scott@gmail.com</a>><span class="Apple-converted-space"> </span><br class=""><b class="">Sent:</b><span class="Apple-converted-space"> </span>14 February 2019 18:31<br class=""><b class="">To:</b><span class="Apple-converted-space"> </span>Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" class="">simonpj@microsoft.com</a>><br class=""><b class="">Cc:</b><span class="Apple-converted-space"> </span><a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a><br class=""><b class="">Subject:</b><span class="Apple-converted-space"> </span>Re: scopedSort and kind variable left-biasing<o:p class=""></o:p></span></div></div></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div class=""><div class=""><div class=""><div class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">Ah, I somehow forgot all about FreeKiTyVars. It turns out that the `freeKiTyVarsAllVars` function [1] is exactly what drives this behavior:<o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><br class="">    freeKiTyVarsAllVars :: FreeKiTyVars -> [Located RdrName]<br class="">    freeKiTyVarsAllVars (FKTV { fktv_kis = kvs, fktv_tys = tvs }) = kvs ++ tvs<o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">That's about as straightforward as it gets. Thanks!<o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">Ryan S.<o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">-----<o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">[1]<span class="Apple-converted-space"> </span><a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fblob%2F5c1f268e2744fab2d36e64c163858995451d7095%2Fcompiler%2Frename%2FRnTypes.hs%23L1604-1605&data=02%7C01%7Csimonpj%40microsoft.com%7Cfc2d7f5498a34d68153008d692aaa9ae%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636857659024990967&sdata=QaWLPwctKWcuIFJFdPAqBhJzA99%2FFtsftSuuAstHVEQ%3D&reserved=0" style="color: purple; text-decoration: underline;" class="">https://gitlab.haskell.org/ghc/ghc/blob/5c1f268e2744fab2d36e64c163858995451d7095/compiler/rename/RnTypes.hs#L1604-1605</a><o:p class=""></o:p></div></div></div></div></div></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">On Thu, Feb 14, 2019 at 12:46 PM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" style="color: purple; text-decoration: underline;" class="">simonpj@microsoft.com</a>> wrote:<o:p class=""></o:p></div></div><blockquote style="border-style: none none none solid; border-left-width: 1pt; border-left-color: rgb(204, 204, 204); padding: 0cm 0cm 0cm 6pt; margin-left: 4.8pt; margin-right: 0cm;" class=""><div class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">See Note [Kind and type-variable binders] in RnTypes, and Note [Ordering of implicit variables].</span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">And the data type FreeKiTyVars.</span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""> </span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">But NB: that in<span class="Apple-converted-space"> </span><a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fmerge_requests%2F361&data=02%7C01%7Csimonpj%40microsoft.com%7Cfc2d7f5498a34d68153008d692aaa9ae%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636857659024990967&sdata=H7euZSJmjiP%2BNE8KD9WD7TeeuHGFvuHzBAGLebPIMPM%3D&reserved=0" target="_blank" style="color: purple; text-decoration: underline;" class="">https://gitlab.haskell.org/ghc/ghc/merge_requests/361</a>, I argue that with this patch we can sweep all this away.</span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""> </span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">If we did, we’d probably end up with [j,a,k,b].  </span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""> </span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">Perhaps that’s an ergonomic reason for retaining the current rather cumbersome code.  (Maybe it could be simplified.)</span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""> </span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class="">Simon</span><o:p class=""></o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt;" class=""> </span><o:p class=""></o:p></div><div style="border-style: none none none solid; border-left-width: 1.5pt; padding: 0cm 0cm 0cm 4pt; border-color: currentcolor currentcolor currentcolor blue;" class=""><div class=""><div style="border-style: solid none none; border-top-width: 1pt; padding: 3pt 0cm 0cm; border-color: currentcolor;" class=""><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><b class=""><span lang="EN-US" class="">From:</span></b><span lang="EN-US" class=""><span class="Apple-converted-space"> </span>ghc-devs <<a href="mailto:ghc-devs-bounces@haskell.org" target="_blank" style="color: purple; text-decoration: underline;" class="">ghc-devs-bounces@haskell.org</a>><span class="Apple-converted-space"> </span><b class="">On Behalf Of<span class="Apple-converted-space"> </span></b>Ryan Scott<br class=""><b class="">Sent:</b><span class="Apple-converted-space"> </span>14 February 2019 15:35<br class=""><b class="">To:</b><span class="Apple-converted-space"> </span><a href="mailto:ghc-devs@haskell.org" target="_blank" style="color: purple; text-decoration: underline;" class="">ghc-devs@haskell.org</a><br class=""><b class="">Subject:</b><span class="Apple-converted-space"> </span>scopedSort and kind variable left-biasing</span><o:p class=""></o:p></div></div></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""> <o:p class=""></o:p></div><div class=""><div class=""><p class="MsoNormal" style="margin: 0cm 0cm 6pt; font-size: 11pt; font-family: Calibri, sans-serif;">Consider this function:<br class=""><br class="">    f :: Proxy (a :: j) -> Proxy (b :: k)<br class=""><br class="">If you just collect the free type variables of `f`'s type in left-to-right order, you'd be left with [a,j,b,k]. But the type of `f` is not `forall (a :: j) j (b :: k) k. Proxy a -> Proxy b`, as that would be ill scoped. `j` must come before `a`, since `j` appears in `a`'s kind, and similarly, `k` must come before `b`.<br class=""><br class="">Fortunately, GHC is quite smart about sorting free variables such that they respect dependency order. If you ask GHCi what the type of `f` is (with -fprint-explicit-foralls enabled), it will tell you this:<br class=""><br class="">    λ> :type +v f<br class="">    f :: forall j k (a :: j) (b :: k). Proxy a -> Proxy b<br class=""><br class="">As expected, `j` appears before `a`, and `k` appears before `b`.<br class=""><br class="">In a different context, I've been trying to implement a type variable sorting algorithm similar to the one that GHC is using. My previous understanding was that the entirely of this sorting algorithm was implemented in `Type.scopedSort`. To test my understanding, I decided to write a program using the GHC API which directly uses `scopedSort` on the example above:<br class=""><br class="">    main :: IO ()<br class="">    main = do<br class="">      let tv :: String -> Int -> Type -> TyVar<br class="">          tv n uniq ty = mkTyVar (mkSystemName (mkUniqueGrimily uniq) (mkTyVarOcc n)) ty<br class="">          j = tv "j" 0 liftedTypeKind<br class="">          a = tv "a" 1 (TyVarTy j)<br class="">          k = tv "k" 2 liftedTypeKind<br class="">          b = tv "b" 3 (TyVarTy k)<br class="">          sorted = scopedSort [a, j, b, k]<br class="">      putStrLn $ showSDocUnsafe $ ppr sorted<br class=""><br class="">To my surprise, however, running this program does /not/ give the answer [j,k,a,b], like what :type reported:<br class=""><br class="">    λ> main<br class="">    [j_0, a_1, k_2, b_3]<br class=""><br class="">Instead, it gives the answer [j,a,k,b]! Strictly speaking, this answer meets the specification of ScopedSort, since it respects dependency order and preserves the left-to-right ordering of variables that don't depend on each other (i.e., `j` appears to the left of `k`, and `a` appears to the left of `b`). But it's noticeably different that what :type reports. The order that :type reports, [j,k,a,b], appears to bias kind variables to the left such that all kind variables (`j` and `k`) appear before any type variables (`a` and `b`).<br class=""><br class="">From what I can tell, scopedSort isn't the full story here. That is, something else appears to be left-biasing the kind variables. My question is: which part of GHC is doing this left-biasing?<o:p class=""></o:p></p></div><div class=""><p class="MsoNormal" style="margin: 0cm 0cm 6pt; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p class=""></o:p></p></div><div class=""><p class="MsoNormal" style="margin: 0cm 0cm 6pt; font-size: 11pt; font-family: Calibri, sans-serif;">Ryan S.<o:p class=""></o:p></p></div></div></div></div></div></blockquote></div></div></div><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">_______________________________________________</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">ghc-devs mailing list</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class=""><a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a></span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class=""><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a></span></div></blockquote></div><br class=""></body></html>