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

how to access a type defined in one .ml file in another .ml file

Expand Messages
  • tarun_sethi4u
    Hi, I m very new to ocaml and i m facing the problem below, In a.ml a record type t is defined and is also defined transparently in a.mli, i.e. in d interface
    Message 1 of 8 , May 12, 2010
    • 0 Attachment
      Hi,

      I m very new to ocaml and i m facing the problem below,

      In a.ml a record type t is defined and is also defined transparently
      in a.mli, i.e. in d interface so that the type definition is available
      to all other files.

      a.ml also has a function, func, which returns a list of t.

      Now in another file, b.ml i m calling func, now obviously ocaml
      compiler wud nt be able to infer d type of objects stored in d list,
      for compiler its just a list. so in b.ml, i hav something like dis,

      let tlist = A.func in
      let vart = List.hd tlist in
      printf "%s\n" vart.name (*name is a field in record t*)

      Now here i get a compiler error sayin "Unbound record field label
      name" which makes sense as compiler can't infer d type of vart.

      my first question: how do I explicitly provide d type of vart as t
      here?
      i tried doing "let vart:A.t = " but got the
      same error.
      Also, i can't use A.name anywhere because "name" as a field name has been used in multiple record type definitions in a.ml. So I have to somehow mention the type name "t" with A to use type t outside a.ml.

      I also tried creating another function to fetch the first element of d
      list and mentioning return type as A.t, but then i got the "Unbound
      value A.t". I did this:

      let firstt = function
      [] -> 0
      | x :: _ -> A.t x ;;

      The problem is compiler is unable to recognize A.t (a type) in b.ml
      but is able to recognize function A.func. If I remove A.t from the
      b.ml, i don'get any compiler errors.

      Please help, its urgent work.
      Thanks in advance!

      ~Tarun
    • Chantal Keller
      Hi, You can access the field name like this : vart.A.name I think there is another mistake in your code: you said that func is a function, so after let tlist =
      Message 2 of 8 , May 12, 2010
      • 0 Attachment
        Hi,

        You can access the field name like this :

        vart.A.name

        I think there is another mistake in your code: you said that func is a
        function, so after let tlist = A.func, tlist is the same function (but
        not a list).

        To compile everything, you can do:

        ocamlc -c a.mli
        ocamlc -c a.ml
        ocamlc -c b.ml
        ocamlc -o foo a.cmo b.cmo

        (or use ocamlopt if you prefer native code).

        Hope this helps,
        Chantal.


        Le 12/05/2010 16:56, tarun_sethi4u a écrit :
        > Hi,
        >
        > I m very new to ocaml and i m facing the problem below,
        >
        > In a.ml a record type t is defined and is also defined transparently
        > in a.mli, i.e. in d interface so that the type definition is available
        > to all other files.
        >
        > a.ml also has a function, func, which returns a list of t.
        >
        > Now in another file, b.ml i m calling func, now obviously ocaml
        > compiler wud nt be able to infer d type of objects stored in d list,
        > for compiler its just a list. so in b.ml, i hav something like dis,
        >
        > let tlist = A.func in
        > let vart = List.hd tlist in
        > printf "%s\n" vart.name (*name is a field in record t*)
        >
        > Now here i get a compiler error sayin "Unbound record field label
        > name" which makes sense as compiler can't infer d type of vart.
        >
        > my first question: how do I explicitly provide d type of vart as t
        > here?
        > i tried doing "let vart:A.t = " but got the
        > same error.
        > Also, i can't use A.name anywhere because "name" as a field name has
        > been used in multiple record type definitions in a.ml. So I have to
        > somehow mention the type name "t" with A to use type t outside a.ml.
        >
        > I also tried creating another function to fetch the first element of d
        > list and mentioning return type as A.t, but then i got the "Unbound
        > value A.t". I did this:
        >
        > let firstt = function
        > [] -> 0
        > | x :: _ -> A.t x ;;
        >
        > The problem is compiler is unable to recognize A.t (a type) in b.ml
        > but is able to recognize function A.func. If I remove A.t from the
        > b.ml, i don'get any compiler errors.
        >
        > Please help, its urgent work.
        > Thanks in advance!
        >
        > ~Tarun
      • tarun_sethi4u
        I tried using vart.A.name , but it gives me syntax error. Also, in a.ml you can say there is another record type, t1 which also has a field name as name , so
        Message 3 of 8 , May 12, 2010
        • 0 Attachment
          I tried using "vart.A.name", but it gives me syntax error.

          Also, in a.ml you can say there is another record type, t1 which also has a field name as "name", so I can't use A.name anywhere as t also has "name" field and t1 too.

          Don't you think, I need to mention type "t" in syntax somewhere in b.ml? I am just looking for that right syntax.

          About the mistake you pointed out, func has expects a bool parameter which I am passing , so it becomes a function call rather than just a let binding. So its actually like this & I think its fine,

          let tlist = A.func false in

          Thanks for your prompt reply.

          ~ Tarun


          --- In ocaml_beginners@yahoogroups.com, Chantal Keller <chantal.keller@...> wrote:
          >
          > Hi,
          >
          > You can access the field name like this :
          >
          > vart.A.name
          >
          > I think there is another mistake in your code: you said that func is a
          > function, so after let tlist = A.func, tlist is the same function (but
          > not a list).
          >
          > To compile everything, you can do:
          >
          > ocamlc -c a.mli
          > ocamlc -c a.ml
          > ocamlc -c b.ml
          > ocamlc -o foo a.cmo b.cmo
          >
          > (or use ocamlopt if you prefer native code).
          >
          > Hope this helps,
          > Chantal.
          >
          >
          > Le 12/05/2010 16:56, tarun_sethi4u a écrit :
          > > Hi,
          > >
          > > I m very new to ocaml and i m facing the problem below,
          > >
          > > In a.ml a record type t is defined and is also defined transparently
          > > in a.mli, i.e. in d interface so that the type definition is available
          > > to all other files.
          > >
          > > a.ml also has a function, func, which returns a list of t.
          > >
          > > Now in another file, b.ml i m calling func, now obviously ocaml
          > > compiler wud nt be able to infer d type of objects stored in d list,
          > > for compiler its just a list. so in b.ml, i hav something like dis,
          > >
          > > let tlist = A.func in
          > > let vart = List.hd tlist in
          > > printf "%s\n" vart.name (*name is a field in record t*)
          > >
          > > Now here i get a compiler error sayin "Unbound record field label
          > > name" which makes sense as compiler can't infer d type of vart.
          > >
          > > my first question: how do I explicitly provide d type of vart as t
          > > here?
          > > i tried doing "let vart:A.t = " but got the
          > > same error.
          > > Also, i can't use A.name anywhere because "name" as a field name has
          > > been used in multiple record type definitions in a.ml. So I have to
          > > somehow mention the type name "t" with A to use type t outside a.ml.
          > >
          > > I also tried creating another function to fetch the first element of d
          > > list and mentioning return type as A.t, but then i got the "Unbound
          > > value A.t". I did this:
          > >
          > > let firstt = function
          > > [] -> 0
          > > | x :: _ -> A.t x ;;
          > >
          > > The problem is compiler is unable to recognize A.t (a type) in b.ml
          > > but is able to recognize function A.func. If I remove A.t from the
          > > b.ml, i don'get any compiler errors.
          > >
          > > Please help, its urgent work.
          > > Thanks in advance!
          > >
          > > ~Tarun
          >
        • Chantal Keller
          ... Well, that works for me... Which version of OCaml do you use? ... I think this is a mistake: the field name in t will not be accessible anyway. ... No,
          Message 4 of 8 , May 12, 2010
          • 0 Attachment
            Le 12/05/2010 17:29, tarun_sethi4u a écrit :
            > I tried using "vart.A.name", but it gives me syntax error.

            Well, that works for me... Which version of OCaml do you use?

            > Also, in a.ml you can say there is another record type, t1 which also
            > has a field name as "name", so I can't use A.name anywhere as t also has
            > "name" field and t1 too.

            I think this is a mistake: the field name in t will not be accessible
            anyway.

            > Don't you think, I need to mention type "t" in syntax somewhere in b.ml?

            No, OCaml can infer the type of vart.

            Chantal.


            > I am just looking for that right syntax.
            >
            > About the mistake you pointed out, func has expects a bool parameter
            > which I am passing , so it becomes a function call rather than just a
            > let binding. So its actually like this & I think its fine,
            >
            > let tlist = A.func false in
            >
            > Thanks for your prompt reply.
            >
            > ~ Tarun
            >
            > --- In ocaml_beginners@yahoogroups.com
            > <mailto:ocaml_beginners%40yahoogroups.com>, Chantal Keller
            > <chantal.keller@...> wrote:
            > >
            > > Hi,
            > >
            > > You can access the field name like this :
            > >
            > > vart.A.name
            > >
            > > I think there is another mistake in your code: you said that func is a
            > > function, so after let tlist = A.func, tlist is the same function (but
            > > not a list).
            > >
            > > To compile everything, you can do:
            > >
            > > ocamlc -c a.mli
            > > ocamlc -c a.ml
            > > ocamlc -c b.ml
            > > ocamlc -o foo a.cmo b.cmo
            > >
            > > (or use ocamlopt if you prefer native code).
            > >
            > > Hope this helps,
            > > Chantal.
            > >
            > >
            > > Le 12/05/2010 16:56, tarun_sethi4u a écrit :
            > > > Hi,
            > > >
            > > > I m very new to ocaml and i m facing the problem below,
            > > >
            > > > In a.ml a record type t is defined and is also defined transparently
            > > > in a.mli, i.e. in d interface so that the type definition is available
            > > > to all other files.
            > > >
            > > > a.ml also has a function, func, which returns a list of t.
            > > >
            > > > Now in another file, b.ml i m calling func, now obviously ocaml
            > > > compiler wud nt be able to infer d type of objects stored in d list,
            > > > for compiler its just a list. so in b.ml, i hav something like dis,
            > > >
            > > > let tlist = A.func in
            > > > let vart = List.hd tlist in
            > > > printf "%s\n" vart.name (*name is a field in record t*)
            > > >
            > > > Now here i get a compiler error sayin "Unbound record field label
            > > > name" which makes sense as compiler can't infer d type of vart.
            > > >
            > > > my first question: how do I explicitly provide d type of vart as t
            > > > here?
            > > > i tried doing "let vart:A.t = " but got the
            > > > same error.
            > > > Also, i can't use A.name anywhere because "name" as a field name has
            > > > been used in multiple record type definitions in a.ml. So I have to
            > > > somehow mention the type name "t" with A to use type t outside a.ml.
            > > >
            > > > I also tried creating another function to fetch the first element of d
            > > > list and mentioning return type as A.t, but then i got the "Unbound
            > > > value A.t". I did this:
            > > >
            > > > let firstt = function
            > > > [] -> 0
            > > > | x :: _ -> A.t x ;;
            > > >
            > > > The problem is compiler is unable to recognize A.t (a type) in b.ml
            > > > but is able to recognize function A.func. If I remove A.t from the
            > > > b.ml, i don'get any compiler errors.
            > > >
            > > > Please help, its urgent work.
            > > > Thanks in advance!
            > > >
            > > > ~Tarun
            > >
            >
            >

            --
            Chantal KELLER
          • tarun_sethi4u
            ... I am using version 3.10.2. Is it fine? ... Why? Do you mean to say that in one .ml file you can not have two record types using the same field names? If
            Message 5 of 8 , May 12, 2010
            • 0 Attachment
              --- In ocaml_beginners@yahoogroups.com, Chantal Keller <chantal.keller@...> wrote:
              >
              > Le 12/05/2010 17:29, tarun_sethi4u a écrit :
              > > I tried using "vart.A.name", but it gives me syntax error.
              >
              > Well, that works for me... Which version of OCaml do you use?

              I am using version 3.10.2. Is it fine?

              > > Also, in a.ml you can say there is another record type, t1 which also
              > > has a field name as "name", so I can't use A.name anywhere as t also has
              > > "name" field and t1 too.
              >
              > I think this is a mistake: the field name in t will not be accessible
              > anyway.

              Why? Do you mean to say that in one .ml file you can not have two record types using the same field names? If that's the case then field names should be unique in a .ml file.

              > > Don't you think, I need to mention type "t" in syntax somewhere in b.ml?
              >
              > No, OCaml can infer the type of vart.
              >
              > Chantal.
              >
              >
              > > I am just looking for that right syntax.
              > >
              > > About the mistake you pointed out, func has expects a bool parameter
              > > which I am passing , so it becomes a function call rather than just a
              > > let binding. So its actually like this & I think its fine,
              > >
              > > let tlist = A.func false in
              > >
              > > Thanks for your prompt reply.
              > >
              > > ~ Tarun
              > >
              > > --- In ocaml_beginners@yahoogroups.com
              > > <mailto:ocaml_beginners%40yahoogroups.com>, Chantal Keller
              > > <chantal.keller@> wrote:
              > > >
              > > > Hi,
              > > >
              > > > You can access the field name like this :
              > > >
              > > > vart.A.name
              > > >
              > > > I think there is another mistake in your code: you said that func is a
              > > > function, so after let tlist = A.func, tlist is the same function (but
              > > > not a list).
              > > >
              > > > To compile everything, you can do:
              > > >
              > > > ocamlc -c a.mli
              > > > ocamlc -c a.ml
              > > > ocamlc -c b.ml
              > > > ocamlc -o foo a.cmo b.cmo
              > > >
              > > > (or use ocamlopt if you prefer native code).
              > > >
              > > > Hope this helps,
              > > > Chantal.
              > > >
              > > >
              > > > Le 12/05/2010 16:56, tarun_sethi4u a écrit :
              > > > > Hi,
              > > > >
              > > > > I m very new to ocaml and i m facing the problem below,
              > > > >
              > > > > In a.ml a record type t is defined and is also defined transparently
              > > > > in a.mli, i.e. in d interface so that the type definition is available
              > > > > to all other files.
              > > > >
              > > > > a.ml also has a function, func, which returns a list of t.
              > > > >
              > > > > Now in another file, b.ml i m calling func, now obviously ocaml
              > > > > compiler wud nt be able to infer d type of objects stored in d list,
              > > > > for compiler its just a list. so in b.ml, i hav something like dis,
              > > > >
              > > > > let tlist = A.func in
              > > > > let vart = List.hd tlist in
              > > > > printf "%s\n" vart.name (*name is a field in record t*)
              > > > >
              > > > > Now here i get a compiler error sayin "Unbound record field label
              > > > > name" which makes sense as compiler can't infer d type of vart.
              > > > >
              > > > > my first question: how do I explicitly provide d type of vart as t
              > > > > here?
              > > > > i tried doing "let vart:A.t = " but got the
              > > > > same error.
              > > > > Also, i can't use A.name anywhere because "name" as a field name has
              > > > > been used in multiple record type definitions in a.ml. So I have to
              > > > > somehow mention the type name "t" with A to use type t outside a.ml.
              > > > >
              > > > > I also tried creating another function to fetch the first element of d
              > > > > list and mentioning return type as A.t, but then i got the "Unbound
              > > > > value A.t". I did this:
              > > > >
              > > > > let firstt = function
              > > > > [] -> 0
              > > > > | x :: _ -> A.t x ;;
              > > > >
              > > > > The problem is compiler is unable to recognize A.t (a type) in b.ml
              > > > > but is able to recognize function A.func. If I remove A.t from the
              > > > > b.ml, i don'get any compiler errors.
              > > > >
              > > > > Please help, its urgent work.
              > > > > Thanks in advance!
              > > > >
              > > > > ~Tarun
              > > >
              > >
              > >
              >
              > --
              > Chantal KELLER
              >
            • vincent
              I guess this would all make it much easier if you gave us the whole files. V.
              Message 6 of 8 , May 12, 2010
              • 0 Attachment
                I guess this would all make it much easier if you gave us the whole files.

                V.
              • Chantal Keller
                ... I have just tried with this version, and it works... Indeed, it would be simpler if you gave the files. ... Not exactly, and it is not a problem of number
                Message 7 of 8 , May 12, 2010
                • 0 Attachment
                  Le 12/05/2010 17:55, tarun_sethi4u a écrit :
                  > --- In ocaml_beginners@yahoogroups.com
                  > <mailto:ocaml_beginners%40yahoogroups.com>, Chantal Keller
                  > <chantal.keller@...> wrote:
                  > >
                  > > Le 12/05/2010 17:29, tarun_sethi4u a écrit :
                  > > > I tried using "vart.A.name", but it gives me syntax error.
                  > >
                  > > Well, that works for me... Which version of OCaml do you use?
                  >
                  > I am using version 3.10.2. Is it fine?

                  I have just tried with this version, and it works... Indeed, it would be
                  simpler if you gave the files.

                  > > > Also, in a.ml you can say there is another record type, t1 which also
                  > > > has a field name as "name", so I can't use A.name anywhere as t
                  > also has
                  > > > "name" field and t1 too.
                  > >
                  > > I think this is a mistake: the field name in t will not be accessible
                  > > anyway.
                  >
                  > Why? Do you mean to say that in one .ml file you can not have two record
                  > types using the same field names? If that's the case then field names
                  > should be unique in a .ml file.

                  Not exactly, and it is not a problem of number of .ml. For instance, you
                  can do:

                  type t = {name : string}
                  let func s = [{name = s}]
                  type t2 = {name : int}
                  let a = func "foo"

                  a will be correctly defined and have type t. But a.name will fail.

                  By the way, for clarity, I think it is better to give different names to
                  the fields of different records...

                  Chantal.
                • tarun_sethi4u
                  It worked with vart.A.name . Thanks guys. I made sure that only one of the types defined in a.ml has name as the field. I think, Chantal is right, its better
                  Message 8 of 8 , May 12, 2010
                  • 0 Attachment
                    It worked with vart.A.name . Thanks guys. I made sure that only one of the types defined in a.ml has "name" as the field.

                    I think, Chantal is right, its better to use different field names, otherwise it fails.

                    Thanks again!

                    ~Tarun

                    --- In ocaml_beginners@yahoogroups.com, Chantal Keller <chantal.keller@...> wrote:
                    >
                    >
                    > Le 12/05/2010 17:55, tarun_sethi4u a écrit :
                    > > --- In ocaml_beginners@yahoogroups.com
                    > > <mailto:ocaml_beginners%40yahoogroups.com>, Chantal Keller
                    > > <chantal.keller@> wrote:
                    > > >
                    > > > Le 12/05/2010 17:29, tarun_sethi4u a écrit :
                    > > > > I tried using "vart.A.name", but it gives me syntax error.
                    > > >
                    > > > Well, that works for me... Which version of OCaml do you use?
                    > >
                    > > I am using version 3.10.2. Is it fine?
                    >
                    > I have just tried with this version, and it works... Indeed, it would be
                    > simpler if you gave the files.
                    >
                    > > > > Also, in a.ml you can say there is another record type, t1 which also
                    > > > > has a field name as "name", so I can't use A.name anywhere as t
                    > > also has
                    > > > > "name" field and t1 too.
                    > > >
                    > > > I think this is a mistake: the field name in t will not be accessible
                    > > > anyway.
                    > >
                    > > Why? Do you mean to say that in one .ml file you can not have two record
                    > > types using the same field names? If that's the case then field names
                    > > should be unique in a .ml file.
                    >
                    > Not exactly, and it is not a problem of number of .ml. For instance, you
                    > can do:
                    >
                    > type t = {name : string}
                    > let func s = [{name = s}]
                    > type t2 = {name : int}
                    > let a = func "foo"
                    >
                    > a will be correctly defined and have type t. But a.name will fail.
                    >
                    > By the way, for clarity, I think it is better to give different names to
                    > the fields of different records...
                    >
                    > Chantal.
                    >
                  Your message has been successfully submitted and would be delivered to recipients shortly.