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

accessing record types

Expand Messages
  • Jean Saint-Remy
    Hi, I am working on a no-frills database application, and wanted to better understand the nullary constructors.  There are some examples in the literature,
    Message 1 of 6 , Apr 15, 2013
      Hi,

      I am working on a no-frills database application, and wanted to better understand the nullary constructors.
       There are some examples in the literature, but the details are scarce. Most of the time, we don't access types
      directly. They are declared as memory containers so that data can be "safely" allocated, and we use data structures
      throughout.


      type db =
        | Person of entry
        | Weekly of sched
      and entry =

        {
            first_name : string;
            last_name : string;
        }
      and sched =

         {
             date : float;
             starting : float;
             ending : float;
        } ;;
      let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
      and week1 : db = Weekly { date = 1355062480.; starting =  1355103310.; ending = 1355122250.; } ;;

      The molly and week1 statements can also be written separately. That would be currying.


      There is also an idiomatic short-hand form

      let molly = Person { first_name = "Molly"; last_name = "Peterson"; } ;;

      The type engine understands that there are two fields of type string.


      It seems we are creating an enumerated type, and these are declared just like records with (expr : type) syntax.To access the fields, can we use molly.first_name or molly.last_name besides writing a function "match x with"
      to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
      long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
      a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
      faith, the types are declared and then we just work with data structures with types being mere place holders.

      Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
      need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
      a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
      with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
      not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?

      With kind regards,

      Jean


      [Non-text portions of this message have been removed]
    • Gabriel Scherer
      Hi, It is rather difficult to know what you are asking for. I don t think there is any link between the (expr : type) syntax and any of what you describe in
      Message 2 of 6 , Apr 15, 2013
        Hi,

        It is rather difficult to know what you are asking for. I don't think
        there is any link between the (expr : type) syntax and any of what you
        describe in your email (in fact this syntax is mostly useless and
        extremely rarely used). I also have no idea what you mean by "nullary
        constructor" in your example. In my book a nullary constructor might
        be (I don't think I've seen the name used consistently) a variant
        constructor with no parameter

        type t =
        | Foo of int
        | Bar (* nullary ? *)

        which you have not used in your example.

        The language manual is intended as a language reference more than a
        comprehensive introduction for beginners. The website page
        http://ocaml.org/books.html has some books available online. In my
        experience both Jason Hickey's "Introduction to OCaml" and the online
        book "Developping Applications with OCaml" (available both in french
        or english) are good, comprehensive resources.


        On Mon, Apr 15, 2013 at 7:43 PM, Jean Saint-Remy
        <jeansaintremy@...> wrote:
        > Hi,
        >
        > I am working on a no-frills database application, and wanted to better understand the nullary constructors.
        > There are some examples in the literature, but the details are scarce. Most of the time, we don't access types
        > directly. They are declared as memory containers so that data can be "safely" allocated, and we use data structures
        > throughout.
        >
        >
        > type db =
        > | Person of entry
        > | Weekly of sched
        > and entry =
        >
        > {
        > first_name : string;
        > last_name : string;
        > }
        > and sched =
        >
        > {
        > date : float;
        > starting : float;
        > ending : float;
        > } ;;
        > let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
        > and week1 : db = Weekly { date = 1355062480.; starting = 1355103310.; ending = 1355122250.; } ;;
        >
        > The molly and week1 statements can also be written separately. That would be currying.
        >
        >
        > There is also an idiomatic short-hand form
        >
        > let molly = Person { first_name = "Molly"; last_name = "Peterson"; } ;;
        >
        > The type engine understands that there are two fields of type string.
        >
        >
        > It seems we are creating an enumerated type, and these are declared just like records with (expr : type) syntax.To access the fields, can we use molly.first_name or molly.last_name besides writing a function "match x with"
        > to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
        > long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
        > a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
        > faith, the types are declared and then we just work with data structures with types being mere place holders.
        >
        > Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
        > need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
        > a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
        > with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
        > not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?
        >
        > With kind regards,
        >
        > Jean
        >
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >
        > ------------------------------------
        >
        > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
        > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
        > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
        >
        >
        >
      • Toby Kelsey
        ... No, currying is treating a function which can take multiple arguments as taking one. Here molly and week1 are separate values, each of type db. Usually
        Message 3 of 6 , Apr 15, 2013
          On 15/04/13 18:43, Jean Saint-Remy wrote:
          > let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
          > and week1 : db = Weekly { date = 1355062480.; starting = 1355103310.; ending = 1355122250.; } ;;
          >
          > The molly and week1 statements can also be written separately. That would be currying.

          No, currying is treating a function which can take multiple arguments as taking
          one. Here molly and week1 are separate values, each of type db.
          Usually (name : type) is used as a hint to the compiler about the type to use,
          but in this case it is not necessary as the constructors Person and Weekly tell
          it the type.
          OCaml can almost always work out the correct type without a hint. Constructors
          don't need hints.

          To access the fields, can we use molly.first_name or molly.last_name besides
          writing a function "match x with"
          > to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
          > long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
          > a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
          > faith, the types are declared and then we just work with data structures with types being mere place holders.

          There is no faith required. The compiler works out (infers) all the types, and
          if it can't it gives you a compiler error.

          > Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
          > need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
          > a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
          > with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
          > not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?

          There are some good tutorials around. To understand constructors and how to use
          'match' I would suggest you start with
          <http://ocaml.org/tutorials/data_types_and_matching.html>. If you have
          problems with specific examples post them here.

          Toby
        • Jean Saint-Remy
          I don t know how to adequately describe 2nd order of abstraction here. My apologies for the omission. Typically the type declaration does not involve
          Message 4 of 6 , Apr 15, 2013
            I don't know how to adequately describe 2nd order of abstraction here. My apologies for the omission. Typically the
            'type' declaration does not involve dangling modifier using "of". I am using Person of entry and then break it down into
            strings for first and last names, but inside a database you have a lot of non-sequteur, heterogenous collections. I want to
            understand how the type engine allows us to work with strings, numbers, booleans, etc. If you are in the car parts
            department, an auto part might have a description, manufacturer part number, secondary market part number, a boolean
            if it is in the inventory, the price may be in floats and item count in ints.

            Jean




            ________________________________
            From: Gabriel Scherer <gabriel.scherer@...>
            To: ocaml_beginners@yahoogroups.com
            Sent: Monday, April 15, 2013 3:07 PM
            Subject: Re: "ocaml_beginners"::[] accessing record types



             
            Hi,

            It is rather difficult to know what you are asking for. I don't think
            there is any link between the (expr : type) syntax and any of what you
            describe in your email (in fact this syntax is mostly useless and
            extremely rarely used). I also have no idea what you mean by "nullary
            constructor" in your example. In my book a nullary constructor might
            be (I don't think I've seen the name used consistently) a variant
            constructor with no parameter

            type t =
            | Foo of int
            | Bar (* nullary ? *)

            which you have not used in your example.

            The language manual is intended as a language reference more than a
            comprehensive introduction for beginners. The website page
            http://ocaml.org/books.html has some books available online. In my
            experience both Jason Hickey's "Introduction to OCaml" and the online
            book "Developping Applications with OCaml" (available both in french
            or english) are good, comprehensive resources.

            On Mon, Apr 15, 2013 at 7:43 PM, Jean Saint-Remy
            <jeansaintremy@...> wrote:
            > Hi,
            >
            > I am working on a no-frills database application, and wanted to better understand the nullary constructors.
            > There are some examples in the literature, but the details are scarce. Most of the time, we don't access types
            > directly. They are declared as memory containers so that data can be "safely" allocated, and we use data structures
            > throughout.
            >
            >
            > type db =
            > | Person of entry
            > | Weekly of sched
            > and entry =
            >
            > {
            > first_name : string;
            > last_name : string;
            > }
            > and sched =
            >
            > {
            > date : float;
            > starting : float;
            > ending : float;
            > } ;;
            > let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
            > and week1 : db = Weekly { date = 1355062480.; starting = 1355103310.; ending = 1355122250.; } ;;
            >
            > The molly and week1 statements can also be written separately. That would be currying.
            >
            >
            > There is also an idiomatic short-hand form
            >
            > let molly = Person { first_name = "Molly"; last_name = "Peterson"; } ;;
            >
            > The type engine understands that there are two fields of type string.
            >
            >
            > It seems we are creating an enumerated type, and these are declared just like records with (expr : type) syntax.To access the fields, can we use molly.first_name or molly.last_name besides writing a function "match x with"
            > to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
            > long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
            > a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
            > faith, the types are declared and then we just work with data structures with types being mere place holders.
            >
            > Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
            > need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
            > a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
            > with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
            > not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?
            >
            > With kind regards,
            >
            > Jean
            >
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >
            > ------------------------------------
            >
            > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
            > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
            > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
            >
            >
            >



            [Non-text portions of this message have been removed]
          • Gabriel Scherer
            I m sorry, but I still have no idea what your question is. Using a metaphor of databases to describe values in the language maybe doesn t help. On Tue, Apr 16,
            Message 5 of 6 , Apr 15, 2013
              I'm sorry, but I still have no idea what your question is. Using a
              metaphor of databases to describe values in the language maybe doesn't
              help.

              On Tue, Apr 16, 2013 at 2:50 AM, Jean Saint-Remy
              <jeansaintremy@...> wrote:
              > I don't know how to adequately describe 2nd order of abstraction here. My apologies for the omission. Typically the
              > 'type' declaration does not involve dangling modifier using "of". I am using Person of entry and then break it down into
              > strings for first and last names, but inside a database you have a lot of non-sequteur, heterogenous collections. I want to
              > understand how the type engine allows us to work with strings, numbers, booleans, etc. If you are in the car parts
              > department, an auto part might have a description, manufacturer part number, secondary market part number, a boolean
              > if it is in the inventory, the price may be in floats and item count in ints.
              >
              > Jean
              >
              >
              >
              >
              > ________________________________
              > From: Gabriel Scherer <gabriel.scherer@...>
              > To: ocaml_beginners@yahoogroups.com
              > Sent: Monday, April 15, 2013 3:07 PM
              > Subject: Re: "ocaml_beginners"::[] accessing record types
              >
              >
              >
              >
              > Hi,
              >
              > It is rather difficult to know what you are asking for. I don't think
              > there is any link between the (expr : type) syntax and any of what you
              > describe in your email (in fact this syntax is mostly useless and
              > extremely rarely used). I also have no idea what you mean by "nullary
              > constructor" in your example. In my book a nullary constructor might
              > be (I don't think I've seen the name used consistently) a variant
              > constructor with no parameter
              >
              > type t =
              > | Foo of int
              > | Bar (* nullary ? *)
              >
              > which you have not used in your example.
              >
              > The language manual is intended as a language reference more than a
              > comprehensive introduction for beginners. The website page
              > http://ocaml.org/books.html has some books available online. In my
              > experience both Jason Hickey's "Introduction to OCaml" and the online
              > book "Developping Applications with OCaml" (available both in french
              > or english) are good, comprehensive resources.
              >
              > On Mon, Apr 15, 2013 at 7:43 PM, Jean Saint-Remy
              > <jeansaintremy@...> wrote:
              >> Hi,
              >>
              >> I am working on a no-frills database application, and wanted to better understand the nullary constructors.
              >> There are some examples in the literature, but the details are scarce. Most of the time, we don't access types
              >> directly. They are declared as memory containers so that data can be "safely" allocated, and we use data structures
              >> throughout.
              >>
              >>
              >> type db =
              >> | Person of entry
              >> | Weekly of sched
              >> and entry =
              >>
              >> {
              >> first_name : string;
              >> last_name : string;
              >> }
              >> and sched =
              >>
              >> {
              >> date : float;
              >> starting : float;
              >> ending : float;
              >> } ;;
              >> let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
              >> and week1 : db = Weekly { date = 1355062480.; starting = 1355103310.; ending = 1355122250.; } ;;
              >>
              >> The molly and week1 statements can also be written separately. That would be currying.
              >>
              >>
              >> There is also an idiomatic short-hand form
              >>
              >> let molly = Person { first_name = "Molly"; last_name = "Peterson"; } ;;
              >>
              >> The type engine understands that there are two fields of type string.
              >>
              >>
              >> It seems we are creating an enumerated type, and these are declared just like records with (expr : type) syntax.To access the fields, can we use molly.first_name or molly.last_name besides writing a function "match x with"
              >> to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
              >> long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
              >> a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
              >> faith, the types are declared and then we just work with data structures with types being mere place holders.
              >>
              >> Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
              >> need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
              >> a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
              >> with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
              >> not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?
              >>
              >> With kind regards,
              >>
              >> Jean
              >>
              >>
              >> [Non-text portions of this message have been removed]
              >>
              >>
              >>
              >> ------------------------------------
              >>
              >> Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
              >> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
              >> Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
              >>
              >>
              >>
              >
              >
              >
              > [Non-text portions of this message have been removed]
              >
              >
              >
              > ------------------------------------
              >
              > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
              > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
              > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
              >
              >
              >
            • Rémi Vanicat
              ... I don t really understand your question, could you show us an example of something that doesn t work for us to comment on? As an explanation, I can say
              Message 6 of 6 , Apr 16, 2013
                Jean Saint-Remy <jeansaintremy@...> writes:

                > I don't know how to adequately describe 2nd order of abstraction here. My apologies for the omission. Typically the
                > 'type' declaration does not involve dangling modifier using "of". I am using Person of entry and then break it down into
                > strings for first and last names, but inside a database you have a lot of non-sequteur, heterogenous collections. I want to
                > understand how the type engine allows us to work with strings, numbers, booleans, etc. If you are in the car parts
                > department, an auto part might have a description, manufacturer part number, secondary market part number, a boolean
                > if it is in the inventory, the price may be in floats and item count in ints.
                >

                I don't really understand your question, could you show us an example of
                something that doesn't work for us to comment on?

                As an explanation, I can say that:

                type a_record = {
                foo : string;
                bar : int
                }

                define a new type that is a record (not a variant). It have two field,
                the first one named foo, containing string, the second one named bar
                containing int.

                All value of type new_type will have the very same structure, they can
                be build by { foo = "a string"; bar = 3 }, and the field can be accessed
                by the dot notation.

                This must not be mistaken with variant type like:

                type a_variant =
                | Foo of string
                | Bar of int

                Note the capital letter at the beginning of the name of both
                constructor: they are very (very) different thing than the field name of
                the record.

                Here value of type a_variant can have two different structure: either
                they are in the Foo case, and contain a string, or are in the Bar case,
                and will contain an int.

                For variant, value will be accessed using a patter matching that will:
                - Test in wich case we are, to act in a different way
                - Deconstruct the value to have the included value (here, either a
                string or an int, but not both)

                [...]


                --
                Rémi Vanicat
              Your message has been successfully submitted and would be delivered to recipients shortly.