Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell. TagSoup library with OverloadedStrings

Tags:

haskell

Good day, question is rather noobish, but i stuck with it.

I decided to migrate from plain Strings to Text in my project and faced the problem, all strings in source yielded compilation errors after adding {-# LANGUAGE OverloadedStrings #-}, for example snippet like:

dropWhile (~/= "<li>") tags

now leads to

Ambiguous type variable t' in the constraints: Data.String.IsString t' arising from the literal "<li>"' at ParserOx.hs:93:42-47 TagRep t' arising from a use of `~=='

What could be wrong here ?

UPD:

And yes, all my functions have signatures, ex:

getContainer :: [Tag Text] -> [Tag Text]
getContainer tags = 
  h
    where 
      (h:t) = sections (~== "<div id=\"itemscontainer\">") tags
like image 294
Dfr Avatar asked Sep 08 '11 17:09

Dfr


2 Answers

The problem is that you have an ambiguous type with two constraints -- the isstring constraint given by overloaded strings, and the tagrep constraint used by tagsoup to allow you to use tags or strings interchangeably. So two methods of "overloading" strings (one in general, and one just for use in tagsoup's matchers) are running into one another and causing confusion. Either turn off overloaded strings in the offending file, or specify your strings as actual strings in the code (i.e. (~/= ("<li>"::String))). Rather than inline type signatures, you can do the following to force types more quietly:

s :: String -> String
s = id
.... (~/= s "<li>") ...
like image 142
sclv Avatar answered Oct 03 '22 10:10

sclv


One option is to define a wrapper around ~== which pins down parts of the type. For example, you could define:

(~===) a b = a ~== (b :: String)

Then you can just write (~=== "<div id=\"itemscontainer\">") with no further annotations at the use site.

like image 45
Neil Mitchell Avatar answered Oct 03 '22 09:10

Neil Mitchell