I'm getting an error trying to pass types through a functor.  The code 
compiles fine if I just make the types explicit.  However, if I try to 
uncomment out the parts that try to use the type sent across as the 
parameter to the functor I get the following error:

     val it : unit = ()
     ### evaluating file /alice/dict2.aml
     def.aml:60.25-60.43: mismatch on application: expression type
        ''26 * (key1type, (key2type, valtype ref) dictionary ref) 
dictionary
     does not match function's argument type
        ''26 * (''26, '27) dictionary
     because type
        ''26
     does not unify with
        key1type

I'm not sure why a generic type ("26) wouldn't match the specific key1type?
Here's the code that compiles (basically a two-level dictionary).

signature DICTIONARY2 =
  sig
     type key1type
     type key2type
     type valtype
     val get : key1type * key2type -> valtype option
     val put : key1type * key2type * valtype -> unit
  end

signature DICTIONARY2TYPES =
  sig
     type key1type
     type key2type
     type valtype
  end

functor Dictionary2 (Typ : DICTIONARY2TYPES) :> DICTIONARY2 (* where 
type key1type = Typ.key1type
                                                              where type 
key2type = Typ.key2type
                                                              where type 
valtype = Typ.valtype *) =
  struct
     type key1type = string (* Typ.key1type *)
     type key2type = int    (* Typ.key2type *)
     type valtype  = real   (* Typ.valtype  *)

     datatype ('a, 'b) dictionary = Tab of ('a, 'b) dictionary ref
                                  | Tree of 'a * 'b * ('a, 'b) 
dictionary ref
                                  | Leaf

     fun make_table () = Tab(ref Leaf) : ('a, 'b) dictionary
     val table = make_table() : (key1type, (key2type, valtype ref) 
dictionary ref) dictionary

     fun assoc (key, Tab xs) = assoc(key, !xs)
       | assoc (key, Leaf) = Leaf
       | assoc (key, record as (Tree(k, v, xs))) =
           if key = k
              then record
              else assoc(key, !xs)
     fun lookup (key, table) =
        let
           val record = assoc(key, table)
        in
           case record of
              Tree(k, v, _) => SOME (!v)
            | _ => NONE
        end
     fun insert (key, value, table) =
        let
           val record = assoc(key, table)
        in
           case record of
              Tree(k, v, _) => (v := value)
            | _ =>
               case table of
                  Tab xs => (xs := Tree(key, ref value, ref (!xs)))
                | _ => raise Domain
        end

     fun get (key1, key2) =
        let
           val record = assoc(key1, table)
        in
           case record of
                Tree(k1, v, _) => lookup(key2, !v)
              | _ => NONE
        end
     fun put (key1, key2, value) =
        let
           val record = assoc(key1, table)
        in
           case record of
              Tree(k, v, _) => insert(key2, value, !v)
            | _ =>
               case table of
                  Tab xs =>
                    let
                       val newtab = make_table() : (key2type, valtype 
ref) dictionary
                    in
                       insert(key2, value, newtab);
                       xs := Tree(key1, ref newtab, ref (!xs))
                    end
                | _ => raise Domain
        end
  end



To see the error just change the part with the comments to:

functor Dictionary2 (Typ : DICTIONARY2TYPES) :> DICTIONARY2 where type 
key1type = Typ.key1type
                                                              where type 
key2type = Typ.key2type
                                                              where type 
valtype = Typ.valtype =
  struct
     type key1type = Typ.key1type
     type key2type = Typ.key2type
     type valtype  = Typ.valtype

Thanks,
Chris Rathman

(Apologies for dumping code, but I couldn't find a reduced way to 
replicate the problem).



_______________________________________________
alice-users mailing list
[email protected]
http://www.ps.uni-sb.de/mailman/listinfo/alice-users

Reply via email to