<s> import operator <EOL> import sys <EOL> import numpy <EOL> from chainer import cuda <EOL> class TypeInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , shape , dtype ) : <EOL> self . shape = shape <EOL> self . dtype = dtype <EOL> self . ndim = len ( shape ) <EOL> class TypeInfoTuple ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return Variable ( len ( self ) , '<STR_LIT>' . format ( self . name ) ) <EOL> def get_types ( data , name , accept_none ) : <EOL> assert ( isinstance ( data , tuple ) ) <EOL> info = TypeInfoTuple ( <EOL> _get_type ( name , i , x , accept_none ) for i , x in enumerate ( data ) ) <EOL> info . name = name <EOL> return info <EOL> def _get_type ( name , index , array , accept_none ) : <EOL> var = '<STR_LIT>' . format ( name , index ) <EOL> if accept_none and array is None : <EOL> return Variable ( TypeInfo ( ( ) , None ) , var ) <EOL> assert ( isinstance ( array , numpy . ndarray ) or <EOL> isinstance ( array , cuda . ndarray ) ) <EOL> return Variable ( TypeInfo ( array . shape , array . dtype ) , var ) <EOL> def _make_un_operator ( exp , priority , func ) : <EOL> def f ( x ) : <EOL> return UnaryOperator ( priority , x , exp , func ) <EOL> return f <EOL> def _make_bin_operator ( exp , priority , func , right_associative = False ) : <EOL> def f ( x , y ) : <EOL> return BinaryOperator ( priority , x , y , exp , func , right_associative ) <EOL> return f <EOL> def _make_bool_operator ( exp , inv , func ) : <EOL> def f ( x , y ) : <EOL> return BoolBinaryOperator ( x , y , exp , inv , func ) <EOL> return f <EOL> def _flip ( f ) : <EOL> return lambda x , y : f ( y , x ) <EOL> class Expr ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , priority ) : <EOL> self . priority = priority <EOL> def eval ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __getattr__ ( self , name ) : <EOL> return GetAttr ( self , name ) <EOL> def __getitem__ ( self , key ) : <EOL> return GetItem ( self , key ) <EOL> def __call__ ( self , * args ) : <EOL> return Call ( self , args ) <EOL> def __nonzero__ ( self ) : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise RuntimeError ( msg ) <EOL> def __bool__ ( self ) : <EOL> self . __nonzero__ ( ) <EOL> __eq__ = _make_bool_operator ( '<STR_LIT>' , '<STR_LIT>' , operator . __eq__ ) <EOL> __ne__ = _make_bool_operator ( '<STR_LIT>' , '<STR_LIT>' , operator . __ne__ ) <EOL> __lt__ = _make_bool_operator ( '<STR_LIT:<>' , '<STR_LIT>' , operator . __lt__ ) <EOL> __le__ = _make_bool_operator ( '<STR_LIT>' , '<STR_LIT:>>' , operator . __le__ ) <EOL> __gt__ = _make_bool_operator ( '<STR_LIT:>>' , '<STR_LIT>' , operator . __gt__ ) <EOL> __ge__ = _make_bool_operator ( '<STR_LIT>' , '<STR_LIT:<>' , operator . __ge__ ) <EOL> __add__ = _make_bin_operator ( '<STR_LIT:+>' , <NUM_LIT:4> , operator . __add__ ) <EOL> __radd__ = _flip ( __add__ ) <EOL> __sub__ = _make_bin_operator ( '<STR_LIT:->' , <NUM_LIT:4> , operator . __sub__ ) <EOL> __rsub__ = _flip ( __sub__ ) <EOL> __mul__ = _make_bin_operator ( '<STR_LIT:*>' , <NUM_LIT:5> , operator . __mul__ ) <EOL> __rmul__ = _flip ( __mul__ ) <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> __div__ = _make_bin_operator ( '<STR_LIT:/>' , <NUM_LIT:5> , operator . __div__ ) <EOL> __rdiv__ = _flip ( __div__ ) <EOL> else : <EOL> __truediv__ = _make_bin_operator ( '<STR_LIT:/>' , <NUM_LIT:5> , operator . __truediv__ ) <EOL> __rtruediv__ = _flip ( __truediv__ ) <EOL> __floordiv__ = _make_bin_operator ( '<STR_LIT>' , <NUM_LIT:5> , operator . __floordiv__ ) <EOL> __rfloordiv__ = _flip ( __floordiv__ ) <EOL> __mod__ = _make_bin_operator ( '<STR_LIT:%>' , <NUM_LIT:5> , operator . __mod__ ) <EOL> __rmod__ = _flip ( __mod__ ) <EOL> __pow__ = _make_bin_operator ( '<STR_LIT>' , <NUM_LIT:7> , operator . __mod__ , <EOL> right_associative = True ) <EOL> __lshift__ = _make_bin_operator ( '<STR_LIT>' , <NUM_LIT:3> , operator . __lshift__ ) <EOL> __rlshift__ = _flip ( __lshift__ ) <EOL> __rshift__ = _make_bin_operator ( '<STR_LIT>' , <NUM_LIT:3> , operator . __rshift__ ) <EOL> __rrshift__ = _flip ( __rshift__ ) <EOL> __and__ = _make_bin_operator ( '<STR_LIT:&>' , <NUM_LIT:2> , operator . __and__ ) <EOL> __rand__ = _flip ( __and__ ) <EOL> __xor__ = _make_bin_operator ( '<STR_LIT>' , <NUM_LIT:1> , operator . __xor__ ) <EOL> __rxor__ = _flip ( __xor__ ) <EOL> __or__ = _make_bin_operator ( '<STR_LIT:|>' , <NUM_LIT:0> , operator . __or__ ) <EOL> __ror__ = _flip ( __or__ ) <EOL> __neg__ = _make_un_operator ( '<STR_LIT:->' , <NUM_LIT:6> , operator . __neg__ ) <EOL> __pos__ = _make_un_operator ( '<STR_LIT:+>' , <NUM_LIT:6> , operator . __pos__ ) <EOL> __invert__ = _make_un_operator ( '<STR_LIT>' , <NUM_LIT:6> , operator . __invert__ ) <EOL> def _eval_expr ( v ) : <EOL> if isinstance ( v , Expr ) : <EOL> return v . eval ( ) <EOL> elif isinstance ( v , list ) : <EOL> return list ( map ( _eval_expr , v ) ) <EOL> elif isinstance ( v , tuple ) : <EOL> return tuple ( map ( _eval_expr , v ) ) <EOL> else : <EOL> return v <EOL> def _repr ( v ) : <EOL> if isinstance ( v , Expr ) : <EOL> return str ( v ) <EOL> elif isinstance ( v , list ) : <EOL> return '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( _repr , v ) ) ) <EOL> elif isinstance ( v , tuple ) : <EOL> if len ( v ) == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif len ( v ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' . format ( _repr ( v [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> return '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( _repr , v ) ) ) <EOL> else : <EOL> return repr ( v ) <EOL> class Atom ( Expr ) : <EOL> def __init__ ( self ) : <EOL> super ( Atom , self ) . __init__ ( <NUM_LIT:8> ) <EOL> class Constant ( Atom ) : <EOL> def __init__ ( self , value ) : <EOL> super ( Constant , self ) . __init__ ( ) <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return _repr ( self . value ) <EOL> def eval ( self ) : <EOL> return self . value <EOL> class Variable ( Atom ) : <EOL> def __init__ ( self , value , name ) : <EOL> super ( Variable , self ) . __init__ ( ) <EOL> self . value = value <EOL> self . name = name <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def eval ( self ) : <EOL> return self . value <EOL> class GetAttr ( Atom ) : <EOL> def __init__ ( self , obj , name ) : <EOL> super ( GetAttr , self ) . __init__ ( ) <EOL> self . obj = obj <EOL> self . name = name <EOL> def __str__ ( self ) : <EOL> if isinstance ( self . name , str ) : <EOL> return '<STR_LIT>' . format ( _repr ( self . obj ) , self . name ) <EOL> elif ( isinstance ( self . name , Constant ) and <EOL> isinstance ( self . name . value , str ) ) : <EOL> return '<STR_LIT>' . format ( _repr ( self . obj ) , self . name . value ) <EOL> else : <EOL> return '<STR_LIT>' . format ( _repr ( self . obj ) , <EOL> _repr ( self . name ) ) <EOL> def eval ( self ) : <EOL> return getattr ( _eval_expr ( self . obj ) , _eval_expr ( self . name ) ) <EOL> def _str_subscript ( exp ) : <EOL> if exp is Ellipsis : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( exp , slice ) : <EOL> def key_str ( v ) : <EOL> return '<STR_LIT>' if v is None else _repr ( v ) <EOL> if exp . step is None : <EOL> return '<STR_LIT>' . format ( key_str ( exp . start ) , <EOL> key_str ( exp . stop ) ) <EOL> else : <EOL> return '<STR_LIT>' . format ( key_str ( exp . start ) , <EOL> key_str ( exp . stop ) , <EOL> key_str ( exp . step ) ) <EOL> elif isinstance ( exp , tuple ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( map ( _str_subscript , exp ) ) <EOL> else : <EOL> return _repr ( exp ) <EOL> class GetItem ( Atom ) : <EOL> def __init__ ( self , obj , key ) : <EOL> super ( GetItem , self ) . __init__ ( ) <EOL> self . obj = obj <EOL> self . key = key <EOL> def __str__ ( self ) : <EOL> key = _str_subscript ( self . key ) <EOL> return '<STR_LIT>' . format ( _repr ( self . obj ) , key ) <EOL> def eval ( self ) : <EOL> return _eval_expr ( self . obj ) [ _eval_expr ( self . key ) ] <EOL> class Call ( Atom ) : <EOL> def __init__ ( self , obj , args ) : <EOL> assert isinstance ( args , tuple ) <EOL> super ( Call , self ) . __init__ ( ) <EOL> self . obj = obj <EOL> self . args = args <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( _repr ( self . obj ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( map ( _repr , self . args ) ) ) <EOL> def eval ( self ) : <EOL> args = map ( _eval_expr , self . args ) <EOL> func = _eval_expr ( self . obj ) <EOL> return func ( * args ) <EOL> class UnaryOperator ( Expr ) : <EOL> def __init__ ( self , priority , term , exp , func ) : <EOL> super ( UnaryOperator , self ) . __init__ ( priority ) <EOL> self . term = term <EOL> self . exp = exp <EOL> self . func = func <EOL> def eval ( self ) : <EOL> return self . func ( _eval_expr ( self . term ) ) <EOL> def __str__ ( self ) : <EOL> exp = _repr ( self . term ) <EOL> if isinstance ( self . term , Expr ) and self . term . priority < self . priority : <EOL> exp = '<STR_LIT:(>' + exp + '<STR_LIT:)>' <EOL> return self . exp + exp <EOL> class BinaryOperator ( Expr ) : <EOL> def __init__ ( self , priority , lhs , rhs , exp , func , right_associative = False ) : <EOL> super ( BinaryOperator , self ) . __init__ ( priority ) <EOL> self . lhs = lhs <EOL> self . rhs = rhs <EOL> self . exp = exp <EOL> self . func = func <EOL> self . right_associative = right_associative <EOL> def eval ( self ) : <EOL> left = self . _eval_left ( ) <EOL> right = self . _eval_right ( ) <EOL> return self . func ( left , right ) <EOL> def _eval_left ( self ) : <EOL> return _eval_expr ( self . lhs ) <EOL> def _eval_right ( self ) : <EOL> return _eval_expr ( self . rhs ) <EOL> def __str__ ( self ) : <EOL> left = _repr ( self . lhs ) <EOL> if isinstance ( self . lhs , Expr ) and ( <EOL> self . priority > self . lhs . priority or <EOL> ( self . right_associative and <EOL> self . priority == self . lhs . priority ) ) : <EOL> left = '<STR_LIT:(>' + left + '<STR_LIT:)>' <EOL> right = _repr ( self . rhs ) <EOL> if isinstance ( self . rhs , Expr ) and ( <EOL> self . priority > self . rhs . priority or <EOL> ( not self . right_associative and <EOL> self . priority == self . rhs . priority ) ) : <EOL> right = '<STR_LIT:(>' + right + '<STR_LIT:)>' <EOL> return '<STR_LIT>' . format ( left , right , self . exp ) <EOL> class Testable ( object ) : <EOL> def expect ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class BoolBinaryOperator ( BinaryOperator , Testable ) : <EOL> def __init__ ( self , lhs , rhs , exp , inv , func ) : <EOL> BinaryOperator . __init__ ( self , - <NUM_LIT:1> , lhs , rhs , exp , func ) <EOL> self . inv = inv <EOL> def expect ( self ) : <EOL> left = self . _eval_left ( ) <EOL> right = self . _eval_right ( ) <EOL> if not self . func ( left , right ) : <EOL> raise InvalidType ( <EOL> '<STR_LIT>' . format ( self . lhs , self . exp , self . rhs ) , <EOL> '<STR_LIT>' . format ( left , self . inv , right ) ) <EOL> class InvalidType ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , expect , actual , msg = None ) : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' . format ( expect , actual ) <EOL> super ( InvalidType , self ) . __init__ ( msg ) <EOL> self . expect = expect <EOL> self . actual = actual <EOL> def expect ( * bool_exprs ) : <EOL> """<STR_LIT>""" <EOL> for expr in bool_exprs : <EOL> assert isinstance ( expr , Testable ) <EOL> expr . expect ( ) <EOL> prod = Variable ( numpy . prod , '<STR_LIT>' ) </s>
