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

fix(cxx): Use temporary variables when unpacking tuples #165

Merged
merged 1 commit into from
Aug 6, 2021

Conversation

riantkb
Copy link
Contributor

@riantkb riantkb commented Aug 6, 2021

#153

Summary

  • tuple を展開する際に、一時変数に一度結果を格納するようにした

Comment

  • 変数名をどういう規則でつければ良いのかわからなかった(これは自分が関数型言語にも言語処理系にも疎いことが原因だと思っています)ので適当な名前がついています……
  • かなり見よう見まねで書いているので、書き方が少し微妙だったり使い方を間違っているものがあったりする場合は直すので言ってください。

Tests

$ cat fib.py

def f(n: int) -> int:
    a = 0
    b = 1
    for _ in range(n):
        c = (a + b) % 1000000007
        a = b
        b = c
    return a


def solve(n: int) -> int:
    return f(n)

$ stack run convert fib.py > fib.cpp
$ cat fib.cpp

// ...
int64_t solve(int64_t n_0) {
    int64_t x1_4 = 1;
    int64_t x1_5 = 0;
    for (int32_t i2 = 0; i2 < int32_t(n_0); ++ i2) {
        int64_t x6 = jikka::mod::plus(jikka::modmat::floormod<2>(std::array<int64_t, 2>{x1_4, x1_5}, 1000000007)[1], jikka::modmat::floormod<2>(std::array<int64_t, 2>{x1_4, x1_5}, 1000000007)[0], 1000000007);
        x1_4 = x6;
        x1_5 = x1_4;
    }
    return x1_5;
}
// ...

下をコメントアウトした状態で生成した fib.cpp
https://github.com/kmyk/Jikka/blob/cae51d2d847c340e0c81f3aa64f49e3d995b67be/src/Jikka/CPlusPlus/Convert.hs#L25

// ...
int64_t solve(int64_t n_0) {
    int64_t x1_4 = 1;
    int64_t x1_5 = 0;
    for (int32_t i2 = 0; i2 < int32_t(n_0); ++ i2) {
        int64_t x6 = jikka::mod::plus(jikka::modmat::floormod<2>(std::array<int64_t, 2>{x1_4, x1_5}, 1000000007)[1], jikka::modmat::floormod<2>(std::array<int64_t, 2>{x1_4, x1_5}, 1000000007)[0], 1000000007);
        int64_t x7 = x1_4;
        x1_4 = x6;
        x1_5 = x7;
    }
    return x1_5;
}
// ...

Copy link
Collaborator

@kmyk kmyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。完璧です


変数名をどういう規則でつければ良いのかわからなかった

まわりの雰囲気と喧嘩しないようなものであれば大丈夫です。Haskell は数学色がかなり強いので、変数の命名も数学みたいに短くなりがちであり、なので競プロをするときと同じ気分で付ければ Haskell としても自然になります

mod を取るようにしたら別のバグで動かない問題については、issue を立てるのお願いします

@kmyk kmyk merged commit 2edaccd into kmyk-jikka:master Aug 6, 2021
@riantkb riantkb deleted the 153-add-temporary-variable branch August 6, 2021 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants