Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch8 restore Exceptions and Mutable State sections #94

Merged

Conversation

milesfrain
Copy link
Member

@milesfrain milesfrain commented Apr 17, 2020

Original text here:
https://leanpub.com/purescript/read#leanpub-auto-handlers-and-actions

Split this into three commits to make changes easier to follow.

@milesfrain
Copy link
Member Author

milesfrain commented Apr 17, 2020

The Mutable State section might need another update if ST is merged with Effect
purescript/purescript-st#31

Edit, summary of ways to track mutable state:

  • Control.Monad.ST
    • Covered in this section of Ch8
  • Effect.Ref
    • Covered in Ch9
    • Use with forE from Effect
    • "Note: Control.Moad.ST provides a safe alternative to global mutable variables when mutation is restricted to a local scope."
  • Control.Monad.State
    • Covered in Ch11
    • This section of the original book contrasts this with ST and Ref
    • Transformer

text/chapter8.md Outdated
Comment on lines 611 to 632
var simulate = function (x0) {
return function (v0) {
return function (time) {
return (function __do() {
var ref = {
value: {
x: x0,
v: v0
}
};
Control_Monad_ST_Internal["for"](0)(time * 1000 | 0)(function (v) {
return Control_Monad_ST_Internal.modify(function (o) {
return {
v: o.v - 9.81 * 1.0e-3,
x: o.x + o.v * 1.0e-3
};
})(ref);
})();
return ref.value.x;
})();
};
};
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not as concise as is was in the original.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the new version have a call to Control_Monad_ST_Internal.modify?

This is the original body:

var ref = { x: x0, v: v0 };     
                                  
Control_Monad_Eff.forE(0)(time * 1000 | 0)(function (i) {    
  return function __do() {    
    ref = (function (o) {    
      return {                                                                         
        v: o.v - 9.81 * 1.0e-3,                                               
        x: o.x + o.v * 1.0e-3    
      };    
    })(ref);    
    return Prelude.unit;    
  };    
})();    
                                       
return ref.x;

This is the new body:

var ref = { value: { x: x0, v: v0 } };                
 
Control_Monad_ST_Internal["for"](0)(time * 1000 | 0)(function (v) {                
  return Control_Monad_ST_Internal.modify(function (o) {                  
    return {                    
      v: o.v - 9.81 * 1.0e-3,                      
      x: o.x + o.v * 1.0e-3                      
    };                    
  })(ref);                  
})();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this probably does indicate a problem. There is an inliner rule for modify built in to the compiler but it looks like it is no longer firing for some reason.

text/chapter8.md Outdated
## Exercises

1. (Medium) Rewrite the `safeDivide` function to throw an exception using `throwException` if the denominator is zero.
1. (Difficult) The following is a simple way to estimate pi: randomly choose a large number `N` of points in the unit square, and count the number `n` which lie in the inscribed circle. An estimate for pi is `4n/N`. Use the `random` and `ST` effects with the `for` function to write a function which estimates pi in this way.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it still possible to blend Effect.random with ST? This is one of the more interesting exercises, so I'd like to keep it. Maybe it should be moved to ch11 in the "State Monad" section.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Once you're in Effect you might as well use Ref. When we were still using Eff you could eliminate the ST effect, but that was always kind of dodgy"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the second exercise can't include randomness, then it seems like it could actually be tackled in ch4 like so:

estimatePi :: Int -> Number
estimatePi r =
  let
    bigN = r * r

    n =
      sum do
        x <- 1 .. r
        y <- 1 .. r
        guard $ x * x + y * y < r * r
        pure 1
  in
    (toNumber (4 * n)) / (toNumber bigN)

I'm not sure if adding this to ch4 would be an improvement, since it doesn't test any new language concepts.

There's still the question of what exercise to use for ST in this chapter.

@milesfrain milesfrain force-pushed the ch8-exception-state-text branch from cbc5fa2 to 7880ed5 Compare April 25, 2020 20:33
@milesfrain milesfrain merged commit 7b13001 into purescript-contrib:master Apr 25, 2020
@milesfrain milesfrain deleted the ch8-exception-state-text branch October 28, 2020 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants