Lauri Alanko | 2 Jun 2003 01:52
Picon
Picon
Favicon

Re: Eval in Haskell

[Moving to cafe, this is only barely Haskell-related.]

On Sun, Jun 01, 2003 at 04:16:36PM -0700, oleg <at> pobox.com wrote:
> Eval of the kind
> 	let x = 1 in eval "x"
> is plainly an abomination.

I agree, though not because of the optimization issues but rather
as a matter of principle: a closed variable should be _closed_, it
should not be visible to anything but the body of the let-expression
that binds it. And an externally acquired eval-able expression is
definitely "anything but".

Nevertheless, this abomination is supported even by some scheme
implementations:

guile> (define local-environment (procedure->syntax (lambda (exp env) env)))
guile> (define (eval-where-x-bound exp)
... (let ((x 'foo)) (local-eval exp (local-environment))))      
guile> (eval-where-x-bound '(list 'x x))
(x foo)

> Incidentally, restricting eval to top-level or "standard" bindings is 
> not a significant limitation. It is, in general, a very good practice
> to apply eval to closed expressions only.

This depends entirely on what you want to achive with eval, so I don't
think the "in general" is justified. If you just want to evaluate closed
expressions whose results are printable and readable, then you really
just have an embedded interpreter which happens to read the same
(Continue reading)

Ashley Yakeley | 2 Jun 2003 05:43

Re: Eval in Haskell

In article <20030601235255.GA4498 <at> la.iki.fi>, Lauri Alanko <la <at> iki.fi> 
wrote:

> Nevertheless, this abomination is supported even by some scheme
> implementations:
> 
> guile> (define local-environment (procedure->syntax (lambda (exp env) env)))
> guile> (define (eval-where-x-bound exp)
> ... (let ((x 'foo)) (local-eval exp (local-environment))))      
> guile> (eval-where-x-bound '(list 'x x))
> (x foo)

Guile seems to be a bit "sloppy" in general. I think it just keeps a 
dictionary of symbol bindings and passes it around at runtime.

guile> (eval '(define b 5))
guile> b
5

--

-- 
Ashley Yakeley, Seattle WA
Pablo Dejuan | 2 Jun 2003 01:07
Picon
Favicon

Re: Is my question bad ?

Karl M Syring wrote:
Antoine Utonium wrote on Sat, 31 May 2003 13:29:09 +0200:
I recently asked you who knows how to link haskell-made code to C apps, or call haskell code from C code, or encapsulate, traduce, Haskell code in C code. I just want to use haskell when I need advanced algorithms, & C for I/O, and GUI (under windows).. If my question has been answered yet, or if my english is too bad, please say me, or give me links to answers. Thanks. Antoine Utonium wrote:
Everything is in the title. I like "simplicity" of Haskell and would like to use what i write in my c/c++ programs. Thanks a lot by advance.
I guess, there are people who have tried, but failed :-) The easiest way would be to make a COM server in Haskell. You should have a look at the HDirect sources, where there are some examples. Generally using a COM dll from Haskell seems to work, while the other way round seems to be problematic. Karl M. Syring
If you are using GHC, you could compile your ".hs" to a Haskell C (".hc") which is a C file, this way:
          ghc -C myfile.hs

There are also other options like compiling an ".o" but I've never tried'em. Wish you luck.

Antoine Utonium | 2 Jun 2003 07:24
Picon
Favicon

Re: Is my question bad ?

Karl M Syring wrote:
> Antoine Utonium wrote on Sat, 31 May 2003 13:29:09 +0200:
>> I recently asked you who knows how to link haskell-made code to C
>> apps, or call haskell code from C code, or encapsulate, traduce,
>> Haskell code in C code.
>> I just want to use haskell when I need advanced algorithms, & C for
>> I/O, and GUI (under windows)..
>> If my question has been answered yet, or if my english is too bad,
>> please say me, or give me links to answers.
>> Thanks.
>>
>> Antoine Utonium wrote:
>>> Everything is in the title. I like "simplicity" of Haskell and would
>>> like to use what i write in my c/c++ programs.
>>> Thanks a lot by advance.
>
> I guess, there are people who have tried, but failed :-)
> The easiest way would be to make a COM server in Haskell.
> You should have a look at the HDirect sources, where there are
> some examples. Generally using a COM dll from Haskell seems to
> work, while the other way round seems to be problematic.

Thank you for your answer. I don't really know what is a COM server but msdn
doc probably will say me more on that. I saw a work at microsoft research,
called Pan, which used Haskell code, and translated it to C to compile it.
But produced code was too obfusquated to reuse it in C sources. So i'm going
to try COM solution.
Lauri Alanko | 2 Jun 2003 07:57
Picon
Picon
Favicon

Re: Eval in Haskell

[We now have lost all pretence of topicality]

On Sun, Jun 01, 2003 at 08:43:03PM -0700, Ashley Yakeley wrote:
> Guile seems to be a bit "sloppy" in general. I think it just keeps a 
> dictionary of symbol bindings and passes it around at runtime.
> 
> guile> (eval '(define b 5))
> guile> b
> 5

This particular kind of sloppiness is pretty common in Scheme REPLs:

la:~$ kawa
#|kawa:1|# (eval '(define b 5))
#|kawa:2|# b
5
#|kawa:3|# 
la:~$ mzscheme
Welcome to MzScheme version 204, Copyright (c) 1995-2003 PLT
> (eval '(define b 5))
> b
5
> 
la:~$ bigloo 
------------------------------------------------------------------------------
Bigloo (2.5c)                                                            ,--^, 
`a practical Scheme compiler'                                      _ ___/ /|/  
Wed Nov 27 10:49:16 CET 2002                                   ,;'( )__, ) '   
Manuel Serrano                                                ;;  //   L__.    
email:                                                        '   \    /  '    
Manuel.Serrano <at> sophia.inria.fr                                     ^   ^       
------------------------------------------------------------------------------

Welcome to the interpreter

1:=> (eval '(define b 5))
b
1:=> b
5
1:=> 
la:~$ mit-scheme 
Scheme Microcode Version 14.9
MIT Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Scheme saved on Tuesday June 18, 2002 at 2:26:05 AM
  Release 7.7.1
  Microcode 14.9
  Runtime 15.1
  SF 4.40
  Liar (Intel i386) 4.115
  Edwin 3.112

1 ]=> (eval '(define b 5) system-global-environment)

;Value: b

1 ]=> b

;Value: 5

1 ]=> 
End of input stream reached
Happy Happy Joy Joy.

... though admittedly it's a bit confusing, since "define" either binds
or assigns to a variable, depending on whether it was already bound.

Lauri Alanko
la <at> iki.fi
Bayley, Alistair | 2 Jun 2003 09:58

RE: Help: writing ffi bindings to a C library

Yes, I had considered this (writing an abstraction layer in C). I have
decided initially to try doing it in Haskell, because I thought that Haskell
would be a better choice for writing abstraction layers (not as verbose as
C). The idea was to write the Haskell-C bindings at as low a level as
possible (i.e. directly to the OCI libraries), and then build up from there.
Although the bindings may be ugly, I thought it would have been easier to
build higher layers in Haskell rather than C.

However, if you think it's probably better to build the first layer in C,
then I'll do that.

Getting back to my question, can I use functions that take twice-deref'ed
pointers as args? This is a function that effectively returns some of its
work in "out" arguments e.g. a C function like this:

    int someCFunction(SomeType **ptrptr, int i)

(It would create an opaque structure and point *ptrptr at it. If it fails
then it would return a negative value.)

Is it simply a case of declaring it like this (in ghc):

> data SomeType
> type SomeTypeRef = Ptr SomeType
> foreign import ccall "blah.h someCFunction" someCFunction :: Ptr
SomeTypeRef -> Int -> Int

Or must C functions that are to be called from Haskell return results only
in the return value?

-----Original Message-----
From: Ronald Legere [mailto:rjljr2 <at> yahoo.com]
Sent: 31 May 2003 15:25
To: Bayley, Alistair; 'haskell-cafe <at> haskell.org'
Subject: RE: Help: writing ffi bindings to a C library

One strategy is to wrap that funtion in another
function (in C) that encapsulates the 'typical'
call you want to make. THen import this function into
haskell. 
In any event, start with something simpler, to 
get the hang of it. Try to write a function (in C)
that adds two integers,or prints something and import
that in to haskell. There are some examples floating
around to get you started, but I can't
find them at the moment on the CVS tree.  Anyone?

Also, read "Tackling the awkward squad" (Google it)

Ron

--- "Bayley, Alistair"
<Alistair_Bayley <at> ldn.invesco.com> wrote:
> So...
> 
> I'm still trying to write this Oracle OCI ffi
> binding. Can anyone tell me
> how to declare the Haskell type for this function:
> 
> sword   OCIEnvCreate (OCIEnv **envp, ub4 mode, dvoid
> *ctxp,
>                  dvoid *(*malocfp)(dvoid *ctxp,
> size_t size),
>                  dvoid *(*ralocfp)(dvoid *ctxp,
> dvoid *memptr, size_t
> newsize),
>                  void   (*mfreefp)(dvoid *ctxp,
> dvoid *memptr),
>                  size_t xtramem_sz, dvoid
> **usrmempp);
> 
> Note that when I use it, I'm passing 0 (NULL) into
> almost all of the args,
> so the usage in C is typically:
> 
> 	rc = OCIEnvCreate(&envhp, OCI_DEFAULT, 0, 0, 0, 0,
> 0, 0);
> 
> i.e. I don't care about most of the args, so that
> should make the Haskell
> declaration simpler. Some of the arguments are
> pointers to functions, for
> example.
> 
> What I can't figure out is how to declare the type
> of the first arg. Is is
> Ptr (Ptr OCIEnv) ?
> 
> Here's what I have so far (not much, I know):
> 
> > module Main where
> > import Foreign
> > import Foreign.C.Types
> > import Foreign.C.String
> > import Foreign.Ptr
> 
> > data OCIEnv
> 
> > foreign import ccall "oci.h OCIEnvCreate"
> ociEnvCreate :: Ptr OCIEnv ->
> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int
> 

*****************************************************************
The information in this email and in any attachments is 
confidential and intended solely for the attention and use 
of the named addressee(s). This information may be 
subject to legal professional or other privilege or may 
otherwise be protected by work product immunity or other 
legal rules.  It must not be disclosed to any person without 
our authority.

If you are not the intended recipient, or a person 
responsible for delivering it to the intended recipient, you 
are not authorised to and must not disclose, copy, 
distribute, or retain this message or any part of it.
*****************************************************************
Alastair Reid | 2 Jun 2003 11:18
Picon

Re: Is my question bad ?

On Saturday 31 May 2003 12:29 pm, Antoine Utonium wrote:
> I recently asked you who knows how to link haskell-made code to C apps, or
> call haskell code from C code, or encapsulate, traduce, Haskell code in C
> code.
> I just want to use haskell when I need advanced algorithms, & C for I/O,
> and GUI (under windows)..

If the library you want to use is already available under COM, I agree with 
Karl M Syring that Haskell->COM tools (i.e., H/Direct) are the way to go.

If they are available as normal C libraries, I'd recommend using either the 
foreign function interface (ffi) 

  http://www.cse.unsw.edu.au/~chak/haskell/ffi/

or one of the tools that sits atop the foreign function interface.  There is a 
very preliminary and slightly dated overview of all the tools here:

  http://www.reid-consulting-uk.ltd.uk/docs/ffi.html

The best place to ask for information is on the ffi mailing list 
(ffi <at> haskell.org).  In general, the more specific your question, the better 
the answer is likely to be.

  http://www.haskell.org/pipermail/ffi/

Hope this helps.

--
Alastair Reid
Marcin 'Qrczak' Kowalczyk | 2 Jun 2003 11:45
Picon

Re: Help: writing ffi bindings to a C library

Dnia pon 2. czerwca 2003 09:58, Bayley, Alistair napisaƂ:

> Yes, I had considered this (writing an abstraction layer in C). I have
> decided initially to try doing it in Haskell, because I thought that
> Haskell would be a better choice for writing abstraction layers (not as
> verbose as C).

You can write it in Haskell too and I would recommend that - unless the C 
library uses some passing convention Haskell cannot handle (e.g. passing 
structs by value, or having variable argument count, or relying heavily on 
macros).

> Getting back to my question, can I use functions that take twice-deref'ed
> pointers as args? This is a function that effectively returns some of its
> work in "out" arguments

Sure. You can use libraries documented in the Haskell FFI proposal.

> e.g. a C function like this:
>
>     int someCFunction(SomeType **ptrptr, int i)
>
> (It would create an opaque structure and point *ptrptr at it. If it fails
> then it would return a negative value.)
>
> Is it simply a case of declaring it like this (in ghc):
> > data SomeType
> > type SomeTypeRef = Ptr SomeType
> > foreign import ccall "blah.h someCFunction" someCFunction ::
> >    Ptr SomeTypeRef -> Int -> Int

foreign import ccall "blah.h someCFunction" someCFunction ::
   Ptr SomeTypeRef -> CInt -> IO CInt

haskellWrapper :: Int -> IO (Maybe SomeTypeRef)
haskellWrapper arg =
   alloca $ \ptr -> do
      res <- someCFunction ptr (fromIntegral arg)
      if res < 0 then return Nothing else liftM Just (peek ptr)

You can raise an IO error instead. You can wrap the result in a ForeignPtr if 
it should be finalized when it's garbage collected.

You can use hsc2hs to access integer constants, enumeration values and structs 
from Haskell, or write C wrappers inline inside Haskell source (e.g. to turn 
macros into functions).

--

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak <at> knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
Naudts, Guido | 2 Jun 2003 13:29
Picon

a readFile problem

Hallo, 
I have the following problem:
my program asks the user for a command, when the command is executed a
new command is asked (this is no problem ; an example is in the HUgs
distribution namely Main.hs in the Prolog example.
However one of the commands is: read filename
i.e. read a file and display it. 
I do not succeed in implementing this. 
Could anyone give an example of: reading commands in a loop where one of
the commands is the read command mentionned above. 
Or is it not possible in Haskell? 
I have already wated a lot of time on this, so if anyone gives me an
answer I will be very gratefull.
Greetings,
Attachment (guido.naudts.vcf): text/x-vcard, 360 bytes
Keith Wansbrough | 2 Jun 2003 16:38
Picon
Picon
Favicon

Re: a readFile problem

> However one of the commands is: read filename
> i.e. read a file and display it. 
> I do not succeed in implementing this. 

http://www.haskell.org/hawiki/ThatAnnoyingIoType
http://www.haskell.org/hawiki/UsingIo

are your friends.

--KW 8-)
--

-- 
Keith Wansbrough <kw217 <at> cl.cam.ac.uk>
http://www.cl.cam.ac.uk/users/kw217/
University of Cambridge Computer Laboratory.

Gmane