Tysonzero

Pseudo closed type class

Mar 1st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses                      
  2.   , ScopedTypeVariables, TypeApplications, TypeFamilies, UndecidableInstances                          
  3. #-}                                                                                                    
  4.                                                                                                        
  5. module Overlap2 where                                                                                  
  6.                                                                                                        
  7. import Data.Monoid ((<>))                                                                              
  8.                                                                                                        
  9. data ViewP (a :: ViewR) = ViewP                                                                        
  10.                                                                                                        
  11. data ViewR = Default | Overload                                                                        
  12.                                                                                                        
  13. type family ViewF a :: ViewR where                                                                      
  14.     ViewF (a, b) = 'Overload                                                                            
  15.    ViewF a = 'Default                                                                                  
  16.                                                                                                        
  17. class View a (r :: ViewR) where                                                                        
  18.     view' :: a -> ViewP r -> String                                                                    
  19.                                                                                                        
  20. instance Show a => View a 'Default where                                                                
  21.     view' = const . show                                                                                
  22.                                                                                                        
  23. instance forall a b. (View a (ViewF a), View b (ViewF b)) => View (a, b) 'Overload where                
  24.     view' (x, y) _ = "(" <> view x <> ", " <> view y <> ")"                                            
  25.                                                                                                        
  26. view :: forall a. (View a (ViewF a)) => a -> String                                                    
  27. view x = view' x (ViewP @(ViewF a))
Add Comment
Please, Sign In to add comment