[英]Is there an accurate way in Haskell to encode tuple of Foo, where some certain combinations are forbidden?
(我很抱歉我不知道如何更好地表達這個問題)
假設我有這樣的數據類型:
data Foo = A | B
現在我想要一對Foo
,帶有禁止(A, A)
的約束。
我可以 go 以簡單的方式列出它們,如下所示:
data Foo2 = AB | BA | BB
但是正如你所看到的,這很快就會失控:如果我們想要Foo
的 n 元組怎么辦? 或者如果有更多Foo
的替代品怎么辦?
當然,另一種選擇是使用newtype
和智能構造函數
newtype Foo2 = Foo2 (Foo, Foo)
mkFoo2 xy = Foo2 xy <$ guard (xy /= (A,A))
但這在某種意義上是“不准確的”,因為當我們破壞Foo2
時,我們總是必須處理實際上無法到達的情況,而編譯器卻沒有這樣的知識:
...
case v :: Foo2 of
...
Foo2 (A, A) -> error "unreachable"
...
我的問題是,有沒有更好的方法來表達“Foo的n元組,其中某些組合,如(A,A)
或(A,B,C)
(當n=3
時)是不可能的”准確?
附帶問題:減法/否定是代數數據類型中的一件事嗎? 我認為我需要的基本上是一種與Foo^n - (forbidden combinations)
用於 n 元組。
沒有簡單的方法來禁止 Haskell 中的“所有A
s”。
在像 Agda/Coq 這樣的依賴類型語言中,我們可以使用 sigma 類型來放置任意約束。 然而,這需要程序員在每次使用構造函數時編寫數學證明,證明我們實際上並沒有嘗試構造“禁止”值之一。
相反,在 Haskell 中,我們沒有這樣的選項。 一種選擇可能是定義一堆類型。
data NotA = B | C
data Any = A | NA NotA
-- 1-tuple, not all As
data NotAllAs1
= N1 NotA
-- 2-tuple, not all As
data NotAllAs2
= N2 NotA Any
| N2a NotAllAs1 -- first A implicit
-- 3-tuple, not all As
data NotAllAs3
= N3 NotA (Any, Any)
| N3a NotAllAs1 -- first A implicit
等等。 這一點都不方便,因為我們需要使用大量的構造函數。 即使最終結果與我們想要的同構,也太麻煩了。
可以使用某些類型族對其進行改進,但看起來仍然相當不方便。
另一種選擇可能是也利用 GADT。
{-# LANGUAGE GADTs, DataKinds, TypeFamilies #-}
-- We define some tags for being A and not A
data IsA = IsA | NotA
-- Type T is indexed with the proper tag
data T (a :: IsA) where
A :: T 'IsA
B :: T 'NotA
C :: T 'NotA
-- We want "at least one non-A" so we define an "or"
-- operation between two tags.
type family Or (a1 :: IsA) (a2 :: IsA) :: IsA where
Or 'IsA a2 = a2
Or 'NotA _ = 'NotA
-- Peano naturals to encode tuple length
data Nat = Z | S Nat
-- The wanted tuple type
type NotAllAs (n :: Nat) = NA n 'NotA
-- NA n t is the type for an n-tuple having either all As
-- (if t ~ IsA) or some non-A (if t ~ NotA)
data NA (n :: Nat) (t :: IsA) where
Nil :: NA 'Z 'IsA
Cons :: T a1 -> NA n a2 -> NA ('S n) (Or a1 a2)
最后,幾個測試,取消注釋一個來嘗試。
test :: NotAllAs ('S ('S ('S 'Z)))
test =
-- Cons A (Cons A (Cons A Nil)) -- Couldn't match type 'IsA with 'NotA
-- Cons A (Cons A (Cons B Nil)) -- OK
-- Cons A (Cons B (Cons A Nil)) -- OK
-- Cons B (Cons A (Cons A Nil)) -- OK
下面的測試測試消除(模式匹配)。 對於不可能的情況A,A,A
它不會觸發警告:匹配被認為是詳盡的。
elim :: NotAllAs ('S ('S ('S 'Z))) -> Int
elim (Cons A (Cons A (Cons B Nil))) = 1
elim (Cons A (Cons A (Cons C Nil))) = 2
elim (Cons A (Cons B _ )) = 3
elim (Cons A (Cons C _ )) = 4
elim (Cons B _ ) = 5
elim (Cons C _ ) = 6
也沒有警告: A,A
是不可能的。
elim2 :: NotAllAs ('S ('S 'Z)) -> Int
elim2 (Cons x (Cons A Nil)) = case x of B -> 1 ; C -> 2
elim2 (Cons _ (Cons B Nil)) = 3
elim2 (Cons _ (Cons C Nil)) = 4
在依賴類型語言中,執行消除並不是那么容易,因為我們需要證明匹配確實是窮舉的,通常通過對包括A,A
在內的所有情況執行依賴匹配,然后得出矛盾。 相比之下,這是 Coq 中消除的樣子:
Inductive T: Set := A | B | C .
(* The constraint is trivial to specify. *)
Definition NotAllA2 := { p: T*T | p <> (A,A) } .
(* We will need this trivial lemma later *)
Lemma lem: forall x, (x,A) <> (A,A) -> x<>A .
Proof.
intros x h h2.
subst.
apply h.
reflexivity.
Qed.
Definition elim2 (v: NotAllA2): nat :=
match v with
| exist _ p h => (* h is the proof that our constraint holds *)
match p return p<>(A,A) -> nat with
| (x,A) => fun h2: (x,A)<>(A,A) =>
match x return x<>A -> nat with
(* We need to prove that A is impossible here *)
| A => fun h3 => match h3 eq_refl with end
| B => fun _ => 1
| C => fun _ => 2
end (lem x h2)
| (_,B) => fun _ => 3
| (_,C) => fun _ => 4
end h
end.
(可能有一個更短/更簡單的 Coq 解決方案,但這是我能設法制作的第一個。)
以下是我用 GADT 表達它的方式:
{-# LANGUAGE DataKinds, KindSignatures, TypeFamilies, GADTs, TypeOperators #-}
{-# LANGUAGE StandaloneDeriving #-} -- for `Show`
import Data.Type.Bool
data Foo = A | B -- this could
data TypedFoo x where -- probably be
TypedA :: TypedFoo 'A -- expressed nicer
TypedB :: TypedFoo 'B -- with singletons
deriving instance Show (TypedFoo x)
type family IsA x where
IsA 'A = 'True
IsA 'B = 'False
data Foo2 where
Foo2 :: ((IsA x && IsA y) ~ 'False)
=> TypedFoo x -> TypedFoo y -> Foo2
deriving instance Show Foo2
現在
ghci> Foo2 TypedA TypedB
Foo2 TypedA TypedB
ghci> Foo2 TypedA TypedA
<interactive>:6:1: error:
• Couldn't match type ‘'True’ with ‘'False’
arising from a use of ‘Foo2’
• In the expression: Foo2 TypedA TypedA
In an equation for ‘it’: it = Foo2 TypedA TypedA
並且以下內容不會給出不完整警告:
elim2 :: Foo2 -> Int
elim2 (Foo2 TypedA TypedB) = 0
elim2 (Foo2 TypedB TypedA) = 1
elim2 (Foo2 TypedB TypedB) = 2
我不確定用 Haskell 數據技術對這種約束進行編碼是否真的有用。 IMO 一個簡單的智能構造函數就可以了,只是不要導出它,這樣人們也不會遇到不應該存在的案例的問題。 相反,試着想想為什么兩個A
的情況沒有意義的更高層次的原因,並相應地設計該類型的公共接口。 那么內部表示允許非法組合並不重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.