Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. module Main where
  2.  
  3. -- We will define our checker by piecing together functions based on the various types of binders.
  4. -- We use prisms to abstract over the data constructors representing binders
  5. type Prism a b = (a -> b, b -> Maybe a)
  6.  
  7. -- As an example, suppose we want to check array binders [_, _, ..., _] for exhaustivity.
  8. -- We could introduce two constructors ArrNil and ArrCons, and require these constructors be
  9. -- binders in our language.
  10. data Array b = ArrNil | ArrCons b b deriving (Show)
  11.  
  12. -- Here, I'm just implementing the "complement" function, which generates all
  13. -- patterns uncovered by a given pattern. This function lifts such a function to
  14. -- handle array binders.
  15. missArray :: Prism Wildcard b -> Prism (Array b) b -> (b -> [b]) -> b -> [b]
  16. missArray (wild, _) (inj, prj) f b =
  17. case prj b of
  18. Just arr -> go arr
  19. Nothing -> f b
  20. where
  21. go ArrNil = [inj (ArrCons (wild Wildcard) (wild Wildcard))]
  22. go (ArrCons x xs) = inj ArrNil : map (inj . (`ArrCons` xs)) (f x) ++ map (inj . ArrCons x) (f xs)
  23.  
  24. -- We can handle wildcards similarly
  25. data Wildcard = Wildcard
  26.  
  27. missWildcard :: Prism Wildcard b -> (b -> [b]) -> b -> [b]
  28. missWildcard (_, prj) f b =
  29. case prj b of
  30. Just _ -> []
  31. Nothing -> f b
  32.  
  33. -- Now we can assemble our complement function piece-by-piece by specifying a few prisms.
  34. data Test = Wild | Arr (Array Test) deriving (Show)
  35.  
  36. test = missArray wildPrism arrPrism
  37. . missWildcard wildPrism
  38. $ test
  39.  
  40. arrPrism = (Arr, isArr)
  41. where
  42. isArr (Arr a) = Just a
  43. isArr _ = Nothing
  44.  
  45. wildPrism = (const Wild, isWild)
  46. where
  47. isWild Wild = Just Wildcard
  48. isWild _ = Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement