OCaml User-Defined Type and Function Return Error











up vote
0
down vote

favorite












I was writing a function with user-defined types in OCaml when I encountered an error message that I don't understand.



I'm currently using the OCaml interactive toplevel and also Visual Studio Code to write my code. The strange thing is that when I run the code in Visual Studio Code, it compiles fine but encounters the error in the interactive toplevel.



The OCaml code that I am referring to is as follows:



type loc = int;;
type id = string;;

type value =
| Num of int
| Bool of bool
| Unit
| Record of (id -> loc)
;;

type memory = (loc * value) list;;

exception NotInMemory;;

let rec memory_lookup : (memory * loc) -> value
= fun (mem, l) ->
match mem with
| -> raise NotInMemory
| hd :: tl -> (match hd with
| (x, a) -> if x = l then a else (memory_lookup (tl, l))
)
;;


The code that I wrote is basically my rudimentary attempt at implementing/emulating looking up memory and returning corresponding values.






Here's an example input:



memory1 = [ (1, Num 1) ; (2, Bool true) ; (3, Unit) ];;


Here's the expected output:



memory_lookup (memory1, 2);;
- : value = Bool true


However, here's the actual output:



Characters: 179-180:
| (x, a) -> if x = l then "a" else (memory_lookup (tl, l)))
Error: This expression has type value/1076
but an expression was expected of type value/1104


(Just for clarification: the error is regarding character a)






Does anybody know what type value/1076 and type value/1104 mean? Also, if there is anything wrong with the code that I wrote, would anybody be kind enough to point it out?



