aelve / haskell-issues

An unofficial issue tracker for all things Haskell-related
18 stars 0 forks source link

The ability to be able to indicate that your pattern synonyms are total #50

Closed mstksg closed 7 years ago

mstksg commented 7 years ago

Sometimes pattern synonyms are just alternate total views to a value, and it's a bit misleading when ghc complains that they are potentially not total. Also, if you ever use a pattern synonym as a way to abstract over real constructors, then pattern synonyms not being recognized as total is a total abstraction leakage.

For example:

pattern State s <- (runState->s) where
    State s = state s

should basically let you pretend that there's a State constructor for State s a that contains an s -> (a, s). For the most part this goes well, except for the fact that if you do pattern match on State, ghc complains erroneously about an incomplete pattern match

Sometimes multiple sets of pattern match can constitute a total match (ie, nil/cons patterns for Seq). I think for this we can borrow the same syntax as the MINIMAL pragma. For example, if we wanted to turn ViewL/ViewR from Seq to be implemented with view patterns, we could do:

pattern x :< xs <- (S.viewl -> x S.:< xs) where
    x :< xs = x <| xs

pattern xs :> x <- (S.viewr -> xs S.:> x) where
    xs :> xs = xs |> x

pattern Nil <- (S.null -> True) where
    Nil = S.empty

And we could specify total matches as MINIMALTOTAL (:<), Nil | (:>), Nil, maybe?

neongreen commented 7 years ago

This is a cool idea. I looked on GHC Trac and found that it's already been proposed: https://ghc.haskell.org/trac/ghc/ticket/8779. It's even implemented already: https://phabricator.haskell.org/D2669. (Well, almost implemented and not merged into GHC yet.)