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

accessing fields in disjoin union types or algebraic types

Expand Messages
  • Jean Saint-Remy
    Hi, I have researched the topic a bit more in the literature. Jason Hickey describes it in the Ocaml Book under disjoin union types, but some details are left
    Message 1 of 1 , Apr 16, 2013
    • 0 Attachment
      Hi,

      I have researched the topic a bit more in the literature. Jason Hickey describes it in the Ocaml Book

      under disjoin union types, but some details are left out. I believe I can frame my question more accurately now.

      Cheech and Chong made millions from royalties on their counter-culture movies, and seeing that Detroit was getting
      run down, they have decided to open an auto parts store. They rented a small warehouse that came with all the shelves
      and fixtures, refurbished the front entrance, added a few lounge chairs, big screen TV, a counter space, several phone
      lines, and two computers. They didn't want to pay thousands of dollars every month for a database "license", so instead
      they hired two guys that can program in OCaml. On opening day, they were assigned a task of creating a database.

      type auto_parts_db =

        | Inventory of entry
        | Price of price
        | Available of demand
      and
        entry =

        {

           descr : string;
           part_num : int;
           mfg_num : int;
          second_mkt : int;
          manufacturer : string;

          model : string;
          year : int;
          door_type : int;
      }
      and
        price =
        {

           list_price : float;
           markup : float;
           discount : float;
          }
      and
         demand =

         {

            num_purchased : int;
            num_sold : int;
            rank : float;
           profit : float;
           available : bool;

        }

      ;;
      For the time being there are no statistics about demand, which is fine. We can populate each set of fields

      with just one constructor.

      let water_pump =

      Inventory
      {
          descr = "two cycle water pump";
          part_num = 10013486;
          mfg_num = 50018732;
          second_mkt = 9991255;
          manufacturer = "Toyota genuine";
          model = "Corolla";
          year = 2007;
          door_type = 2;
      } ;;

      let water_pump =
      Price
      {
          list_price = 128.99;
          markup = 15.00;
          discount = 0.10;
      } ;;

      Now, you cannot access the record field
      water_pump.list_price ;;
      -: Error, the expected type was entry, but here used with float


      Jason Hickey however gives this example

      type db_entry =

       {
           name : string;
           height : int;
           phone : string;
           salary : float;
      }
      let jason = { name = "Jason"; height = 172; phone = "0113324362857; salary = 102385.15; } ;;

      If we want Jason's phone number


      jason.phone ;;

      -: val string = "0113324362857"

      The difference between the two declarations is the constructor. Simple indirection works, not so for the water pump.
      This is a key concept in OCaml. Jason goes right away into binary trees, recursive types, and data structures. Does not

      develop the topic beyond the simple example. In OCaml core this is described as an extension to the language. Is it true

      the onlyaccess to the fields is by way of matching?


      With kind regards,

      Jean

      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.