Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Non type-variable argument in the constraint error, while using with infix

Unfortunately I have been facing a strange error. It happens while using infix with data constructor.
I am new to Haskell. Can any one help me in this regard ?

Prelude> data L a = Cons a (L a) | Emp deriving Show
Prelude> 10 `Cons` Emp
Cons 10 Emp
Prelude> 10 `Cons` 10 `Cons` Emp

<interactive>:43:1: error:
    • Non type-variable argument in the constraint: Num (L a)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. (Num a, Num (L a)) => L (L a)
Prelude> 10 `Cons` (10 `Cons` Emp) 
Cons 10 (Cons 10 Emp)
Prelude> 10 `Cons` 10 `Cons` Emp

<interactive>:45:1: error:
    • Non type-variable argument in the constraint: Num (L a)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. (Num a, Num (L a)) => L (L a)
Prelude> data L a = Emp | Cons a (L a)  deriving Show
Prelude> 10 `Cons` 10 `Cons` Emp

<interactive>:47:1: error:
    • Non type-variable argument in the constraint: Num (L a)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. (Num a, Num (L a)) => L (L a)
Prelude> 

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can define the data constructor to be right-associative,

infixr 5 `Cons`

data L a = Cons a (L a) | Emp deriving Show

Then it will work as you expected.

main = print $ (21::Int) `Cons` 42 `Cons` Emp

-- => infixr 5 `Cons`
-- Cons 21 (Cons 42 Emp)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading