Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Jojgov, G.I." <g.i.jojgov@TUE.nl>
To: <caml-list@inria.fr>
Subject: [Caml-list] Translation between datatypes with binding
Date: Mon, 5 Jul 2004 16:36:48 +0200	[thread overview]
Message-ID: <9F38CF35D80CAE409B979F3EB5242B4A014B1CCF@winex2.campus.tue.nl> (raw)

Hello,

I am relatively new to the OCAML programming, so my question may be
trivial, but did not find related messages in the list archive and I
would appreciate any tips. 

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


type
	expr1 =
	 SomeValue1 of int |
	 SomeBinder1 of int * (expr1 -> expr1);;
type
	expr2 = 
	 SomeValue2 of string |
	 SomeBinder2 of string * (expr2 -> expr2);;

let rec translate (term:expr1) : expr2 =
	match term with
		SomeValue1 x -> SomeValue2 (string_of_int x) |
		SomeBinder1 (x,body) -> SomeBinder2 ((string_of_int
x),(function (v:expr2) -> (translate (body v))))
;;


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

Of course, this code is not type-correct because "(function (v:expr2) ->
(translate (body v)))" is not. If expr1 and expr2 could "behave" like
classes I could have defined a common base class, say "expr", and then
redefine the binders as

	...
	 SomeBinder1 of int * (expr -> expr1);;
	...
	 SomeBinder2 of string * (expr -> expr2);;

and then the code would be "type-correct".


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.


Thanks in advance for your comments,

Georgi


-------------------
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 14:36 UTC|newest]

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

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=9F38CF35D80CAE409B979F3EB5242B4A014B1CCF@winex2.campus.tue.nl \
    --to=g.i.jojgov@tue.nl \
    --cc=caml-list@inria.fr \
    /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