caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Writing Awk in OCaml
@ 2013-07-28  8:43 Michael
  2013-07-29 12:40 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: Michael @ 2013-07-28  8:43 UTC (permalink / raw)
  To: caml-list

Hi list!

I am trying to write a Awk in OCaml and would like to write it as a  
variadic function.  I have seen several examples of variadic functions  
in OCaml, but I am still unsure if it is possible. Maybe can someone  
help me to solve this?


DEFINITION OF (MINI)-AWK

(Mini)-Awk is a module with the following (probably wrong) signature:

sig
   type 'a field
   type row = string array
   val make : int -> (string -> 'a) -> 'a field
   val get : 'a field -> row -> 'a
   val bind : (row -> 'a -> 'b) -> 'a field -> row -> 'b
end

With this signature [make 1 int_of_string] if a field [f] such that  
[get f row = int_of_string (row.(1))].

Now assume you have two fields f1 : int field and f2 : string field  
and two functions u : int -> state -> state and v : int -> string ->  
state -> state (where state is a random type).

With the above signature, we can write (1)

bind u f1: row -> state -> stat
bind (bind v f1) f2 : row -> state -> stat

But it would be quite nice to be able to write the following  
expressions so that they are defined and correctly type (2):

bind u f1 : row -> state -> state
bind v f1 f2 : row -> state -> state

Is there any way to rewrite things so that the cumbersome (1) can be  
replaced by the slick (2) ?

Thank you for your insight!
Michael

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

* Re: [Caml-list] Writing Awk in OCaml
  2013-07-28  8:43 [Caml-list] Writing Awk in OCaml Michael
@ 2013-07-29 12:40 ` Xavier Leroy
  2013-07-30 17:57   ` Michael
  0 siblings, 1 reply; 3+ messages in thread
From: Xavier Leroy @ 2013-07-29 12:40 UTC (permalink / raw)
  To: Michael; +Cc: caml-list

On 2013-07-28 10:43, Michael wrote:
> With the above signature, we can write (1)
> 
> bind u f1: row -> state -> stat
> bind (bind v f1) f2 : row -> state -> stat
> 
> But it would be quite nice to be able to write the following expressions so
> that they are defined and correctly type (2):
> 
> bind u f1 : row -> state -> state
> bind v f1 f2 : row -> state -> state
> 
> Is there any way to rewrite things so that the cumbersome (1) can be replaced
> by the slick (2) ?

Just use a left-associative infix symbol for "bind", it will be real slick:

    let (++) = bind

    v ++ f1 ++ f2     (* i.e. bind (bind  v f1) f2  *)

Hope this helps,

- Xavier Leroy

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

* Re: [Caml-list] Writing Awk in OCaml
  2013-07-29 12:40 ` Xavier Leroy
@ 2013-07-30 17:57   ` Michael
  0 siblings, 0 replies; 3+ messages in thread
From: Michael @ 2013-07-30 17:57 UTC (permalink / raw)
  To: Xavier Leroy; +Cc: caml-list

Hello Xavier,

Xavier Leroy wrote:
> On 2013-07-28 10:43, Michael wrote:
>> With the above signature, we can write (1)
>>
>> bind u f1: row -> state -> stat
>> bind (bind v f1) f2 : row -> state -> stat
>>
>> But it would be quite nice to be able to write the following expressions so
>> that they are defined and correctly type (2):
>>
>> bind u f1 : row -> state -> state
>> bind v f1 f2 : row -> state -> state
>>
>> Is there any way to rewrite things so that the cumbersome (1) can be replaced
>> by the slick (2) ?
> Just use a left-associative infix symbol for "bind", it will be real slick:
>
>     let (++) = bind
>
>     v ++ f1 ++ f2     (* i.e. bind (bind  v f1) f2  *)
>
> Hope this helps,
>
thank you very much for your answer, it is nearly perfect!  It has
however a drawback: I thought I had found a plausible alibi to learn how
to produce and use these strange variadics!  It looks like I have to
find something else. :)

Best regards,
Michael


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

end of thread, other threads:[~2013-07-30 17:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-28  8:43 [Caml-list] Writing Awk in OCaml Michael
2013-07-29 12:40 ` Xavier Leroy
2013-07-30 17:57   ` Michael

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