|
List Info
Thread: "ocaml_beginners"::[] Module signatures: unmatched
|
|
| "ocaml_beginners"::[] Module
signatures: unmatched |
  Portugal |
2007-05-08 02:53:29 |
|
Hello,
I have a tuple defined so:
type symb_id = int
and arity = int
and typ = int
and key = Term2.term_t * symb_id * arity * typ
and would like to use that as a key for a set. To do this I used:
module Key_set = Set.Make (struct
type t = key
let compare a b =
let a, sa, aa, ta = a in
let b, sb, ab, tb = b
in
if a == b &&
sa == sb &&
aa == ab &&
ta == tb then 0
else if a != b then a - b
else if sa != sb then sa -sb
else if aa != ab then aa - ab
else ta - tb
end)
but the compiler complains that:
the Set.OrderedType signature of
val compare : t -> t -> int
does not match:
val compare : int * int * int * int -> int * int * int * int -> int
So I want Set.OrderedType.t to be the same as my key. How do I declare
this? I know I have to use something that adds:
(SET with type key = Set.OrderedType.t.t)
but I cannot get the syntax correct.
TIA,
Hugo Ferreira.
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Module signatures: unmatched |
  United Kingdom |
2007-05-08 03:13:19 |
|
On Tuesday 08 May 2007 08:53, Hugo Ferreira wrote:
> So I want Set.OrderedType.t to be the same as my key. How do I declare
> this? I know I have to use something that adds:
>
> (SET with type key = Set.OrderedType.t.t)
>
> but I cannot get the syntax correct.
I think Dave Benjamin explained that on this list a few days ago:
On Thursday 03 May 2007 22:36, Dave Benjamin wrote:
> On Thu, 3 May 2007, Fabrice Marchant wrote:
> >> I had to add a "with" constraint in order to create any nested TreeSets:
> >> ...
> >
> > David, please, could you show me the code you used to test this
> > structure, in order to teach me ?
>
> First, I created an element of type Tree.t:
>
> # let elt = Tree.Content (5, TreeSet.empty);;
> val elt : Tree.t = Tree.Content (5, <abstr>)
>
> This worked as expected. Next, I tried adding this element to an empty
> TreeSet:
>
> # let set = TreeSet.add elt TreeSet.empty;;
> Characters 22-25:
> let set = TreeSet.add elt TreeSet.empty;;
> ^^^
> This expression has type Tree.t but is here used with type TreeSet.elt
>
> The problem is that OCaml does not know that Tree.t is the same as
> TreeSet.elt. As a result, TreeSet.elt is abstract; we have no way to
> create values for that type.
Here:
> By rewriting the type of the TreeSet module to read "Set.S with type elt =
> Tree.t", we inform OCaml that these types are in fact the same. Now, the
> above example works:
>
> # let elt = Tree.Content (5, TreeSet.empty);;
> val elt : Tree.t = Tree.Content (5, <abstr>)
> # let set = TreeSet.add elt TreeSet.empty;;
> val set : TreeSet.t = <abstr>
>
> And we can now construct a new element containing this TreeSet.t:
>
> # let elt' = Tree.Content (5, set);;
> val elt' : Tree.t = Tree.Content (5, <abstr>)
>
> Dave
HTH!
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Module signatures: unmatched |
  Portugal |
2007-05-08 03:49:36 |
|
Hello,
Jon Harrop wrote:
> On Tuesday 08 May 2007 08:53, Hugo Ferreira wrote:
>> So I want Set.OrderedType.t to be the same as my key. How do I declare
>> this? I know I have to use something that adds:
>>
>> (SET with type key = Set.OrderedType.t.t)
>>
>> but I cannot get the syntax correct.
>
> I think Dave Benjamin explained that on this list a few days ago:
>
Unfortunately my diagnostic was incorrect. The problem is a simple
typing problem. My tuple is:
type symb_id = int
and arity = int
and typ = int
and key = Term2.term_t * symb_id * arity * typ
but Term2.term_t is not an int... hence the error.
Thanks for the help,
Hugo F.
> On Thursday 03 May 2007 22:36, Dave Benjamin wrote:
>> On Thu, 3 May 2007, Fabrice Marchant wrote:
>>>> I had to add a "with" constraint in order to create any nested TreeSets:
>>>> ...
>>> David, please, could you show me the code you used to test this
>>> structure, in order to teach me ?
>> First, I created an element of type Tree.t:
>>
>> # let elt = Tree.Content (5, TreeSet.empty);;
>> val elt : Tree.t = Tree.Content (5, <abstr>)
>>
>> This worked as expected. Next, I tried adding this element to an empty
>> TreeSet:
>>
>> # let set = TreeSet.add elt TreeSet.empty;;
>> Characters 22-25:
>> let set = TreeSet.add elt TreeSet.empty;;
>> ^^^
>> This expression has type Tree.t but is here used with type TreeSet.elt
>>
>> The problem is that OCaml does not know that Tree.t is the same as
>> TreeSet.elt. As a result, TreeSet.elt is abstract; we have no way to
>> create values for that type.
>
> Here:
>
>> By rewriting the type of the TreeSet module to read "Set.S with type elt =
>> Tree.t", we inform OCaml that these types are in fact the same. Now, the
>> above example works:
>>
>> # let elt = Tree.Content (5, TreeSet.empty);;
>> val elt : Tree.t = Tree.Content (5, <abstr>)
>> # let set = TreeSet.add elt TreeSet.empty;;
>> val set : TreeSet.t = <abstr>
>>
>> And we can now construct a new element containing this TreeSet.t:
>>
>> # let elt' = Tree.Content (5, set);;
>> val elt' : Tree.t = Tree.Content (5, <abstr>)
>>
>> Dave
>
> HTH!
>
__._,_.___
.
__,_._,___
|
| Re Module signatures: unmatched |
  Moldova, Republic of |
