Home > Haskell Undefined > Haskell Undefined Data Constructor Error

Haskell Undefined Data Constructor Error

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Undefined data constructor “?” in Hugs intepreter up vote 0 down vote favorite Strange error when attempting to load file into hugs: Hugs> :l test.hs ERROR "test.hs":3 - Undefined data constructor "?" Full content of file (yes, one line): test :: [a] -> ([a],[a]) Can anyone enlighten me? Note: Attempting to load the same file in ghci doesn't work either, but for reasons that make sense: Prelude> :r [1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:1:1: The type signature for ‘test’ lacks an accompanying binding Failed, modules loaded: none. haskell hugs share|improve this question edited Oct 27 '15 at 19:10 AJFarmar 4,04111236 asked Oct 27 '15 at 18:31 abc32112 77931736 5 The hugs error is weird. ghci makes sense, you have only written a type signature for test, it needs a definition also, for example test xs = (xs, xs) (which is probably not what you were going for, but it will typecheck at least) –luqui Oct 27 '15 at 18:41 4 Hugs hasn't been maintained since 2006 or so. Very few people use it any more, and you probably shouldn't either. –dfeuer Oct 27 '15 at 20:03 2 I'm going to make a totally unsubstantiated guess that your source code file starts with a Unicode byte-order-mark, which Hugs has no ide

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Issues Calling data Constructor in Haskell up vote 1 down vote favorite I am attempting to write my own polymorphic list type, List a, to act as the default library list. I believe that the code is correct, however I cannot seem to call it successfully in Hugs98. What would be the correct way to call the http://stackoverflow.com/questions/33375873/undefined-data-constructor-in-hugs-intepreter function and count the length of a non empty list, using the type List a? My Haskell Code is: data List a = Nil | Cons a (List a) len :: List a -> Int len Nil = 0 len (Cons _ xs) = 1 + len xs Thanks in advance! haskell constructor functional-programming declarative hugs share|improve this question asked Nov 10 '12 at 14:09 user1814580 61 5 len (1 `Cons` 2 `Cons` 3 `Cons` Nil) for example, if you put an infixr 8 `Cons` declaration in the file. What did http://stackoverflow.com/questions/13322752/issues-calling-data-constructor-in-haskell you try, and how did hugs complain about that? –Daniel Fischer Nov 10 '12 at 14:13 The [a] type, [] and : , the associated [item1,item2,item3] bracket-and-comma syntax, and all the [1..5] style ellipsis forms are built into the language itself. You can't simply replace Haskell's implementation of lists with your own. –NovaDenizen Nov 11 '12 at 14:43 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote I'm assuming you've saved that code in a file called Lists.hs Here's how you can call your len function in Hugs. Main> :l Lists Main> len Nil 0 Main> len (Cons 1 Nil) 1 Main> len (Cons 'a' Nil) 1 Main> len (Cons 'a' (Cons 'b' Nil)) 2 Main> len (Cons 'a' (Cons 'b' (Cons 'c' Nil))) 3 The brackets are a bit ugly, though. Here's a way of making it nicer: infixr 5 :. data List a = Nil | a :. (List a) deriving Show The infixr line tells Hugs that the constructor :. should associate to the right, so that there are implicit brackets to the right, this means that 'a' :. 'b' :. Nil = 'a' :. ('b' :. Nil) If you don't put this, Hugs will assume that :. associates to the left, so it would think 'a' :. 'b' :. Nil = ('a' :. 'b') :. Nil Which doesn't make sense - you'd get Main> 'a' :. 'b' :. Nil ERROR - Type error in application *** Expression : 'a' :. 'b' *** Term : 'b' *** Type : Char *** Does not match : List a or more confusingly, if it's numbers,

