Haskell 新的GHC8和隐式参数
在his中,Oleg反映类型级别的值 有了GHC8的新工具,他的演讲还有什么可以改进的地方? 例如,我们可以在类型应用程序中删除undefined的用法,但是还有什么呢 这是介绍之后的原文Haskell 新的GHC8和隐式参数,haskell,typeclass,implicit,Haskell,Typeclass,Implicit,在his中,Oleg反映类型级别的值 有了GHC8的新工具,他的演讲还有什么可以改进的地方? 例如,我们可以在类型应用程序中删除undefined的用法,但是还有什么呢 这是介绍之后的原文 {-# LANGUAGE PolyKinds #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE StandaloneDeriving #-} #!/usr/bin/env stack -- stack --resolver lts-9.2 script --pac
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE StandaloneDeriving #-}
#!/usr/bin/env stack
-- stack --resolver lts-9.2 script --package http-conduit --package tagged --package lens
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ExplicitForAll #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-unused-local-binds -fno-warn-missing-signatures #-}
import Prelude
import Control.Lens.Operators ((<&>))
import Data.Function
import Data.Kind
使用类型应用程序,我们可以很好地清理未定义的
--- Type Application
-- outillage
newtype Tagged s a = Tagged { unTag :: a } deriving Show
deriving instance Functor (Tagged s)
instance Applicative (Tagged s) where
pure = Tagged
Tagged f <*> Tagged x = Tagged (f x)
_ *> n = n
retag :: Tagged s b -> Tagged t b
retag = Tagged . unTag
class ReflectNum s where reflectNum :: Num a => Tagged s a
instance ReflectNum Zero where reflectNum = Tagged 0
instance ReflectNum s => ReflectNum (Pred s) where reflectNum = (reflectNum @ s) <&> ((-) 1) & retag
instance ReflectNum s => ReflectNum (Succ s) where reflectNum = (reflectNum @ s) <&> ((+) 1) & retag
instance ReflectNum s => ReflectNum (Twice s) where reflectNum = (reflectNum @ s) <&> (* 2) & retag
getReflNum :: forall s a. (ReflectNum s, Num a) => a
getReflNum = unTag (reflectNum @ s)
v3 :: Int = getReflNum @ Zero
---类型应用程序
--优胜劣汰
newtype taged s a=taged{unTag::a}派生显示
派生实例函子(带标记的)
实例应用程序(已标记),其中
纯=标记
标记的f标记的x=标记的(f x)
_*>n=n
重新标记::标记的s b->标记的t b
重新标记=标记。东帝汶过渡当局
类ReflectNum s,其中ReflectNum::Num a=>标记了s a
实例ReflectNum Zero,其中ReflectNum=标记为0
实例ReflectNum s=>ReflectNum(Pred s),其中ReflectNum=(ReflectNum@s)((-)1)和retag
实例ReflectNum s=>ReflectNum(succs),其中ReflectNum=(ReflectNum@s)(++)1)和retag
实例ReflectNum s=>ReflectNum(两次s),其中ReflectNum=(ReflectNum@s)(*2)和retag
getReflNum::对于所有s a。(反射数s,反射数a)=>a
getReflNum=unTag(reflectNum@s)
v3::Int=getReflNum@Zero
我们能做得更好吗?就像用更好的方式表达更多的属性一样?是。。。它漂亮吗?太可怕了吗?@melpomene噢,谢谢你!
--- Type Application
-- outillage
newtype Tagged s a = Tagged { unTag :: a } deriving Show
deriving instance Functor (Tagged s)
instance Applicative (Tagged s) where
pure = Tagged
Tagged f <*> Tagged x = Tagged (f x)
_ *> n = n
retag :: Tagged s b -> Tagged t b
retag = Tagged . unTag
class ReflectNum s where reflectNum :: Num a => Tagged s a
instance ReflectNum Zero where reflectNum = Tagged 0
instance ReflectNum s => ReflectNum (Pred s) where reflectNum = (reflectNum @ s) <&> ((-) 1) & retag
instance ReflectNum s => ReflectNum (Succ s) where reflectNum = (reflectNum @ s) <&> ((+) 1) & retag
instance ReflectNum s => ReflectNum (Twice s) where reflectNum = (reflectNum @ s) <&> (* 2) & retag
getReflNum :: forall s a. (ReflectNum s, Num a) => a
getReflNum = unTag (reflectNum @ s)
v3 :: Int = getReflNum @ Zero