caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: keiko@kaba.or.jp
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] polymorphic recursion with type constraint?
Date: Mon, 07 Feb 2005 13:32:48 +0900 (JST)	[thread overview]
Message-ID: <20050207.133248.51779961.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20050207.121707.74656685.keiko@kaba.or.jp>

From: nakata keiko <keiko@kaba.or.jp>
> 
> Can I make the following function 'subst' well-behave,
> so that it can be used in the contexts where:
> 1) 'p' (the first argument) is assumed of type 'path', and 
>   'env' (the first argument)is assumed of type '(string * tau) list;
> and 
> 2)'p' is assumed of type 'tau', and 
>   'env' is assumed of type '(string * tau) list;
> 
> 
>     type tau = [`Root | `Apply of path * tau |`Var of string]  
>     and  path = [`Root | `Apply of path * tau] 
>     
>     let rec subst p env = 
>     	 match p with 
>     	    `Root -> `Root
>     	  | `Apply (p1, p2) -> `Apply(subst p1 env , subst p2 env)
>     	  | `Var s -> List.assoc s env
> 
> Yet 'subst' is well-typed, 
> 
>     let sub (p : path) (env : (string * tau)) = subst p env
> 
> fails.

To make the problem setting more explicit, you want to use the above
function as both
   tau -> (string * tau) list -> tau
and
   path -> (string * tau) list -> path

Independently of recursion problems, this is impossible because usual
variant typing assumes that all branches in a pattern matching will be
used, so that the typing will at least be
   path -> (string * tau) list -> tau
due to the `Var case.

Maximal sharing of code is obtained by the following program:

let rec subst_path env = function
    `Root -> `Root
  | `Apply (p1, p2) -> `Apply (subst_path env p1, subst_tau env p2)
and subst_tau env = function
    #path as p -> (subst_path env p :> tau)
  | `Var s -> List.assoc s env

Your first program would be typable assuming both "conditional typing
of branches" and polymorphic recursion. The second is available
inderectly, but the first was only implemented in an experimental
version of the compiler.

Jacques Garrigue


      reply	other threads:[~2005-02-07  4:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-07  3:17 nakata keiko
2005-02-07  4:32 ` Jacques Garrigue [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=20050207.133248.51779961.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=keiko@kaba.or.jp \
    /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).