caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <markus@oefai.at>
To: "Jojgov, G.I." <g.i.jojgov@TUE.nl>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Translation between datatypes with binding
Date: Mon, 5 Jul 2004 18:39:43 +0200	[thread overview]
Message-ID: <20040705163943.GA8625@fichte.ai.univie.ac.at> (raw)
In-Reply-To: <9F38CF35D80CAE409B979F3EB5242B4A014B1CCF@winex2.campus.tue.nl>

On Mon, 05 Jul 2004, Jojgov, G.I. wrote:
> I am trying to use OCAML as a logical framework to implement two systems
> with binding and to define a translation from one to the other. Ideally,
> I would like to be able to write code like this

It may be more convenient to use polymorphic variants here (see
below for example), because you can keep the code more abstract.
Another possibility would be to use type functors.  To make the latter
convenient, this would however require the use of the compiler flag
"-rectypes" so I won't go into details.

Here is my solution using polymorphic variants:

---------------------------------------------------------------------------
let rec trans trans_id inv_trans = function
  | `SomeValue id -> `SomeValue (trans_id id)
  | `SomeBinder (id, body) ->
      let trans_body v = inv_trans (body (trans trans_id inv_trans v)) in
      `SomeBinder (trans_id id, trans_body)

let rec trans_1_2 expr1 = trans string_of_int trans_2_1 expr1
and trans_2_1 expr2 = trans int_of_string trans_1_2 expr2
---------------------------------------------------------------------------

> (this is a simplified version of the real datatypes and the translation
> that I want to use)

It's not correct: you also need a conversion function from expr2 to
expr1 in order to implement translations for higher-order stuff (i.e.
functions like "body").  Objects won't help here either, because they,
too, cannot have a model for the following in the general case (you'd
always be stuck with a finite amount of translations):

  val invert : ('a -> 'b) -> ('b -> 'a)

> My questions are: Is there a natural way to define a function like the
> translation function above on datatypes? Is there another way to
> implement these structures? I guess the only solution is to use classes,
> but I already have some code dealing with the two concrete types and
> preferably I would like to reuse the code.

If you can't do something with algebraic datatypes, you won't be able
to do it with objects either.  If you are not afraid of higher-order
functions, code reuse is also easy with algebraic datatypes.

Regards,
Markus

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@oefai.at

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


      reply	other threads:[~2004-07-05 16:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-05 14:36 Jojgov, G.I.
2004-07-05 16:39 ` Markus Mottl [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040705163943.GA8625@fichte.ai.univie.ac.at \
    --to=markus@oefai.at \
    --cc=caml-list@inria.fr \
    --cc=g.i.jojgov@TUE.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).