I am currently working on implementing a simple parser combinator library in Idris to learn the language and better understand type systems in general, but am having some trouble wrapping my head around how GADTs are declared and used. The parser data type that I am trying to formulate looks like (in Haskell): type Parser a = String -> [(a,String)]
, from this paper. Basically, the type is a function that takes a string and returns a list.
In Idris I have:
data Parser : Type -> Type where
fail : a -> (String -> [])
pass : a -> (String -> [(a,String)])
where the fail
instance is a parser that always fails (i.e.- will be a function that always returns the empty list) and the pass
instance is a parser that consumed some symbol. When loading the above into the interpreter, I get an error that there is a type mismatch between List elem
and the expected type Type
. But when I check the returned type of the parser in the repl with :t String -> List Type
I get Type
, which looks like it should work.
I would be very grateful if anyone could give a good explanation of why this data declaration does not work, or a better alternative to representing the parser data type.