Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycle in type synonym declarations

Tags:

types

haskell

I want to try out Haskell type and data with the following code

type Program a = Maybe a -> (Operation a, Maybe Program a)
data Operation a = Read a | Write a

Program follows a continuation passing style (CPS) and Nothing indicates termination. a may be instantiated with Int or something else.

However, GHC complains that:

main.hs:1:1:
    Cycle in type synonym declarations:
      main.hs:1:1-58: type Program a =
                          Maybe a -> (Operation a, Maybe Program a)

I'm new to Haskell so I don't understand why this is not allowed. How can I express this type in Haskell?

like image 422
Xiao Jia Avatar asked Oct 25 '25 05:10

Xiao Jia


1 Answers

There are two problems here:

  • it should be Maybe (Program a), not Maybe Program a
  • type synonyms cannot be recursive

A working version would be:

newtype Program a = Program { runProgram :: Maybe a -> (Operation a, Maybe (Program a)) }
data Operation a = Read a | Write a
like image 126
Ptharien's Flame Avatar answered Oct 26 '25 23:10

Ptharien's Flame