[Haskell-cafe] Intermediate type variables

2010-03-19 Thread Giuseppe Maggiore
Hi! How can I specify intermediate type variables to help the compiler do
its job?

The following, simple, code does not compile because the compiler cannot
infer that the type variable a' is CInt; this makes sense, because finding
that out would be quite hard. How can I tell the compiler that a' is CInt?

 {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
FlexibleInstances,
  UndecidableInstances, FlexibleContexts, EmptyDataDecls,
ScopedTypeVariables,
  TypeOperators, TypeSynonymInstances #-}
class C a a'  where convert :: a - a'
class F a b   where apply :: a - b
class S s a   where select :: s - a
data CInt = CInt Int
instance S (Int,String) Int where select (i,_) = i
instance C Int CInt where convert = CInt
instance F CInt Int where apply (CInt i) = i + 1
f :: forall s a a' b . (S s a, C a a', F a' b) = s - b
f s =
  let v = select s :: a
  v' = convert v :: a'
  y = apply v' :: b
  in y

x :: Int
x = f (10,foo)


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


[Haskell-cafe] Code that doesn't compile - but should :)

2010-03-16 Thread Giuseppe Maggiore
Hi! Can anyone tell me why this code does not work? I cannot seem to
figure why it is broken...



{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances,
  UndecidableInstances, FlexibleContexts, EmptyDataDecls, ScopedTypeVariables,
  TypeOperators, TypeSynonymInstances #-}

data Data k = Pair Integer (() - k)
data RecData = RecData (Data RecData)
mk_data x = RecData(Pair x (\() - mk_data (x+1)))

class Converter a f where convert :: a - f a

instance Converter RecData Data where
  convert (RecData r) = r

class Selector s a where select :: s - a

f :: (Selector s (a-f a), Converter a f) = s - (a-a)
f s =
  let method = select s
  in (\x -
let res = method x
in convert res)

--
Giuseppe Maggiore
Ph.D. Student (Languages and Games)
Microsoft Student Partner
Mobile: +393319040031
Office: +390412348444
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Code that doesn't compile - but should :)

2010-03-16 Thread Giuseppe Maggiore
The error message (obtained by loading the file with ghci) is:

GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main (
C:\Users\pulcy\Desktop\Papers\Monads\Objec
tiveMonad\HObject\Experiments\FunctorsProblems.hs, interpreted )

C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObject\Experiments\Functors
Problems.hs:18:15:
Could not deduce (Selector s (f a - a))
  from the context (Selector s (a1 - f1 a1), Converter a1 f1)
  arising from a use of `select'
   at
C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObjec
t\Experiments\FunctorsProblems.hs:18:15-22
Possible fix:
  add (Selector s (f a - a)) to the context of
the type signature for `f'
  or add an instance declaration for (Selector s (f a - a))
In the expression: select s
In the definition of `method': method = select s
In the expression:
let method = select s in (\ x - let res = ... in convert res)

C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObject\Experiments\Functors
Problems.hs:21:11:
Couldn't match expected type `a1' against inferred type `f a'
  `a1' is a rigid type variable bound by
   the type signature for `f'
 at
C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObject\Expe
riments\FunctorsProblems.hs:16:18
In the expression: convert res
In the expression: let res = method x in convert res
In the expression: (\ x - let res = method x in convert res)
Failed, modules loaded: none.
Prelude

On Tue, Mar 16, 2010 at 2:31 AM, Ivan Lazar Miljenovic 
ivan.miljeno...@gmail.com wrote:

 Giuseppe Maggiore giuseppe...@gmail.com writes:

  Hi! Can anyone tell me why this code does not work? I cannot seem to
  figure why it is broken...

 The error message (and how you got it) would help...

  {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
 FlexibleInstances,
UndecidableInstances, FlexibleContexts, EmptyDataDecls,
 ScopedTypeVariables,
TypeOperators, TypeSynonymInstances #-}

 You sure you have enough language extensions there? ;-)


Barely :)



 --
 Ivan Lazar Miljenovic
 ivan.miljeno...@gmail.com
 IvanMiljenovic.wordpress.com http://ivanmiljenovic.wordpress.com/




-- 
Giuseppe Maggiore
Ph.D. Student (Languages and Games)
Microsoft Student Partner
Mobile: +393319040031
Office: +390412348444
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Code that doesn't compile - but should :)

2010-03-16 Thread Giuseppe Maggiore
Well, first of all thanks!

Second, why the need for explicit quantification?
On Tue, Mar 16, 2010 at 2:39 AM, Chris Eidhof ch...@eidhof.nl wrote:

 What about this?

  {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
 FlexibleInstances,
   UndecidableInstances, FlexibleContexts, EmptyDataDecls,
 ScopedTypeVariables,
   TypeOperators, TypeSynonymInstances #-}
 
  data Data k = Pair Integer (() - k)
  data RecData = RecData (Data RecData)
  mk_data x = RecData(Pair x (\() - mk_data (x+1)))
 

 The I had to change the type of the Converter typeclass

  class Converter a f where convert :: f a - a
 
  -- instance Converter RecData Data where
  --  convert (RecData r) = r
 
  class Selector s a where select :: s - a
 

 And explicitly quantify the type variables:

  f :: forall f s a . (Selector s (a-f a), Converter a f) = s - (a-a)
  f s =
   let method = select s :: a - f a
   in (\x -
 let res = method x
 in convert res)

 -chris

 On 16 mrt 2010, at 10:36, Giuseppe Maggiore wrote:

  The error message (obtained by loading the file with ghci) is:
  GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
  Loading package ghc-prim ... linking ... done.
  Loading package integer ... linking ... done.
  Loading package base ... linking ... done.
  [1 of 1] Compiling Main (
 C:\Users\pulcy\Desktop\Papers\Monads\Objec
  tiveMonad\HObject\Experiments\FunctorsProblems.hs, interpreted )
 
 
 C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObject\Experiments\Functors
  Problems.hs:18:15:
  Could not deduce (Selector s (f a - a))
from the context (Selector s (a1 - f1 a1), Converter a1 f1)
arising from a use of `select'
 at
 C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObjec
  t\Experiments\FunctorsProblems.hs:18:15-22
  Possible fix:
add (Selector s (f a - a)) to the context of
  the type signature for `f'
or add an instance declaration for (Selector s (f a - a))
  In the expression: select s
  In the definition of `method': method = select s
  In the expression:
  let method = select s in (\ x - let res = ... in convert res)
 
 
 C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObject\Experiments\Functors
  Problems.hs:21:11:
  Couldn't match expected type `a1' against inferred type `f a'
`a1' is a rigid type variable bound by
 the type signature for `f'
   at
 C:\Users\pulcy\Desktop\Papers\Monads\ObjectiveMonad\HObject\Expe
  riments\FunctorsProblems.hs:16:18
  In the expression: convert res
  In the expression: let res = method x in convert res
  In the expression: (\ x - let res = method x in convert res)
  Failed, modules loaded: none.
  Prelude
 
 
  On Tue, Mar 16, 2010 at 2:31 AM, Ivan Lazar Miljenovic 
 ivan.miljeno...@gmail.com wrote:
  Giuseppe Maggiore giuseppe...@gmail.com writes:
 
   Hi! Can anyone tell me why this code does not work? I cannot seem to
   figure why it is broken...
 
  The error message (and how you got it) would help...
 
   {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
 FlexibleInstances,
 UndecidableInstances, FlexibleContexts, EmptyDataDecls,
 ScopedTypeVariables,
 TypeOperators, TypeSynonymInstances #-}
 
  You sure you have enough language extensions there? ;-)
 
  Barely :)
 
 
  --
  Ivan Lazar Miljenovic
  ivan.miljeno...@gmail.com
  IvanMiljenovic.wordpress.com http://ivanmiljenovic.wordpress.com/
 
 
 
  --
  Giuseppe Maggiore
  Ph.D. Student (Languages and Games)
  Microsoft Student Partner
  Mobile: +393319040031
  Office: +390412348444
 
 
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
Giuseppe Maggiore
Ph.D. Student (Languages and Games)
Microsoft Student Partner
Mobile: +393319040031
Office: +390412348444
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Type variables

2010-03-09 Thread Giuseppe Maggiore
Hi everyone, this is my first post: I am a ph.d. student from Italy who is
learning the wonderful world of Haskell :)

