caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* any types in signatures of functor arguments
@ 2005-10-24  8:51 Keiko Nakata
  2005-10-24 14:08 ` [Caml-list] " Brian Hurt
  2005-10-25 12:15 ` Hendrik Tews
  0 siblings, 2 replies; 7+ messages in thread
From: Keiko Nakata @ 2005-10-24  8:51 UTC (permalink / raw)
  To: caml-list

Hello.

Why I cannot have *any types* in signatures of functor arguments?
Concretely, the following F is not typable.

module F(X:sig type t = [`A of _ ] val f : t -> int end) = 
  struct
    let f = function `A x as a -> X.f a | `B -> 0
  end

The above F could be made typable as in

module F(X:sig type s type t = [`A of s ] val f : t -> int end) = 
  struct
    let f = function `A x as a -> X.f a | `B -> 0
  end

However, this change requires me to add the type component s
to every module to which F are going to be applied.
This is not very nice...

Regards,
Keiko Nakata




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] any types in signatures of functor arguments
  2005-10-24  8:51 any types in signatures of functor arguments Keiko Nakata
@ 2005-10-24 14:08 ` Brian Hurt
  2005-10-25  2:22   ` Keiko Nakata
  2005-10-25 12:15 ` Hendrik Tews
  1 sibling, 1 reply; 7+ messages in thread
From: Brian Hurt @ 2005-10-24 14:08 UTC (permalink / raw)
  To: Keiko Nakata; +Cc: caml-list



On Mon, 24 Oct 2005, Keiko Nakata wrote:

> Hello.
>
> Why I cannot have *any types* in signatures of functor arguments?
> Concretely, the following F is not typable.
>
> module F(X:sig type t = [`A of _ ] val f : t -> int end) =
>  struct
>    let f = function `A x as a -> X.f a | `B -> 0
>  end

I think the problem is that `A holds something, but you're not giving 
Ocaml any idea of what.  For example, the following compiles:

module F(X:sig type 'a t = [`A of 'a ] val f : 'a t -> int end) =
   struct
     let f = function `A x as a -> X.f a | `B -> 0
   end

Hope this helps.

Brian


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] any types in signatures of functor arguments
  2005-10-24 14:08 ` [Caml-list] " Brian Hurt
@ 2005-10-25  2:22   ` Keiko Nakata
  2005-10-25  8:53     ` Virgile Prevosto
  0 siblings, 1 reply; 7+ messages in thread
From: Keiko Nakata @ 2005-10-25  2:22 UTC (permalink / raw)
  To: bhurt; +Cc: caml-list

From: Brian Hurt <bhurt@spnz.org>
> I think the problem is that `A holds something, but you're not giving 
> Ocaml any idea of what.  For example, the following compiles:
> 
> module F(X:sig type 'a t = [`A of 'a ] val f : 'a t -> int end) =
>    struct
>      let f = function `A x as a -> X.f a | `B -> 0
>    end

I see...

So it seems that I cannot avoid modifying modules to which F is applied.
Actually, I already written several modules, say M and N, in separate files.
Later, I defined the functor F so as to apply it to the modules M and N.
I thought that it would be nice if I can avoid modifying 
the implementations and interface files of M and N,
since it will affect other parts of my program.

Thanks for the reply.

Regards,
Keiko Nakata






 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] any types in signatures of functor arguments
  2005-10-25  2:22   ` Keiko Nakata
@ 2005-10-25  8:53     ` Virgile Prevosto
  2005-10-25  9:49       ` Keiko Nakata
  0 siblings, 1 reply; 7+ messages in thread
From: Virgile Prevosto @ 2005-10-25  8:53 UTC (permalink / raw)
  To: caml-list

Le mar 25 oct 2005 11:22:20 CEST, Keiko Nakata <keiko@kurims.kyoto-
u.ac.jp> a ecrit:

> So it seems that I cannot avoid modifying modules to which F is
> applied. Actually, I already written several modules, say M and N, in
> separate files. Later, I defined the functor F so as to apply it to
> the modules M and N. I thought that it would be nice if I can avoid
> modifying the implementations and interface files of M and N,
> since it will affect other parts of my program.
> 

It might be possible to only modify the arguments you give to F, with
the use of include:

module M = struct let f (`A x) = x end

module F(X:sig type s type t = [ `A of s] val f: t -> int end) =
  struct let f = function `A _ as a -> X.f a | `B -> 0
  end;;

module FM = F(struct include M type s = int type t = [ `A of s ])

-- 
E tutto per oggi, a la prossima volta
Virgile


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] any types in signatures of functor arguments
  2005-10-25  8:53     ` Virgile Prevosto
@ 2005-10-25  9:49       ` Keiko Nakata
  2005-10-25 10:08         ` fva en UMBRIEL
  0 siblings, 1 reply; 7+ messages in thread
From: Keiko Nakata @ 2005-10-25  9:49 UTC (permalink / raw)
  To: virgile.prevosto; +Cc: caml-list


From: Virgile Prevosto <virgile.prevosto@m4x.org>

> It might be possible to only modify the arguments you give to F, with
> the use of include:
> 
> module M = struct let f (`A x) = x end
> 
> module F(X:sig type s type t = [ `A of s] val f: t -> int end) =
>   struct let f = function `A _ as a -> X.f a | `B -> 0
>   end;;
> 
> module FM = F(struct include M type s = int type t = [ `A of s ])

Thanks for the reply.

I should describe my program in more detail.
What I want to do would be summarized in the following program.
(Suppose that modules Int, String, and Subst are in separate files,
and have many more type definitions and functions actually.)

