我想在Dhall中代表一个Wiki(一组包含有向图的文档)。这些文档将呈现为HTML,我想防止产生断开的链接。如我所见,这可以通过使无效图形(具有不存在的节点的链接的图形)无法通过类型系统来表示,或者通过编写函数以返回任何可能的图形中的错误列表(例如“在可能的图形中”)来实现X,节点A包含到不存在的节点B的链接”)。
天真的邻接列表表示可能看起来像这样:
let Node : Type = {
id: Text,
neighbors: List Text
}
let Graph : Type = List Node
let example : Graph = [
{ id = "a", neighbors = ["b"] }
]
in example
如该示例所示,此类型接受与有效图不对应的值(不存在ID为“ b”的节点,而ID为“ a”的节点规定了ID为“ b”的邻居) 。此外,由于Dhall不支持按设计进行字符串比较,因此无法通过折叠每个Node的邻居来生成这些问题的列表。
是否存在任何表示可以通过类型系统计算断开链接列表或排除断开链接的表述?
更新:我刚刚发现Naturals在Dhall中具有可比性。因此,我假设可以编写一个函数来识别任何无效边缘(“断开的链接”),并且如果标识符是Naturals,则可以重复使用标识符。
不过,关于是否可以定义Graph类型的最初问题仍然存在。
答案 0 :(得分:20)
是的,您可以在Dhall中为类型安全,有向,可能是循环的图形建模,如下所示:
let List/map =
https://prelude.dhall-lang.org/v14.0.0/List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680
let Graph
: Type
= forall (Graph : Type)
-> forall ( MakeGraph
: forall (Node : Type)
-> Node
-> (Node -> { id : Text, neighbors : List Node })
-> Graph
)
-> Graph
let MakeGraph
: forall (Node : Type)
-> Node
-> (Node -> { id : Text, neighbors : List Node })
-> Graph
= \(Node : Type)
-> \(current : Node)
-> \(step : Node -> { id : Text, neighbors : List Node })
-> \(Graph : Type)
-> \ ( MakeGraph
: forall (Node : Type)
-> Node
-> (Node -> { id : Text, neighbors : List Node })
-> Graph
)
-> MakeGraph Node current step
let -- Get `Text` label for the current node of a Graph
id
: Graph -> Text
= \(graph : Graph)
-> graph
Text
( \(Node : Type)
-> \(current : Node)
-> \(step : Node -> { id : Text, neighbors : List Node })
-> (step current).id
)
let -- Get all neighbors of the current node
neighbors
: Graph -> List Graph
= \(graph : Graph)
-> graph
(List Graph)
( \(Node : Type)
-> \(current : Node)
-> \(step : Node -> { id : Text, neighbors : List Node })
-> let neighborNodes
: List Node
= (step current).neighbors
let nodeToGraph
: Node -> Graph
= \(node : Node)
-> \(Graph : Type)
-> \ ( MakeGraph
: forall (Node : Type)
-> forall (current : Node)
-> forall ( step
: Node
-> { id : Text
, neighbors : List Node
}
)
-> Graph
)
-> MakeGraph Node node step
in List/map Node Graph nodeToGraph neighborNodes
)
let {- Example node type for a graph with three nodes
For your Wiki, replace this with a type with one alternative per document
-}
Node =
< Node0 | Node1 | Node2 >
let {- Example graph with the following nodes and edges between them:
Node0 ↔ Node1
↓
Node2
↺
The starting node is Node0
-}
example
: Graph
= let step =
\(node : Node)
-> merge
{ Node0 = { id = "0", neighbors = [ Node.Node1, Node.Node2 ] }
, Node1 = { id = "1", neighbors = [ Node.Node0 ] }
, Node2 = { id = "2", neighbors = [ Node.Node2 ] }
}
node
in MakeGraph Node Node.Node0 step
in assert : List/map Graph Text id (neighbors example) === [ "1", "2" ]
这种表示保证了没有断边。
我还将这个答案变成了可以使用的软件包:
编辑:以下是相关资源和其他说明,可帮助您阐明正在发生的事情:
首先,从以下tree的Haskell类型开始:
data Tree a = Node { id :: a, neighbors :: [ Tree a ] }
您可以将这种类型看作是一种惰性的,可能是无限的数据结构,表示如果您继续访问邻居会得到什么。
现在,让我们假设上述Tree
表示实际上是我们的Graph
,只需将数据类型重命名为Graph
:
data Graph a = Node { id :: a, neighbors :: [ Graph a ] }
...但是,即使我们要使用此类型,我们也无法直接在Dhall中对该类型进行建模,因为Dhall语言不提供对递归数据结构的内置支持。那我们该怎么办?
幸运的是,实际上有一种方法可以将递归数据结构和递归函数嵌入到非递归语言(如Dhall)中。实际上,有两种方法!
我读到的第一件事向我介绍了这个技巧,是Wadler撰写的以下草稿:
...但是我可以使用以下两种Haskell类型来总结基本思想:
{-# LANGUAGE RankNTypes #-}
-- LFix is short for "Least fixed point"
newtype LFix f = LFix (forall x . (f x -> x) -> x)
...和:
{-# LANGUAGE ExistentialQuantification #-}
-- GFix is short for "Greatest fixed point"
data GFix f = forall x . GFix x (x -> f x)
LFix
和GFix
的工作方式是,您可以给它们“所需的”递归或“ corecursive”类型的“一层”(即f
),然后他们给出可以提供与所需类型一样强大的功能,而无需语言支持递归或corecursion。
让我们以列表为例。我们可以使用以下ListF
类型对列表的“一层”进行建模:
-- `ListF` is short for "List functor"
data ListF a next = Nil | Cons a next
将该定义与我们使用普通的递归数据类型定义通常定义OrdinaryList
的方式进行比较:
data OrdinaryList a = Nil | Cons a (OrdinaryList a)
主要区别在于ListF
接受了一个额外的类型参数(next
),我们将其用作该类型的所有递归/合并递归事件的占位符。
现在,有了ListF
,我们可以像下面这样定义递归和核心递归列表:
type List a = LFix (ListF a)
type CoList a = GFix (ListF a)
...其中:
List
是在没有语言支持递归的情况下实现的递归列表CoList
是在没有语言支持corecursion的情况下实现的corecursive列表这两种类型均等同于{“ 1同构”“ []
,表示:
List
和[]
之间来回转换CoList
和[]
之间来回转换让我们通过定义这些转换函数来证明!
fromList :: List a -> [a]
fromList (LFix f) = f adapt
where
adapt (Cons a next) = a : next
adapt Nil = []
toList :: [a] -> List a
toList xs = LFix (\k -> foldr (\a x -> k (Cons a x)) (k Nil) xs)
fromCoList :: CoList a -> [a]
fromCoList (GFix start step) = loop start
where
loop state = case step state of
Nil -> []
Cons a state' -> a : loop state'
toCoList :: [a] -> CoList a
toCoList xs = GFix xs step
where
step [] = Nil
step (y : ys) = Cons y ys
因此,实现Dhall类型的第一步是转换递归Graph
类型:
data Graph a = Node { id :: a, neighbors :: [ Graph a ] }
...等同于等效的递归表示形式:
data GraphF a next = Node { id ::: a, neighbors :: [ next ] }
data GFix f = forall x . GFix x (x -> f x)
type Graph a = GFix (GraphF a)
...尽管我稍微简化了类型,但发现将GFix
专门化为f = GraphF
的情况比较容易:
data GraphF a next = Node { id ::: a, neighbors :: [ next ] }
data Graph a = forall x . Graph x (x -> GraphF a x)
Haskell没有像Dhall这样的匿名记录,但是如果这样做,我们可以通过内联GraphF
的定义来进一步简化类型:
data Graph a = forall x . MakeGraph x (x -> { id :: a, neighbors :: [ x ] })
现在,这开始看起来像Graph
的Dhall类型,尤其是当我们将x
替换为node
时:
data Graph a = forall node . MakeGraph node (node -> { id :: a, neighbors :: [ node ] })
但是,最后还有一个棘手的部分,就是如何将ExistentialQuantification
从Haskell转换为Dhall。事实证明,您始终可以使用以下等效性将存在性量化转换为通用量化(即forall
):
exists y . f y ≅ forall x . (forall y . f y -> x) -> x
我相信这叫做“死刑”
有关更多详细信息,请参见:
...,最后一个技巧为您提供了Dhall类型:
let Graph
: Type
= forall (Graph : Type)
-> forall ( MakeGraph
: forall (Node : Type)
-> Node
-> (Node -> { id : Text, neighbors : List Node })
-> Graph
)
-> Graph
...,其中forall (Graph : Type)
与上一个公式中的forall x
扮演相同的角色,而forall (Node : Type)
与上一个公式中的forall y
扮演相同的角色。