- 関数型プログラミング言語Haskell Part29 [転載禁止]©2ch.net
412 :デフォルトの名無しさん[sage]:2015/08/31(月) 00:21:09.78 ID:VGvD8wHt - ()もいいけど、型をラベルみたいにして情報載せるのは、ファントムタイプの出番ですよ。
data Umai a = Umai { defaultLevel :: Int, season :: Season} data Season = Spring | Summer | Autumn | Winter deriving Eq data Nasu data Tomato class Level a where level :: a -> Int instance Level (Umai Nasu) where level = defaultLevel * 100 instance Level (Umai Tomato) where level = defaultLevel * (liftA (== Summer) season ?) 200 100 {-- > level (Umai 1 Summer :: Umai Nasu) 100 > level (Umai 1 Summer :: Umai Tomato) 200 > level (Umai 2 Summer :: Umai Tomato) 400 --}
| - 関数型プログラミング言語Haskell Part29 [転載禁止]©2ch.net
413 :412[sage]:2015/08/31(月) 00:22:07.09 ID:VGvD8wHt - ちな
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances,TypeFamilies #-} module Main where import Control.Applicative (412の中身) instance Num n => Num (a -> n) where (+)= liftA2 (+) (-) = liftA2 (-) (*) = liftA2 (*) abs = liftA abs signum = liftA signum fromInteger = const . fromInteger class Conditional q a where type ConditionalExec q a (?) :: q -> ConditionalExec q a -> ConditionalExec q a -> ConditionalExec q a infixr 1 ? instance Conditional Bool a where type ConditionalExec Bool a = a (?) b x y = if b then x else y instance Conditional (a -> Bool) b where type ConditionalExec (a -> Bool) b = a -> b (?) = liftA3 (?) main :: IO () main = print $ level (Umai 1 Spring :: Umai Tomato)
|
|