Changes between Version 18 and Version 19 of PatternSynonyms


Ignore:
Timestamp:
Aug 29, 2013 8:27:02 AM (20 months ago)
Author:
simonpj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PatternSynonyms

    v18 v19  
    3434   isInt _ = False 
    3535}}} 
     36 
     37Here is a second example from [http://www.reddit.com/r/haskell/comments/1kmods/patternsynonyms_ghc_trac/ pigworker on Reddit]. 
     38Your basic sums-of-products functors can be built from this kit. 
     39{{{ 
     40newtype K a        x  = K a 
     41newtype I          x  = I x 
     42newtype (:+:) f g  x  = Sum (Either (f x) (g x)) 
     43newtype (:*:) f g  x  = Prod (f x, g x) 
     44}}} 
     45and then you can make recursive datatypes via 
     46{{{ 
     47newtype Fix f = In (f (Fix f)) 
     48}}} 
     49e.g., 
     50{{{ 
     51type Tree = Fix (K () :+: (I :*: I)) 
     52}}} 
     53and you can get useful generic operations cheaply because the functors in the kit are all `Traversable`, admit a partial zip operation, etc. 
     54 
     55You can define friendly constructors for use in expressions 
     56{{{ 
     57leaf :: Tree 
     58leaf = In (Sum (Left (K ()))) 
     59node :: Tree -> Tree -> Tree 
     60node l r = In (Sum (Right (Prod (I l, I r)))) 
     61}}} 
     62but any `Tree`-specific pattern matching code you write will be wide and obscure. Turning these definitions into pattern synonyms means you can have both readable type-specific programs and handy generics without marshalling your data between views. 
    3663 
    3764== Pattern-only synonyms ==