Skip to content

Commit

Permalink
Merge pull request #86 from kmyk/list-append
Browse files Browse the repository at this point in the history
feat: Make `xs.append(x)` available
  • Loading branch information
kmyk authored Jul 20, 2021
2 parents b6c4cab + 9fdf5c8 commit e64620f
Show file tree
Hide file tree
Showing 36 changed files with 206 additions and 159 deletions.
5 changes: 5 additions & 0 deletions docs/language.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ Python とほとんど同じです。

- 処理系は `assert` 文を最適化のヒントとして利用できます。

### expr statements

式文は副作用のある処理を実行します。
現在は `xs.append(x)` の形の式文のみが利用可能です。


## Semantics (Exprs)

Expand Down
5 changes: 5 additions & 0 deletions docs/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ The augmented assignment `x @= a` statement binds the value `x @ a` to the varia

- Implementations can use `assert` statements as hints for optimization.

### expr statements

Expr statements runs processes with side effects.
Now only `xs.append(x)` is available.


## Semantics (Exprs)

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- CODE FESTIVAL 2015 決勝 [D - 足ゲームII](https://atcoder.jp/contests/code-festival-2015-final-open/tasks/codefestival_2015_final_d)
- :hourglass: TLE `m_solutions2019_e.py`
- M-SOLUTIONS Programming Contest [E - Product of Arithmetic Progression](https://atcoder.jp/contests/m-solutions2019/tasks/m_solutions2019_e?lang=ja)
- :warning: CE `wip/point_add_range_sum.py`
- :hourglass: TLE `wip/point_add_range_sum.py`
- Library Checker [Point Add Range Sum](https://judge.yosupo.jp/problem/point_add_range_sum)
- A normal segment tree / 通常のセグメント木
- :warning: CE `wip/dp_w.py`
Expand Down
4 changes: 4 additions & 0 deletions examples/data/dp_w.sample-1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5 3
1 3 10
2 4 -10
3 5 10
1 change: 1 addition & 0 deletions examples/data/dp_w.sample-1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
5 changes: 5 additions & 0 deletions examples/data/dp_w.sample-2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30
1 change: 1 addition & 0 deletions examples/data/dp_w.sample-2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
90
2 changes: 2 additions & 0 deletions examples/data/dp_w.sample-3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 1
1 1 -10
1 change: 1 addition & 0 deletions examples/data/dp_w.sample-3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
6 changes: 6 additions & 0 deletions examples/data/dp_w.sample-4.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 5
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 change: 1 addition & 0 deletions examples/data/dp_w.sample-4.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5000000000
9 changes: 9 additions & 0 deletions examples/data/dp_w.sample-5.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
6 8
5 5 3
1 1 10
1 6 -8
3 6 5
3 4 9
5 5 -2
1 3 -6
4 6 -7
1 change: 1 addition & 0 deletions examples/data/dp_w.sample-5.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
7 changes: 7 additions & 0 deletions examples/data/point_add_range_sum.sample-1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
5 5
1 2 3 4 5
1 0 5
1 2 4
0 3 10
1 0 5
1 0 3
4 changes: 4 additions & 0 deletions examples/data/point_add_range_sum.sample-1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
15
7
25
6
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ def solve(n: int, q: int, a: List[int], t: List[int], args1: List[int], args2: L
r = args2[i]
ans.append(sum(a[l:r]))
return ans

def main() -> None:
n, q = map(int, input().split())
a = list(map(int, input().split()))
assert len(a) == n
t = list(range(q))
args1 = list(range(q))
args2 = list(range(q))
for i in range(q):
t[i], args1[i], args2[i] = map(int, input().split())
ans = solve(n, q, a, t, args1, args2)
for i in range(len(ans)):
print(ans[i])

if __name__ == '__main__':
main()
18 changes: 16 additions & 2 deletions examples/wip/dp_w.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# https://atcoder.jp/contests/dp/tasks/dp_w
import math
from typing import *

INF = 10 ** 18

def solve(n: int, m: int, l: List[int], r: List[int], a: List[int]) -> int:
assert 1 <= n <= 2 * 10 ** 5
assert 1 <= m <= 2 * 10 ** 5
Expand All @@ -11,7 +12,7 @@ def solve(n: int, m: int, l: List[int], r: List[int], a: List[int]) -> int:
assert all(1 <= l[i] <= r[i] <= n for i in range(m))
assert all(abs(a_i) <= 10 ** 9 for a_i in a)

dp = [-math.inf for _ in range(n + 1)]
dp = [-INF for _ in range(n + 1)]
dp[0] = 0
for x in range(1, n + 1):
for y in range(x):
Expand All @@ -21,3 +22,16 @@ def solve(n: int, m: int, l: List[int], r: List[int], a: List[int]) -> int:
b += a[i]
dp[x] = min(dp[x], b)
return dp[n]

def main() -> None:
n, m = map(int, input().split())
l = list(range(m))
r = list(range(m))
a = list(range(m))
for i in range(m):
l[i], r[i], a[i] = map(int, input().split())
ans = solve(n, m, l, r, a)
print(ans)

if __name__ == '__main__':
main()
9 changes: 9 additions & 0 deletions src/Jikka/CPlusPlus/Convert/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ runAppBuiltin env f args = wrapError' ("converting builtin " ++ X.formatBuiltinI
],
Y.Var ys
)
X.Snoc t -> go2' $ \xs x -> do
t <- runType t
ys <- Y.newFreshName Y.LocalNameKind
return
( [ Y.Declare (Y.TyVector t) ys (Just xs),
Y.callMethod' (Y.Var ys) "push_back" [x]
],
Y.Var ys
)
X.Foldl t1 t2 -> go3'' $ \f init xs -> do
(stmtsInit, init) <- runExpr env init
(stmtsXs, xs) <- runExpr env xs
Expand Down
39 changes: 18 additions & 21 deletions src/Jikka/Common/IOFormat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,16 @@ normalizeFormatTree = \case
Seq formats -> formats
format -> [format]
in Seq (concatMap (unSeq . normalizeFormatTree) formats)
Loop i n body -> Loop i n (normalizeFormatTree body)

substFormatExpr :: String -> String -> FormatExpr -> FormatExpr
substFormatExpr name value = go
where
go = \case
Var x -> Var (if x == name then value else x)
Plus e k -> Plus (go e) k
At e i -> At (go e) (if i == name then value else i)
Len e -> Len (go e)

substFormatTree :: String -> String -> FormatTree -> FormatTree
substFormatTree name value = mapFormatTree $ \case
Exp e -> Exp (substFormatExpr name value e)
format -> format
Loop i n body -> case normalizeFormatTree body of
Seq [] -> Seq []
body -> Loop i n body

normalizeIOFormat :: IOFormat -> IOFormat
normalizeIOFormat format =
format
{ inputTree = normalizeFormatTree (inputTree format),
outputTree = normalizeFormatTree (outputTree format)
}

hasNewline :: FormatTree -> Bool
hasNewline = \case
Expand All @@ -95,16 +90,17 @@ formatFormatTree =
go text | patt `isPrefixOf` text = subst ++ go (drop (length patt) text)
go [] = []
go (c : s) = c : go s
unwords' = replace "\n " "\n" . replace " \n" "\n" . unwords
unwords' = replace "\n\n" "\n" . replace "\n " "\n" . replace " \n" "\n" . unwords
in \case
Exp e -> formatFormatExpr e
Newline -> "\n"
Newline -> "(newline)\n"
Seq formats -> unwords' (map formatFormatTree formats)
Loop i n body ->
let first = substFormatTree i "0" body
last = substFormatTree i (formatFormatExpr n) body
dots = if hasNewline body then "...\n" else "..."
in unwords' [formatFormatTree first, dots, formatFormatTree last]
unwords'
[ "for " ++ i ++ " < " ++ formatFormatExpr n ++ " {\n",
formatFormatTree body ++ "\n",
"}"
]

formatIOFormat :: IOFormat -> String
formatIOFormat format =
Expand Down Expand Up @@ -172,6 +168,7 @@ makeReadValueIO toInt fromInt toList fromList format = wrapError' "Jikka.Common.
n <- case n of
Var n -> toInt =<< lookup n
Plus (Var n) k -> (+ k) <$> (toInt =<< lookup n)
Len (Var xs) -> toInteger . V.length <$> (toList =<< lookup xs)
_ -> throwInternalError $ "invalid loop size in input tree: " ++ formatFormatExpr n
liftIO $ modifyIORef' sizes (M.insert i n)
forM_ [0 .. n -1] $ \i' -> do
Expand Down
81 changes: 0 additions & 81 deletions src/Jikka/Core/Convert/MakeEager.hs

This file was deleted.

9 changes: 7 additions & 2 deletions src/Jikka/Core/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import qualified Data.Vector as V
import Jikka.Common.Alpha
import Jikka.Common.Error
import Jikka.Common.Matrix
import qualified Jikka.Core.Convert.MakeEager as MakeEager
import Jikka.Core.Format (formatBuiltinIsolated)
import Jikka.Core.Language.BuiltinPatterns
import Jikka.Core.Language.Expr
import Jikka.Core.Language.Lint
import Jikka.Core.Language.Runtime
Expand Down Expand Up @@ -181,6 +181,7 @@ callBuiltin builtin args = wrapError' ("while calling builtin " ++ formatBuiltin
ModMatPow _ -> go3' pure valueToInt valueToInt valueFromModMatrix $ \f k m -> join (matpow' <$> valueToModMatrix m f <*> pure k)
-- list functions
Cons _ -> go2 pure valueToList ValList V.cons
Snoc _ -> go2 valueToList pure ValList V.snoc
Foldl _ _ -> go3' pure pure valueToList id $ \f x a -> V.foldM (\x y -> callValue f [x, y]) x a
Scanl _ _ -> go3' pure pure valueToList ValList $ \f x a -> scanM (\x y -> callValue f [x, y]) x a
Len _ -> go1 valueToList ValInt (fromIntegral . V.length)
Expand Down Expand Up @@ -243,6 +244,11 @@ evaluateExpr env = \case
Nothing -> throwInternalError $ "undefined variable: " ++ unVarName x
Just val -> return val
Lit lit -> literalToValue lit
If' _ p e1 e2 -> do
p <- valueToBool =<< evaluateExpr env p
if p
then evaluateExpr env e1
else evaluateExpr env e2
e@(App _ _) -> do
let (f, args) = curryApp e
f <- evaluateExpr env f
Expand Down Expand Up @@ -279,5 +285,4 @@ callProgram prog args = wrapError' "Jikka.Core.Evaluate" $ do

run :: (MonadAlpha m, MonadFix m, MonadError Error m) => Program -> [Value] -> m Value
run prog args = do
prog <- MakeEager.run prog
callProgram prog args
1 change: 1 addition & 0 deletions src/Jikka/Core/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ analyzeBuiltin = \case
ModMatPow _ -> fun "modmatpow"
-- list functions
Cons t -> Fun [t] "cons"
Snoc t -> Fun [t] "snoc"
Foldl t1 t2 -> Fun [t1, t2] "foldl"
Scanl t1 t2 -> Fun [t1, t2] "scanl"
Iterate t -> Fun [t] "iterate"
Expand Down
2 changes: 2 additions & 0 deletions src/Jikka/Core/Language/BuiltinPatterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ pattern Nil' t = Lit (LitNil t)

pattern Cons' t e1 e2 = AppBuiltin2 (Cons t) e1 e2

pattern Snoc' t e1 e2 = AppBuiltin2 (Snoc t) e1 e2

pattern Foldl' t1 t2 e1 e2 e3 = AppBuiltin3 (Foldl t1 t2) e1 e2 e3

pattern Scanl' t1 t2 e1 e2 e3 = AppBuiltin3 (Scanl t1 t2) e1 e2 e3
Expand Down
2 changes: 2 additions & 0 deletions src/Jikka/Core/Language/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ data Builtin

-- | \(: \forall \alpha. \alpha \to \list(\alpha) \to \list(\alpha)\)
Cons Type
| -- | \(: \forall \alpha. \list(alpha) \to \alpha \to \list(\alpha)\)
Snoc Type
| -- | \(: \forall \alpha \beta. (\beta \to \alpha \to \beta) \to \beta \to \list(\alpha) \to \beta\)
Foldl Type Type
| -- | \(: \forall \alpha \beta. (\beta \to \alpha \to \beta) \to \beta \to \list(\alpha) \to \list(\beta)\)
Expand Down
1 change: 1 addition & 0 deletions src/Jikka/Core/Language/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ builtinToType = \case
ModMatPow n -> Fun3Ty (matrixTy n n) IntTy IntTy (matrixTy n n)
-- list functions
Cons t -> Fun2Ty t (ListTy t) (ListTy t)
Snoc t -> Fun2Ty (ListTy t) t (ListTy t)
Foldl t1 t2 -> Fun3Ty (Fun2Ty t2 t1 t2) t2 (ListTy t1) t2
Scanl t1 t2 -> Fun3Ty (Fun2Ty t2 t1 t2) t2 (ListTy t1) (ListTy t2)
Len t -> FunTy (ListTy t) IntTy
Expand Down
2 changes: 2 additions & 0 deletions src/Jikka/Core/Language/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mapTypeInBuiltin f = \case
ModMatPow n -> ModMatPow n
-- list functionslist
Cons t -> Cons (f t)
Snoc t -> Snoc (f t)
Foldl t1 t2 -> Foldl (f t1) (f t2)
Scanl t1 t2 -> Scanl (f t1) (f t2)
Len t -> Len (f t)
Expand Down Expand Up @@ -272,6 +273,7 @@ isConstantTimeBuiltin = \case
ModMatPow _ -> True
-- list functions
Cons _ -> False
Snoc _ -> False
Foldl _ _ -> False
Scanl _ _ -> False
Len _ -> True
Expand Down
Loading

0 comments on commit e64620f

Please sign in to comment.