Skip to content

Instantly share code, notes, and snippets.

@kuribas
Last active July 21, 2018 09:31
Show Gist options
  • Select an option

  • Save kuribas/fa0955ee330145fc87c0f57825060c3e to your computer and use it in GitHub Desktop.

Select an option

Save kuribas/fa0955ee330145fc87c0f57825060c3e to your computer and use it in GitHub Desktop.
haskell inheritance
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
module Subtype where
data Derived super sub = Derived super sub
class IsInstance b a where
getData :: (a -> v) -> b -> v
setData :: (a -> s -> s) -> b -> s -> s
instance IsInstance (Derived a b) a where
getData getter (Derived a _) = getter a
setData setter (Derived a _) = setter a
instance IsInstance b c => IsInstance (Derived a b) c where
getData getter (Derived _ b) = getData getter b
setData setter (Derived _ b) = getData setter b
data Super = Super {name :: String}
data Sub = Sub {age :: Int}
getName :: IsInstance b Super => b -> String
getName = getData name
getAge :: IsInstance b Sub => b -> Int
getAge = getData age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment