Skip to content

Instantly share code, notes, and snippets.

@rntz
Created August 20, 2014 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rntz/6eda3532ee7b6cfb4507 to your computer and use it in GitHub Desktop.
Save rntz/6eda3532ee7b6cfb4507 to your computer and use it in GitHub Desktop.
module Main where
import Control.Monad (mapM_)
import Data.List (intersperse)
import System.CPUTime
import Text.Printf
import qualified Data.Set as Set
import Text.Derp
time :: IO t -> IO t
time a = do start <- getCPUTime
v <- a
end <- getCPUTime
let diff :: Double
diff = (fromIntegral (end - start)) / (10 ** 12)
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v
-- amb is the grammar we want, namely russ cox's
-- S = S + S | 1
testOk :: Int -> [Token String]
testOk n = intersperse (Token "+" "+") (replicate n (Token "1" "1"))
runOk :: Int -> Set.Set String
runOk n = runParse (amb ()) (testOk n)
test n = parser `seq` -- don't count time to construct parser
length input `seq` -- or input
time $ do let parses = runParse parser input
let first = head (Set.toList parses)
printf "on input of length %d first parse is: %s...\n"
n (take 10 first)
where parser = amb ()
input = testOk n
main = mapM_ test [1..14]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment