caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tyng-Ruey Chuang <trc@iis.sinica.edu.tw>
To: athena@fftw.org (Matteo Frigo)
Cc: caml-list@inria.fr, trc@iiserv.iis.sinica.edu.tw
Subject: Re: [Caml-list] Problem with polymorphic letrec
Date: Mon, 24 Sep 2001 14:25:35 +0800 (CST)	[thread overview]
Message-ID: <200109240625.f8O6PZg09586@mail.iis.sinica.edu.tw> (raw)
In-Reply-To: <m3d74qhmf8.fsf@glauke.fftw.org> from "Matteo Frigo" at Sep 16, 2001 05:10:19 PM

> ... Ignoring the base case of the recursion and the details of the
> algorithm, I therefore need to code something like
> 
> let rec polymul mul p q =
>    polymul (polymul mul) (fun _ -> p) (fun _ -> q)
> 
> This expression does not typecheck in ocaml 2 because polymul is used
> polymorphically within its own body.
> 
> I could not come up with any hack to code polymul without changing the
> representation of polynomials, which I'd rather not change for other
> reasons.  
> 
> Any suggestion on how to work around this problem?
> 
> Thanks in advance,
> Matteo Frigo

If you are certain the definition of a polymorphically recursive
function f is type-safe, you can use "(Obj.magic f)" in the
body of f to coerce the inferred type of f into one that is
not constrained, so the definition of f can be type-checked.

Note that Obj.magic has type

	Obj.magic: 'a -> 'b

so it in fact bypasses the type system. Use with care. :-)

Still I find it useful when defining map functions
of "nested datatypes" like Nested.map below.

Have fun!

Tyng-Ruey

---------

module Pair =
struct
  type 'a t = 'a * 'a
  let map f (x, y) = (f x, f y) 
end

module Node =
struct 
  type ('a, 'b) t = Nil | Cons of 'a * 'b
  let map (f, g) x =
      match x with
            Nil -> Nil
          | Cons (x, y) -> Cons (f x, g y)
end 

module Nested = 
struct
  type 'a t = Rec of ('a, 'a Pair.t t) Node.t

  let rec map f (Rec x) =
      Rec (Node.map (f, Obj.magic map (Pair.map f)) x)   (*** HERE ***)
end                                                                          

let n0 = Nested.Rec  Node.Nil
let n1 = Nested.Rec (Node.Cons (((1,2),(3,4)), n0))
let n2 = Nested.Rec (Node.Cons ((1,2), n1))
let n3 = Nested.Rec (Node.Cons (1, n2))

let double x = x + x
let square x = x * x

(* Some test cases *)

let d3 = Nested.map double n3
let f3 = Nested.map string_of_int  (Nested.map square n3)

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


      parent reply	other threads:[~2001-09-24  6:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-16 21:10 Matteo Frigo
2001-09-19 19:10 ` Chris Quinn
2001-09-24  6:25 ` Tyng-Ruey Chuang [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=200109240625.f8O6PZg09586@mail.iis.sinica.edu.tw \
    --to=trc@iis.sinica.edu.tw \
    --cc=athena@fftw.org \
    --cc=caml-list@inria.fr \
    --cc=trc@iiserv.iis.sinica.edu.tw \
    /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).