caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* lazy evaluation of combinator parsers
@ 2007-03-24  1:28 Jeffrey Loren Shaw
  2007-03-24  6:48 ` [Caml-list] " skaller
  0 siblings, 1 reply; 2+ messages in thread
From: Jeffrey Loren Shaw @ 2007-03-24  1:28 UTC (permalink / raw)
  To: caml-list

Hello fellow Camlers, 

I'm trying to write a combinator parser in ocaml that uses lazy evaluation. 
My goal is that it won't get stuck on infinite loops. So for instance I want 
to be able to parse something like (in regex syntax) 

a* 

let alt p1 p2 xs = append (p1 xs) (p2 xs) 

(* regex ? *)
let opt a = alt a epsilon 

let rec recseq a =
 seq
   a
   (recseq a) 

(* regex * *)
let rec star a =
 recseq (opt a) 

However, "star (symbol 'a')" causes a stack overflow. I'm not sure why, 
since my seq and alt functions are lazy. seq depends on fold_right, map and 
append, and alt depends on append. fold_right, map and append are all lazy. 
The definitions of alt and seq contain no Lazy.forces. 

sources: 

http://www.msu.edu/~shawjef3/combparslazy3.ml
http://www.msu.edu/~shawjef3/lazylist3.ml 

I would greatly appreciate any help! 

Jeff


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] lazy evaluation of combinator parsers
  2007-03-24  1:28 lazy evaluation of combinator parsers Jeffrey Loren Shaw
@ 2007-03-24  6:48 ` skaller
  0 siblings, 0 replies; 2+ messages in thread
From: skaller @ 2007-03-24  6:48 UTC (permalink / raw)
  To: Jeffrey Loren Shaw; +Cc: caml-list

On Fri, 2007-03-23 at 21:28 -0400, Jeffrey Loren Shaw wrote:

> let alt p1 p2 xs = append (p1 xs) (p2 xs) 
> 
> (* regex ? *)
> let opt a = alt a epsilon 
> 
> let rec recseq a =
>  seq
>    a
>    (recseq a) 
> 
> (* regex * *)
> let rec star a =
>  recseq (opt a) 
> 
> However, "star (symbol 'a')" causes a stack overflow. I'm not sure why, 
> since my seq and alt functions are lazy. 

Arguments are evaluated right to left eagerly, so

let rec recseq a = seq a (recseq a) 

is instantly an infinite loop. seq is not called here,
it's second argument diverges.



-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-03-24  6:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-24  1:28 lazy evaluation of combinator parsers Jeffrey Loren Shaw
2007-03-24  6:48 ` [Caml-list] " skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).