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

Add Stream Sieve benchmark from Typed Racket #466

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions benchmarks/streamSieve/lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- Naive stream-based prime sieve benchmark. (Based on a typed-racket benchmark)
-- Based on https://github.com/bennn/gtp-benchmarks/tree/master/benchmarks/sieve

local m = {}

----------------------------------------------

-- Simple streams library for building infinite lists.
-- A stream is a cons of a value and a thunk that computes the next value.



-- TODO: with recursive types, this could be (any -> Stream)


local function make_stream(first, rest)
return {first=first, rest=rest}
end

local function stream_head(st)
return st.first
end

local function stream_tail(st)
return st.rest()
end

local function stream_get(st, n)
for _ = 1, n-1 do
st = stream_tail(st)
end
return stream_head(st)
end

----------------------------------------------

-- Build a stream of integers starting from 1
local function count_from(n)
return make_stream(n, function() return count_from(n+1) end)
end

-- Filter all multiples of n
local function sift(n, st)
local hd = stream_head(st)
local tl = stream_tail(st)
if hd % n == 0 then
return sift(n, tl)
else
return make_stream(hd, function() return sift(n, tl) end)
end
end

-- Naive sieve of Erasthostenes
local function sieve(st)
local hd = stream_head(st)
local tl = stream_tail(st)
return make_stream(hd, function() return sieve(sift(hd, tl)) end)
end

----

function m.get_prime(n)
local primes = sieve(count_from(2))
return stream_get(primes, n)
end

return m
11 changes: 11 additions & 0 deletions benchmarks/streamSieve/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--
-- This benchmark runs a naive prime sieve using lazy streams. Taken from the
-- Typed Racket benchmarks:
-- * https://github.com/nuprl/gradual-typing-performance/tree/master/benchmarks/sieve
-- * https://github.com/bennn/gtp-benchmarks/tree/master/benchmarks/sieve
--

local sieve = require(arg[1])
local N = tonumber(arg[2]) or 2000 -- or 5500

print(string.format("primes(%d) = %d", N, sieve.get_prime(N)))
67 changes: 67 additions & 0 deletions benchmarks/streamSieve/pallene.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- Naive stream-based prime sieve benchmark. (Based on a typed-racket benchmark)
-- Based on https://github.com/bennn/gtp-benchmarks/tree/master/benchmarks/sieve

local m = {}

----------------------------------------------

-- Simple streams library for building infinite lists.
-- A stream is a cons of a value and a thunk that computes the next value.

record Stream
first: any
rest: () -> any -- TODO: with recursive types, this could be (any -> Stream)
end

local function make_stream(first: any, rest: ()->any): Stream
return {first=first, rest=rest}
end

local function stream_head(st: Stream): any
return st.first
end

local function stream_tail(st: Stream): Stream
return st.rest() as Stream
end

local function stream_get(st: Stream, n: integer): any
for _ = 1, n-1 do
st = stream_tail(st)
end
return stream_head(st)
end

----------------------------------------------

-- Build a stream of integers starting from 1
local function count_from(n: integer): Stream
return make_stream(n, function() return count_from(n+1) end)
end

-- Filter all multiples of n
local function sift(n: integer, st: Stream): Stream
local hd = stream_head(st) as integer
local tl = stream_tail(st) as Stream
if hd % n == 0 then
return sift(n, tl)
else
return make_stream(hd, function() return sift(n, tl) end)
end
end

-- Naive sieve of Erasthostenes
local function sieve(st: Stream): Stream
local hd = stream_head(st) as integer
local tl = stream_tail(st) as Stream
return make_stream(hd, function() return sieve(sift(hd, tl)) end)
end

----

function m.get_prime(n: integer): integer
local primes = sieve(count_from(2))
return stream_get(primes, n)
end

return m