Why is my immutable object mutating Scala - debugging

I have come across a most troubling bug in my code below. The buttons Map mutates even though I pass it as an immutable Map. The keys remain the same, and the map points to two immutable Ints, yet below you can see the map clearly has different values during the run. I am absolutely stumped and have no idea what is happening.
def makeTrace(trace : List[(String)], buttons : Map[String, (Int,Int)],
outputScreen : ScreenRegion, hashMap : Map[Array[Byte], String])
: (List[(String,String)], Map[Array[Byte], String]) = {
println(buttons.toString)
//clearing the device
val clear = buttons.getOrElse("clear", throw new Exception("Clear Not Found"))
//clear.circle(3000)
val thisButton = new ScreenLocation(clear._1, clear._2)
click(thisButton)
//updates the map and returns a list of (transition, state)
trace.foldLeft((Nil : List[(String,String)], hashMap))( (list, trace) => {
println(buttons.toString)
val transition : String = trace
val location = buttons.getOrElse(transition, throw new Exception("whatever"))
val button = new ScreenLocation(location._1, location._2)
button.circle(500)
button.label(transition, 500)
click(button)
//reading and hashing
pause(500)
val capturedImage : BufferedImage = outputScreen.capture()
val outputStream : ByteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(capturedImage, "png", outputStream)
val byte : Array[Byte] = outputStream.toByteArray();
//end hash
//if our state exists for the hash
if (hashMap.contains(byte)){ list match {
case (accumulator, map) => ((transition , hashMap.getOrElse(byte, throw new Exception("Our map broke if"))):: accumulator, map)
}
//if we need to update the map
}else list match {
case (accumulator, map) => {
//adding a new state based on the maps size
val newMap : Map[Array[Byte], String] = map + ((byte , "State" + map.size.toString))
val imageFile : File = new File("State" + map.size.toString + ".png");
ImageIO.write(capturedImage, "png", imageFile);
((transition, newMap.getOrElse(byte, throw new Exception("Our map broke else"))) :: accumulator, newMap)
}
}
})
}
Right before I call this function I initialize the map to an immutable map that points to immutable objects.
val buttons = makeImmutable(MutButtons)
val traceAndMap = TraceFinder.makeTrace(("clear" ::"five"::"five"::"minus"::"five"::"equals":: Nil), buttons, outputScreen, Map.empty)
Where makeImmutable is
def makeImmutable(buttons : Map[String, (Int,Int)]) : Map[String, (Int,Int)] = {
buttons.mapValues(button => button match {
case (x, y) =>
val newX = x
val newY = y
(newX,newY)
})
}
Here is the output, you can see the state change for clear, minus, and five
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (842,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (842,377))
Map(equals -> (959,425), clear -> (959,345), minus -> (920,409), five -> (842,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (842,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (881,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (842,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,441), five -> (842,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (881,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (842,377))
Map(equals -> (959,425), clear -> (842,313), minus -> (920,409), five -> (842,377))

First, try sprinkling println(map.getClass) around your code. Make sure it really is the map you think it is. It should have immutable in its package name, e.g.:
scala> println(Map(1->1, 2->2, 3->3, 4->4).getClass)
class scala.collection.immutable.Map$Map4
Second, make sure you really are printing out the exact same map you think you are; using the reference identity hash code is a good way to do that: println(System.identityHashCode(map)).
Chances are extraordinarily good that one of these things will not give you the expected result. Then you just have to figure out where the problem leaked in. Without a full runnable example it's hard to offer better advice without inordinate amounts of code-staring.

I suspect you have an import for scala.collection.Map in scope of the makeImmutable function, which allows you to pass MutButtons (presumably a mutable Map) to the makeImmutable function. The mapValues method on Map returns a view of the underlying map, not an immutable copy. For example:
import scala.collection.Map
import scala.collection.mutable.{Map => MutableMap}
def doubleValues(m: Map[Int, Int]) = m mapValues { _ * 2 }
val m = MutableMap(1 -> 1, 2 -> 2)
val n = doubleValues(m)
println("m = " + m)
println("n = " + n)
m += (3 -> 3)
println("n = " + n)
Running this program produces this output:
m = Map(2 -> 2, 1 -> 1)
n = Map(2 -> 4, 1 -> 2)
n = Map(2 -> 4, 1 -> 2, 3 -> 6)
To return a truly immutable map from makeImmutable, call .toMap after mapping over the values.

Related

How To Count Occurrences Of Each Character In String In Java 8(lambda exp or streams) but not in traditional method?

Sample output All Is Well : { =2, A=1, s=1, e=1, W=1, I=1, l=4}
You may try this,
Arrays.stream("inputstring".split(""))
.map(String::toLowerCase)
.collect(Collectors.groupingBy(s -> s, LinkedHashMap::new, Collectors.counting()))
.forEach((k, v) -> System.out.println(k + " = " + v));

Tail recursion with the State and CPS monads?

I am in the process of creating a simple parsing library in Haskell, which compiles down the parser specification to optimized code using Template Haskell.
However, I am trying to figure out what kind of code is the most efficient to optimize to, so let us consider a simple parserly-written parser snippet.
A parser can be seen as a function String -> (a, String) where a is whatever we want to recognize during parsing, and the resulting string is whatever part of the input we have not matched. When parsers are run in sequence, the next parser should continue working on this string. At the end we can say a full string was successfully parsed if the end result is of the shape ("", _).
In this example, we'll use a parser that parses any number of 'a'-characters.
(Error handling, that is reporting an error if any character other than an 'a' was seen, has been left out of the code snippets to keep everything concise for the sake of question clarity).
Straightforward (naive?) implementation
If we write the code for a recursive parser parserly, a straightforward hand-rolled implementation might be:
parser_raw :: String -> (String, String)
parser_raw str =
case str of
[] -> ([], [])
('a' : rest) ->
let
(res', rest') = parser_raw rest
in
(('a' : res'), rest')
This matches any number of 'a''s. The parser recurses until the string is empty.
Looking at the created GHC Core, we see the following output:
Rec {
-- RHS size: {terms: 36, types: 45, coercions: 0, joins: 0/1}
$wparser_raw
= \ w_s7BO ->
case w_s7BO of {
[] -> (# [], [] #);
: ds_d736 rest_a5S6 ->
case ds_d736 of { C# ds1_d737 ->
case ds1_d737 of {
__DEFAULT -> case lvl6_r7Fw of wild2_00 { };
'a'# ->
let {
ds3_s7vv
= case $wparser_raw rest_a5S6 of { (# ww1_s7C3, ww2_s7C4 #) ->
(ww1_s7C3, ww2_s7C4)
} } in
(# : lvl2_r7Fs
(case ds3_s7vv of { (res'_a65m, rest'_a65n) -> res'_a65m }),
case ds3_s7vv of { (res'_a65m, rest'_a65n) -> rest'_a65n } #)
}
}
}
end Rec }
To my untrained eye, it seems like this is not tail recursive, as we first do the recursive call and then still need to combine the output into a tuple afterwards. I also find it odd/interesting that the let in the source code still is there in the compiled code, which makes it seem like the computation really needs to be done in multiple (i.e. non-tail-recursive) steps.
Two other approaches to write this code come to mind. (Maybe there are more?)
continuation-passing style
parser_cps :: String -> (String, String)
parser_cps str = parser_cps' str id
where
parser_cps' :: String -> ((String, String) -> (String, String)) -> (String, String)
parser_cps' str fun =
case str of
[] -> fun ([], [])
('a' : rest) ->
parser_cps' rest ((\(tl, final) -> (('a' : tl), final) ) . fun)
This results in:
Rec {
-- RHS size: {terms: 28, types: 29, coercions: 0, joins: 0/0}
parser_cps_parser_cps'
= \ str_a5S8 fun_a5S9 ->
case str_a5S8 of {
[] -> fun_a5S9 lvl9_r7FM;
: ds_d72V rest_a5Sa ->
case ds_d72V of { C# ds1_d72W ->
case ds1_d72W of {
__DEFAULT -> lvl8_r7FL;
'a'# ->
parser_cps_parser_cps'
rest_a5Sa
(\ x_i72P ->
case fun_a5S9 x_i72P of { (tl_a5Sb, final_a5Sc) ->
(: lvl2_r7FF tl_a5Sb, final_a5Sc)
})
}
}
}
end Rec }
-- RHS size: {terms: 4, types: 4, coercions: 0, joins: 0/0}
parser_cps = \ str_a5S6 -> parser_cps_parser_cps' str_a5S6 id
This looks tail-recursive, in the sense that we perform a tail-call. It does seem like the result is built up as a large thunk however. Are we using a lot of extra heap space until the base of the recursion is reached?
the State monad
-- Using `mtl`'s
-- import Control.Monad.State.Strict
parser_state :: String -> (String, String)
parser_state = runState parser_state'
where
parser_state' :: State String String
parser_state' = do
str <- get
case str of
[] -> return []
('a' : rest) -> do
put rest
res <- parser_state'
return ('a' : res)
This results in:
Rec {
-- RHS size: {terms: 26, types: 36, coercions: 0, joins: 0/0}
$wparser_state'
= \ w_s7Ca ->
case w_s7Ca of {
[] -> (# [], [] #);
: ds_d72p rest_a5Vb ->
case ds_d72p of { C# ds1_d72q ->
case ds1_d72q of {
__DEFAULT -> case lvl11_r7FO of wild2_00 { };
'a'# ->
case $wparser_state' rest_a5Vb of { (# ww1_s7Cl, ww2_s7Cm #) ->
(# : lvl2_r7FF ww1_s7Cl, ww2_s7Cm #)
}
}
}
}
end Rec }
-- RHS size: {terms: 8, types: 14, coercions: 6, joins: 0/0}
parser_state1
= \ w_s7Ca ->
case $wparser_state' w_s7Ca of { (# ww1_s7Cl, ww2_s7Cm #) ->
(ww1_s7Cl, ww2_s7Cm) `cast` <Co:6>
}
-- RHS size: {terms: 1, types: 0, coercions: 7, joins: 0/0}
parser_state = parser_state1 `cast` <Co:7>
Here, I'm not sure how to interpret the core: The let of the parser_raw-code is gone. Instead, the recursive call ends up immediately in the case. Afterwards we still have to put the result in a tuple, but is this "sans cons" enough to please the recursion gods?
So, to summarize: these are three techniques to write a simple parsing function. I would like to know which one of these is the most memory-efficient and how to get some intuition in recognizing tail-recursion from looking at GHC Core output.

Error on accessing data in a list of tuples F#

Cannot assign a position to View.Pin from a list of tuples (There are 300 tuples in the real program), please comment how to fix it.
Error : The type 'a list' is not compatible with the type 'View Element'
let tokyo = Position(35.652832, 139.839478)
// create sample tuples
let t1 = ("35", 48.856, 2.3522)
let t2 = ("32", 51.5074, -0.1278)
let t3 = ("25", 50.9513, 1.8587)
// create sample list
let lst = [ t1; t2; t3 ]
let map =
View.Map
(hasZoomEnabled = true, hasScrollEnabled = true,
pins = [ lst |> List.map (fun (_, a, b) -> View.Pin(Position(a, b), label = "Dummy", pinType = PinType.Place)) ],
requestedRegion = MapSpan.FromCenterAndRadius(tokyo, Distance.FromKilometers(300.)))
I found a solution from "https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/list.map%5B%27t%2C%27u%5D-function-%5Bfsharp%5D"
To my surprise, F# is an awesome functional language to handle large data set in only a few lines of codes.
let view (model : Model) dispatch =
// A list of 300 tuples
let jpnDatas = jsonData.jpnData
let pins =
jpnDatas
|> List.map (fun (_, a, _, _, b, c) -> View.Pin(Position(b, c), label = a, pinType = PinType.Place))
let tokyo = Position(35.652832, 139.839478)
let map =
View.Map
(hasZoomEnabled = true, hasScrollEnabled = true,
pins = [ for pin in pins do yield pin ],
requestedRegion = MapSpan.FromCenterAndRadius(tokyo, Distance.FromKilometers(300.)))

Why is the "better" digit-listing function slower?

I was playing around with Project Euler #34, and I wrote these functions:
import Data.Time.Clock.POSIX
import Data.Char
digits :: (Integral a) => a -> [Int]
digits x
| x < 10 = [fromIntegral x]
| otherwise = let (q, r) = x `quotRem` 10 in (fromIntegral r) : (digits q)
digitsByShow :: (Integral a, Show a) => a -> [Int]
digitsByShow = map (\x -> ord x - ord '0') . show
I thought that for sure digits has to be the faster one, as we don't convert to a String. I could not have been more wrong. I ran the two versions via pe034:
pe034 digitFunc = sum $ filter sumFactDigit [3..2540160]
where
sumFactDigit :: Int -> Bool
sumFactDigit n = n == (sum $ map sFact $ digitFunc n)
sFact :: Int -> Int
sFact n
| n == 0 = 1
| n == 1 = 1
| n == 2 = 2
| n == 3 = 6
| n == 4 = 24
| n == 5 = 120
| n == 6 = 720
| n == 7 = 5040
| n == 8 = 40320
| n == 9 = 362880
main = do
begin <- getPOSIXTime
print $ pe034 digitsByShow -- or digits
end <- getPOSIXTime
print $ end - begin
After compiling with ghc -O, digits consistently takes .5 seconds, while digitsByShow consistently takes .3 seconds. Why is this so? Why is the function which stays within Integer arithmetic slower, whereas the function which goes into string comparison is faster?
I ask this because I come from programming in Java and similar languages, where the % 10 trick of generating digits is way faster than the "convert to String" method. I haven't been able to wrap my head around the fact that converting to a string could be faster.
This is the best I can come up with.
digitsV2 :: (Integral a) => a -> [Int]
digitsV2 n = go n []
where
go x xs
| x < 10 = fromIntegral x : xs
| otherwise = case quotRem x 10 of
(q,r) -> go q (fromIntegral r : xs)
when compiled with -O2 and tested with Criterion
digits runs in 470.4 ms
digitsByShow runs in 421.8 ms
digitsV2 runs in 258.0 ms
results may vary
edit:
I am not sure why building the list like this helps so much.
But you can improve your codes speed by strictly evaluating quotRem x 10
You can do this with BangPatterns
| otherwise = let !(q, r) = x `quotRem` 10 in (fromIntegral r) : (digits q)
or with case
| otherwise = case quotRem x 10 of
(q,r) -> fromIntegral r : digits q
Doing this drops digits down to 323.5 ms
edit: time without using Criterion
digits = 464.3 ms
digitsStrict = 328.2 ms
digitsByShow = 259.2 ms
digitV2 = 252.5 ms
note: The criterion package measures software performance.
Let's investigate why #No_signal's solution is faster.
I made three runs of ghc:
ghc -O2 -ddump-simpl digits.hs >digits.txt
ghc -O2 -ddump-simpl digitsV2.hs >digitsV2.txt
ghc -O2 -ddump-simpl show.hs >show.txt
digits.hs
digits :: (Integral a) => a -> [Int]
digits x
| x < 10 = [fromIntegral x]
| otherwise = let (q, r) = x `quotRem` 10 in (fromIntegral r) : (digits q)
main = return $ digits 1
digitsV2.hs
digitsV2 :: (Integral a) => a -> [Int]
digitsV2 n = go n []
where
go x xs
| x < 10 = fromIntegral x : xs
| otherwise = let (q, r) = x `quotRem` 10 in go q (fromIntegral r : xs)
main = return $ digits 1
show.hs
import Data.Char
digitsByShow :: (Integral a, Show a) => a -> [Int]
digitsByShow = map (\x -> ord x - ord '0') . show
main = return $ digitsByShow 1
If you'd like to view the complete txt files, I placed them on ideone (rather than paste a 10000 char dump here):
digits.txt
digitsV2.txt
show.txt
If we carefully look through digits.txt, it appears that this is the relevant section:
lvl_r1qU = __integer 10
Rec {
Main.$w$sdigits [InlPrag=[0], Occ=LoopBreaker]
:: Integer -> (# Int, [Int] #)
[GblId, Arity=1, Str=DmdType <S,U>]
Main.$w$sdigits =
\ (w_s1pI :: Integer) ->
case integer-gmp-1.0.0.0:GHC.Integer.Type.ltInteger#
w_s1pI lvl_r1qU
of wild_a17q { __DEFAULT ->
case GHC.Prim.tagToEnum# # Bool wild_a17q of _ [Occ=Dead] {
False ->
let {
ds_s16Q [Dmd=<L,U(U,U)>] :: (Integer, Integer)
[LclId, Str=DmdType]
ds_s16Q =
case integer-gmp-1.0.0.0:GHC.Integer.Type.quotRemInteger
w_s1pI lvl_r1qU
of _ [Occ=Dead] { (# ipv_a17D, ipv1_a17E #) ->
(ipv_a17D, ipv1_a17E)
} } in
(# case ds_s16Q of _ [Occ=Dead] { (q_a11V, r_X12h) ->
case integer-gmp-1.0.0.0:GHC.Integer.Type.integerToInt r_X12h
of wild3_a17c { __DEFAULT ->
GHC.Types.I# wild3_a17c
}
},
case ds_s16Q of _ [Occ=Dead] { (q_X12h, r_X129) ->
case Main.$w$sdigits q_X12h
of _ [Occ=Dead] { (# ww1_s1pO, ww2_s1pP #) ->
GHC.Types.: # Int ww1_s1pO ww2_s1pP
}
} #);
True ->
(# GHC.Num.$fNumInt_$cfromInteger w_s1pI, GHC.Types.[] # Int #)
}
}
end Rec }
digitsV2.txt:
lvl_r1xl = __integer 10
Rec {
Main.$wgo [InlPrag=[0], Occ=LoopBreaker]
:: Integer -> [Int] -> (# Int, [Int] #)
[GblId, Arity=2, Str=DmdType <S,U><L,U>]
Main.$wgo =
\ (w_s1wh :: Integer) (w1_s1wi :: [Int]) ->
case integer-gmp-1.0.0.0:GHC.Integer.Type.ltInteger#
w_s1wh lvl_r1xl
of wild_a1dp { __DEFAULT ->
case GHC.Prim.tagToEnum# # Bool wild_a1dp of _ [Occ=Dead] {
False ->
case integer-gmp-1.0.0.0:GHC.Integer.Type.quotRemInteger
w_s1wh lvl_r1xl
of _ [Occ=Dead] { (# ipv_a1dB, ipv1_a1dC #) ->
Main.$wgo
ipv_a1dB
(GHC.Types.:
# Int
(case integer-gmp-1.0.0.0:GHC.Integer.Type.integerToInt ipv1_a1dC
of wild2_a1ea { __DEFAULT ->
GHC.Types.I# wild2_a1ea
})
w1_s1wi)
};
True -> (# GHC.Num.$fNumInt_$cfromInteger w_s1wh, w1_s1wi #)
}
}
end Rec }
I actually couldn't find the relevant section for show.txt. I'll work on that later.
Right off the bat, digitsV2.hs produces shorter code. That's probably a good sign for it.
digits.hs seems to be following this psuedocode:
def digits(w_s1pI):
if w_s1pI < 10: return [fromInteger(w_s1pI)]
else:
ds_s16Q = quotRem(w_s1pI, 10)
q_X12h = ds_s16Q[0]
r_X12h = ds_s16Q[1]
wild3_a17c = integerToInt(r_X12h)
ww1_s1pO = r_X12h
ww2_s1pP = digits(q_X12h)
ww2_s1pP.pushFront(ww1_s1pO)
return ww2_s1pP
digitsV2.hs seems to be following this psuedocode:
def digitsV2(w_s1wh, w1_s1wi=[]): # actually disguised as go(), as #No_signal wrote
if w_s1wh < 10:
w1_s1wi.pushFront(fromInteger(w_s1wh))
return w1_s1wi
else:
ipv_a1dB, ipv1_a1dC = quotRem(w_s1wh, 10)
w1_s1wi.pushFront(integerToIn(ipv1a1dC))
return digitsV2(ipv1_a1dC, w1_s1wi)
It might not be that these functions mutate lists like my psuedocode suggests, but this immediately suggests something: it looks as if digitsV2 is fully tail-recursive, whereas digits is actually not (may have to use some Haskell trampoline or something). It appears as if Haskell needs to store all the remainders in digits before pushing them all to the front of the list, whereas it can just push them and forget about them in digitsV2. This is purely speculation, but it is well-founded speculation.

How can I use map function with random number

This code is from http://elm-lang.org/edit/examples/Intermediate/Stamps.elm. I did a minor change, see below.
import Mouse
import Window
import Random
main : Signal Element
main = lift2 scene Window.dimensions clickLocations
-- for a good time, remove "sampleOn Mouse.clicks" ;)
clickLocations : Signal [(Int,Int)]
clickLocations = foldp (::) [] (sampleOn Mouse.clicks Mouse.position)
scene : (Int,Int) -> [(Int,Int)] -> Element
scene (w,h) locs =
let p = Random.float (fps 25)
drawPentagon p (x,y) =
ngon 5 20 |> filled (hsla p 0.9 0.6 0.7)
|> move (toFloat x - toFloat w / 2, toFloat h / 2 - toFloat y)
|> rotate (toFloat x)
in layers [ collage w h (map (drawPentagon <~ p) locs) // I want to change different color each time, error here!
, plainText "Click to stamp a pentagon." ]
How can I pass a signal when using map function?
In your code, you have drawPentagon <~ p which has the type Signal ((Int, Int) -> Form)
The type of map is map : (a -> b) -> [a] -> [b] which is causing the type error. It is basically saying, that map is expecting a function a -> b but you've given it a Signal ((Int, Int) -> From).
One way to try and accomplish what you're doing is to make p a parameter of scene and use lift3 to pass in Random.float (fps 25). So, you would end up with this:
import Mouse
import Window
import Random
main : Signal Element
main = lift3 scene Window.dimensions clickLocations (Random.float (fps 25))
-- for a good time, remove "sampleOn Mouse.clicks" ;)
clickLocations : Signal [(Int,Int)]
clickLocations = foldp (::) [] (sampleOn Mouse.clicks Mouse.position)
scene : (Int,Int) -> [(Int,Int)] -> Float -> Element
scene (w,h) locs p =
let drawPentagon p (x,y) =
ngon 5 20 |> filled (hsla p 0.9 0.6 0.7)
|> move (toFloat x - toFloat w / 2, toFloat h / 2 - toFloat y)
|> rotate (toFloat x)
in layers [ collage w h (map (drawPentagon p) locs)
, plainText "Click to stamp a pentagon." ]
Is this what you were trying to do?

Resources