caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Problem extending the Standard library
@ 2014-08-27 14:52 Yotam Barnoy
  2014-08-27 14:59 ` Leo White
  2014-08-27 15:04 ` Pierrick Couderc
  0 siblings, 2 replies; 4+ messages in thread
From: Yotam Barnoy @ 2014-08-27 14:52 UTC (permalink / raw)
  To: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1055 bytes --]

I've recently tried to extend the Map in the standard library with extra
functionality. This is what I've tried doing:

In a file my_map.ml:

module type S =
  sig
    include Map.S
    val find_lt: key -> 'a t -> 'a
    val find_gt: key -> 'a t -> 'a
  end

module Make(Ord: Map.OrderedType) = struct
  include Map.Make(Ord)

  let find_almost move_f x n =
    let rec loop lastval n =
      match n, lastval with
      | Empty, None   -> raise Not_found
      | Empty, Some i -> i
      | Node(l, v, d, r, _) ->
          let c = Ord.compare x v in
          let lastval', next = move_f c lastval l d r in
          loop lastval' next
  in loop None n

  let move_lt c lastval l d r =
    if c <= 0 then lastval, l else Some d, r

  let move_gt c lastval l d r =
    if c < 0  then Some d, l else lastval, r

  let find_gt x n = find_almost move_gt x n
  let find_lt x n = find_almost move_lt x n
end

Can anyone tell me why the compiler complained about Empty being an unbound
constructor when it's declared in Map.Make, which I include?

Thanks
Yotam

[-- Attachment #2: Type: text/html, Size: 1674 bytes --]

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

* Re: [Caml-list] Problem extending the Standard library
  2014-08-27 14:52 [Caml-list] Problem extending the Standard library Yotam Barnoy
@ 2014-08-27 14:59 ` Leo White
  2014-08-27 15:01   ` Yotam Barnoy
  2014-08-27 15:04 ` Pierrick Couderc
  1 sibling, 1 reply; 4+ messages in thread
From: Leo White @ 2014-08-27 14:59 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: Ocaml Mailing List

>
> Can anyone tell me why the compiler complained about Empty being an unbound constructor when it's declared in Map.Make,
> which I include?
>

I don't think `Empty` is declared in the interface of Map.Make, it may
be declared in the implementation but it is not available in the
interface. Making it available in the interface would break abstraction:
it would force `Map.Make` to keep using that particular implementation.

Regards,

Leo

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

* Re: [Caml-list] Problem extending the Standard library
  2014-08-27 14:59 ` Leo White
@ 2014-08-27 15:01   ` Yotam Barnoy
  0 siblings, 0 replies; 4+ messages in thread
From: Yotam Barnoy @ 2014-08-27 15:01 UTC (permalink / raw)
  To: Leo White; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 606 bytes --]

Good point -- wasn't looking at the map.mli file. Thanks.

Yotam


On Wed, Aug 27, 2014 at 10:59 AM, Leo White <lpw25@cam.ac.uk> wrote:

> >
> > Can anyone tell me why the compiler complained about Empty being an
> unbound constructor when it's declared in Map.Make,
> > which I include?
> >
>
> I don't think `Empty` is declared in the interface of Map.Make, it may
> be declared in the implementation but it is not available in the
> interface. Making it available in the interface would break abstraction:
> it would force `Map.Make` to keep using that particular implementation.
>
> Regards,
>
> Leo
>

[-- Attachment #2: Type: text/html, Size: 1009 bytes --]

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

* Re: [Caml-list] Problem extending the Standard library
  2014-08-27 14:52 [Caml-list] Problem extending the Standard library Yotam Barnoy
  2014-08-27 14:59 ` Leo White
@ 2014-08-27 15:04 ` Pierrick Couderc
  1 sibling, 0 replies; 4+ messages in thread
From: Pierrick Couderc @ 2014-08-27 15:04 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1420 bytes --]

Actually, the type of "+'a t" is not exported in Map.S, therefore it cannot
be used directly, even when including the module (or I am mistaking the
include semantics).
Le 27 août 2014 16:54, "Yotam Barnoy" <yotambarnoy@gmail.com> a écrit :

> I've recently tried to extend the Map in the standard library with extra
> functionality. This is what I've tried doing:
>
> In a file my_map.ml:
>
> module type S =
>   sig
>     include Map.S
>     val find_lt: key -> 'a t -> 'a
>     val find_gt: key -> 'a t -> 'a
>   end
>
> module Make(Ord: Map.OrderedType) = struct
>   include Map.Make(Ord)
>
>   let find_almost move_f x n =
>     let rec loop lastval n =
>       match n, lastval with
>       | Empty, None   -> raise Not_found
>       | Empty, Some i -> i
>       | Node(l, v, d, r, _) ->
>           let c = Ord.compare x v in
>           let lastval', next = move_f c lastval l d r in
>           loop lastval' next
>   in loop None n
>
>   let move_lt c lastval l d r =
>     if c <= 0 then lastval, l else Some d, r
>
>   let move_gt c lastval l d r =
>     if c < 0  then Some d, l else lastval, r
>
>   let find_gt x n = find_almost move_gt x n
>   let find_lt x n = find_almost move_lt x n
> end
>
> Can anyone tell me why the compiler complained about Empty being an
> unbound constructor when it's declared in Map.Make, which I include?
>
> Thanks
> Yotam
>

[-- Attachment #2: Type: text/html, Size: 2210 bytes --]

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

end of thread, other threads:[~2014-08-27 15:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-27 14:52 [Caml-list] Problem extending the Standard library Yotam Barnoy
2014-08-27 14:59 ` Leo White
2014-08-27 15:01   ` Yotam Barnoy
2014-08-27 15:04 ` Pierrick Couderc

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