Version française
Home     About     Download     Resources     Contact us    
Browse thread
any types in signatures of functor arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Keiko Nakata <keiko@k...>
Subject: Re: [Caml-list] any types in signatures of functor arguments

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