Why does F# Interactive behave differently than compiler with regards to immutable value definition?

Guy Coder

In reading John Palmer's answer to What is the difference between mutable values and immutable value redefinition?, John notes that

This sort of redefinition will only work in fsi.

In working with F# Interactive (fsi) I guess I subconsciously knew it, but never paid attention to it.

Now that it is apparent, why the difference?

More specifically please explain how the internals are different between fsi and the compiler such that this occurs by design or result of differences?

If the answer can elaborate on the internal structures that hold the bindings that would be appreciated.

Stephen Swensen

The semantics are consistent with the way FSI compiles interactive submissions to an FSI session: each interactive submission is compiled as module which is open to subsequent interactive submissions.

The following is close to what FSI actual does and illustrates how let binding shadowing works across interactive submissions:

FSI Submission #1: let x = 1;;

module FSI_1 = 
    let x = 1

open FSI_1 //FSI_1.x is now bound to 1 and available at the top level

FSI Submission #2: let x = 2;;

module FSI_2 = 
    let x = 2

open FSI_2 //FSI_1.x is now shadowed by FSI_2.x which is bound to 2 and available at the top level

You can see the actual details how how the dynamic FSI assembly is compiled by using reflection on the FSI_ASSEMBLY assembly within the FSI app domain. Each interactive submission is literally emitted as a module (.NET class) with the naming pattern FSI_####. FsEye uses these facts to discover the state of FSI top-level bindings: https://code.google.com/p/fseye/source/browse/tags/2.0.1/FsEye/Fsi/SessionQueries.fs#24

The key takeaway in regard to @JohnPalmer's answer is that top-level FSI definitions cannot be mutated, when they are "redefined" they are merely being shadowed. We can show this as follows:

> let x = 1;; //our original definition of x
val x : int = 1

> let f () = x;; //capture x
val f : unit -> int

> let x = 2;; //shadow our original definition of x
val x : int = 2

> f();; //returns the original x value, which is still 1 rather than 2
val it : int = 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does "include" behave differently in the global context than it does in a class?

From Dev

Clojure - Why does into behave differently with a list than a vector?

From Dev

Why does this constraint behave differently on different simulators?

From Dev

Why does FINDSTR behave differently in powershell and cmd?

From Dev

Why does this array initialize behave differently?

From Dev

Why does this contextmanager behave differently with dict comprehensions?

From Dev

Why does the complement behave differently through printf?

From Dev

Why does Jetty JSR356 behave differently with regards to checkOrigin and modifyHandshake

From Dev

Why does strptime() behave differently on OSX and on Linux?

From Dev

Why does EOF behave differently in fgets and read

From Dev

Why does width:auto behave differently than height:auto?

From Dev

Why does "last" behave differently in Perl in these examples?

From Dev

Why does .foreach behave differently than for...of when executing a recursive callback?

From Dev

Why does this code behave differently in NodeJS?

From Dev

Why does less css behave differently when served over port 80 than other ports?

From Dev

Why does "include" behave differently in the global context than it does in a class?

From Dev

Why does "grep" behave differently in this example?

From Dev

Why does F# Interactive behave differently than compiler with regards to immutable value definition?

From Dev

Why does `curl <this machine's IP>` behave differently than `curl localhost`?

From Dev

Why does this array initialize behave differently?

From Dev

Why does this contextmanager behave differently with dict comprehensions?

From Dev

Why does `==` behave differently inside `[ ... ]` in zsh and bash?

From Dev

Why does awk behave differently for $1 if the value is 0 (number zero)?

From Dev

Why does strptime() behave differently on OSX and on Linux?

From Dev

Why does type inference in mutable and immutable sets behave so differently?

From Dev

Why does PowerShell behave differently than cmd.exe given the same command?

From Dev

Why does /* */ comment behave differently ? Javascript bug?

From Dev

Why does `xargs -n ` on SmartOS (SunOS) behave differently than other implementations?

From Dev

Why does jQuery behave differently than javascript?

Related Related

  1. 1

    Why does "include" behave differently in the global context than it does in a class?

  2. 2

    Clojure - Why does into behave differently with a list than a vector?

  3. 3

    Why does this constraint behave differently on different simulators?

  4. 4

    Why does FINDSTR behave differently in powershell and cmd?

  5. 5

    Why does this array initialize behave differently?

  6. 6

    Why does this contextmanager behave differently with dict comprehensions?

  7. 7

    Why does the complement behave differently through printf?

  8. 8

    Why does Jetty JSR356 behave differently with regards to checkOrigin and modifyHandshake

  9. 9

    Why does strptime() behave differently on OSX and on Linux?

  10. 10

    Why does EOF behave differently in fgets and read

  11. 11

    Why does width:auto behave differently than height:auto?

  12. 12

    Why does "last" behave differently in Perl in these examples?

  13. 13

    Why does .foreach behave differently than for...of when executing a recursive callback?

  14. 14

    Why does this code behave differently in NodeJS?

  15. 15

    Why does less css behave differently when served over port 80 than other ports?

  16. 16

    Why does "include" behave differently in the global context than it does in a class?

  17. 17

    Why does "grep" behave differently in this example?

  18. 18

    Why does F# Interactive behave differently than compiler with regards to immutable value definition?

  19. 19

    Why does `curl <this machine's IP>` behave differently than `curl localhost`?

  20. 20

    Why does this array initialize behave differently?

  21. 21

    Why does this contextmanager behave differently with dict comprehensions?

  22. 22

    Why does `==` behave differently inside `[ ... ]` in zsh and bash?

  23. 23

    Why does awk behave differently for $1 if the value is 0 (number zero)?

  24. 24

    Why does strptime() behave differently on OSX and on Linux?

  25. 25

    Why does type inference in mutable and immutable sets behave so differently?

  26. 26

    Why does PowerShell behave differently than cmd.exe given the same command?

  27. 27

    Why does /* */ comment behave differently ? Javascript bug?

  28. 28

    Why does `xargs -n ` on SmartOS (SunOS) behave differently than other implementations?

  29. 29

    Why does jQuery behave differently than javascript?

HotTag

Archive