[haskell-llvm] Beginner - Anonymous Functions (fwd)

Henning Thielemann lemming at henning-thielemann.de
Sat Nov 19 20:43:09 GMT 2011


---------- Forwarded message ----------
Date: Fri, 18 Nov 2011 16:02:18 +0100
From: Henning Thielemann <lemming at henning-thielemann.de>
To: Elliott Pace <etp20 at cam.ac.uk>
Cc: haskell-llvm at projects.haskell.org
Subject: Re: [haskell-llvm] Beginner - Anonymous Functions

Elliott Pace wrote:
> Thanks again for the help. I realised I was perhaps rushing things too much 
> when I couldn't really think out the types.

I have still no idea, what you actually want to achieve in the end.

> A problem I'm having is that pointers seem to be well-typed, which causes 
> problems with my current representation of a function value. What's the 
> correct method of dealing with this?

You can cast pointers to and from Ptr (). But before you resort to this you 
should check for a type-safe solution.

> So, my function value is: A pointer to a pair of pointers, one to a function 
> and one to a linked list of arguments. Therefore I would have something like
> 
> type FunctionValue f = Ptr (Ptr f)
> -- type FunctionValue[4] f = Ptr (LLNode a)

What do you mean with this comment?

> Now, trying to code my "call'" function, that given a pointer to a function 
> value and an argument, calls our function with argument and linked list as 
> needed:
> 
> call' :: Value (FunctionValue (LLNode a -> b -> IO c)) -> b -> 
> CodeGenFunction r c
> call' fValue x = do
>     f <- load fValue
>        listPtr <- getElementPtr fValue (1 :: Word32, ())
>     let _ = listPtr :: Value (Ptr (LLNode a))
>
>     llNode <- load listPtr
>
>     call f llNode x
> 
> This causes problems (amongst others present, but babysteps!).

What problems does it cause?

I think it would work to put a sequence of function pointers into an array and 
traverse this array using
    nextFuncPtr <- getElementPtr currentFuncPtr (1 :: Word32, ())

The type of a linked list is recursive and it is difficult to define in 
Haskell, such that it can be translated to LLVM. In this case it might be the 
simplest way to use Ptr () for the link to the next node, and to cast it to the 
list node type whenever you read it.



More information about the Haskell-llvm mailing list