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

Phantom Types.

Expand Messages
  • vamsika2004
    We have to build a binary tree data structure for out software. And on the tree we distinguish nodes into either ordinary nodes or handles. type node = Leaf of
    Message 1 of 1 , Nov 2, 2005
      We have to build a binary tree data structure for out software. And on
      the tree we distinguish nodes into either ordinary nodes or handles.

      type node = Leaf of int * int | Interior of int * int * int * int

      This node is for unrooted leaf-labelled binary trees. Now to
      distinguish nodes between ordinary nodes and handles, we came up with
      the following solution using phantom types (based on an email in this
      list):

      in the .mli file:

      type 'p t

      type node =
      Leaf of int * int
      | Interior of int * int * int * int
      | Single of int

      val get_node : IntMap.key -> tree -> [`Node] t
      val get_handle : IntSet.elt -> tree -> [`Handle] t
      val break_edge : [`Handle] t * [`Node] t -> tree -> tree

      in the .ml file:

      let get_node id t =
      IntMap.find id t.topology

      let get_handle id t =
      if IntSet.mem id t.handles then
      IntMap.find id t.topology
      else raise Illegal_argument

      Is this a reasonable way to use Phantom Types to distinguish or "type"
      nodes. Also is there a "Gentle Introduction to Phantom Types" kind of
      document available? (Googling has proved unsuccessful so far!).

      Thanks,
      --Vamsi
    Your message has been successfully submitted and would be delivered to recipients shortly.