2007-05-08 07:13:03 |
|
Shalom, Hugo.
HF> Unfortunately my diagnostic was incorrect. The problem
HF> is a simple typing problem. My tuple is:
HF>
HF> type symb_id = int
HF> and arity = int
HF> and typ = int
HF> and key = Term2.term_t * symb_id * arity * typ
HF>
HF> but Term2.term_t is not an int... hence the error.
btw, I saw "typ1 - typ2" or similar expression in your
original code. It's not the right way to compare integers
(for example, "max_int - min_int = -1", but it doesn't mean
that "max_int < min_int").
The best way is to use simple Pervasives.compare
as comparing function for values of type key, if
values of type Term2.term_t can be compared by
Pervasives.compare.
Otherwise, if you have function for comparing values
of type Term2.term_t (suppose some term2_compare), you can
write something like this:
value key_compare (te1,s1,a1,ty1) (te2,s2,a2,ty2) =
let r = term2_compare te1 te2 in
if r <> 0 then r else
let r = Pervasives.compare s1 s2 in
if r <> 0 then r else
let r = Pervasives.compare a1 a2 in
if r <> 0 then r else
Pervasives.compare ty1 ty2 in
;
(code duplication is here for performance reasons)
Or, if term2_compare return not arbitrary
positive/negative integers but small enough values
(1, -1 and 0 are ok, exact range -- no more than
(max_int-7)/8 by absolute value), and best performance
is not required, you can write shorter code:
value key_compare (te1,s1,a1,ty1) (te2,s2,a2,ty2) =
8*(term2_compare te1 te2)
+ 4*(Pervasives.compare s1 s2)
+ 2*(Pervasives.compare a1 a2)
+ (Pervasives.compare ty1 ty2)
;
--
WBR,
dmitry mailto: gds-mlsts%40moldavcable.com">gds-mlsts moldavcable.com
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Module signatures: unmatched |
  Portugal |
2007-05-08 08:28:05 |
|
Hello Dmitry,
dmitry grebeniuk wrote:
> Shalom, Hugo.
>
> HF> Unfortunately my diagnostic was incorrect. The problem
> HF> is a simple typing problem. My tuple is:
> HF>
> HF> type symb_id = int
> HF> and arity = int
> HF> and typ = int
> HF> and key = Term2.term_t * symb_id * arity * typ
> HF>
> HF> but Term2.term_t is not an int... hence the error.
>
> btw, I saw "typ1 - typ2" or similar expression in your
> original code. It's not the right way to compare integers
> (for example, "max_int - min_int = -1", but it doesn't mean
> that "max_int < min_int").
>
I see. I was assuming the integers would be small values and did
not think of this. Better safe than sorry.
> The best way is to use simple Pervasives.compare
> as comparing function for values of type key, if
> values of type Term2.term_t can be compared by
> Pervasives.compare.
>
It is a simple variant and I have confirmed that indeed it works
with Pervasives.compare (I had originally converted these to integer
values, no need for this).
> Otherwise, if you have function for comparing values
> of type Term2.term_t (suppose some term2_compare), you can
> write something like this:
>
> value key_compare (te1,s1,a1,ty1) (te2,s2,a2,ty2) =
> let r = term2_compare te1 te2 in
> if r <> 0 then r else
> let r = Pervasives.compare s1 s2 in
> if r <> 0 then r else
> let r = Pervasives.compare a1 a2 in
> if r <> 0 then r else
> Pervasives.compare ty1 ty2 in
> ;
> (code duplication is here for performance reasons)
>
Ok, makes sense. I was explicitly checking if "x != y" and
then returned the difference, so I was needlessly repeating
"comparisons".
> Or, if term2_compare return not arbitrary
> positive/negative integers but small enough values
> (1, -1 and 0 are ok, exact range -- no more than
> (max_int-7)/8 by absolute value), and best performance
> is not required, you can write shorter code:
>
> value key_compare (te1,s1,a1,ty1) (te2,s2,a2,ty2) =
> 8*(term2_compare te1 te2)
> + 4*(Pervasives.compare s1 s2)
> + 2*(Pervasives.compare a1 a2)
> + (Pervasives.compare ty1 ty2)
> ;
>
I think I will stick the first version. It is clearer.
Thanks,
Hugo F.
__._,_.___
.
__,_._,___
|
[1-5]
|
|