I have encountered a problem and I cannot find a way to get past it (or even
to begin to understand it). I wish to define a simple type class that
defines how to convert a type function into its argument (like going from a
dumb constructor like data T a = T a into a itself):


class Convert rec where convert :: rec a - a

now when I try to use the conversion operator

class (CNum n, HasField n (a - (b,rec a)) l, Convert rec) = HasMethod n l
a b rec where

  (..!) :: l - n - (a - (b,a))



instance (CNum n, HasField n (a - (b,rec a)) l, Convert rec) = HasMethod n
l a b rec where

  l ..! n =

 let m = l .! n

 in (\x -

  let (y,v) = m x

  in (y,convert v))



in what looks to me as a straightforward use, the compiler complains that :

*References :load Main.hs
[1 of 5] Compiling Records  ( Records.hs, interpreted )
[2 of 5] Compiling References   ( References.hs, interpreted )
[3 of 5] Compiling Methods  ( Methods.hs, interpreted )

Methods.hs:25:14:
Could not deduce (HasField n (a - (b, rec a)) l)
  from the context (HasMethod n l a b rec1,
CNum n,
HasField n (a - (b, rec1 a)) l,
Convert rec1)
  arising from a use of `.!' at Methods.hs:25:14-19
Possible fix:
  add (HasField n (a - (b, rec a)) l) to the context of
the instance declaration
  or add an instance declaration for (HasField n (a - (b, rec a)) l)
In the expression: l .! n
In the definition of `m': m = l .! n
In the expression:
let m = l .! n in (\ x - let (y, v) = ... in (y, convert v))
Failed, modules loaded: References, Records.
*References

