Picon

FFI export lazy list of string

Hi, Haskellers.


Advise me please, how I can export lazy and potentially infinite list of string from Haskell program. I think I must call it iteratively: the first call initiate some structure and other calls iterate over it, something like pair of function `find_first' and `find_next'. And how to marshall this structure between programs. Or think in a wrong way? Does any example exist how I can make it?

Alexander Popov


<div>
<p>Hi, Haskellers.</p>
<div><br></div>
<div>Advise me please, how I can export lazy and potentially infinite list of string from Haskell program. I think I must call it iteratively: the first call initiate some structure and other calls iterate over it, something like pair of function `find_first' and `find_next'. And how to marshall this structure between programs. Or think in a wrong way? Does any example exist how I can make it?</div>
<div><br></div>
<div>Alexander Popov</div>
<div><br></div>
<div><br></div>
</div>
Edward Z. Yang | 5 Dec 20:13 2011
Picon

Re: FFI export lazy list of string

Hello Alexander,

Your best bet is to make a Haskell functions which gives access to
the head and tail of a list (the head should be converted into a C-friendly
form) and then export them as functions which are callable from C.

http://www.haskell.org/haskellwiki/GHC/Using_the_FFI#Calling_Haskell_from_C

Edward

Excerpts from Alexander.Vladislav.Popov's message of Mon Dec 05 03:08:59 -0500 2011:
> Hi, Haskellers.
> 
> Advise me please, how I can export lazy and potentially infinite list of
> string from Haskell program. I think I must call it iteratively: the first
> call initiate some structure and other calls iterate over it, something
> like pair of function `find_first' and `find_next'. And how to marshall
> this structure between programs. Or think in a wrong way? Does any example
> exist how I can make it?
> 
> Alexander Popov

Picon

Re: FFI export lazy list of string

Hello Edward,


Would you help me to map this:

-- genexlib.hs
{-# LANGUAGE BangPatterns, ForeignFunctionInterface #-}

module GenexLib where

import Regex.Genex
import System.IO
import System.Environment

data CLazyList a = Empty | CLL !a [a]

instance (Show a) => Show (CLazyList a) where
  show Empty = "Empty"
  show (CLL x xs) = show x ++ ":.."

next (CLL _ [])     = Empty
next (CLL _ (x:xs)) = CLL x xs

fromList []     = Empty
fromList (x:xs) = CLL x xs

open = fromList . genexPure
-- end genexlib.hs

to

-- genex.c
typedef struct CLL {
  unsigned char empty;
  char          *current;
  struct CLL    *next;
} CLL;

CLL *open(const char *regex);
CLL *next(const CLL  *cll);

// void printf(const char *s, ...);

void usage() {
  CLL *genex = open("\\d+");
  while(!genex->empty) {
    printf(genex->current);
    genex = next(genex->next);
  }
}
-- end genex.c

Alexander Popov


2011/12/6 Edward Z. Yang <ezyang <at> mit.edu>
Hello Alexander,

Your best bet is to make a Haskell functions which gives access to
the head and tail of a list (the head should be converted into a C-friendly
form) and then export them as functions which are callable from C.

http://www.haskell.org/haskellwiki/GHC/Using_the_FFI#Calling_Haskell_from_C

Edward

Excerpts from Alexander.Vladislav.Popov's message of Mon Dec 05 03:08:59 -0500 2011:
> Hi, Haskellers.
>
> Advise me please, how I can export lazy and potentially infinite list of
> string from Haskell program. I think I must call it iteratively: the first
> call initiate some structure and other calls iterate over it, something
> like pair of function `find_first' and `find_next'. And how to marshall
> this structure between programs. Or think in a wrong way? Does any example
> exist how I can make it?
>
> Alexander Popov

<div>
<p>Hello Edward,</p>
<div><br></div>
<div>Would you help me to map this:</div>
<div><br></div>
<div>-- genexlib.hs</div>
<div>
<div>{-# LANGUAGE BangPatterns, ForeignFunctionInterface #-}</div>
<div><br></div>
<div>module GenexLib where</div>
<div><br></div>
<div>import Regex.Genex</div>
<div>import System.IO</div>
<div>import System.Environment</div>
<div><br></div>
<div>data CLazyList a = Empty | CLL !a [a]</div>
<div><br></div>
<div>instance (Show a) =&gt; Show (CLazyList a) where</div>
<div>&nbsp; show Empty = "Empty"</div>
<div>&nbsp; show (CLL x xs) = show x ++ ":.."</div>
<div><br></div>
<div>next (CLL _ []) &nbsp; &nbsp; = Empty</div>
<div>next (CLL _ (x:xs)) = CLL x xs</div>
<div><br></div>
<div>fromList [] &nbsp; &nbsp; = Empty</div>
<div>fromList (x:xs) = CLL x xs</div>
<div><br></div>
<div>open = fromList . genexPure</div>
<div>-- end genexlib.hs</div>
<div><br></div>
<div>to</div>
<div><br></div>
<div>-- genex.c</div>
<div>
<div>typedef struct CLL {</div>
<div>
&nbsp; unsigned char empty;</div>
<div>&nbsp; char &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*current;</div>
<div>&nbsp; struct CLL &nbsp; &nbsp;*next;</div>
<div>} CLL;</div>
<div><br></div>
<div>CLL *open(const char *regex);</div>
<div>CLL *next(const CLL &nbsp;*cll);</div>
<div><br></div>
<div>// void printf(const char *s, ...);</div>
<div><br></div>
<div>void usage() {</div>
<div>&nbsp; CLL *genex = open("\\d+");</div>
<div>&nbsp; while(!genex-&gt;empty) {</div>
<div>&nbsp; &nbsp; printf(genex-&gt;current);</div>
<div>&nbsp; &nbsp; genex = next(genex-&gt;next);</div>
<div>&nbsp; }</div>
<div>}</div>
</div>
<div>-- end genex.c</div>
<div><br></div>
<div>Alexander Popov</div>
<div><br></div>
<br><div class="gmail_quote">2011/12/6 Edward Z. Yang <span dir="ltr">&lt;<a href="mailto:ezyang <at> mit.edu">ezyang <at> mit.edu</a>&gt;</span><br><blockquote class="gmail_quote">Hello Alexander,<br><br>
Your best bet is to make a Haskell functions which gives access to<br>
the head and tail of a list (the head should be converted into a C-friendly<br>
form) and then export them as functions which are callable from C.<br><br><a href="http://www.haskell.org/haskellwiki/GHC/Using_the_FFI#Calling_Haskell_from_C" target="_blank">http://www.haskell.org/haskellwiki/GHC/Using_the_FFI#Calling_Haskell_from_C</a><br><br>
Edward<br><br>
Excerpts from Alexander.Vladislav.Popov's message of Mon Dec 05 03:08:59 -0500 2011:<br><div class="HOEnZb"><div class="h5">&gt; Hi, Haskellers.<br>
&gt;<br>
&gt; Advise me please, how I can export lazy and potentially infinite list of<br>
&gt; string from Haskell program. I think I must call it iteratively: the first<br>
&gt; call initiate some structure and other calls iterate over it, something<br>
&gt; like pair of function `find_first' and `find_next'. And how to marshall<br>
&gt; this structure between programs. Or think in a wrong way? Does any example<br>
&gt; exist how I can make it?<br>
&gt;<br>
&gt; Alexander Popov<br>
</div></div>
</blockquote>
</div>
<br>
</div>
</div>
Edward Z. Yang | 15 Dec 23:19 2011
Picon

Re: FFI export lazy list of string

I did a writeup for an even simpler example, which hopefully will give you the
right idea how to do it in your case:

    http://blog.ezyang.com/2011/12/accessing-lazy-structures-from/

Cheers,
Edward

Alexander.Vladislav.Popov | 19 Dec 17:41 2011
Picon

Re: FFI export lazy list of string

Hi, Edward.


Thank you very much for your help. Very concisely.Russian say: brevity is the sister of talent. I made my own naive solution which based on VoidEx's advise. In Russian, but I think, you'll see the idea. It has some noise such as CLazyList what is no more than code atavism from previous versions.

Happy hacking, too!
Alexander.
 
2011/12/16 Edward Z. Yang <ezyang <at> mit.edu>
I did a writeup for an even simpler example, which hopefully will give you the
right idea how to do it in your case:

   http://blog.ezyang.com/2011/12/accessing-lazy-structures-from/

Cheers,
Edward

<div>
<p>Hi, Edward.</p>
<div><br></div>
<div>Thank you very much for your help. Very concisely.Russian say: brevity is the sister of talent. I made my own naive&nbsp;<a href="http://rsdn.ru/forum/decl/4543044.1.aspx">solution</a>&nbsp;which based on&nbsp;<a href="http://rsdn.ru/forum/decl/4541766.1.aspx">VoidEx's advise</a>. In Russian, but I think, you'll see the idea. It has some noise such as CLazyList what is no more than code atavism from previous versions.</div>
<div><br></div>
<div>Happy hacking, too!</div>
<div>Alexander.</div>
<div>&nbsp;<br><div class="gmail_quote">2011/12/16 Edward Z. Yang <span dir="ltr">&lt;<a href="mailto:ezyang <at> mit.edu">ezyang <at> mit.edu</a>&gt;</span><br><blockquote class="gmail_quote">
I did a writeup for an even simpler example, which hopefully will give you the<br>
right idea how to do it in your case:<br><br>
 &nbsp; &nbsp;<a href="http://blog.ezyang.com/2011/12/accessing-lazy-structures-from/" target="_blank">http://blog.ezyang.com/2011/12/accessing-lazy-structures-from/</a><br><br>
Cheers,<br>
Edward<br>
</blockquote>
</div>
<br>
</div>
</div>

Gmane