• code Haskell typefun
    смотрите какая классная штуковина:
    
    {-# LANGUAGE GADTs #-}
    import Data.Dependent.Map as D
    import Data.GADT.Compare
    
    import GHC.Generics
    
    data Hint a where
      HintPort   :: Hint Int
      HintHost   :: Hint String
      HintTeapot :: Hint Int
    
    instance GEq Hint where
      HintPort `geq` HintPort = Just Refl
      HintHost `geq` HintHost = Just Refl
      HintTeapot `geq` HintTeapot = Just Refl
      _ `geq` _ = Nothing
    
    instance GCompare Hint where
      HintPort `gcompare` HintPort = GEQ
      HintHost `gcompare` HintHost = GEQ
      HintTeapot `gcompare` HintTeapot = GEQ
      HintPort `gcompare` HintHost = GGT
      HintPort `gcompare` HintTeapot = GGT
      HintHost `gcompare` HintTeapot = GGT
      a `gcompare` b = case b `gcompare` a of
                         GEQ -> GEQ
    		     GGT -> GLT
    		     GLT -> GGT
    
    fibs = 1:1:zipWith (+) fibs (tail fibs)
    
    test :: DMap Hint
    test = D.insert HintTeapot 7 $ D.insert HintPort 99 $ D.insert HintPort 7 D.empty
    
    и потом:
    
    *Main> test ! HintPort
    99
    *Main> test ! HintTeapot
    7
    *Main> test ! HintHost
    "*** Exception: DMap.find: element not in the map
    *Main> HintHost `D.lookup` test
    Nothing
    
    
    ещё бы инстансы автоматом генерить (хотя это наверняка в либе есть)
    ♡ recommended by @segfault

Replies (12)