List Info

Thread: "ocaml_beginners"::[] Module signatures: unmatched




"ocaml_beginners"::[] Module signatures: unmatched
country flaguser name
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
country flaguser name
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);;
&gt; val elt : Tree.t = Tree.Content (5, <abstr&gt;)
>
> This worked as expected. Next, I tried adding this element to an empty
&gt; TreeSet:
>
>; # let set = TreeSet.add elt TreeSet.empty;;
&gt; Characters 22-25:
&gt; let set = TreeSet.add elt TreeSet.empty;;
&gt; ^^^
> 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&quot;, we inform OCaml that these types are in fact the same. Now, the
> above example works:
&gt;
> # let elt = Tree.Content (5, TreeSet.empty);;
&gt; val elt : Tree.t = Tree.Content (5, <abstr&gt;)
> # let set = TreeSet.add elt TreeSet.empty;;
&gt; val set : TreeSet.t = <abstr&gt;
>
> And we can now construct a new element containing this TreeSet.t:
>
&gt; # let elt' = Tree.Content (5, set);;
&gt; val elt' : Tree.t = Tree.Content (5, <abstr&gt;)
>
> 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
country flaguser name
Portugal
2007-05-08 03:49:36

Hello,

Jon Harrop wrote:
&gt; On Tuesday 08 May 2007 08:53, Hugo Ferreira wrote:
&gt;> 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:
&gt;>
&gt;> (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.

&gt; On Thursday 03 May 2007 22:36, Dave Benjamin wrote:
&gt;> On Thu, 3 May 2007, Fabrice Marchant wrote:
&gt;>>> I had to add a "with" constraint in order to create any nested TreeSets:
>>&gt;> ...
>>> David, please, could you show me the code you used to test this
>;>> structure, in order to teach me ?
>&gt; First, I created an element of type Tree.t:
>>
>> # let elt = Tree.Content (5, TreeSet.empty);;
&gt;> val elt : Tree.t = Tree.Content (5, <abstr&gt;)
>>
>> This worked as expected. Next, I tried adding this element to an empty
&gt;> TreeSet:
>>
>> # let set = TreeSet.add elt TreeSet.empty;;
&gt;> Characters 22-25:
&gt;> let set = TreeSet.add elt TreeSet.empty;;
&gt;> ^^^
>> 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
>&gt; TreeSet.elt. As a result, TreeSet.elt is abstract; we have no way to
>&gt; create values for that type.
&gt;
> Here:
&gt;
>>; By rewriting the type of the TreeSet module to read "Set.S with type elt =
>&gt; Tree.t&quot;, we inform OCaml that these types are in fact the same. Now, the
>> above example works:
&gt;>
&gt;> # let elt = Tree.Content (5, TreeSet.empty);;
&gt;> val elt : Tree.t = Tree.Content (5, <abstr&gt;)
>> # let set = TreeSet.add elt TreeSet.empty;;
&gt;> val set : TreeSet.t = <abstr&gt;
>&gt;
>&gt; And we can now construct a new element containing this TreeSet.t:
>>
>> # let elt' = Tree.Content (5, set);;
&gt;> val elt' : Tree.t = Tree.Content (5, <abstr&gt;)
>>
>> Dave
>;
> HTH!
>;

__._,_.___
.

__,_._,___
Re Module signatures: unmatched
country flaguser name
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&gt;
HF> type symb_id = int
HF&gt; and arity = int
HF&gt; and typ = int
HF&gt; and key = Term2.term_t * symb_id * arity * typ
HF&gt;
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&quot;).

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-mlstsmoldavcable.com

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Module signatures: unmatched
country flaguser name
Portugal
2007-05-08 08:28:05

Hello Dmitry,

dmitry grebeniuk wrote:
&gt; Shalom, Hugo.
&gt;
> 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.
&gt;
> 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&quot;).
>;

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
&gt; of type Term2.term_t (suppose some term2_compare), you can
> write something like this:
&gt;
> 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&quot;.

> Or, if term2_compare return not arbitrary
> positive/negative integers but small enough values
&gt; (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:
&gt;
> 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]

about | contact  Other archives ( Real Estate discussion Medical topics )