Skip to content

Instantly share code, notes, and snippets.

@viercc
Last active October 5, 2019 01:03
Show Gist options
  • Save viercc/d51b0c4c7ae8333d9f82e59af2aa34c7 to your computer and use it in GitHub Desktop.
Save viercc/d51b0c4c7ae8333d9f82e59af2aa34c7 to your computer and use it in GitHub Desktop.
Flexible Instances I have found in wild

GHC itself

base

https://gitlab.haskell.org/ghc/ghc/blob/a31b24a591aecc734449d3af479e3a3d1834b0ed/libraries/base/Data/Fixed.hs

(I didn't think of kind polymorphism and don't know how that relates to FlexibleInstances)

-- Fixed is owned
instance (Typeable k,Typeable a) => Data (Fixed (a :: k)) where

ghc

https://gitlab.haskell.org/ghc/ghc/blob/5119296440e6846c553c72b8a93afc5ecfa576f0/ghc/GHCi/UI/Monad.hs

-- GhciMonad and GHCi are owned
instance GhciMonad (InputT GHCi) where
instance GhcMonad (InputT GHCi) where

ghc-heap

https://gitlab.haskell.org/ghc/ghc/blob/795986aaf33e2ffc233836b86a92a77366c91db2/libraries/ghc-heap/GHC/Exts/Heap.hs

instance HasHeapRep (a :: TYPE 'LiftedRep) where

compiler

https://gitlab.haskell.org/ghc/ghc/blob/f3cb8c7cb99e05feb0f62f5a076400dcf9f930a0/compiler/iface/IfaceType.hs

-- IfaceType is owned
instance Binary (DefMethSpec IfaceType) where

https://gitlab.haskell.org/ghc/ghc/blob/447864a94a1679b5b079e08bb7208a0005381cef/compiler/cmm/CmmExpr.hs

-- MPTC
-- UserOfRegs, DefinerOfRegs, LocalReg, CmmReg, CmmExpr are owned
instance UserOfRegs LocalReg CmmReg where
instance DefinerOfRegs LocalReg CmmReg where
instance UserOfRegs GlobalReg CmmReg where
instance DefinerOfRegs GlobalReg CmmReg where
instance Ord r => UserOfRegs r r where
instance Ord r => DefinerOfRegs r r where
instance (Ord r, UserOfRegs r CmmReg) => UserOfRegs r CmmExpr where
instance UserOfRegs r a => UserOfRegs r [a] where
instance DefinerOfRegs r a => DefinerOfRegs r [a] where

https://gitlab.haskell.org/ghc/ghc/blob/5119296440e6846c553c72b8a93afc5ecfa576f0/compiler/GHC/Hs/Pat.hs

-- (-Worphans is explicitly disabled in this module for another instance)
-- Pat is owned
type LPat a = Pat a
instance HasSrcSpan (LPat (GhcPass p)) where

stack

https://github.com/commercialhaskell/stack/blob/0b2c555c3080ea374179416b00d3afa78dfbabdf/src/Stack/Types/Nix.hs#L52

-- NixOptsMonoid is owned
instance FromJSON (WithJSONWarnings NixOptsMonoid) where

https://github.com/commercialhaskell/stack/blob/06cda686573cb8615f5f78f845a781fdb55a53d3/src/Stack/Lock.hs#L36

-- LockedLocation is owned
instance ( FromJSON (WithJSONWarnings (Unresolved a))
         , FromJSON (WithJSONWarnings (Unresolved b))
         ) =>
         FromJSON (WithJSONWarnings (Unresolved (LockedLocation a b))) where

https://github.com/commercialhaskell/stack/blob/be1970e48d85ad9d227e8aaa443706309ae770f3/src/Stack/Upload.hs#L61

-- HackageCreds is owned
instance FromJSON (FilePath -> HackageCreds) where

My codebase

https://github.com/viercc/relation/blob/d798e5bf04fca9bedb896582e328de0acd3bc84e/test/Data/Relation/LTRSpec.hs

class Iso t u where
instance (Ord a, Ord b) => Iso (Rel a b) (NaiveRel a b)
instance Iso a a where

https://gitlab.com/viercc/lambda/blob/master/src/Data/Lambda/Module.hs

-- Qualified is owned

-- | Never provide @Formattable (Qualified v)@ instance for
--   general @v@, as syntax for them varies.
instance Formattable (Qualified VarName) where

generic-lens

https://github.com/kcsongor/generic-lens/blob/631ba53627b06db447034c09625ca4ab2ca413d2/src/Data/Generics/Product/Fields.hs

class HasField (field :: Symbol) s t a b | s field -> a, t field -> b, s field b -> t, t field a -> s where

instance  -- see Note [Changing type parameters]
  ( HasTotalFieldP field (Rep s) ~~ 'Just a
  , HasTotalFieldP field (Rep t) ~~ 'Just b
  , HasTotalFieldP field (Rep (Indexed s)) ~~ 'Just a'
  , HasTotalFieldP field (Rep (Indexed t)) ~~ 'Just b'
  , t ~~ Infer s a' b
  , s ~~ Infer t b' a
  , HasField0 field s t a b
  ) => HasField field s t a b where
  field f s = field0 @field f s

(Note that this library uses FunDeps and Flexible & Undecidable instances extensively. This usage is not like instance PrettyPrint (MyExpr MyVarType))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment