简体   繁体   中英

How to Make `Var` Instance of `Free` Data Type?

While reading Typeclassopedia 's chapter on Monads, I saw:

data Free f a = Var a
               | Node (f (Free f a))

I typed :i Free to get information on it.

ghci> :i Free
type role Free nominal nominal
data Free (f :: * -> *) a = Var a | Node (f (Free f a))

I've seen types with kind 's of * -> * :

ghci> :k []
[] :: * -> *
ghci> :k Maybe
Maybe :: * -> *

But, I'm not sure how to make a simple Var instance with an argument of * -> * .

How can I do that?

The way you create a Var is just Var value , like Var "x" for example. Since f does not actually appear in the definition of Var , your choice of f does not actually affect how you create a Var - it only matters when creating Nodes.

So when creating a Node with f being [] , you'd write Node [Var "x"] and for Maybe you'd write Node (Some (Var "x")) . As you see the use of Var stays the same in both cases, you just pass a different "collection" to the Node constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM