Jikka-5.0.11.1: A transpiler from Python to C++ for competitive programming
Copyright (c) Kimiyuki Onaka 2021 Apache License 2.0 kimiyuki95@gmail.com experimental portable None Haskell2010

Jikka.RestrictedPython.Convert.ToCore

Description

Synopsis

# Documentation

run :: (MonadAlpha m, MonadError Error m) => Program -> m Program Source #

run converts programs of our restricted Python-like language to programs of our core language. This assumes the follwing conditions:

• doesntHaveSubscriptionInLoopCounters
• doesntHaveLeakOfLoopCounters
• doesntHaveAssignmentToLoopCounters
• doesntHaveAssignmentToLoopIterators
• doesntHaveReturnInLoops
• doesntHaveNonTrivialSubscriptedAssignmentInForLoops

For example, this converts the following:

def solve(n):
if n == 0:
return 1
else:
return n * solve(n - 1)

to:

let solve n =
if n == 0 then
1
else:
n * solve (n - 1)
in solve

Also, this converts the following:

def solve(n):
a = 0
b = 1
for _ in range(n):
c = a + b
a = b
b = c
return a

to:

let solve n =
fst (foldl (fun (a, b) i -> (b, a + b)) (0, 1) [0 .. n - 1])
in solve

runForStatement :: (MonadState Env m, MonadAlpha m, MonadError Error m) => Target' -> Expr' -> [Statement] -> [Statement] -> [[Statement]] -> m Expr Source #

runForStatement converts for-loops to foldl. For example, this converts the following:

# a, b are defined
for _ in range(n):
c = a + b
a = b
b = c
...

to:

let (a, b) = foldl (fun (a, b) i -> (b, a + b)) (a, b) (range n)
in ...

runIfStatement :: (MonadState Env m, MonadAlpha m, MonadError Error m) => Expr' -> [Statement] -> [Statement] -> [Statement] -> [[Statement]] -> m Expr Source #

runIfStatement converts if-loops to if-exprs.

# a, b are defined
if True:
a = 0
b = 1
c = 3
else:
a = 1
c = 10
...

to:

let (a, c) = if true then (0, 3) else (1, 10)
in ...