我正在尝试使用Haskells Type Families添加约束的代码。我想强制输出始终是一个翻转,这个代码可以吗?
{-# LANGUAGE GADTs, TypeFamilies #-}
module TF where
data Even
data Odd
data Coin where
Up :: Coin
Down :: Coin
type family Flip n :: *
type instance Flip Even = Odd
type instance Flip Odd = Even
up = Up
down = Down
flip Up = Down
flip Down = Up
答案 0 :(得分:4)
将我的评论写入答案:
{-# LANGUAGE GADTs, DataKinds, TypeFamilies #-}
data Parity = Even | Odd
data Coin :: Parity -> * where
Up :: Coin Even
Down :: Coin Odd
type family Flip (p :: Parity) :: Parity where
Flip Even = Odd
Flip Odd = Even
flip :: Coin p -> Coin (Flip p)
flip Up = Down
flip Down = Up