Der*_*urk 8 haskell types ghc dependent-type idris
我在Idris中有一个简单的概念证明,它使用依赖类型来强制执行一些不太复杂的业务逻辑.一些名字已被改变以保护不那么无辜,但我们的想法是我们想要按顺序收集"线".每一行都属于特定部分,但只有一个(EconProduction)有我们关心的任何内容.通常,行具有特定于节的关键字和表达式,其表单/类型可能取决于所使用的关键字.
对于此特定部分,每行描述"阶段"(Prod)的某些数字,或者继续最后命名的"阶段"(Continue).
在Idris,我们可以这样做:
data EconSection
= EconGeneral
| EconProduction
data EconPhase
= Oil
| Water
| NumPhase Nat
data ContState
= ContNone
| ContProd EconPhase
data Keyword : EconSection -> ContState -> ContState -> Type where
Prod : (p : EconPhase) -> Keyword EconProduction c (ContProd p)
Continue : Keyword s c c
data Expression : (s : EconSection) ->
(d : ContState) ->
Keyword s c d ->
Type where
ExProc : Double -> Double -> Expression EconProduction (ContProd p) k
data Line : EconSection -> ContState -> ContState -> Type where
L : (k : Keyword s c d) -> Expression s d k -> Line s c d
data Lines : EconSection -> ContState -> Type where
First : Line s ContNone d -> Lines s d
Then : Lines s c -> Line s c d -> Lines s d
infixl 0 `Then`
good : Lines EconProduction (ContProd (NumPhase 1))
good = First (L (Prod Oil) (ExProc 23.2 70.1))
`Then` (L (Continue) (ExProc 27.9 1.2))
`Then` (L (Prod (NumPhase 1)) (ExProc 91.2 7014.1))
`Then` (L (Continue) (ExProc 91.2 7014.1))
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好!通常的依赖类型状态业务.出于非常实际的商业原因,我们希望在GHC Haskell中实际实现这一逻辑.我用单例构建它(根据需要自己编译而不是使用singletons包,只是为了简短的概念证明):
{-# LANGUAGE GADTs, KindSignatures, DataKinds #-}
{-# LANGUAGE RankNTypes, TypeInType, TypeOperators #-}
{-# LANGUAGE TypeFamilies, TypeFamilyDependencies, MultiParamTypeClasses #-}
import Data.Kind (Type)
data Nat
= Z
| S Nat
data SNat :: Nat -> Type where
SZ :: SNat 'Z
SS :: SNat n -> SNat ('S n)
data SSNat :: forall (n :: Nat) . SNat n -> Type where
SSZ :: SSNat 'SZ
SSS :: SSNat n -> SSNat ('SS n)
type family SingNat (n :: Nat) :: SNat n where
SingNat 'Z = 'SZ
SingNat ('S n) = 'SS (SingNat n)
data EconSection
= EconGeneral
| EconProduction
data SEconSection :: EconSection -> Type where
SEconGeneral :: SEconSection 'EconGeneral
SEconProduction :: SEconSection 'EconProduction
type family SingSection (s :: EconSection) :: SEconSection s where
SingSection 'EconGeneral = 'SEconGeneral
SingSection 'EconProduction = 'SEconProduction
data EconPhase
= Oil
| Water
| NumPhase Nat
data SEconPhase :: EconPhase -> Type where
SOil :: SEconPhase 'Oil
SWater :: SEconPhase 'Water
SNumPhase :: SNat n -> SEconPhase ('NumPhase n)
data SSEconPhase :: forall (p :: EconPhase) . SEconPhase p -> Type where
SSOil :: SSEconPhase 'SOil
SSWater :: SSEconPhase 'SWater
SSNumPhase :: SSNat n -> SSEconPhase ('SNumPhase n)
type family SingEconPhase (p :: EconPhase) :: SEconPhase p where
SingEconPhase 'Oil = 'SOil
SingEconPhase 'Water = 'SWater
SingEconPhase ('NumPhase n) = 'SNumPhase (SingNat n)
data ContState
= ContNone
| ContProd EconPhase
data SContState :: ContState -> Type where
SContNone :: SContState 'ContNone
SContProd :: SEconPhase p -> SContState ('ContProd p)
type family SingContState (c :: ContState) :: SContState c where
SingContState 'ContNone = 'SContNone
SingContState (ContProd p) = 'SContProd (SingEconPhase p)
data Keyword :: EconSection -> ContState -> ContState -> Type where
Prod :: SEconPhase p -> Keyword 'EconProduction c ('ContProd p)
Continue :: Keyword s c c
data SKeyword :: forall (s :: EconSection) (c :: ContState) (d :: ContState) .
Keyword s c d -> Type where
SProd :: SSEconPhase p -> SKeyword ('Prod p)
SContinue :: SKeyword 'Continue
data Expression :: forall (s :: EconSection) (c :: ContState) (d :: ContState) .
SEconSection s -> SContState d -> Keyword s c d -> Type where
ExProc :: Double -> Double -> Expression SEconProduction (SContProd p) k
type family KWSection k where
KWSection (Keyword s _ _) = s
type family KWFrom k where
KWFrom (Keyword _ c _) = c
type family KWTo k where
KWTo (Keyword _ _ d) = d
data Line :: EconSection -> ContState -> ContState -> Type where
L :: SKeyword (k :: Keyword s c d)
-> Expression (SingSection s) (SingContState d) k
-> Line s c d
data Lines :: EconSection -> ContState -> Type where
First :: Line s 'ContNone d -> Lines s d
Then :: Lines s c -> Line s c d -> Lines s d
infixl 0 `Then`
good :: Lines 'EconProduction ('ContProd ('NumPhase ('S 'Z)))
good = First (L (SProd SSOil) (ExProc 23.2 70.1))
`Then` (L (SContinue) (ExProc 27.9 1.2))
`Then` (L (SProd (SSNumPhase (SSS SSZ))) (ExProc 91.2 7014.1))
`Then` (L (SContinue) (ExProc 91.2 7014.1))
Run Code Online (Sandbox Code Playgroud)
这是我的问题.有没有办法避免"单身人士"?我根本不喜欢像SSNat等等的东西,但这是我通过将每个pi类型转换为额外的单一层来获得的.我还没有能够使任何更简单的方法工作,我没有在singletons包中看到任何聪明的想法,使这更容易,虽然我可能很容易错过所有模板Haskell下面的东西.
是.
考虑到,通过单例类型的定义,您在单例中具有与该单例的单例一样多的类型信息,因为它们都具有唯一的实例.
考虑到上述情况,我们可以从代码中删除SSNat和SSEconPhase声明.然后,在SProd构造函数中
SProd :: SSEconPhase p - > SKeyword ('Prod p)
Run Code Online (Sandbox Code Playgroud)
我们知道这SEconPhase将足以决定p,所以我们可以将其改写为
SProd :: SEconPhase p - > SKeyword ('Prod p)
Run Code Online (Sandbox Code Playgroud)
这会产生一种错误 - 我们需要的是类型转换
SomeType :: (p :: EconPhase) -> SEconPhase p
Run Code Online (Sandbox Code Playgroud)
您已在代码中定义的那个SingEconPhase.结果是
SProd :: SEconPhase p - > SKeyword ('Prod (SingEconPhase p))
Run Code Online (Sandbox Code Playgroud)
您永远不必编写单例的单例 - 如果您需要将类型参数"提升"为单例类型,那么正确的选择就是编写一个类型系列.