Lazy State monad in Idris - lazy-evaluation

I am trying to generate a Stream value in a monadic context. For example, the following code hangs in Idris (or segfaults):
import Control.Monad.State
count : State Int (Stream Int)
count = do
x <- get
put (x+1)
t <- count
pure (x :: t)
stream : List Int
stream = take 10 (evalState count 0)
However, similar code works in Haskell, using the lazy state monad:
import Control.Monad.State.Lazy
count :: State Int [Int]
count = do
x <- get
put (x+1)
t <- count
return (x : t)
stream :: [Int]
stream = take 10 (evalState count 0)
This is probably because of lazy pattern matching used in the bind operation of the state monad in Haskell. Is there something similar in Idris?

Related

Compute NFA transitions in parallel

I have this piece of code written in F#:
type NondeterministicFiniteAutomaton = {
initialState: string
finalStates: string List
transitions: Map<string * char, string List>
}
let private nextState (symbol:char) (state:string) (transitions:Map<string * char, string List>) =
transitions |> Map.tryFind (state, symbol)
let rec private haltState (input:string) (index:int) (state:string) (transitions:Map<string * char, string List>) =
match index with
| x when x = input.Length -> state
| _ ->
match nextState input.[index] state transitions with
| x when x.IsNone -> null
| x -> haltState input (index+1) x.Value transitions
In the last line, x.Value will return a list of states that my automaton can enter, say ["s1"; "s2"; "s3"]. For each state in this list, I want to call haltState in parallel, thus calculating each possible path in parallel.
How can I call them in parallel?
How can I "join" them when they are done?
I'd recommend first writing the complete sequential version. Then you can see if adding parallelism makes sense for the computations you'll be doing.
As for joining the results, this is something you'll need to do even in the sequential version. Your haltState function returns a single string, but if this is a NFA, then it may end in multiple different states. So you could change it to return a sequence of possible results:
let rec private haltState (input:string) (index:int) (state:string) (transitions:Map<string * char, string List>) =
match index with
| x when x = input.Length -> Seq.singleton x
| _ ->
match nextState input.[index] state transitions with
| None -> Seq.empty
| Some states ->
states |> Seq.collect (fun state ->
haltState input (index+1) state transitions)
This returns a sequence and it joins the sequences generated for multiple possible states using Seq.collect. Note that I also used more idiomatic pattern matching on option values.
You could parallelize this using Array.Parallel.map, but I doubt this will make the processing faster - the overhead is probably going to be larger.
let rec private haltState (input:string) (index:int) (state:string) (transitions:Map<string * char, string List>) =
match index with
| x when x = input.Length -> [| state |]
| _ ->
match nextState input.[index] state transitions with
| None -> [| |]
| Some states ->
states
|> Array.ofSeq
|> Array.Parallel.collect (fun state -> haltState input (index+1) state transitions)

Remove the first char to extract an integer (unhandled exception: Subscript)

I'm trying to write a function which extracts only the integer in a string.
All my strings have the format Ci where C is a single character and i is an integer. I would like to be able to remove the C from my string.
I tried something like this :
fun transformKripke x =
if size x > 1
then String.substring (x, 1, size x)
else x
But unfortunately, I get an error like unhandled exception: Subscript.
I assume it's because sometimes my string will be empty and size of empty string is not working. But I don't know how to make it work... :/
Thanks in advance for your help
Best Regards.
The problem is calling String.substring (x, 1, size x) when x is not long enough.
The following should fix your immediate problem:
fun transformKripke s =
if size s = 0
then s
else String.substring (s, 1, size s)
or slightly prettier:
fun transformKripke s =
if size s = 0
then s
else String.extract (s, 1, NONE) (* means "until the end" *)
But you may want to consider naming your function something more general so that it can be useful in more senses than performing a Kripke transform (whatever that is). For example, you may want to be able to extract an actual int the first time one occurs anywhere in a string, regardless of how many non-integer characters that precede it:
fun extractInt s =
let val len = String.size s
fun helper pos result =
if pos = len
then result
else let val c = String.sub (s, pos)
val d = ord c - ord #"0"
in case (Char.isDigit c, result) of
(true, NONE) => helper (pos+1) (SOME d)
| (true, SOME ds) => helper (pos+1) (SOME (ds * 10 + d))
| (false, NONE) => helper (pos+1) NONE
| (false, SOME ds) => SOME ds
end
in helper 0 NONE
end
My mistake was stupid,
The string is finishing at size x -1 not size x. So now it's correct :
fun transformKripke x =
if size x > 1
then String.substring (x, 1, (size x)-1)
else x
Hope it will help ! :)

performance of static member constraint functions

I'm trying to learn static member constraints in F#. From reading Tomas Petricek's blog post, I understand that writing an inline function that "uses only operations that are themselves written using static member constraints" will make my function work correctly for all numeric types that satisfy those constraints. This question indicates that inline works somewhat similarly to c++ templates, so I wasn't expecting any performance difference between these two functions:
let MultiplyTyped (A : double[,]) (B : double[,]) =
let rA, cA = (Array2D.length1 A) - 1, (Array2D.length2 A) - 1
let cB = (Array2D.length2 B) - 1
let C = Array2D.zeroCreate<double> (Array2D.length1 A) (Array2D.length2 B)
for i = 0 to rA do
for k = 0 to cA do
for j = 0 to cB do
C.[i,j] <- C.[i,j] + A.[i,k] * B.[k,j]
C
let inline MultiplyGeneric (A : 'T[,]) (B : 'T[,]) =
let rA, cA = Array2D.length1 A - 1, Array2D.length2 A - 1
let cB = Array2D.length2 B - 1
let C = Array2D.zeroCreate<'T> (Array2D.length1 A) (Array2D.length2 B)
for i = 0 to rA do
for k = 0 to cA do
for j = 0 to cB do
C.[i,j] <- C.[i,j] + A.[i,k] * B.[k,j]
C
Nevertheless, to multiply two 1024 x 1024 matrixes, MultiplyTyped completes in an average of 2550 ms on my machine, whereas MultiplyGeneric takes about 5150 ms. I originally thought that zeroCreate was at fault in the generic version, but changing that line to the one below didn't make a difference.
let C = Array2D.init<'T> (Array2D.length1 A) (Array2D.length2 B) (fun i j -> LanguagePrimitives.GenericZero)
Is there something I'm missing here to make MultiplyGeneric perform the same as MultiplyTyped? Or is this expected?
edit: I should mention that this is VS2010, F# 2.0, Win7 64bit, release build. Platform target is x64 (to test larger matrices) - this makes a difference: x86 produces similar results for the two functions.
Bonus question: the type inferred for MultiplyGeneric is the following:
val inline MultiplyGeneric :
^T [,] -> ^T [,] -> ^T [,]
when ( ^T or ^a) : (static member ( + ) : ^T * ^a -> ^T) and
^T : (static member ( * ) : ^T * ^T -> ^a)
Where does the ^a type come from?
edit 2: here's my testing code:
let r = new System.Random()
let A = Array2D.init 1024 1024 (fun i j -> r.NextDouble())
let B = Array2D.init 1024 1024 (fun i j -> r.NextDouble())
let test f =
let sw = System.Diagnostics.Stopwatch.StartNew()
f() |> ignore
sw.Stop()
printfn "%A" sw.ElapsedMilliseconds
for i = 1 to 5 do
test (fun () -> MultiplyTyped A B)
for i = 1 to 5 do
test (fun () -> MultiplyGeneric A B)
Good question. I'll answer the easy part first: the ^a is just part of the natural generalization process. Imagine you had a type like this:
type T = | T with
static member (+)(T, i:int) = T
static member (*)(T, T) = 0
Then you can still use your MultiplyGeneric function with arrays of this type: multiplying elements of A and B will give you ints, but that's okay because you can still add them to elements of C and get back values of type T to store back into C.
As to your performance question, I'm afraid I don't have a great explanation. Your basic understanding is right - using MultiplyGeneric with double[,] arguments should be equivalent to using MultiplyTyped. If you use ildasm to look at the IL the compiler generates for the following F# code:
let arr = Array2D.zeroCreate 1024 1024
let f1 = MultiplyTyped arr
let f2 = MultiplyGeneric arr
let timer = System.Diagnostics.Stopwatch()
timer.Start()
f1 arr |> ignore
printfn "%A" timer.Elapsed
timer.Restart()
f2 arr |> ignore
printfn "%A" timer.Elapsed
then you can see that the compiler really does generate identical code for each of them, putting the inlined code for MultipyGeneric into an internal static function. The only difference that I see in the generated code is in the names of locals, and when running from the command line I get roughly equal elapsed times. However, running from FSI I see a difference similar to what you've reported.
It's not clear to me why this would be. As I see it there are two possibilities:
FSI's code generation may be doing something slightly different than the static compiler
The CLR's JIT compiler may be treat code generated at runtime slightly differently from compiled code. For instance, as I mentioned my code above using MultiplyGeneric actually results in an internal method that contains the inlined body. Perhaps the CLR's JIT handles the difference between public and internal methods differently when they are generated at runtime than when they are in statically compiled code.
I'd like to see your benchmarks. I don't get the same results (VS 2012 F# 3.0 Win 7 64-bit).
let m = Array2D.init 1024 1024 (fun i j -> float i * float j)
let test f =
let sw = System.Diagnostics.Stopwatch.StartNew()
f() |> ignore
sw.Stop()
printfn "%A" sw.Elapsed
test (fun () -> MultiplyTyped m m)
> 00:00:09.6013188
test (fun () -> MultiplyGeneric m m)
> 00:00:09.1686885
Decompiling with Reflector, the functions look identical.
Regarding your last question, the least restrictive constraint is inferred. In this line
C.[i,j] <- C.[i,j] + A.[i,k] * B.[k,j]
because the result type of A.[i,k] * B.[k,j] is unspecified, and is passed immediately to (+), an extra type could be involved. If you want to tighten the constraint you can replace that line with
let temp : 'T = A.[i,k] * B.[k,j]
C.[i,j] <- C.[i,j] + temp
That will change the signature to
val inline MultiplyGeneric :
A: ^T [,] -> B: ^T [,] -> ^T [,]
when ^T : (static member ( * ) : ^T * ^T -> ^T) and
^T : (static member ( + ) : ^T * ^T -> ^T)
EDIT
Using your test, here's the output:
//MultiplyTyped
00:00:09.9904615
00:00:09.5489653
00:00:10.0562346
00:00:09.7023183
00:00:09.5123992
//MultiplyGeneric
00:00:09.1320273
00:00:08.8195283
00:00:08.8523408
00:00:09.2496603
00:00:09.2950196
Here's the same test on ideone (with a few minor changes to stay within the time limit: 512x512 matrix and one test iteration). It runs F# 2.0 and produced similar results.

What's the Scala syntax for a function taking any subtype of Ordered[A]?

I want to write a function that works on any Scala type with a total ordering (i.e. I can use '<' on it). What's the syntax for that? The best I've come up with is
def lessThan[T <: Ordered[T]](x: T, Y: T) = x < y
That doesn't work, though, when I try using it from the REPL:
scala> lessThan(1, 2)
<console>:8: error: inferred type arguments [Int] do not conform to method lessThan's type parameter bounds [T <: Ordered[T]]
lessThan(1, 2)
^
scala> import runtime._
import runtime._
scala> lessThan(new RichInt(1), new RichInt(2))
<console>:8: error: inferred type arguments [scala.runtime.RichInt] do not conform to method lessThan's type parameter bounds [T <: Ordered[T]]
lessThan(new RichInt(1), new RichInt(2))
Essentially, I believe I want the equivalent of this Haskell code:
lessThan :: (Ord a) => a -> a -> Bool
lessThan x y = x < y
I'm using scala 2.7.3 on a Debian system.
What am I missing, and where?
The equivalent of Haskell's type classes in Scala is done via implicits. There are two ways to do what you want
The first is with view bounds
scala> def lessThan[T <% Ordered[T]](x : T, y : T) = x < y
lessThan: [T](T,T)(implicit (T) => Ordered[T])Boolean
scala> lessThan(1,2)
res0: Boolean = true
The second is with an implicit parameter
scala> def lessThan[T](x : T, y : T)(implicit f : T => Ordered[T]) = x < y
lessThan: [T](T,T)(implicit (T) => Ordered[T])Boolean
scala> lessThan(4,3)
res1: Boolean = false
The former is syntax sugar for the later. The later allows more flexibility.

How do I translate this Haskell to F#?

I'm trying to learn F# by translating some Haskell code I wrote a very long time ago, but I'm stuck!
percent :: Int -> Int -> Float
percent a b = (fromInt a / fromInt b) * 100
freqs :: String -> [Float]
freqs ws = [percent (count x ws) (lowers ws) | x <- ['a' .. 'z']]
I've managed this:
let percent a b = (float a / float b) * 100.
although i dont like having to have the . after the 100.
What is the name of the operation I am performing in freqs, and how do I translate it to F#?
Edit: count and lowers are Char -> String -> Int and String -> Int respectively, and I have translated these already.
This is a list comprehension, and in F# it looks like the last two lines below:
// stub out since don't know the implementation
let count (c:char) (s:string) = 4
let lowers (s:string) = 10
// your code
let percent a b = (float a / float b) * 100.
let freq ws = [for x in ['a'..'z'] do
yield percent (count x ws) (lowers ws)]
More generally I think Haskell list comprehensions have the form suggested by the example below, and the corresponding F# is shown.
// Haskell
// [e(x,y) | x <- l1, y <- l2, pred(x,y)]
// F#
[for x in l1 do
for y in l2 do
if pred(x,y) then
yield e(x,y)]
Note that Brian's F# code:
let freq ws = [for x in ['a'..'z'] do yield percent (count x ws) (lowers ws)]
Can be written more elegantly as:
let freq ws = [for x in 'a'..'z' -> percent (count x ws) (lowers ws)]

Resources