Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Christopher L Conway" <cconway@cs.nyu.edu>
To: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Induction over types
Date: Thu, 31 Jan 2008 13:18:57 -0500	[thread overview]
Message-ID: <4a051d930801311018k6aa4b8bdge32400c6a36b9b78@mail.gmail.com> (raw)
In-Reply-To: <47A20799.4030706@wp.pl>

Dawid,

Your example seems to require dependent types (e.g., "nested lists of
depth k") or runtime type inspection. Neither is directly supported by
OCaml (although there might be experimental extensions out there that
do what you want). You might be interested the "Scrap Your
Boilerplate" approach which is available in the latest versions of
Haskell (http://www.cs.vu.nl/boilerplate). I think this would
accomplish what you're asking. But it's implementation and use is
non-trivial.

Chris

On Jan 31, 2008 12:38 PM, Dawid Toton <d0@wp.pl> wrote:
> I want to write a polymorphic function that invokes itself recursively,
> but with different types.
> For example I'd like to translate the following pseudo-code into OCaml:
>
> let rec deep_rev (lst:'a list) = List.rev (List.map (deep_rev:'a->'a) lst)
> let deep_rev (element:'a) = element  (* initial induction step *)
>
> let rec super_wrap (depth:int) (lst:'a list) =
>   match depth with
>    | 0 -> lst
>    | d -> super_wrap (d+1) [lst]
>
> And how can I have arbitrary transposition:
>
> val transpose: int list -> 'a list -> 'a list
>
> # transpose [2;0;1]
>   [[[1;2];[3;4]]
>   ;[[5;6];[7;8]]
>   ]
> - : int list list list = [[[1; 3]; [5; 7]]; [[2; 4]; [6; 8]]]
>
> Do I have to have separate functions:
>
> val transpose1: int list -> 'a list -> 'a list   (* identity *)
> val transpose2: int list -> 'a list list -> 'a list list
> val transpose3: int list -> 'a list list list -> 'a list list list
> ...
>
> But this is huge amount of redundant code!
> Here is for example my transpose3:
> http://www.toton.2-0.pl/OCaml/transpose.ml
>
> Dawid Toton
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


  reply	other threads:[~2008-01-31 18:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-31 17:38 Dawid Toton
2008-01-31 18:18 ` Christopher L Conway [this message]
2008-01-31 21:43   ` [Caml-list] " Dirk Thierbach
2008-02-01 21:31     ` Christophe TROESTLER

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=4a051d930801311018k6aa4b8bdge32400c6a36b9b78@mail.gmail.com \
    --to=cconway@cs.nyu.edu \
    --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