Thank you.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I was writing a function with user-defined types in OCaml when I encountered an error message that I don't understand.



    I'm currently using the OCaml interactive toplevel and also Visual Studio Code to write my code. The strange thing is that when I run the code in Visual Studio Code, it compiles fine but encounters the error in the interactive toplevel.



    The OCaml code that I am referring to is as follows:



    type loc = int;;
    type id = string;;

    type value =
    | Num of int
    | Bool of bool
    | Unit
    | Record of (id -> loc)
    ;;

    type memory = (loc * value) list;;

    exception NotInMemory;;

    let rec memory_lookup : (memory * loc) -> value
    = fun (mem, l) ->
    match mem with
    | -> raise NotInMemory
    | hd :: tl -> (match hd with
    | (x, a) -> if x = l then a else (memory_lookup (tl, l))
    )
    ;;


    The code that I wrote is basically my rudimentary attempt at implementing/emulating looking up memory and returning corresponding values.






    Here's an example input:



    memory1 = [ (1, Num 1) ; (2, Bool true) ; (3, Unit) ];;


    Here's the expected output:



    memory_lookup (memory1, 2);;
    - : value = Bool true


    However, here's the actual output:



    Characters: 179-180:
    | (x, a) -> if x = l then "a" else (memory_lookup (tl, l)))
    Error: This expression has type value/1076
    but an expression was expected of type value/1104


    (Just for clarification: the error is regarding character a)






    Does anybody know what type value/1076 and type value/1104 mean? Also, if there is anything wrong with the code that I wrote, would anybody be kind enough to point it out?



    Thank you.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I was writing a function with user-defined types in OCaml when I encountered an error message that I don't understand.



      I'm currently using the OCaml interactive toplevel and also Visual Studio Code to write my code. The strange thing is that when I run the code in Visual Studio Code, it compiles fine but encounters the error in the interactive toplevel.



      The OCaml code that I am referring to is as follows:



      type loc = int;;
      type id = string;;

      type value =
      | Num of int
      | Bool of bool
      | Unit
      | Record of (id -> loc)
      ;;

      type memory = (loc * value) list;;

      exception NotInMemory;;

      let rec memory_lookup : (memory * loc) -> value
      = fun (mem, l) ->
      match mem with
      | -> raise NotInMemory
      | hd :: tl -> (match hd with
      | (x, a) -> if x = l then a else (memory_lookup (tl, l))
      )
      ;;


      The code that I wrote is basically my rudimentary attempt at implementing/emulating looking up memory and returning corresponding values.






      Here's an example input:



      memory1 = [ (1, Num 1) ; (2, Bool true) ; (3, Unit) ];;


      Here's the expected output:



      memory_lookup (memory1, 2);;
      - : value = Bool true


      However, here's the actual output:



      Characters: 179-180:
      | (x, a) -> if x = l then "a" else (memory_lookup (tl, l)))
      Error: This expression has type value/1076
      but an expression was expected of type value/1104


      (Just for clarification: the error is regarding character a)






      Does anybody know what type value/1076 and type value/1104 mean? Also, if there is anything wrong with the code that I wrote, would anybody be kind enough to point it out?



      Thank you.










      share|improve this question













      I was writing a function with user-defined types in OCaml when I encountered an error message that I don't understand.



      I'm currently using the OCaml interactive toplevel and also Visual Studio Code to write my code. The strange thing is that when I run the code in Visual Studio Code, it compiles fine but encounters the error in the interactive toplevel.



      The OCaml code that I am referring to is as follows:



      type loc = int;;
      type id = string;;

      type value =
      | Num of int
      | Bool of bool
      | Unit
      | Record of (id -> loc)
      ;;

      type memory = (loc * value) list;;

      exception NotInMemory;;

      let rec memory_lookup : (memory * loc) -> value
      = fun (mem, l) ->
      match mem with
      | -> raise NotInMemory
      | hd :: tl -> (match hd with
      | (x, a) -> if x = l then a else (memory_lookup (tl, l))
      )
      ;;


      The code that I wrote is basically my rudimentary attempt at implementing/emulating looking up memory and returning corresponding values.






      Here's an example input:



      memory1 = [ (1, Num 1) ; (2, Bool true) ; (3, Unit) ];;


      Here's the expected output:



      memory_lookup (memory1, 2);;
      - : value = Bool true


      However, here's the actual output:



      Characters: 179-180:
      | (x, a) -> if x = l then "a" else (memory_lookup (tl, l)))
      Error: This expression has type value/1076
      but an expression was expected of type value/1104


      (Just for clarification: the error is regarding character a)






      Does anybody know what type value/1076 and type value/1104 mean? Also, if there is anything wrong with the code that I wrote, would anybody be kind enough to point it out?



      Thank you.







      error-handling ocaml valueerror






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:39









      Sean

      306212




      306212
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          This kind of error happens in the toplevel when a type is defined multiple times, and some values of the old type are left in scope. A simple example is



          type t = A
          let x = A;;
          type t = A
          let y = A;;
          x = y;;



          Error: This expression has type t/1012 but an expression was expected of type
          t/1009




          The numerical part after the type name in value/1076 is a binding time for the type value. This binding time is used as a last resort to differentiate between two different types that happens to have the same name. Thus




          Error: This expression has type value/1076
          but an expression was expected of type value/1104




          means that the value memory1 was defined with a type value defined at time 1076, whereas the function memory_lookup expected values of the type value defined at a later date (aka at time 1104). The binding times are a bit arbitrary , so they may be replaced by simply value/1 and value/2 in OCaml 4.08 .






          share|improve this answer





















          • Oh wow thank you! I didn't know about this before.
            – Sean
            Nov 8 at 12:59











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206005%2focaml-user-defined-type-and-function-return-error%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          This kind of error happens in the toplevel when a type is defined multiple times, and some values of the old type are left in scope. A simple example is



          type t = A
          let x = A;;
          type t = A
          let y = A;;
          x = y;;



          Error: This expression has type t/1012 but an expression was expected of type
          t/1009




          The numerical part after the type name in value/1076 is a binding time for the type value. This binding time is used as a last resort to differentiate between two different types that happens to have the same name. Thus




          Error: This expression has type value/1076
          but an expression was expected of type value/1104




          means that the value memory1 was defined with a type value defined at time 1076, whereas the function memory_lookup expected values of the type value defined at a later date (aka at time 1104). The binding times are a bit arbitrary , so they may be replaced by simply value/1 and value/2 in OCaml 4.08 .






          share|improve this answer





















          • Oh wow thank you! I didn't know about this before.
            – Sean
            Nov 8 at 12:59















          up vote
          1
          down vote



          accepted










          This kind of error happens in the toplevel when a type is defined multiple times, and some values of the old type are left in scope. A simple example is



          type t = A
          let x = A;;
          type t = A
          let y = A;;
          x = y;;



          Error: This expression has type t/1012 but an expression was expected of type
          t/1009




          The numerical part after the type name in value/1076 is a binding time for the type value. This binding time is used as a last resort to differentiate between two different types that happens to have the same name. Thus




          Error: This expression has type value/1076
          but an expression was expected of type value/1104




          means that the value memory1 was defined with a type value defined at time 1076, whereas the function memory_lookup expected values of the type value defined at a later date (aka at time 1104). The binding times are a bit arbitrary , so they may be replaced by simply value/1 and value/2 in OCaml 4.08 .






          share|improve this answer





















          • Oh wow thank you! I didn't know about this before.
            – Sean
            Nov 8 at 12:59













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          This kind of error happens in the toplevel when a type is defined multiple times, and some values of the old type are left in scope. A simple example is



          type t = A
          let x = A;;
          type t = A
          let y = A;;
          x = y;;



          Error: This expression has type t/1012 but an expression was expected of type
          t/1009




          The numerical part after the type name in value/1076 is a binding time for the type value. This binding time is used as a last resort to differentiate between two different types that happens to have the same name. Thus




          Error: This expression has type value/1076
          but an expression was expected of type value/1104




          means that the value memory1 was defined with a type value defined at time 1076, whereas the function memory_lookup expected values of the type value defined at a later date (aka at time 1104). The binding times are a bit arbitrary , so they may be replaced by simply value/1 and value/2 in OCaml 4.08 .






          share|improve this answer












          This kind of error happens in the toplevel when a type is defined multiple times, and some values of the old type are left in scope. A simple example is



          type t = A
          let x = A;;
          type t = A
          let y = A;;
          x = y;;



          Error: This expression has type t/1012 but an expression was expected of type
          t/1009




          The numerical part after the type name in value/1076 is a binding time for the type value. This binding time is used as a last resort to differentiate between two different types that happens to have the same name. Thus




          Error: This expression has type value/1076
          but an expression was expected of type value/1104




          means that the value memory1 was defined with a type value defined at time 1076, whereas the function memory_lookup expected values of the type value defined at a later date (aka at time 1104). The binding times are a bit arbitrary , so they may be replaced by simply value/1 and value/2 in OCaml 4.08 .







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:16









          octachron

          3,8381312




          3,8381312












          • Oh wow thank you! I didn't know about this before.
            – Sean
            Nov 8 at 12:59


















          • Oh wow thank you! I didn't know about this before.
            – Sean
            Nov 8 at 12:59
















          Oh wow thank you! I didn't know about this before.
          – Sean
          Nov 8 at 12:59




          Oh wow thank you! I didn't know about this before.
          – Sean
          Nov 8 at 12:59


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206005%2focaml-user-defined-type-and-function-return-error%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff