[Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread AUGER Cédric

Hi all, I am an Haskell newbie; can someone explain me why there is
no reported error in @legSome@ but there is one in @legSomeb@

(I used leksah as an IDE, and my compiler is:
$ ghc -v
Glasgow Haskell Compiler, Version 7.2.1, stage 2 booted by GHC version
6.12.3 )

What I do not understand is that the only difference was a typing
anotation to help the type inference, but I believed that this
annotation was already given by the signature I give, so I am quite
lost.

Thanks in advance!

==
{-# OPTIONS_GHC -XScopedTypeVariables #-}
-- why isn't this option always enabled...

{-# OPTIONS_GHC -XGADTs #-}

import Data.Word
import qualified Data.Map as M
import qualified Data.Set as S

data Symbols nt t = NT nt -- ^ non terminal
  | T t  -- ^ terminal
 deriving (Eq, Ord)

type Sem s = [s]-s

data Rule nt t s = Rule { refined :: nt
, expression :: [Symbols nt t]
, emit :: Sem s
}

type RRule nt t s = ([Symbols nt t], Sem s)
data LegGram nt t s = Ord nt = LegGram (M.Map nt [RRule nt t s])

legSome :: LegGram nt t s - nt - Either String ([t], s)
-- ^^
--isn't this redundant?
--vv
legSome ((LegGram g)::LegGram nt t s) ntV =
   case M.lookup ntV g of
 Nothing - Left No word accepted!
 Just l - let sg = legSome (LegGram (M.delete ntV g))
   subsome :: [RRule nt t s] - Either String ([t], s)
   subsome [] = Left No word accepted!
   subsome ((r,sem):l) =
 let makeWord [] = Right ([],[])
 makeWord ((NT nnt):ll) =
   do (m, ss) - sg nnt
  (mm, sss) - makeWord ll
  return (m++mm, ss:sss)
 makeWord ((T tt):ll) =
   do (mm, sss) - makeWord ll
  return (tt:mm, sss)
  in
case makeWord r of
  Right (ll, mm) - Right (ll, sem mm)
  Left err - subsome l
   in subsome l

legSomeb :: LegGram nt t s - nt - Either String ([t], s) 
-- but without it I have an error reported
legSomeb (LegGram g) ntV =
   case M.lookup ntV g of
 Nothing - Left No word accepted!
 Just l - let sg = legSomeb (LegGram (M.delete ntV g))
   subsome :: [RRule nt t s] - Either String ([t], s)
   subsome [] = Left No word accepted!
   subsome ((r,sem):l) =
 let makeWord [] = Right ([],[])
 makeWord ((NT nnt):ll) =
   do (m, ss) - sg nnt
  (mm, sss) - makeWord ll
  return (m++mm, ss:sss)
 makeWord ((T tt):ll) =
   do (mm, sss) - makeWord ll
  return (tt:mm, sss)
  in
case makeWord r of
  Right (ll, mm) - Right (ll, sem mm)
  Left err - subsome l
   in subsome l

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yves Parès
Perhaps you should give us the error the compiler give you.

Plus:
data LegGram nt t s = Ord nt = LegGram (M.Map nt [RRule nt t s])
will become invalid. Currently, such class constraints are ignored.

You should remove the 'Ord nt' constraint and add it to you legSome
function. (Maybe that's a track to solve your problem...)


You have also another solution: make your LegGram type available *for
all*Ord nt (with GADTs or ExistentialQuantification), thus making you
unable to
know which type 'nt' exactly is:

data LegGram t s = forall nt. Ord nt = LegGram (M.Map nt [RRule nt t s])
or
data LegGram t s where
LegGram :: Ord nt = M.Map nt [RRule nt t s] - LegGram t s
should be both valid. I tend to prefer the latter (the use of a GADT), as
it makes you declare and handle your type constructor just like any
function.
But I don't know if it fits you requirements.


2012/1/3 AUGER Cédric sedri...@gmail.com


 Hi all, I am an Haskell newbie; can someone explain me why there is
 no reported error in @legSome@ but there is one in @legSomeb@

 (I used leksah as an IDE, and my compiler is:
 $ ghc -v
 Glasgow Haskell Compiler, Version 7.2.1, stage 2 booted by GHC version
 6.12.3 )

 What I do not understand is that the only difference was a typing
 anotation to help the type inference, but I believed that this
 annotation was already given by the signature I give, so I am quite
 lost.

 Thanks in advance!

 ==
 {-# OPTIONS_GHC -XScopedTypeVariables #-}
 -- why isn't this option always enabled...

 {-# OPTIONS_GHC -XGADTs #-}

 import Data.Word
 import qualified Data.Map as M
 import qualified Data.Set as S

 data Symbols nt t = NT nt -- ^ non terminal
  | T t  -- ^ terminal
  deriving (Eq, Ord)

 type Sem s = [s]-s

 data Rule nt t s = Rule { refined :: nt
, expression :: [Symbols nt t]
, emit :: Sem s
}

 type RRule nt t s = ([Symbols nt t], Sem s)
 data LegGram nt t s = Ord nt = LegGram (M.Map nt [RRule nt t s])

 legSome :: LegGram nt t s - nt - Either String ([t], s)
 -- ^^
 --isn't this redundant?
 --vv
 legSome ((LegGram g)::LegGram nt t s) ntV =
   case M.lookup ntV g of
 Nothing - Left No word accepted!
 Just l - let sg = legSome (LegGram (M.delete ntV g))
   subsome :: [RRule nt t s] - Either String ([t], s)
   subsome [] = Left No word accepted!
   subsome ((r,sem):l) =
 let makeWord [] = Right ([],[])
 makeWord ((NT nnt):ll) =
   do (m, ss) - sg nnt
  (mm, sss) - makeWord ll
  return (m++mm, ss:sss)
 makeWord ((T tt):ll) =
   do (mm, sss) - makeWord ll
  return (tt:mm, sss)
  in
case makeWord r of
  Right (ll, mm) - Right (ll, sem mm)
  Left err - subsome l
   in subsome l

 legSomeb :: LegGram nt t s - nt - Either String ([t], s)
 -- but without it I have an error reported
 legSomeb (LegGram g) ntV =
   case M.lookup ntV g of
 Nothing - Left No word accepted!
 Just l - let sg = legSomeb (LegGram (M.delete ntV g))
   subsome :: [RRule nt t s] - Either String ([t], s)
   subsome [] = Left No word accepted!
   subsome ((r,sem):l) =
 let makeWord [] = Right ([],[])
 makeWord ((NT nnt):ll) =
   do (m, ss) - sg nnt
  (mm, sss) - makeWord ll
  return (m++mm, ss:sss)
 makeWord ((T tt):ll) =
   do (mm, sss) - makeWord ll
  return (tt:mm, sss)
  in
case makeWord r of
  Right (ll, mm) - Right (ll, sem mm)
  Left err - subsome l
   in subsome l

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Tue, Jan 3, 2012 at 5:43 PM, AUGER Cédric sedri...@gmail.com wrote:
 legSomeb :: LegGram nt t s - nt - Either String ([t], s)
 -- but without it I have an error reported
 legSomeb (LegGram g) ntV =
   case M.lookup ntV g of
     Nothing - Left No word accepted!
     Just l - let sg = legSomeb (LegGram (M.delete ntV g))
                   subsome :: [RRule nt t s] - Either String ([t], s)
                   subsome [] = Left No word accepted!
                   subsome ((r,sem):l) =
                     let makeWord [] = Right ([],[])
                         makeWord ((NT nnt):ll) =
                           do (m, ss) - sg nnt
                              (mm, sss) - makeWord ll
                              return (m++mm, ss:sss)
                         makeWord ((T tt):ll) =
                           do (mm, sss) - makeWord ll
                              return (tt:mm, sss)
                      in
                    case makeWord r of
                      Right (ll, mm) - Right (ll, sem mm)
                      Left err - subsome l
               in subsome l

I found it compiling well if removing this line:

subsome :: [RRule nt t s] - Either String ([t], s)

It seems to me that the compiler is not sure the two 'nt' are equal.
The ScopedTypeVariables can make the compiler believe they are equal.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Brandon Allbery
On Tue, Jan 3, 2012 at 05:17, Yucheng Zhang yczhan...@gmail.com wrote:

 subsome :: [RRule nt t s] - Either String ([t], s)

 It seems to me that the compiler is not sure the two 'nt' are equal.
 The ScopedTypeVariables can make the compiler believe they are equal.


But ScopedTypeVariables is enabled already.

OTOH, I *think* the embedded Ord constraint is screwing things up (which is
why it's showing up in the error message), because it's embedded and
therefore has its own limited scope which prevents ScopedTypeVariables from
doing the right thing.  This idiosyncrasy is why those embedded constraints
are being removed from the language except in more controllable and useful
ways (namely GADTs; the H98 version is not capable of doing anything
useful, as I understand it, it can only screw up typing in situations like
this without actually adding anything useful to the type system, as it's
not possible to bring the Ord constraint usefully into scope outside the
data declaration itself).

(More technically, and I think this is correct:  the effective data type is

(forall nt. Ord nt = LegGram (M.Map nt [RRule nt t s]))

Note the outer parentheses; the Ord constraint is *only* active within
those outer parentheses, meaning that it prevents the type of nt from
matching that in any other use of the type, even in the same expression,
even with ScopedTypeVariables.)

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Tue, Jan 3, 2012 at 6:44 PM, Brandon Allbery allber...@gmail.com wrote:
 On Tue, Jan 3, 2012 at 05:17, Yucheng Zhang yczhan...@gmail.com wrote:

 subsome :: [RRule nt t s] - Either String ([t], s)

 It seems to me that the compiler is not sure the two 'nt' are equal.
 The ScopedTypeVariables can make the compiler believe they are equal.


 But ScopedTypeVariables is enabled already.


Sorry, I meant actually using ScopedTypeVariables as in the first function,
which compiles well:

legSome :: LegGram nt t s - nt - Either String ([t], s)
-- ^^
--isn't this redundant?
--vv
legSome ((LegGram g)::LegGram nt t s) ntV =
  case M.lookup ntV g of
Nothing - Left No word accepted!
Just l - let sg = legSome (LegGram (M.delete ntV g))
  subsome :: [RRule nt t s] - Either String ([t], s)
  subsome [] = Left No word accepted!
  subsome ((r,sem):l) =
let makeWord [] = Right ([],[])
makeWord ((NT nnt):ll) =
  do (m, ss) - sg nnt
 (mm, sss) - makeWord ll
 return (m++mm, ss:sss)
makeWord ((T tt):ll) =
  do (mm, sss) - makeWord ll
 return (tt:mm, sss)
 in
   case makeWord r of
 Right (ll, mm) - Right (ll, sem mm)
 Left err - subsome l
  in subsome l

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Brandon Allbery
On Tue, Jan 3, 2012 at 05:50, Yucheng Zhang yczhan...@gmail.com wrote:

 On Tue, Jan 3, 2012 at 6:44 PM, Brandon Allbery allber...@gmail.com
 wrote:
  On Tue, Jan 3, 2012 at 05:17, Yucheng Zhang yczhan...@gmail.com wrote:
 
  subsome :: [RRule nt t s] - Either String ([t], s)
 
  It seems to me that the compiler is not sure the two 'nt' are equal.
  The ScopedTypeVariables can make the compiler believe they are equal.
 
  But ScopedTypeVariables is enabled already.

 Sorry, I meant actually using ScopedTypeVariables as in the first function,
 which compiles well:

 legSome :: LegGram nt t s - nt - Either String ([t], s)


Except he's not; an explicit forall is needed to indicate the type
variables to be brought into scope.  It would need to be

 legSome :: forall nt t s. LegGram nt t s - nt - Either String ([t], s)

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
As I understand it, both ways work.

 legSome ((LegGram g)::LegGram nt t s) ntV

If you compile this without ScopedTypeVariables extension, GHC will
remind you of it:

Illegal signature in pattern: LegGram nt t s
Use -XScopedTypeVariables to permit it

So another solution is to avoid the ugly redundancy is:

 legSome :: forall nt t s . LegGram nt t s - nt - Either String ([t], s)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yves Parès
That is error-prone.
Plus the code does not need ScopedTypeVariables. The real problem comes
from the use of a class constraint on the LegGram data constructor.

data LegGram nt t s = Ord nt = LegGram (M.Map nt [RRule nt t s])

Short answer: you *can't *add class constraints to an already declared type
variable:

The LegGram *type *constructor declares the 'nt' variable (and then brings
it into scope), so trying afterwards to add a constraint to it for the
LegGram *data *constructor is invalid, so the compiler understands this:
data LegGram nt t s = *forall nt.* Ord nt = LegGram (M.Map *nt*
[RRule *nt*t s])
...the declaration and scoping of a *new* type variable.

This is it, right?

2012/1/3 Yucheng Zhang yczhan...@gmail.com

 As I understand it, both ways work.

  legSome ((LegGram g)::LegGram nt t s) ntV

 If you compile this without ScopedTypeVariables extension, GHC will
 remind you of it:

 Illegal signature in pattern: LegGram nt t s
 Use -XScopedTypeVariables to permit it

 So another solution is to avoid the ugly redundancy is:

  legSome :: forall nt t s . LegGram nt t s - nt - Either String ([t], s)

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Brandon Allbery
On Tue, Jan 3, 2012 at 06:35, Yucheng Zhang yczhan...@gmail.com wrote:

  legSome ((LegGram g)::LegGram nt t s) ntV

 If you compile this without ScopedTypeVariables extension, GHC will
 remind you of it:

 Illegal signature in pattern: LegGram nt t s
 Use -XScopedTypeVariables to permit it


Right, but I think this is conflating two aspects of ScopedTypeVariables
and may not bring them into scope fully.  Although, that's a question for
someone who understands ghc's type system far better than I do.

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Tue, Jan 3, 2012 at 7:46 PM, Brandon Allbery allber...@gmail.com wrote:
 Right, but I think this is conflating two aspects of ScopedTypeVariables and
 may not bring them into scope fully.  Although, that's a question for
 someone who understands ghc's type system far better than I do.


I found some descriptions of ScopedTypeVariables here:

http://hackage.haskell.org/trac/haskell-prime/wiki/ScopedTypeVariables

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Brandon Allbery
On Tue, Jan 3, 2012 at 06:43, Yves Parès limestr...@gmail.com wrote:

 That is error-prone.
 Plus the code does not need ScopedTypeVariables. The real problem comes
 from the use of a class constraint on the LegGram data constructor.

 data LegGram nt t s = Ord nt = LegGram (M.Map nt [RRule nt t s])

 Short answer: you *can't *add class constraints to an already declared
 type variable:

 The LegGram *type *constructor declares the 'nt' variable (and then
 brings it into scope), so trying afterwards to add a constraint to it for
 the LegGram *data *constructor is invalid, so the compiler understands
 this:
 data LegGram nt t s = *forall nt.* Ord nt = LegGram (M.Map *nt* [RRule *
 nt* t s])
 ...the declaration and scoping of a *new* type variable.

 This is it, right?


Yep, with the additional gotcha that two uses of the same data constructor
in an expression will not unify those type variables because it's scoped to
the data constructor itself.  Which is the confusing and error-prone
behavior that led to it being removed from the next version of the standard
(probably; the proposal was accepted but H'2011 was bypassed and H'2012 has
not yet started).

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Tue, Jan 3, 2012 at 7:49 PM, Yucheng Zhang yczhan...@gmail.com wrote:
 I found some descriptions of ScopedTypeVariables here:

 http://hackage.haskell.org/trac/haskell-prime/wiki/ScopedTypeVariables

Sorry, I found just now a more up-to-date description in the GHC documentation:

http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread AUGER Cédric
Thanks all,

I finally give up to put Ord in the LegGram type.

What was annoying me was that @legsome@ was in fact an instance of a
class I defined. So I changed its signature to make it depend on Ord.

That is not very nice, since at first glance, there could be
implementations which does not require nt to be ordered.

But as in some other method of this class I need it, I guess it isn't
that annoying.

I still do find the reported error strange, but thanks to your remarks,
I can remove some typing annotation in the body of the function (which
I forgot to remove and used while I was debugging my types) to avoid
this ugly redundancy (I guess I could also remove the global type
signature, but I like to keep them).

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Brent Yorgey
The other much simpler solution no one has mentioned yet is to just
pull 'subsome' out as its own top-level declaration.  Having such a
big function nested locally within a 'let' is ugly anyway, and it
makes it harder to test and debug than necessary.

-Brent

On Tue, Jan 03, 2012 at 05:44:01PM +0100, Yves Parès wrote:
 Remove subsome type signature. You are redeclaring type variables which
 obviously cannot match those of legSome.
 This cannot work without scoped type variables (and ad-hoc foralls to bring
 them to scope, of course).
 
 2012/1/3 Yucheng Zhang yczhan...@gmail.com
 
  As I investigated the code more carefully, I found that the type
  unification
  failure may not be related to the suspected class constraint on data
  constructor.
 
  I have made minor changes to the original code to remove the Ord
  constraint,
  including introducing a FakedMap with no requirement on Ord. The type
  unification
  failure continues:
 
  Couldn't match type `nt1' with `nt'
`nt1' is a rigid type variable bound by
  the type signature for
subsome :: [RRule nt1 t1 s1] - Either String ([t1], s1)
  at xx.hs:34:19
`nt' is a rigid type variable bound by
 the type signature for
   legSome :: LegGram nt t s - nt - Either String ([t], s)
 at xx.hs:29:1
  Expected type: [Symbols nt1 t1]
Actual type: [Symbols nt t]
  In the first argument of `makeWord', namely `r'
  In the expression: makeWord r
 
  The complete changed code follows:
 
 
  data Symbols nt t = NT nt -- ^ non terminal
   | T t  -- ^ terminal
   deriving (Eq, Ord)
 
  type Sem s = [s]-s
 
  data Rule nt t s = Rule { refined :: nt
, expression :: [Symbols nt t]
, emit :: Sem s
}
 
  type RRule nt t s = ([Symbols nt t], Sem s)
 
 
 
  data FakedMap a b = FakedMap
 
  delete :: k - FakedMap k a - FakedMap k a
  delete a b = b
 
  lookup :: k - FakedMap k a - Maybe a
  lookup a b = Nothing
 
 
 
  data LegGram nt t s = LegGram (FakedMap nt [RRule nt t s])
 
  legSome :: LegGram nt t s - nt - Either String ([t], s)
  legSome (LegGram g) ntV =
   case Main.lookup ntV g of
  Nothing - Left No word accepted!
  Just l - let sg = legSome (LegGram (Main.delete ntV g))
subsome :: [RRule nt t s] - Either String ([t], s)
   subsome [] = Left No word accepted!
   subsome ((r,sem):l) =
 let makeWord [] = Right ([],[])
 makeWord ((NT nnt):ll) =
   do (m, ss) - sg nnt
  (mm, sss) - makeWord ll
  return (m++mm, ss:sss)
 makeWord ((T tt):ll) =
   do (mm, sss) - makeWord ll
  return (tt:mm, sss)
  in
case makeWord r of
  Right (ll, mm) - Right (ll, sem mm)
  Left err - subsome l
   in subsome l
 
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Wed, Jan 4, 2012 at 12:44 AM, Yves Parès limestr...@gmail.com wrote:
 Remove subsome type signature. You are redeclaring type variables which
 obviously cannot match those of legSome.
 This cannot work without scoped type variables (and ad-hoc foralls to bring
 them to scope, of course).

That subsome type signature is from the original code.

I wonder why the redeclared type variables cannot match those of legSome?




p.s. I just realized that my changed code missed the line:

 import qualified Data.Map as M

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yves Parès
Brent is right. Separating functions is nicer to read and cleaner. Plus it
enhances testability.

 I wonder why the redeclared type variables cannot match those of legSome?
Try to put a totally wrong type to subsome, like
subsome :: Int
and tell us from the error what type is actually inferred.

2012/1/3 Yucheng Zhang yczhan...@gmail.com

 On Wed, Jan 4, 2012 at 12:44 AM, Yves Parès limestr...@gmail.com wrote:
  Remove subsome type signature. You are redeclaring type variables which
  obviously cannot match those of legSome.
  This cannot work without scoped type variables (and ad-hoc foralls to
 bring
  them to scope, of course).

 That subsome type signature is from the original code.

 I wonder why the redeclared type variables cannot match those of legSome?




 p.s. I just realized that my changed code missed the line:

  import qualified Data.Map as M

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Wed, Jan 4, 2012 at 1:07 AM, Yves Parès limestr...@gmail.com wrote:
 Try to put a totally wrong type to subsome, like
 subsome :: Int
 and tell us from the error what type is actually inferred.

The error is like

Couldn't match type `nt' with `Int'
  `nt' is a rigid type variable bound by
   the type signature for
 legSome :: LegGram nt t s - nt - Either String ([t], s)
   at xx.hs:35:1
Expected type: [RRule Int t s]
  Actual type: [RRule nt t s]
In the first argument of `subsome', namely `l'
In the expression: subsome l

However, this error cannot be resolved by a use of ScopedTypeVariable,
while the original error can be resolved.

I still don't see where the mismatch happens in the original error. Any help
is appreciated.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Wed, Jan 4, 2012 at 1:38 AM, Yucheng Zhang yczhan...@gmail.com wrote:
 On Wed, Jan 4, 2012 at 1:07 AM, Yves Parès limestr...@gmail.com wrote:
 Try to put a totally wrong type to subsome, like
 subsome :: Int
 and tell us from the error what type is actually inferred.


Sorry, I found I misunderstood the suggestion. I will try.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Wed, Jan 4, 2012 at 1:07 AM, Yves Parès limestr...@gmail.com wrote:
 I wonder why the redeclared type variables cannot match those of legSome?
 Try to put a totally wrong type to subsome, like
 subsome :: Int
 and tell us from the error what type is actually inferred.


The actually inferred type is

Couldn't match expected type `Int'
with actual type `[([Symbols nt t], [s] - t0)]
  - Either [Char] ([t], t0)'
In the expression: subsome :: Int

I still don't understand why the original mismatch happens.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Bardur Arantsson

2012/1/3 Yucheng Zhangyczhan...@gmail.com



(Hopefully being a little more explicit about this can help you 
understand where things are going wrong.)


[--snip--]


legSome :: LegGram nt t s -  nt -  Either String ([t], s)


The 'nt' you see above


legSome (LegGram g) ntV =
  case Main.lookup ntV g of
 Nothing -  Left No word accepted!
 Just l -  let sg = legSome (LegGram (Main.delete ntV g))
   subsome :: [RRule nt t s] -  Either String ([t], s)


... isn't the same as the 'nt' you see in this line, so it constrains 
'subsome' to a different type than the one you intended -- and indeed 
one which can't be unified with the inferred type. (Unless you use 
ScopedTypeVariables.)


As Brent suggested, you should probably pull the subsome function out 
into a top-level function in any case.


Cheers,


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yucheng Zhang
On Wed, Jan 4, 2012 at 2:48 AM, Bardur Arantsson s...@scientician.net wrote:
 'subsome' to a different type than the one you intended -- and indeed one
 which can't be unified with the inferred type. (Unless you use
 ScopedTypeVariables.)

Thanks for the reply.

Actually, my question is why the different type can't be unified with
the inferred type? Could you point me some related resources?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Is the haddock generator on Hackage down?

2012-01-03 Thread Bardur Arantsson

Hi all,

No Haddock documentation seems to have been generated on Hackage in the 
past few days. See e.g.


   http://hackage.haskell.org/package/copilot-2.0.3
or http://hackage.haskell.org/package/derive-2.5.5

Does anyone know if the cron job (or whatever) isn't running for some 
reason?


Cheers,


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] space-efficient, composable list transformers [was: Re: Reifying case expressions]

2012-01-03 Thread Heinrich Apfelmus

Jan Christiansen wrote:

On Jan 2, 2012, at 2:34 PM, Heinrich Apfelmus wrote:


Without an explicit guarantee that the function is incremental, we can't do 
anything here. But we can just add another constructor to that effect if we 
turn  ListTo  into a GADT:

   data ListTo a b where
   CaseOf   :: b -  (a - ListTo a b)  - ListTo a b
   Fmap :: (b - c) - ListTo a b   - ListTo a c

   FmapCons :: b - ListTo a [b] - ListTo a [b]


I did not follow your discussion but how about using an additional GADT for the 
argument of Fmap, that is

data Fun a b where
  Fun :: (a - b) - Fun a b
  Cons :: a - Fun [a] [a]

data ListTo a b where
  CaseOf   :: b -  (a - ListTo a b) - ListTo a b
  Fmap :: Fun b c - ListTo a b   - ListTo a c

and provide a function to interpret this data type as well

interpretFun :: Fun a b - a - b
interpretFun (Fun f)  = f
interpretFun (Cons x) = (x:)

for the sequential composition if I am not mistaken.

(.) :: ListTo b c - ListTo a [b] - ListTo a c
(CaseOf _ cons) . (Fmap (Cons y) b) = cons y . b
(Fmap f a)  . (Fmap g b) = Fmap f $ a . (Fmap g b)
a . (CaseOf nil cons)= CaseOf (interpret a nil) ((a .) . cons)
a . (Fmap f b)   = fmap (interpret a . interpretFun f) b


-- functor instance
instance Functor (ListTo a) where
  fmap f = normalize . Fmap (Fun f)

normalize :: ListTo a b - ListTo a b
normalize (Fmap (Fun f) (Fmap (Fun g) c)) = fmap (f . g) c
normalize x = x

Cheers, Jan


Nice, that is a lot simpler indeed, and even closer to the core of the idea.


Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is the haddock generator on Hackage down?

2012-01-03 Thread Ross Paterson
On Tue, Jan 03, 2012 at 07:14:58PM +, Bardur Arantsson wrote:
 No Haddock documentation seems to have been generated on Hackage in the 
 past few days.

The machine's down, I think.  I'll give it a kick tomorrow.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Experiments in Haskell Packaging

2012-01-03 Thread Chris Dornan
Hi Ryan,

Ryan Grant [mailto:rgr...@rgrant.org] said:

 Are packages cabal-installed using --global?

The packages that come with a compiler or Haskell Platform are installed
--global. Every other package is installed with cabal, by default into the
user's space.

 Is it mostly reliant on cabal or cabal-dev?

It works with cabal. I have not tested interworking with cabal-dev yet.
(There is a big overlap in the functionality of course.)

 How easily can I upgrade cabal?
  http://stackoverflow.com/questions/5380888/cabal-install-and-debian

The stackoverflow discussion seems a little confused in that 'cabal update'
just updates the local cache of package headers from the server, not any of
the installed packages.

But I am using the latest version of cabal-install (0.10.2), compiled with
cabal-1.10.1.0 library (from March rather than June's cabal-1.10.2.0).

Users should never have to build their own cabal-install, but just keep up
to date with the distro. If users should need to use their own private Cabal
builds for whatever reason we will provide a wiki page showing them how to
do it.

 Can the idea be extended to OSX, as well?

I see no reason why not. The idea behind my post was really to say that we
should be doing this with all distros.

I will be making the hub sources available through hackage/github when beta
testing is complete (but sooner if demand dictates). Folks can install it on
their own systems and hook in their own GHC and HP builds. Best of all folks
can make it available in other Haskell distros.

The interesting question is whether anyone else decides to structure their
distros in the same way as the justhub distro. That  is the real challenge!

 How does this strategy relate to the Nix package manager?
  http://nixos.org/nix/

In a sense the philosophy is similar because I am arguing that we shouldn't
be updating distributions, just adding to them. In that sense it is a
functional philosophy. But after that 30,000 foot observation the comparison
diverges as far as I can see.

Great questions!

Thanks,

Chris



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Solved but strange error in type inference

2012-01-03 Thread Yves Parès
 Actually, my question is why the different type can't be unified with the
inferred type?

Because without ScopedTypeVariable, both types got expanded to :

legSome :: *forall nt* t s. LegGram nt t s - nt - Either String ([t], s)

subsome :: *forall nt* t s. [RRule nt t s] -  Either String ([t], s)

So you see subsome declare new variables, which *do not *match the *rigid *ones
declared by legSome signature, hence the incompatibility.

As we said before, you have three ways to work it out:
1) Use scoped type variables with explicit forall nt on legSome and *no
forall *on subsome, so that nt in subsome matches nt declared in legSome.
2) Don't give a type signature for subsome and let GHC find out which is
its correct type.
3) Extract subsome so that it becomes a top-level function with a
polymorphic type (Recommended).


Now, concerning the error I asked you to deliberately provoke, that's the
quickest way I found to know what is the output of the type inferer, and
maybe the only simple one.
So this error is:
 Couldn't match expected type `Int'
   with actual type `[([Symbols nt t], [s] - t0)]
 - Either [Char] ([t], t0)'
   In the expression: subsome :: Int
GHC tells you the type it infers for subsome: [([Symbols nt t], [s] - t0)]
- Either [Char] ([t], t0)
The nt you see is the one from legSome, those messages show scoped
variables. (You'd get something like 'nt1' or 'nt0' if it was another
variable, meant to be instanciated to a different type).
This accords with your previous error message, which happens to give you
more details about where the rigid type variable nt comes from:
  `nt' is a rigid type variable bound by
   the type signature for
 legSome :: LegGram nt t s - nt - Either String ([t], s)

HTH.

2012/1/3 Yucheng Zhang yczhan...@gmail.com

 On Wed, Jan 4, 2012 at 2:48 AM, Bardur Arantsson s...@scientician.net
 wrote:
  'subsome' to a different type than the one you intended -- and indeed one
  which can't be unified with the inferred type. (Unless you use
  ScopedTypeVariables.)

 Thanks for the reply.

 Actually, my question is why the different type can't be unified with
 the inferred type? Could you point me some related resources?

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How to split this string.

2012-01-03 Thread Jonathan Frywater
If you're interested in learning parsec, RWH covered this topic in depth in
Chapter 16, Choices and Errors:
http://book.realworldhaskell.org/read/using-parsec.html.

On Mon, Jan 2, 2012 at 3:44 AM, max m...@mtw.ru wrote:

 I want to write a function whose behavior is as follows:

 foo string1\nstring2\r\nstring3\nstring4 = [string1,
 string2\r\nstring3, string4]

 Note the sequence \r\n, which is ignored. How can I do this?

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] GHC compilation issue: Working around bug #4374?

2012-01-03 Thread Sanket Agrawal


I am getting the compilation error below when building GHC 7.0.4 on RHEL5 
(x86_64) – I configured make to point to ncurses and libgmp. I will appreciated 
pointers on how to work around the issue below. I don’t have root privileges. 
My apologies if this has already been discussed in this forum. I searched for 
the issue but could find only the GHC bug ticket below.

GHC bug ticket: http://hackage.haskell.org/trac/ghc/ticket/4374 - it doesn’t 
have description on how to work around the bug.

Compilation error:
---
$ 
C_INCLUDE_PATH=/efs/dist/fsf/ncurses/5.6/common/include/ncurses:/home/saagrawa/scripts/misc/libgmp/include
 
LIBRARY_PATH=/efs/dist/fsf/ncurses/5.6/exec/lib:/home/saagrawa/scripts/misc/libgmp/lib
 make
===--- updating makefiles phase 0
make -r --no-print-directory -f ghc.mk phase=0 just-makefiles
===--- updating makefiles phase 1
make -r --no-print-directory -f ghc.mk phase=1 just-makefiles

===--- updating makefiles phase 2
make -r --no-print-directory -f ghc.mk phase=2 just-makefiles
===--- updating makefiles phase 3
make -r --no-print-directory -f ghc.mk phase=3 just-makefiles
===--- finished updating makefiles
make -r --no-print-directory -f ghc.mk all
inplace/bin/ghc-stage1 
libraries/integer-gmp/dist-install/build/GHC/Integer.dyn_o 
libraries/integer-gmp/dist-install/build/GHC/Integer/GMP/Internals.dyn_o 
libraries/integer-gmp/dist-install/build/GHC/Integer/Type.dyn_o 
libraries/integer-gmp/dist-install/build/cbits/gmp-wrappers.dyn_o   
libraries/integer-gmp/dist-install/build/cbits/cbits.dyn_o
libraries/integer-gmp/gmp/objs/*.o `/usr/bin/find 
libraries/integer-gmp/dist-install/build -name *_stub.dyn_o -print` -shared 
-dynamic -dynload deploy -dylib-install-name 
/home/saagrawa/scripts/misc/ghc/7.0.4/lib/ghc-7.0.4/`basename 
libraries/integer-gmp/dist-install/build/libHSinteger-gmp-0.2.0.3-ghc7.0.4.so 
| sed 's/^libHS//;s/[-]ghc.*//'`/`basename 
libraries/integer-gmp/dist-install/build/libHSinteger-gmp-0.2.0.3-ghc7.0.4.so`
 -no-auto-link-packages -package ghc-prim-0.2.0.0 -o  
libraries/integer-gmp/dist-install/build/libHSinteger-gmp-0.2.0.3-ghc7.0.4.so
/efs/dist/fsf/gcc/4.4.3-build004/.exec/x86-64.rhel.5/libexec/gcc/x86_64-unknown-linux-gnu/4.4.3/ld:
 libraries/integer-gmp/gmp/objs/aors.o: relocation R_X86_64_32 against 
`__gmpz_sub' can not be used when making a shared object; recompile with -fPIC
libraries/integer-gmp/gmp/objs/aors.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[1]: *** 
[libraries/integer-gmp/dist-install/build/libHSinteger-gmp-0.2.0.3-ghc7.0.4.so] 
Error 1
make: *** [all] Error 2

$ ll /home/saagrawa/scripts/misc/libgmp/lib|awk '{print $9,$10,$11}' ### the 
libgmp lib folder has dynamic libraries

libgmp.so.3.5.2*
libgmp.so.3 - libgmp.so.3.5.2*
libgmp.so - libgmp.so.3.5.2*
libgmp.la*
libgmp.a___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC compilation issue: Working around bug #4374?

2012-01-03 Thread Sanket Agrawal
My apologies for posting it here. Glasgow-haskell-users mailing list seems
more relevant for this question. So, I am going to send it there instead,
with a note about this cross-posting.

On Tue, Jan 3, 2012 at 4:26 PM, Sanket Agrawal sanket.agra...@gmail.comwrote:



 I am getting the compilation error below when building GHC 7.0.4 on RHEL5
 (x86_64) – I configured make to point to ncurses and libgmp. I will
 appreciated pointers on how to work around the issue below. I don’t have
 root privileges. My apologies if this has already been discussed in this
 forum. I searched for the issue but could find only the GHC bug ticket
 below.

 GHC bug ticket: http://hackage.haskell.org/trac/ghc/ticket/4374 - it
 doesn’t have description on how to work around the bug.

 Compilation error:
 ---
 $
 C_INCLUDE_PATH=/efs/dist/fsf/ncurses/5.6/common/include/ncurses:/home/saagrawa/scripts/misc/libgmp/include
 LIBRARY_PATH=/efs/dist/fsf/ncurses/5.6/exec/lib:/home/saagrawa/scripts/misc/libgmp/lib
 make
 ===--- updating makefiles phase 0
 make -r --no-print-directory -f ghc.mk phase=0 just-makefiles
 ===--- updating makefiles phase 1
 make -r --no-print-directory -f ghc.mk phase=1 just-makefiles

 ===--- updating makefiles phase 2
 make -r --no-print-directory -f ghc.mk phase=2 just-makefiles
 ===--- updating makefiles phase 3
 make -r --no-print-directory -f ghc.mk phase=3 just-makefiles
 ===--- finished updating makefiles
 make -r --no-print-directory -f ghc.mk all
 inplace/bin/ghc-stage1
 libraries/integer-gmp/dist-install/build/GHC/Integer.dyn_o
 libraries/integer-gmp/dist-install/build/GHC/Integer/GMP/Internals.dyn_o
 libraries/integer-gmp/dist-install/build/GHC/Integer/Type.dyn_o
 libraries/integer-gmp/dist-install/build/cbits/gmp-wrappers.dyn_o
 libraries/integer-gmp/dist-install/build/cbits/cbits.dyn_o
 libraries/integer-gmp/gmp/objs/*.o `/usr/bin/find
 libraries/integer-gmp/dist-install/build -name *_stub.dyn_o -print`
 -shared -dynamic -dynload deploy -dylib-install-name
 /home/saagrawa/scripts/misc/ghc/7.0.4/lib/ghc-7.0.4/`basename
 libraries/integer-gmp/dist-install/build/
 libHSinteger-gmp-0.2.0.3-ghc7.0.4.so | sed
 's/^libHS//;s/[-]ghc.*//'`/`basename
 libraries/integer-gmp/dist-install/build/
 libHSinteger-gmp-0.2.0.3-ghc7.0.4.so` -no-auto-link-packages -package
 ghc-prim-0.2.0.0 -o libraries/integer-gmp/dist-install/build/
 libHSinteger-gmp-0.2.0.3-ghc7.0.4.so
 /efs/dist/fsf/gcc/4.4.3-build004/.exec/x86-64.rhel.5/libexec/gcc/x86_64-unknown-linux-gnu/4.4.3/ld:
 libraries/integer-gmp/gmp/objs/aors.o: relocation R_X86_64_32 against
 `__gmpz_sub' can not be used when making a shared object; recompile with
 -fPIC
 libraries/integer-gmp/gmp/objs/aors.o: could not read symbols: Bad value
 collect2: ld returned 1 exit status
 make[1]: *** [libraries/integer-gmp/dist-install/build/
 libHSinteger-gmp-0.2.0.3-ghc7.0.4.so] Error 1
 make: *** [all] Error 2

 $ ll /home/saagrawa/scripts/misc/libgmp/lib|awk '{print $9,$10,$11}' ###
 the libgmp lib folder has dynamic libraries
 **
 **
 libgmp.so.3.5.2*
 libgmp.so.3 - libgmp.so.3.5.2*
 libgmp.so - libgmp.so.3.5.2*
 libgmp.la*
 libgmp.a

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe