Skip to content

TRManderson/fibunsafearray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Context

On http://fpchat.com Slack, I asked as to how reliably I could use unsafePerformIO in the context of 'either reading or writing using an idempotent operation'. The specific context I was considering was using an IOArray as a cache for the fibonacci sequence up to a fixed n, and making all operations seem pure by using unsafePerformIO on reads and writes. I specifically mentioned idempotence because I intended for each cell to either contain 0 or their final value, and if races occured for simultaneous reads or writes, it would waste a few CPU cycles but still have a correct result..

This repo contains the solution I usedd to test this thought experiment. It seems to work, but please don't do this in production code.

Output

6765

array (1,100) [(1,1),(2,1),(3,2),(4,3),(5,5),(6,8),(7,13),(8,21),(9,34),(10,55),(11,89),(12,144),(13,233),(14,377),(15,610),(16,987),(17,1597),(18,2584),(19,4181),(20,6765),(21,0),(22,0),(23,0),(24,0),(25,0),(26,0),(27,0),(28,0),(29,0),(30,0),(31,0),(32,0),(33,0),(34,0),(35,0),(36,0),(37,0),(38,0),(39,0),(40,0),(41,0),(42,0),(43,0),(44,0),(45,0),(46,0),(47,0),(48,0),(49,0),(50,0),(51,0),(52,0),(53,0),(54,0),(55,0),(56,0),(57,0),(58,0),(59,0),(60,0),(61,0),(62,0),(63,0),(64,0),(65,0),(66,0),(67,0),(68,0),(69,0),(70,0),(71,0),(72,0),(73,0),(74,0),(75,0),(76,0),(77,0),(78,0),(79,0),(80,0),(81,0),(82,0),(83,0),(84,0),(85,0),(86,0),(87,0),(88,0),(89,0),(90,0),(91,0),(92,0),(93,0),(94,0),(95,0),(96,0),(97,0),(98,0),(99,0),(100,0)]

190392490709135

array (1,100) [(1,1),(2,1),(3,2),(4,3),(5,5),(6,8),(7,13),(8,21),(9,34),(10,55),(11,89),(12,144),(13,233),(14,377),(15,610),(16,987),(17,1597),(18,2584),(19,4181),(20,6765),(21,10946),(22,17711),(23,28657),(24,46368),(25,75025),(26,121393),(27,196418),(28,317811),(29,514229),(30,832040),(31,1346269),(32,2178309),(33,3524578),(34,5702887),(35,9227465),(36,14930352),(37,24157817),(38,39088169),(39,63245986),(40,102334155),(41,165580141),(42,267914296),(43,433494437),(44,701408733),(45,1134903170),(46,1836311903),(47,2971215073),(48,4807526976),(49,7778742049),(50,12586269025),(51,20365011074),(52,32951280099),(53,53316291173),(54,86267571272),(55,139583862445),(56,225851433717),(57,365435296162),(58,591286729879),(59,956722026041),(60,1548008755920),(61,2504730781961),(62,4052739537881),(63,6557470319842),(64,10610209857723),(65,17167680177565),(66,27777890035288),(67,44945570212853),(68,72723460248141),(69,117669030460994),(70,190392490709135),(71,0),(72,0),(73,0),(74,0),(75,0),(76,0),(77,0),(78,0),(79,0),(80,0),(81,0),(82,0),(83,0),(84,0),(85,0),(86,0),(87,0),(88,0),(89,0),(90,0),(91,0),(92,0),(93,0),(94,0),(95,0),(96,0),(97,0),(98,0),(99,0),(100,0)]

354224848179261915075

array (1,100) [(1,1),(2,1),(3,2),(4,3),(5,5),(6,8),(7,13),(8,21),(9,34),(10,55),(11,89),(12,144),(13,233),(14,377),(15,610),(16,987),(17,1597),(18,2584),(19,4181),(20,6765),(21,10946),(22,17711),(23,28657),(24,46368),(25,75025),(26,121393),(27,196418),(28,317811),(29,514229),(30,832040),(31,1346269),(32,2178309),(33,3524578),(34,5702887),(35,9227465),(36,14930352),(37,24157817),(38,39088169),(39,63245986),(40,102334155),(41,165580141),(42,267914296),(43,433494437),(44,701408733),(45,1134903170),(46,1836311903),(47,2971215073),(48,4807526976),(49,7778742049),(50,12586269025),(51,20365011074),(52,32951280099),(53,53316291173),(54,86267571272),(55,139583862445),(56,225851433717),(57,365435296162),(58,591286729879),(59,956722026041),(60,1548008755920),(61,2504730781961),(62,4052739537881),(63,6557470319842),(64,10610209857723),(65,17167680177565),(66,27777890035288),(67,44945570212853),(68,72723460248141),(69,117669030460994),(70,190392490709135),(71,308061521170129),(72,498454011879264),(73,806515533049393),(74,1304969544928657),(75,2111485077978050),(76,3416454622906707),(77,5527939700884757),(78,8944394323791464),(79,14472334024676221),(80,23416728348467685),(81,37889062373143906),(82,61305790721611591),(83,99194853094755497),(84,160500643816367088),(85,259695496911122585),(86,420196140727489673),(87,679891637638612258),(88,1100087778366101931),(89,1779979416004714189),(90,2880067194370816120),(91,4660046610375530309),(92,7540113804746346429),(93,12200160415121876738),(94,19740274219868223167),(95,31940434634990099905),(96,51680708854858323072),(97,83621143489848422977),(98,135301852344706746049),(99,218922995834555169026),(100,354224848179261915075)]

Attempt 2

On attempt 2 I tried to add naive write counting. Of course because of the way this is implemented, any race that occured would likely mean that two threads are simultaneously writing 1 to their write counts. This commit also includes code changes

Comments

I didn't really make use of parallelism so the test wasn't very useful.

About

A Haskell thought experiment to test if unsafePerformIO can be used safely

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published