module Int = struct
  type mexp = [`Str of item list | `Var of string]
  and item = [`Mod of mexp | `Term of int]
  type env = (string * mexp) list
  let subst env name = List.assoc name env
end

module String = struct
  type mexp = [`Str of item list | `Var of string]
  and item = [`Mod of mexp | `Term of string]
  type env = (string * mexp) list
  let subst env name = List.assoc name env
end

module Subst (X: sig 
  type mexp = [`Str of item list | `Var of string]
  and item = [`Mod of mexp | `Term of _ ]
  type env = (string * mexp) list
  val subst : env -> string -> mexp end) = struct 
    let rec subst_mexp env = function
	`Str items -> `Str (List.map (subst_item env) items)
      | `Var name -> X.subst env name
    and subst_item env = function
	`Mod mexp -> `Mod (subst_mexp env mexp)
      | `Term t -> `Term t
  end
    

One may say that I should fist define the type item0 as

type ('a,'b) item0 = [`Mod of 'a | `Term of 'b ]

then define type mexp and item as

type mexp = [`Str of item list | `Var of string]
and item = (mexp, int) item0 

This will work.
However, I cannot decide where to put the definition of item0 
when Int and String are in separate files.
Additionally, while it will not a big problem,
this encoding will make my program more verbose.



Regard,
Keiko NAKATA







^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] any types in signatures of functor arguments
  2005-10-25  9:49       ` Keiko Nakata
@ 2005-10-25 10:08         ` fva en UMBRIEL
  0 siblings, 0 replies; 7+ messages in thread
From: fva en UMBRIEL @ 2005-10-25 10:08 UTC (permalink / raw)
  To: Keiko Nakata; +Cc: caml-list

Hi,

given your problem I wonder whether the use of modules, if elegant, is 
not overkill. I recall an extensible solution to such syntactic trees 
written with polymorphic variants in one of the papers/tutorials by 
Jacques Garrigue. Perhaps you could have a look at those for inspiration.

Regards

    F. Valverde

Keiko Nakata wrote:

>Thanks for the reply.
>
>I should describe my program in more detail.
>What I want to do would be summarized in the following program.
>(Suppose that modules Int, String, and Subst are in separate files,
>and have many more type definitions and functions actually.)
>
>module Int = struct
>  type mexp = [`Str of item list | `Var of string]
>  and item = [`Mod of mexp | `Term of int]
>  type env = (string * mexp) list
>  let subst env name = List.assoc name env
>end
>
>module String = struct
>  type mexp = [`Str of item list | `Var of string]
>  and item = [`Mod of mexp | `Term of string]
>  type env = (string * mexp) list
>  let subst env name = List.assoc name env
>end
>
>module Subst (X: sig 
>  type mexp = [`Str of item list | `Var of string]
>  and item = [`Mod of mexp | `Term of _ ]
>  type env = (string * mexp) list
>  val subst : env -> string -> mexp end) = struct 
>    let rec subst_mexp env = function
>	`Str items -> `Str (List.map (subst_item env) items)
>      | `Var name -> X.subst env name
>    and subst_item env = function
>	`Mod mexp -> `Mod (subst_mexp env mexp)
>      | `Term t -> `Term t
>  end
>    
>
>One may say that I should fist define the type item0 as
>
>type ('a,'b) item0 = [`Mod of 'a | `Term of 'b ]
>
>then define type mexp and item as
>
>type mexp = [`Str of item list | `Var of string]
>and item = (mexp, int) item0 
>
>This will work.
>However, I cannot decide where to put the definition of item0 
>when Int and String are in separate files.
>Additionally, while it will not a big problem,
>this encoding will make my program more verbose.
>  
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] any types in signatures of functor arguments
  2005-10-24  8:51 any types in signatures of functor arguments Keiko Nakata
  2005-10-24 14:08 ` [Caml-list] " Brian Hurt
@ 2005-10-25 12:15 ` Hendrik Tews
  1 sibling, 0 replies; 7+ messages in thread
From: Hendrik Tews @ 2005-10-25 12:15 UTC (permalink / raw)
  To: caml-list

Keiko Nakata <keiko@kurims.kyoto-u.ac.jp> writes:

   Why I cannot have *any types* in signatures of functor arguments?
   Concretely, the following F is not typable.
   
   module F(X:sig type t = [`A of _ ] val f : t -> int end) = 
     struct
       let f = function `A x as a -> X.f a | `B -> 0
     end
   

The problem is rather that your type abbreviation is illegal:

  # type t = [`A of _ ];;
  A type variable is unbound in this type declaration.
  In case `A of 'a the variable 'a is unbound

Use 

  module F(X:sig val f : [`A of _ ] -> int end) = 
    struct
      let f = function `A x as a -> X.f a | `B -> 0
  end
   
or a type abbreviation that works. Either 

  type 'a t = [`A of _ ] as 'a;;

or 

  type 'a t = [`A of _ as 'a ];;

Where the latter is the same as the already proposed

  type 'a t = [ `A of 'a ];;

Bye,

Hendrik


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-10-25 12:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-24  8:51 any types in signatures of functor arguments Keiko Nakata
2005-10-24 14:08 ` [Caml-list] " Brian Hurt
2005-10-25  2:22   ` Keiko Nakata
2005-10-25  8:53     ` Virgile Prevosto
2005-10-25  9:49       ` Keiko Nakata
2005-10-25 10:08         ` fva en UMBRIEL
2005-10-25 12:15 ` Hendrik Tews

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).