Loading ...
Sorry, an error occurred while loading the content.

3281Re: "ocaml_beginners"::[] Modules

Expand Messages
  • William D.Neumann
    May 1, 2005
      On May 1, 2005, at 9:50 AM, jonboy3182 wrote:

      > How do I have multiple implementations of a module use one interface
      > (without using Objects). For instance:
      >
      > Interface: Tree
      > Implementations: BinaryTree, AvlTree, RedBlackTree, AATree

      There are a couple of ways you can do this, e.g. functors would work,
      but they're likely overkill for this. The best way is to probably use
      module types. Here's a really quick example using a couple of terrible
      implementations of stacks:

      module type BadStack =
      sig

      type 'a t
      exception Empty
      exception Full

      val empty : 'a t
      val push : 'a t -> 'a -> 'a t
      val pop : 'a t -> 'a * 'a t
      val isEmpty : 'a t -> bool
      end;;

      module BadListStack =
      struct
      type 'a t = 'a list
      exception Empty
      exception Full

      let empty = []

      let push s v = v :: s

      let pop = function
      [] -> raise Empty
      | h::t -> h,t

      let isEmpty = function
      | [] -> true
      | _ -> false
      end;;

      module BadArrayStack =
      struct
      type 'a t = { stack : 'a array; count : int}
      exception Empty
      exception Full

      let empty = { stack = [||]; count = 0}

      let push s v =
      if s.stack = [||] then { stack = Array.make 100 v; count = 1} else
      if s.count > 99 then raise Full else
      (s.stack.(s.count) <- v; { s with count = s.count + 1 })

      let pop s =
      if s.count = 0 then raise Empty else
      s.stack.(s.count - 1),{ s with count = s.count - 1}

      let isEmpty s =
      s.count = 0
      end;;

      module BSA = (BadListStack : BadStack);;
      module BSL = (BadArrayStack : BadStack);;

      # let s1 = BSL.empty;;
      val s1 : 'a BSL.t = <abstr>
      # let s1 = BSL.push s1 10;;
      val s1 : int BSL.t = <abstr>
      # let s1 = BSL.push s1 20;;
      val s1 : int BSL.t = <abstr>
      # let s1 = BSL.push s1 30;;
      val s1 : int BSL.t = <abstr>
      # BSL.isEmpty s1;;
      - : bool = false
      # let v,s1 = BSL.pop s1;;
      val v : int = 30
      val s1 : int BSL.t = <abstr>
      # let v,s1 = BSL.pop s1;;
      val v : int = 20
      val s1 : int BSL.t = <abstr>
      # let v,s1 = BSL.pop s1;;
      val v : int = 10
      val s1 : int BSL.t = <abstr>
      # let v,s1 = BSL.pop s1;;
      Exception: BadListStack.Empty.
      # BSL.isEmpty s1;;
      - : bool = true

      # let s2 = BSA.empty;;
      val s2 : 'a BSA.t = <abstr>
      # let s2 = BSA.push s2 10;;
      val s2 : int BSA.t = <abstr>
      # let s2 = BSA.push s2 20;;
      val s2 : int BSA.t = <abstr>
      # let s2 = BSA.push s2 30;;
      val s2 : int BSA.t = <abstr>
      # BSA.isEmpty s2;;
      - : bool = false
      # let v,s2 = BSA.pop s2;;
      val v : int = 30
      val s2 : int BSA.t = <abstr>
      # let v,s2 = BSA.pop s2;;
      val v : int = 20
      val s2 : int BSA.t = <abstr>
      # let v,s2 = BSA.pop s2;;
      val v : int = 10
      val s2 : int BSA.t = <abstr>
      # let v,s2 = BSA.pop s2;;
      Exception: BadArrayStack.Empty.
      # BSA.isEmpty s2;;
      - : bool = true

      It's a pretty ugly example, so let me know if you have more questions.
      For more information, read chapter 2 of the OCaml manual.

      William D. Neumann

      "You've got Rita Marlowe in the palm of your hand."
      "Palm of my hand? You haven't seen Rita Marlowe..."

      -- Will Success Spoil Rock Hunter?
    • Show all 17 messages in this topic