从普通列表创建索引链表
create Index Linked List from a plain List
我目前正在玩索引链表。基本数据类型由
给出
data LList (n :: Nat) (a :: Type) where
Nil ::LList 0 a
(:@) ::a -> LList n a -> LList (n + 1) a
我想知道是否可以定义从 []
到 LList
的映射?
return 类型取决于运行时信息,因为列表的长度当然在编译时不可用。
fromList :: ? => [a] => LList ? a
fromList = undefined
游乐场的完整源代码is available here。
是的,只是使用存在主义。这将列表的长度和列表本身包装成一对,在类型中不显示它的长度。
data SomeLList a = forall n. SomeLList (LList n a)
这表示 SomeLList a
由 SomeLList @(n :: Nat) (_ :: LList n a)
形式的项组成。这种类型实际上等同于 []
(除了一个额外的底部和没有无穷大)
fromList :: [a] -> SomeLList a
fromList [] = Nil
fromList (x : xs) | SomeList xs' <- fromList xs = SomeList (x :@ xs)
你可以通过匹配得到类型对:
something :: [a] -> ()
something xs
| SomeList xs' <- fromList xs
= -- here, xs' :: SomeList n xs, where n :: Nat is a new type (invisibly) extracted from the match
-- currently, we don't know anything about n except its type, but we could e.g. match on xs', which is a GADT and could tell us about n
()
我目前正在玩索引链表。基本数据类型由
给出data LList (n :: Nat) (a :: Type) where
Nil ::LList 0 a
(:@) ::a -> LList n a -> LList (n + 1) a
我想知道是否可以定义从 []
到 LList
的映射?
return 类型取决于运行时信息,因为列表的长度当然在编译时不可用。
fromList :: ? => [a] => LList ? a
fromList = undefined
游乐场的完整源代码is available here。
是的,只是使用存在主义。这将列表的长度和列表本身包装成一对,在类型中不显示它的长度。
data SomeLList a = forall n. SomeLList (LList n a)
这表示 SomeLList a
由 SomeLList @(n :: Nat) (_ :: LList n a)
形式的项组成。这种类型实际上等同于 []
(除了一个额外的底部和没有无穷大)
fromList :: [a] -> SomeLList a
fromList [] = Nil
fromList (x : xs) | SomeList xs' <- fromList xs = SomeList (x :@ xs)
你可以通过匹配得到类型对:
something :: [a] -> ()
something xs
| SomeList xs' <- fromList xs
= -- here, xs' :: SomeList n xs, where n :: Nat is a new type (invisibly) extracted from the match
-- currently, we don't know anything about n except its type, but we could e.g. match on xs', which is a GADT and could tell us about n
()