caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Functional scanning facilities
@ 2002-07-12  1:36 Alessandro Baretta
  2002-07-12  2:43 ` John Prevost
  0 siblings, 1 reply; 7+ messages in thread
From: Alessandro Baretta @ 2002-07-12  1:36 UTC (permalink / raw)
  To: Ocaml

How about something like the follwing--the idea coming from 
posts I read a while ago about CPS programming:

module Scan : sig

(* type 'a set *)

type ('a, 'b) conversion  = ( 'b -> 'a ) -> 'a

(* the following signatures refer to stdin scanners *)
val s  : (string -> 'a) -> 'a
val d  : (int    -> 'a) -> 'a
val i  : (int    -> 'a) -> 'a
val o  : (int    -> 'a) -> 'a
val x  : (int    -> 'a) -> 'a
val f  : (float  -> 'a) -> 'a
val c  : int -> (string -> 'a) -> 'a
(* val set: char set -> (string -> 'a) -> 'a *)

type ('a, 'b) scanner = ( 'b -> 'a ) -> ('a, 'c) scanner
val nil :  (unit, unit) scanner

val (+)  : 'a scanner -> ('a, 'b) conversion -> 'a scanner

val get  : ('a, 'b) scanner -> 'a list

end

Basically, a scanner is a fifo of conversions. Scanners are 
built by +ing a scanner (left operand) and a conversion 
(right operand). The fifo must be initialized with a nil 
scanner, containing no conversions. After a scanner has been 
built, it must be applied self-recursively to a succession 
of parameters, each representing a callback for a conversion 
  in the fifo of the scanner. Because a scanner has a 
recursive type, the result of its application to a callback 
is a scanner with one less convertions in its fifo. As the 
convertions are performed, each the result of each callback 
is appended to a list maintained internally by the scanner. 
Finally, when all conversions have been performed, the 'a 
list built by the scanner can be gotten with the get function.

Of course, all this works in my best dreams. The scanner 
type cannot be defined as I have because of the unbound type 
parameter 'c. But there are work arounds, although not 
terribly elegant. For example, we could define a big variant
type callback_parameter = String of string | Int of int | 
Float of float | ...
but this would greatly reduce the beauty of such a solution.

At 3:36 in the morning I can do no better than this. Pierre, 
Xavier, your turn.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-07-12 11:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-12  1:36 [Caml-list] Functional scanning facilities Alessandro Baretta
2002-07-12  2:43 ` John Prevost
2002-07-12  7:24   ` Pierre Weis
2002-07-12  7:55     ` Basile STARYNKEVITCH
2002-07-12  7:58     ` John Prevost
2002-07-12  8:25     ` Stefano Zacchiroli
2002-07-12 11:16       ` Pierre Weis

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).