caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Didier Remy <remy@morgon.inria.fr>
To: Tyng-Ruey Chuang <trc@iis.sinica.edu.tw>
Cc: caml-list@inria.fr,
	Jean-Christophe.Filliatre@lri.fr (Jean-Christophe Filliatre)
Subject: Re: [Caml-list] Weird types
Date: 18 Jun 2001 14:15:21 +0200	[thread overview]
Message-ID: <m38d782t25i.fsf@morgon.inria.fr> (raw)
In-Reply-To: Tyng-Ruey Chuang's message of "Mon, 18 Jun 2001 16:04:32 +0800 (CST)"

Tyng-Ruey Chuang <trc@iis.sinica.edu.tw> writes:

> Jean-Christophe FILLIATRE wrote:
> > Actually, there  is a  type-able way of  writing this  function, which
> > consists in duplicating it into two functions, like this:
> > 
> > ======================================================================
> > type ('a,'b,'c) t =
> >   | A of 'a * 'b * 'c
> >   | B of ('b, 'a, 'c) t
> > 
> > let rec gamma = function
> >   | A _ -> 0
> >   | B x -> 1 + gamma' x
> > 
> > and gamma' = function
> >   | A _ -> 0
> >   | B x -> 1 + gamma x
> > ======================================================================
> > 
> > which gives the expected types:
> > 
> > ======================================================================
> > val gamma : ('a, 'b, 'c) t -> int = <fun>
> > val gamma' : ('a, 'b, 'c) t -> int = <fun>
> > ======================================================================
> 
> Interesting! But then size of the duplicated code grows exponentially.

Actually, you can share a little more: 

        let gamma_body gamma = function 
           | A _ -> 0
           | B x -> 1 + gamma x

        let rec gamma  x = gamma_body gamma' x 
            and gamma' x = gamma_body gamma  x;;
        ;;

and a for three parameters: 

        type ('a, 'b, 'c) sigma = 
          | I of 'a * 'b * 'c 
          | T of ('b, 'a, 'c) sigma
          | P of ('b, 'c, 'a) sigma

        let body gT gP = function
           | I _ -> 0
           | T x -> 1 + gT x
           | P x -> 1 + gP x

        let rec abc s = body bac bca s
            and acb s = body cab cba s
            and bac s = body abc acb s
            and bca s = body cba cab s
            and cab s = body acb abc s
            and cba s = body bca bac s
        ;;

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


  reply	other threads:[~2001-06-19  7:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-15 23:36 Berke Durak
2001-06-16 17:37 ` Tyng-Ruey Chuang
2001-06-18  7:14   ` Jean-Christophe Filliatre
2001-06-18  8:04     ` Tyng-Ruey Chuang
2001-06-18 12:15       ` Didier Remy [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-06-15 19:35 Berke Durak

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=m38d782t25i.fsf@morgon.inria.fr \
    --to=remy@morgon.inria.fr \
    --cc=Jean-Christophe.Filliatre@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=trc@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).