I apologize for what might look like a huge dumping of code, but I have no
idea what might have caused this to further refine the code or to write a
more focused example...
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Type variables

2010-03-09 Thread Giuseppe Maggiore
Thanks, that helped (monomorphism restriction and copointed)...

I'll see if I can come up with a more specific example!


On Tue, Mar 9, 2010 at 6:12 PM, Maciej Piechotka uzytkown...@gmail.comwrote:

 On Tue, 2010-03-09 at 15:50 +0100, Giuseppe Maggiore wrote:
 
  class (CNum n, HasField n (a - (b,rec a)) l, Convert rec) =
  HasMethod n l a b rec where
 
(..!) :: l - n - (a - (b,a))
 
 
 
  instance (CNum n, HasField n (a - (b,rec a)) l, Convert rec) =
  HasMethod n l a b rec where
 
l ..! n =
 
   let m = l .! n
 
   in (\x -
 
let (y,v) = m x
 
in (y,convert v))
 
 

 i think you have in mind something like:

 import Control.Arrow

 (..!) :: (CNum n, HasField n (a - (b,rec a)) l, Convert rec) =
 l - n - (a - (b,a))
 l ..! n = second convert . (l .! n)

 1. I don't see a point of creating class. I mean you provide a wildcard
 implementation - why not provide just a method?
 2. I'm afraid that it might be monomorphism restriction. But I'm not
 sure.
 3. Without code I can hardly test the problems ;) I can write what I
 _think_ code would look like.

 Regards
 PS. I would be grateful for ASCII-only posts:
 http://www.asciiribbon.org/
 PPS. convert looks strangly similar to copointed:

 http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/Control-Functor-Pointed.html#t%3ACopointed



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




-- 
Giuseppe Maggiore
Ph.D. Student (Languages and Games)
Microsoft Student Partner
Mobile: +393319040031
Office: +390412348444
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe