<s> import collections <EOL> import itertools <EOL> import sys <EOL> from hindley_milner import TypeVariable <EOL> from hindley_milner import ListType <EOL> from hindley_milner import unify <EOL> from type_system import typeof <EOL> from type_system import Typeclass <EOL> from type_system import Hask <EOL> from type_system import build_instance <EOL> from typeclasses import Show <EOL> from typeclasses import show <EOL> from typeclasses import Eq <EOL> from typeclasses import Ord <EOL> from syntax import Syntax <EOL> from syntax import instance <EOL> from syntax import sig <EOL> from syntax import H <EOL> class Enum ( Typeclass ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , toEnum , fromEnum ) : <EOL> def succ ( a ) : <EOL> return toEnum ( fromEnum ( a ) + <NUM_LIT:1> ) <EOL> def pred ( a ) : <EOL> return toEnum ( fromEnum ( a ) - <NUM_LIT:1> ) <EOL> def enumFromThen ( start , second ) : <EOL> pointer = fromEnum ( start ) <EOL> step = fromEnum ( second ) - pointer <EOL> while True : <EOL> yield toEnum ( pointer ) <EOL> pointer += step <EOL> def enumFrom ( start ) : <EOL> return enumFromThen ( start , succ ( start ) ) <EOL> def enumFromThenTo ( start , second , end ) : <EOL> if start == end : <EOL> yield start <EOL> return <EOL> elif ( second >= start > end ) or ( second <= start < end ) : <EOL> return <EOL> pointer , stop = fromEnum ( start ) , fromEnum ( end ) <EOL> step = fromEnum ( second ) - pointer <EOL> while ( start < end and pointer <= stop ) or ( start > end and pointer >= stop ) : <EOL> yield toEnum ( pointer ) <EOL> pointer += step <EOL> return <EOL> def enumFromTo ( start , end ) : <EOL> second = succ ( start ) if start < end else pred ( start ) <EOL> return enumFromThenTo ( start , second , end ) <EOL> attrs = { "<STR_LIT>" : toEnum , "<STR_LIT>" : fromEnum , "<STR_LIT>" : succ , "<STR_LIT>" : <EOL> pred , "<STR_LIT>" : enumFromThen , "<STR_LIT>" : enumFrom , <EOL> "<STR_LIT>" : enumFromThenTo , "<STR_LIT>" : enumFromTo } <EOL> build_instance ( Enum , cls , attrs ) <EOL> return <EOL> @ sig ( H / "<STR_LIT:a>" >> int ) <EOL> def fromEnum ( a ) : <EOL> """<STR_LIT>""" <EOL> return Enum [ a ] . toEnum ( a ) <EOL> @ sig ( H / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def succ ( a ) : <EOL> """<STR_LIT>""" <EOL> return Enum [ a ] . succ ( a ) <EOL> @ sig ( H / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def pred ( a ) : <EOL> """<STR_LIT>""" <EOL> return Enum [ a ] . pred ( a ) <EOL> @ sig ( H / "<STR_LIT:a>" >> "<STR_LIT:a>" >> [ "<STR_LIT:a>" ] ) <EOL> def enumFromThen ( start , second ) : <EOL> """<STR_LIT>""" <EOL> return L [ Enum [ start ] . enumFromThen ( start , second ) ] <EOL> @ sig ( H / "<STR_LIT:a>" >> [ "<STR_LIT:a>" ] ) <EOL> def enumFrom ( start ) : <EOL> """<STR_LIT>""" <EOL> return L [ Enum [ start ] . enumFrom ( start ) ] <EOL> @ sig ( H / "<STR_LIT:a>" >> "<STR_LIT:a>" >> "<STR_LIT:a>" >> [ "<STR_LIT:a>" ] ) <EOL> def enumFromThenTo ( start , second , end ) : <EOL> """<STR_LIT>""" <EOL> return L [ Enum [ start ] . enumFromThenTo ( start , second , end ) ] <EOL> @ sig ( H / "<STR_LIT:a>" >> "<STR_LIT:a>" >> [ "<STR_LIT:a>" ] ) <EOL> def enumFromTo ( start , end ) : <EOL> """<STR_LIT>""" <EOL> return L [ Enum [ start ] . enumFromTo ( start , end ) ] <EOL> instance ( Enum , int ) . where ( fromEnum = int , toEnum = int ) <EOL> instance ( Enum , bool ) . where ( fromEnum = int , toEnum = bool ) <EOL> instance ( Enum , str ) . where ( fromEnum = ord , toEnum = chr ) <EOL> if sys . version [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> instance ( Enum , long ) . where ( fromEnum = int , toEnum = long ) <EOL> class List ( collections . Sequence , Hask ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , head = None , tail = None ) : <EOL> self . __head = [ ] <EOL> self . __tail = itertools . chain ( [ ] ) <EOL> self . __is_evaluated = True <EOL> if head is not None and len ( head ) > <NUM_LIT:0> : <EOL> fst = head [ <NUM_LIT:0> ] <EOL> for fst , other in zip ( itertools . repeat ( fst ) , head ) : <EOL> unify ( typeof ( fst ) , typeof ( other ) ) <EOL> self . __head . extend ( head ) <EOL> if tail is not None : <EOL> self . __tail = itertools . chain ( self . __tail , tail ) <EOL> self . __is_evaluated = False <EOL> return <EOL> def __type__ ( self ) : <EOL> if self . __is_evaluated : <EOL> if len ( self . __head ) == <NUM_LIT:0> : <EOL> return ListType ( TypeVariable ( ) ) <EOL> return ListType ( typeof ( self [ <NUM_LIT:0> ] ) ) <EOL> elif len ( self . __head ) == <NUM_LIT:0> : <EOL> self . __next ( ) <EOL> return self . __type__ ( ) <EOL> return ListType ( typeof ( self [ <NUM_LIT:0> ] ) ) <EOL> def __next ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __is_evaluated : <EOL> raise StopIteration <EOL> else : <EOL> try : <EOL> next_iter = next ( self . __tail ) <EOL> if len ( self . __head ) > <NUM_LIT:0> : <EOL> unify ( typeof ( self [ <NUM_LIT:0> ] ) , typeof ( next_iter ) ) <EOL> self . __head . append ( next_iter ) <EOL> except StopIteration : <EOL> self . __is_evaluated = True <EOL> return <EOL> def __evaluate ( self ) : <EOL> """<STR_LIT>""" <EOL> while not self . __is_evaluated : <EOL> self . __next ( ) <EOL> return <EOL> def __rxor__ ( self , item ) : <EOL> """<STR_LIT>""" <EOL> unify ( self . __type__ ( ) , ListType ( typeof ( item ) ) ) <EOL> if self . __is_evaluated : <EOL> return List ( head = [ item ] + self . __head ) <EOL> return List ( head = [ item ] + self . __head , tail = self . __tail ) <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> unify ( self . __type__ ( ) , typeof ( other ) ) <EOL> if self . __is_evaluated and other . __is_evaluated : <EOL> return List ( head = self . __head + other . __head ) <EOL> elif self . __is_evaluated and not other . __is_evaluated : <EOL> return List ( head = self . __head + other . __head , <EOL> tail = other . __tail ) <EOL> return List ( head = self . __head , <EOL> tail = itertools . chain ( self . __tail , iter ( other ) ) ) <EOL> def __str__ ( self ) : <EOL> if len ( self . __head ) == <NUM_LIT:0> and self . __is_evaluated : <EOL> return "<STR_LIT>" <EOL> elif len ( self . __head ) == <NUM_LIT:1> and self . __is_evaluated : <EOL> return "<STR_LIT>" % show ( self . __head [ <NUM_LIT:0> ] ) <EOL> body = "<STR_LIT:U+002CU+0020>" . join ( ( show ( s ) for s in self . __head ) ) <EOL> return "<STR_LIT>" % body if self . __is_evaluated else "<STR_LIT>" % body <EOL> def __cmp__ ( self , other ) : <EOL> if self . __is_evaluated and other . __is_evaluated : <EOL> return cmp ( self . __head , other . __head ) <EOL> elif len ( self . __head ) >= len ( other . __head ) : <EOL> heads = zip ( self . __head [ : len ( other . __head ) ] , other . __head ) <EOL> heads_comp = ( ( cmp ( h1 , h2 ) for h1 , h2 in heads ) ) <EOL> for comp in heads_comp : <EOL> if comp != <NUM_LIT:0> : <EOL> return comp <EOL> while len ( self . __head ) > len ( other . __head ) : <EOL> if other . __is_evaluated : <EOL> return <NUM_LIT:1> <EOL> other . __next ( ) <EOL> comp = cmp ( self . __head [ len ( other . __head ) - <NUM_LIT:1> ] , other . __head [ - <NUM_LIT:1> ] ) <EOL> if comp != <NUM_LIT:0> : <EOL> return comp <EOL> while not self . __is_evaluated or not other . __is_evaluated : <EOL> if not self . __is_evaluated : <EOL> self . __next ( ) <EOL> if not other . __is_evaluated : <EOL> other . __next ( ) <EOL> len_comp = cmp ( len ( self . __head ) , len ( other . __head ) ) <EOL> if len_comp != <NUM_LIT:0> : <EOL> return len_comp <EOL> if len ( self . __head ) > <NUM_LIT:0> : <EOL> value_comp = cmp ( self . __head [ - <NUM_LIT:1> ] , other . __head [ - <NUM_LIT:1> ] ) <EOL> if value_comp != <NUM_LIT:0> : <EOL> return value_comp <EOL> elif len ( other . __head ) > len ( self . __head ) : <EOL> return - other . __cmp__ ( self ) <EOL> return <NUM_LIT:0> <EOL> def __eq__ ( self , other ) : <EOL> return self . __cmp__ ( other ) == <NUM_LIT:0> <EOL> def __lt__ ( self , other ) : <EOL> return self . __cmp__ ( other ) == - <NUM_LIT:1> <EOL> def __gt__ ( self , other ) : <EOL> return self . __cmp__ ( other ) == <NUM_LIT:1> <EOL> def __le__ ( self , other ) : <EOL> comp = self . __cmp__ ( other ) <EOL> return comp in ( - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def __ge__ ( self , other ) : <EOL> comp = self . __cmp__ ( other ) <EOL> return comp in ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def __len__ ( self ) : <EOL> self . __evaluate ( ) <EOL> return len ( self . __head ) <EOL> def __iter__ ( self ) : <EOL> for item in self . __head : <EOL> yield item <EOL> for item in self . __tail : <EOL> self . __head . append ( item ) <EOL> yield item <EOL> def count ( self , x ) : <EOL> unify ( self . __type__ ( ) , ListType ( typeof ( x ) ) ) <EOL> self . __evaluate ( ) <EOL> return self . __head . count ( x ) <EOL> def index ( self , x ) : <EOL> unify ( self . __type__ ( ) , ListType ( typeof ( x ) ) ) <EOL> self . __evaluate ( ) <EOL> return self . __head . index ( x ) <EOL> def __contains__ ( self , x ) : <EOL> unify ( self . __type__ ( ) , ListType ( typeof ( x ) ) ) <EOL> for item in iter ( self ) : <EOL> if item is x : <EOL> return True <EOL> return False <EOL> def __getitem__ ( self , ix ) : <EOL> is_slice = isinstance ( ix , slice ) <EOL> if is_slice : <EOL> i = ix . start if ix . stop is None else ix . stop <EOL> else : <EOL> i = ix <EOL> if i >= <NUM_LIT:0> : <EOL> while ( i + <NUM_LIT:1> ) > len ( self . __head ) : <EOL> try : <EOL> self . __next ( ) <EOL> except StopIteration : <EOL> break <EOL> else : <EOL> self . __evaluate ( ) <EOL> if is_slice : <EOL> if ix . stop is None and not self . __is_evaluated : <EOL> return List ( head = self . __head [ ix ] , tail = self . __tail ) <EOL> return List ( head = self . __head [ ix ] ) <EOL> return self . __head [ i ] <EOL> instance ( Show , List ) . where ( <EOL> show = List . __str__ <EOL> ) <EOL> instance ( Eq , List ) . where ( <EOL> eq = List . __eq__ <EOL> ) <EOL> instance ( Ord , List ) . where ( <EOL> lt = List . __lt__ , <EOL> gt = List . __gt__ , <EOL> le = List . __le__ , <EOL> ge = List . __ge__ <EOL> ) <EOL> class __list_comprehension__ ( Syntax ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , lst ) : <EOL> if isinstance ( lst , tuple ) and len ( lst ) < <NUM_LIT:5> and any ( ( Ellipsis is x for x in lst ) ) : <EOL> if len ( lst ) == <NUM_LIT:2> and lst [ <NUM_LIT:1> ] is Ellipsis : <EOL> return enumFrom ( lst [ <NUM_LIT:0> ] ) <EOL> elif len ( lst ) == <NUM_LIT:3> and lst [ <NUM_LIT:2> ] is Ellipsis : <EOL> return enumFromThen ( lst [ <NUM_LIT:0> ] , lst [ <NUM_LIT:1> ] ) <EOL> elif len ( lst ) == <NUM_LIT:3> and lst [ <NUM_LIT:1> ] is Ellipsis : <EOL> return enumFromTo ( lst [ <NUM_LIT:0> ] , lst [ <NUM_LIT:2> ] ) <EOL> elif len ( lst ) == <NUM_LIT:4> and lst [ <NUM_LIT:2> ] is Ellipsis : <EOL> return enumFromThenTo ( lst [ <NUM_LIT:0> ] , lst [ <NUM_LIT:1> ] , lst [ <NUM_LIT:3> ] ) <EOL> raise SyntaxError ( "<STR_LIT>" % str ( lst ) ) <EOL> elif hasattr ( lst , "<STR_LIT>" ) or hasattr ( lst , "<STR_LIT>" ) : <EOL> return List ( tail = lst ) <EOL> return List ( head = lst ) <EOL> L = __list_comprehension__ ( "<STR_LIT>" ) </s>
