@@ -191,10 +191,10 @@ data Phase = CompileTime | RunTime
191191-- Work Right to Left
192192
193193simpleCase :: Bool -> Bool -> Bool ->
194− Phase -> FC -> [Type] ->
194+ Phase -> FC -> [Int] -> [Type] ->
195195 [([Name], Term, Term)] ->
196196 TC CaseDef
197−simpleCase tc cover reflect phase fc argtys cs
197+simpleCase tc cover reflect phase fc inacc argtys cs
198198 = sc' tc cover phase fc (filter (\(_, _, r) ->
199199 case r of
200200 Impossible -> False
@@ -212,7 +212,7 @@ simpleCase tc cover reflect phase fc argtys cs
212212 OK pats ->
213213 let numargs = length (fst (head pats))
214214 ns = take numargs args
215− (ns', ps') = order ns pats
215+ (ns', ps') = order [(n, i `elem` inacc) | (i,n) <- zip [0..] ns] pats
216216 (tree, st) = runState
217217 (match ns' ps' (defaultCase cover))
218218 ([], numargs, [])
@@ -381,21 +381,25 @@ partition xs = error $ "Partition " ++ show xs
381381-- reorder the patterns so that the one with most distinct names
382382-- comes next. Take rightmost first, otherwise (i.e. pick value rather
383383-- than dependency)
384−
385−order :: [Name] -> [Clause] -> ([Name], [Clause])
386−order [] cs = ([], cs)
387−order ns [] = (ns, [])
388−order ns cs = let patnames = transpose (map (zip ns) (map fst cs))
389− pats' = transpose (sortBy moreDistinct (reverse patnames)) in
390− (getNOrder pats', zipWith rebuild pats' cs)
384+--
385+-- The first argument means [(Name, IsInaccessible)].
386+
387+order :: [(Name, Bool)] -> [Clause] -> ([Name], [Clause])
388+order [] cs = ([], cs)
389+order ns' [] = (map fst ns', [])
390+order ns' cs = let patnames = transpose (map (zip ns') (map fst cs))
391+ -- note: sortBy . reverse is not nonsense because sortBy is stable
392+ pats' = transpose (sortBy moreDistinct (reverse patnames)) in
393+ (getNOrder pats', zipWith rebuild pats' cs)
391394 where
392− getNOrder [] = error $ "Failed order on " ++ show (ns, cs)
393− getNOrder (c : _) = map fst c
395+ getNOrder [] = error $ "Failed order on " ++ show (map fst ns', cs)
396+ getNOrder (c : _) = map (fst . fst) c
394397
395398 rebuild patnames clause = (map snd patnames, snd clause)
396399
397− moreDistinct xs ys = compare (numNames [] (map snd ys))
398− (numNames [] (map snd xs))
400+ -- this compares (+isInaccessible, -numberOfCases)
401+ moreDistinct xs ys = compare (snd . fst . head $ xs, numNames [] (map snd ys))
402+ (snd . fst . head $ ys, numNames [] (map snd xs))
399403
400404 numNames xs (PCon n _ _ : ps)
401405 | not (Left n `elem` xs) = numNames (Left n : xs) ps