Re: [Haskell-cafe] Newbie Q: composeMaybe :: (a - Maybe b) - (b - Maybe c) - (a - Maybe c)

2006-11-08 Thread DeeJay-G615

Hi Dmitri,

your f1 function has 3 arguments, f, g and x.

you pass f as the first argument to mapMaybe, so it naturally must have type (a 
- b).
you pass the result of (g x) to the second argument of mapMaybe, so (g x) must 
have type Maybe a. This means g must have the type (t - Maybe a) where t is the 
type of x.


This gives f1 :: (a - b) - (t - Maybe a) - t - Maybe b

you are going to be passing in something with type (c - Maybe d) as the first 
argument to f1. (I used different type variables to reduce confusion)


This constraint gives f1 the following type

f1 :: (c - Maybe d) - (t - Maybe c) - t - Maybe (Maybe d)

substituting different type variable names gives

f1 :: (b - Maybe c) - (a - Maybe b) - a - Maybe (Maybe c)

So you are very close to finishing... :)
Hope this helps.

DeeJay

Dmitri O.Kondratiev wrote:
I am trying to solve a problem from The Craft of Functional 
Programming book:


14.38 ... define the function:
data Maybe a = Nothing | Just a
composeMaybe :: (a - Maybe b) - (b - Maybe c) - (a - Maybe c)

using functions:

squashMaybe :: Maybe (Maybe a) - Maybe a
squashMaybe (Just (Just x)) = Just x
squashMaybe _ = Nothing

mapMaybe :: (a - b) - Maybe a - Maybe b
mapMaybe f Nothing = Nothing
mapMaybe f (Just x) = Just (f x)

As a first step to the solution I defined auxilary function:
f1 f g x = mapMaybe f (g x)

GHCi gives the following type for this function:

f1 :: (a - b) - (t - Maybe a) - t - Maybe b
 ^^^
Q: I don't quite understand this signature. I would expect this
instead (by mapMaybe definition):
f1 :: (a - b) - (t - Maybe a) - Maybe b


From where does the second 't' come from? What are the arguments and


what f1 returns in this case?
___
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] StablePtr's and castStablePtrToPtr

2006-07-25 Thread DeeJay-G615

I have a query which is asked out of interest's sake...

I'm essentially looking for an affirmation of what I think I already 
understand (or some info if I'm deluded ;)).


To put this in context...


I have some C code...

  typedef int func(void *);

  void from_maybe_int(void *val, func *my_func)
  {
int i;
i = my_func(val);
printf(value within C: %d\n, i);
  }

And some Haskell code...

  type FromMaybeInt = StablePtr (Maybe Int) - IO CInt

  foreign import ccall wrapper
  wrapFromMaybeInt :: FromMaybeInt
   - IO (FunPtr FromMaybeInt)

  foreign import ccall from_maybe_int
  cFromMaybeInt :: StablePtr (Maybe Int)
- FunPtr GetJust
- IO ()

  functionToPass :: FromMaybeInt
  functionToPass sPtr = do
m - deRefStablePtr sPtr
case m of
  Nothing - return (-1)
  Just i - return (fromIntegral i)

  main :: IO ()
  main = do sPtr - newStablePtr (Just 3) -- for example
funPtr - wrapFromMaybeInt functionToPass
cFromMaybeInt sPtr funPtr
freeStablePtr sPtr


The compiled program works fine. However I wanted to check this was 
correct usage. As in, is perfectly fine to pass a value of type 
StablePtr a into C?


Am I correct in thinking that StablePtr is defined as a void pointer in 
C? From my very limited understanding of C, it is also the case that you 
can implicitly cast a void pointer to any other pointer type and 
vice-versa. Some appear to deem it bad practice if you explictly give 
the cast.


So therefore I am somewhat hazy on the use for castStablePtrToPtr. I 
found the ghc docs to be quite cryptic for this function.


Google dug up a few examples of it's use in PUGS... which has lead me to 
think that the function is purely for type 'convience' in Haskell. Is 
this the case or am I missing a use case here?


Disclaimer: My knowledge and experience of C is somewhat limited. 
Ironically I have started playing with C by way of Haskell. (Sick and 
twisted I know).


Thanks, Daniel James

P.S.  I must say, I really like the Haskell FFI. Very clean and 
enjoyable to work with.




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