identical to an existing one but which has a separate identity in the type system. In Haskell, the newtype declaration creates a new type from an existing one. For example, natural https://www.haskell.org/tutorial/moretypes.html numbers can be represented by the type Integer using the following declaration: newtypeNatural=MakeNaturalInteger https://www.cs.kent.ac.uk/people/staff/sjt/craft2e/errors/allErrors.html This creates an entirely new type, Natural, whose only constructor contains a single Integer. The constructor MakeNatural converts between an Natural and an Integer: toNatural::Integer->Natural toNaturalx|x<0=error"Can'tcreatenegativenaturals!" |otherwise=MakeNaturalx fromNatural::Natural->Integer fromNatural(MakeNaturali)=i The following instance declaration admits Natural to the Num class: instanceNumNaturalwhere fromInteger=toNatural x+y=toNatural(fromNaturalx+fromNaturaly) x-y=letr=fromNaturalx-fromNaturalyin ifr<0thenerror"Unnaturalsubtraction" elsetoNaturalr x*y=toNatural(fromNaturalx*fromNaturaly) Without this declaration, Natural would not be in haskell undefined Num. Instances declared for the old type do not carry over to the new one. Indeed, the whole purpose of this type is to introduce a different Num instance. This would not be possible if Natural were defined as a type synonym of Integer. All of this works using a data declaration instead of a newtype declaration. However, the data declaration incurs extra overhead in the representation of haskell undefined data Natural values. The use of newtype avoids the extra level of indirection (caused by laziness) that the data declaration would introduce. See section 4.2.3 of the report for a more discussion of the relation between newtype, data, and type declarations. [Except for the keyword, the newtype declaration uses the same syntax as a data declaration with a single constructor containing a single field. This is appropriate since types defined using newtype are nearly identical to those created by an ordinary data declaration.] 6.2Field Labels The fields within a Haskell data type can be accessed either positionally or by name using field labels. Consider a data type for a two-dimensional point: dataPoint=PtFloatFloat The two components of a Point are the first and second arguments to the constructor Pt. A function such as pointx::Point->Float pointx(Ptx_)=x may be used to refer to the first component of a point in a more descriptive way, but, for large structures, it becomes tedious to create such functions by hand. Constructors in a data declaration may be declared with associated field names, enclosed in braces. These field names identify the components of constructor by name rather than by position. This is an alternative way to define Point: dataPoint=Pt{pointx,pointy::Float}

you have provoked can hopefully help you to diagnose your particular problem. This page shows some code fragments, in red, followed by the error message they provoke, in blue, and an explanation of the source of the problem. You can search for particular errors by giving keywords. Please mail me with any examples which you would like to be included in the list Thanks to Daniel Barie, Eerke Boiten, Jane Chan, Rodger Hayes, Brian Herlihy, Ralf Hinze, Stefan Kahrs, Tom Kaiser, Richard O'Keefe and the students on the functional programming courses at the University of Kent for contributing many of these errors. 3 'div' 4 ERROR: Improperly terminated character constant The problem here is the use of the wrong sort of quotes. To turn a function, which is written before its arguments, into an operator, which is written between its arguments, you need to enclose it in backquotes. The backquote is round on the same key as the tilde on US keyboards (commonly the top left hand corner of the main block, just above TAB). data BTree a = EmptyBTree | Node a (BTree a) (BTree a) card :: BTree a -> Integer card EmptyBTree = 0 card (Node x) lt rt = (height lt) + (height rt) + 1 ERROR: Equations give different arities for "card" Incorrect bracketing causes the problem here. It looks as though card is a one argument function (arity 1) from the first equation in the definition, and as though it's a three argument function from the second. The misplaced closing bracket is the culprit, and a correct version of the code says card (Node x lt rt) = (height lt) + (height rt) + 1 >maxFour :: Int -> Int -> Int -> Int -> Int >maxFour >| a >= b && a >= c && a >= d = a >| b >= c && b >= d= b >| c >= d = c >| otherwise = d ERROR "test.lhs" (line 3): Undefined variable "a" The definition of maxFour has its variables missing. The second line should read maxFour a b c d. >exOr :: bool -> bool -> bool >exOr b1 b2 >= (b1 && not b2) || (b2 && not b1) ERROR "test.lhs" (line 3): Declared type too general *** Expression: exOr *** Declared type : a -> a -> a *** Inferred type : Bool -> Bool -> Bool The problem here is with the bools in the type. The Boolean type is written Bool and so the first line should read exOr :: Bool -> Bool -> Bool What does the error message mean? The bool is taken to be a type variable, and so the declaration asserts that the function has a polymorphic type (which of course it doesn't). >ex = (2==3)) ERROR "test.lhs" (line 1): Syntax error in input (unexpected `)') The presence of an extra closing parenthesis is signalled as an unexpected piece of i

 
No related pages.