lynn / pysearch Goto Github PK
View Code? Open in Web Editor NEWBrute-force search for short Python expressions (for code golf)
License: MIT License
Brute-force search for short Python expressions (for code golf)
License: MIT License
The implementation for logical operators like or
and and
do not fully account for their short-circuiting behaviors. For example, the expression x<1 or 1//x
should return 1
for x=0
, but pysearch cannot find it because 1//x
is immediately pruned due to a division by zero.
The way to deal with this is to make Num
an Option<Num>
and allow errors to be represented as None
instead of pruning them entirely. Fixing this will likely hurt performance, so we would probably want to implement this as an optional flag in params.rs.
Doing this would also allow None
outputs, which might be useful if the user wants a certain input to throw an error (termination by error is common in code golf :).
➜ docker run -it -v $(pwd):/target --rm docker.io/silkeh/clang:12 bash
root@ca8a6333daea:/# cd target/
root@ca8a6333daea:/target# clang --version
clang version 12.0.0 (https://github.com/llvm/llvm-project/ b978a93635b584db380274d7c8963c73989944a1)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
root@ca8a6333daea:/target# clang++ -O3 -std=c++17 pysearch.cpp
pysearch.cpp:226:11: error: no matching function for call to 'cache_if_better'
cache_if_better(cn, +(oL < oR), Expr{&eL, &eR, Operator::Lt, 0, mask});
^~~~~~~~~~~~~~~
pysearch.cpp:199:6: note: candidate function not viable: no known conversion from '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__less, _ValArray, _ValArray, int, int>>, std::_Expr<std::__detail::_BinClos<std::__less, _ValArray, _ValArray, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__less, _ValArray, _ValArray, int, int>>, bool>') to 'const Vec' (aka 'const valarray<int>') for 2nd argument
void cache_if_better(CacheLevel& level, const Vec& output, const Expr& expr) {
^
pysearch.cpp:262:41: error: invalid operands to binary expression ('std::tuple_element<0, const std::pair<const std::valarray<int>, Expr>>::type' (aka 'const std::valarray<int>') and '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, std::_Expr<std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, bool>'))
cache_if_better(cn, oL + oR * +(oL == 0), Expr{&eL, &eR, Operator::Or, 0, mask});
~~ ^ ~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate function [with _Dom = std::__detail::_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>] not viable: no known conversion from 'std::tuple_element<0, const std::pair<const std::valarray<int>, Expr>>::type' (aka 'const std::valarray<int>') to 'const typename _UnClos<__unary_plus, std::_Expr, _BinClos<__equal_to, _ValArray, _Constant, int, int>>::value_type' (aka 'const bool') for 1st argument
_DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:370:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const typename _Dom::value_type& __t, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate function [with _Dom = std::__detail::_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>] not viable: no known conversion from 'valarray<int>' to 'const valarray<typename _UnClos<__unary_plus, std::_Expr, _BinClos<__equal_to, _ValArray, _Constant, int, int>>::value_type>' for 1st argument
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:396:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const valarray<typename _Dom::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate function [with _Tp = int] not viable: no known conversion from '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, std::_Expr<std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, bool>') to 'const typename valarray<int>::value_type' (aka 'const int') for 2nd argument
_DEFINE_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1166:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
_DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:344:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:357:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:383:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate template ignored: could not match 'valarray' against '_Expr'
_DEFINE_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1155:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate template ignored: could not match 'valarray' against '_Expr'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1177:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const typename valarray<_Tp>::value_type& __t, \
^
pysearch.cpp:265:11: error: no matching function for call to 'cache_if_better'
cache_if_better(cn, +(oL <= oR), Expr{&eL, &eR, Operator::Leq, 0, mask});
^~~~~~~~~~~~~~~
pysearch.cpp:199:6: note: candidate function not viable: no known conversion from '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__less_equal, _ValArray, _ValArray, int, int>>, std::_Expr<std::__detail::_BinClos<std::__less_equal, _ValArray, _ValArray, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__less_equal, _ValArray, _ValArray, int, int>>, bool>') to 'const Vec' (aka 'const valarray<int>') for 2nd argument
void cache_if_better(CacheLevel& level, const Vec& output, const Expr& expr) {
^
pysearch.cpp:282:41: error: invalid operands to binary expression ('std::tuple_element<0, const std::pair<const std::valarray<int>, Expr>>::type' (aka 'const std::valarray<int>') and '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, std::_Expr<std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, bool>'))
cache_if_better(cn, oL + oR * +(oL == 0), Expr{&eL, &eR, Operator::SpaceOr, 0, mask});
~~ ^ ~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate function [with _Dom = std::__detail::_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>] not viable: no known conversion from 'std::tuple_element<0, const std::pair<const std::valarray<int>, Expr>>::type' (aka 'const std::valarray<int>') to 'const typename _UnClos<__unary_plus, std::_Expr, _BinClos<__equal_to, _ValArray, _Constant, int, int>>::value_type' (aka 'const bool') for 1st argument
_DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:370:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const typename _Dom::value_type& __t, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate function [with _Dom = std::__detail::_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>] not viable: no known conversion from 'valarray<int>' to 'const valarray<typename _UnClos<__unary_plus, std::_Expr, _BinClos<__equal_to, _ValArray, _Constant, int, int>>::value_type>' for 1st argument
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:396:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const valarray<typename _Dom::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate function [with _Tp = int] not viable: no known conversion from '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, std::_Expr<std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, bool>') to 'const typename valarray<int>::value_type' (aka 'const int') for 2nd argument
_DEFINE_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1166:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
_DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:344:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:357:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:383:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate template ignored: could not match 'valarray' against '_Expr'
_DEFINE_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1155:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate template ignored: could not match 'valarray' against '_Expr'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1177:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const typename valarray<_Tp>::value_type& __t, \
^
pysearch.cpp:284:41: error: invalid operands to binary expression ('std::tuple_element<0, const std::pair<const std::valarray<int>, Expr>>::type' (aka 'const std::valarray<int>') and '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, std::_Expr<std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, bool>'))
cache_if_better(cn, oL + oR * +(oL == 0), Expr{&eL, &eR, Operator::OrSpace, 0, mask});
~~ ^ ~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate function [with _Dom = std::__detail::_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>] not viable: no known conversion from 'std::tuple_element<0, const std::pair<const std::valarray<int>, Expr>>::type' (aka 'const std::valarray<int>') to 'const typename _UnClos<__unary_plus, std::_Expr, _BinClos<__equal_to, _ValArray, _Constant, int, int>>::value_type' (aka 'const bool') for 1st argument
_DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:370:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const typename _Dom::value_type& __t, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate function [with _Dom = std::__detail::_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>] not viable: no known conversion from 'valarray<int>' to 'const valarray<typename _UnClos<__unary_plus, std::_Expr, _BinClos<__equal_to, _ValArray, _Constant, int, int>>::value_type>' for 1st argument
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:396:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const valarray<typename _Dom::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate function [with _Tp = int] not viable: no known conversion from '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, std::_Expr<std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>, bool>::value_type>' (aka '_Expr<_UnClos<std::__unary_plus, std::_Expr, std::__detail::_BinClos<std::__equal_to, _ValArray, _Constant, int, int>>, bool>') to 'const typename valarray<int>::value_type' (aka 'const int') for 2nd argument
_DEFINE_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1166:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
_DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:344:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:357:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:407:5: note: candidate template ignored: could not match '_Expr' against 'valarray'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/valarray_after.h:383:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate template ignored: could not match 'valarray' against '_Expr'
_DEFINE_BINARY_OPERATOR(*, __multiplies)
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1155:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1187:1: note: candidate template ignored: could not match 'valarray' against '_Expr'
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/valarray:1177:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const typename valarray<_Tp>::value_type& __t, \
^
5 errors generated.
All this time I figured I was one of the few who couldn't get it to work but it does actually. Well, sort of.
After cloning the repository for another attempt and prior to running cargo run --release
, I checked the contents of the params.rs
file in order to verify if the search parameters are still identical to your example. They are, and so I ran it just like that to see what would happen this time.
Eventually it terminates successfully with the following results and although it actually processed 6,758 expressions, it appears as if it spent twenty seconds on nothing.
Finding length 14...
.
.
.
Explored 0 expressions in 19.8816743s
Total: 7694843 expressions in 84.4192928s
Going all the way up to the results of length 13, I noticed how it already knew how many expressions it'd process in total. Uh, I cannot understand how. Am I possibly missing something important in all of this?
Finding length 13...
.
.
.
Explored 0 expressions in 5.6641441s
Total: 7694843 expressions in 64.5369573s
Everything looks normal prior to processing lengths 11-14 and the results of length 10 already show a current total of 7,694,843 expressions.
Regarding the expressions it finds, I'm not succeeding in obtaining the following example output.
--- Length 5 ---
n%5-3
I have to abort execution really quick in order to see the shortest results and that's when I noticed that it does find your example.
$ cargo run --release
Finished `release` profile [optimized] target(s) in 0.06s
Running `target\release\pysearch.exe`
sizeof(Expr) = 40
Finding length 1...
Explored 10 expressions in 20.3µs
Total: 10 expressions in 357.8µs
Finding length 2...
Explored 43 expressions in 15.5µs
Total: 53 expressions in 988.2µs
Finding length 3...
Explored 100 expressions in 103.2µs
Total: 153 expressions in 1.7371ms
Finding length 4...
Explored 396 expressions in 52.2µs
Total: 549 expressions in 2.1059ms
Finding length 5...
n%5-3
Explored 2123 expressions in 456.9µs
Total: 2672 expressions in 2.8153ms
After modifying the search parameters and aborting execution really quick to see if it finds the second expression, it appears to have found three expressions of the same length and although all of them seem valid, the example of yours, 1-n//2%3
, is nowhere to be found.
Finding length 8...
3-n%6>>1
4-n%6>>2
1-n%6//2
Explored 278791 expressions in 171.0213ms
Total: 354731 expressions in 199.1973ms
Would you be so kind to shed some light on everything that's going on here, please? Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.