<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>tyCoVarsOfTypesList guarantees that it returns its answer in a deterministic order. For the longest time, I must have assumed that this order was left to right. However, it appears that my assumption was wrong! This can be demonstrated with this program:</div>    module Main where<br>    <br>    import Name<br>    import TyCoRep<br>    import TysPrim<br>    import Var<br>    <br>    main :: IO ()<br>    main = do<br>      putStrLn "(1)"<br>      print $ map (getOccString . tyVarName)<br>            $ tyCoVarsOfTypesList<br>              [TyVarTy alphaTyVar, TyVarTy betaTyVar]<br>    <br>      putStrLn "(2)"<br>      print $ map (getOccString . tyVarName)<br>            $ tyCoVarsOfTypesList<br>              [TyVarTy alphaTyVar, TyVarTy betaTyVar, TyVarTy alphaTyVar]<div><br></div><div>This gives the following output:</div><div><br></div><div>    (1)<br>    ["a","b"]<br>    (2)<br>    ["b","a"]</div><div><br></div><div>The first one makes total sense to me. The second, one however, does not. If the free variables of that list were returned in left-to-right order (or even right-to-left order!), then (2) should give the same answer as (1). Instead, it lists "b" _before_ "a", which I find incredibly baffling.</div><div><br></div><div>To explain why I care so much about this, we're currently trying to improve Haddock's logic for choosing when to put explicit `forall`s in front of types [1]. Our litmus test is this: if the order in which a user wrote the `forall`d variables differs from the order in which the free variables of the body would normally occur, then have Haddock display an explicit forall. I would have assumed that tyCoVarsOfTypesList [2] would be enough to determine the "normal" order of the free variables, but as the example above proves, this sometimes gives unexpected orderings when there are multiple occurrences of the same variable.</div><div><br></div><div>We are currently having to work around this issue [1] by implementing our own custom versions of tyCoFVsOfType and friends that accumulate variables in reverse order (and then reversing the list at the end!) to get the order we expect. This feels incredibly wasteful to me, so I'd like to know if there's a better way. In particular:</div><div><br></div><div>1. Is this behavior of tyCoVarsOfTypesList expected?</div><div>2. If not, should we change it?<br></div><div><br></div><div>Ryan S.</div><div>-----</div><div>[1] See <a href="https://github.com/haskell/haddock/pull/931">https://github.com/haskell/haddock/pull/931</a></div><div>[2] Actually, I would use tyCoVarsOfTypesWellScoped, but dependency order doesn't come into play in the example I gave above.<br></div></div></div></div></div></div>