How do you represent nested types using the Scott Encoding?

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

An ADT can be represented using the Scott Encoding by replacing products by tuples and sums by matchers. For example:

data List a = Cons a (List a) | Nil

Can be encoded using the Scott Encoding as:

cons =  h t c n . c h t) nil  =  c n . n)

But I couldn't find how nested types can be encoded using SE:

data Tree a = Node (List (Tree a)) | Leaf a

How can it be done?

回答1:

If the Wikipedia article is correct, then

data Tree a = Node (List (Tree a)) | Leaf a

has Scott encoding

node = λ a . λ node leaf . node a leaf = λ a . λ node leaf . leaf a

It looks like the Scott encoding is indifferent to (nested) types. All it's concerned with is delivering the correct number of parameters to the constructors.



回答2:

Scott encodings are basically representing a T by the type of its case expression. So for lists, we would define a case expression like so:

listCase :: List a -> r -> (a -> List a -> r) -> r listCase []     n c = n listCase (x:xs) n c = c x xs

this gives us an analogy like so:

case xs of { [] -> n ; (x:xs) -> c } = listCase xs n (\x xs -> c)

This gives a type

newtype List a = List { listCase :: r -> (a -> List a -> r) -> r }

The constructors are just the values that pick the appropriate branches:

nil :: List a nil = List $ \n c -> n  cons :: a -> List a -> List a cons x xs = List $ \n c -> c x xs

We can work backwards then, from a boring case expression, to the case function, to the type, for your trees:

case t of { Leaf x -> l ; Node xs -> n }

which should be roughly like

treeCase t (\x -> l) (\xs -> n)

So we get

treeCase :: Tree a -> (a -> r) -> (List (Tree a) -> r) -> r treeCase (Leaf x)  l n = l x treeCase (Node xs) l n = n xs  newtype Tree a = Tree { treeCase :: (a -> r) -> (List (Tree a) -> r) -> r }  leaf :: a -> Tree a leaf x = Tree $ \l n -> l x  node :: List (Tree a) -> Tree a node xs = Tree $ \l n -> n xs

Scott encodings are very easy tho, because they're only case. Church encodings are folds, which are notoriously hard for nested types.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!