Ejemplo n.º 1
0
 def __init__(self, name='', cs={}):
     self.name = name
     if self.name == '': self.name = gensym('T')
     self.comps = Rec(cs)
     self.witness_cache = []
     self.supertype_cache = []
     self.witness_conditions = []
Ejemplo n.º 2
0
def _cps_lambda(lambda_ast: LambdaAst, k: Callable[[Ast], Ast]) -> Ast:
    continuation = gensym("K")
    body = to_cps(
        lambda_ast.body,
        lambda lambda_body: CallAst(VarAst(continuation), [lambda_body]))
    return k(
        LambdaAst(lambda_ast.name, [continuation] + lambda_ast.params, body))
Ejemplo n.º 3
0
 def __init__(self,name='',d=None):
     self.name = name
     if self.name == '': self.name = gensym('M')
     if d is None:
         self.model={}
     else:
         self.model = d
Ejemplo n.º 4
0
def _cps_if(if_ast: IfAst, k: Callable[[Ast], Ast]) -> CallAst:
    """
    The previous version of cps_if passed the k on compiling both branches,
    resulting in massive code growth for consecutive if-s.
    The (quite expensive) fix is to wrap the rest of the program
    in a continuation, and we'll transform the "if" node into an IIFE
    which receives that continuation as an argument.
    The body of this IIFE will just call the continuation
    with the result of the if expression.

    The following is a sample transformation:

    a = if foo then 1 else 2;
    print(a);

    (λ (ifcont){
        if foo then ifcont(1) else ifcont(2);
    })(λ (ifret){
        a = ifret;
        print(a);
    });
    """
    def cps_cond(cond_ast: Ast) -> Ast:
        def cps_then_and_else(result: Ast) -> Ast:
            return CallAst(VarAst(if_continuation), [result])

        return IfAst(cond_ast, to_cps(if_ast.then, cps_then_and_else),
                     to_cps(if_ast.else_, cps_then_and_else))

    if_continuation = gensym("I")
    return CallAst(
        LambdaAst('', [if_continuation], to_cps(if_ast.cond, cps_cond)),
        [_make_continuation(k)])
Ejemplo n.º 5
0
 def __init__(self, name='', d=None):
     self.name = name
     if self.name == '': self.name = gensym('M')
     if d is None:
         self.model = {}
     else:
         self.model = d
Ejemplo n.º 6
0
 def __init__(self,name=gensym('BT')):
     self.name=name
     self.comps = Rec({})
     self.witness_cache = []
     self.supertype_cache = []
     self.witness_conditions = []
     self.witness_types = []
     self.poss = ''
Ejemplo n.º 7
0
 def __init__(self, name=gensym('BT')):
     self.name = name
     self.comps = Rec({})
     self.witness_cache = []
     self.supertype_cache = []
     self.witness_conditions = []
     self.witness_types = []
     self.poss = ''
Ejemplo n.º 8
0
def combine_dep_types(f1,f2):
    if isinstance(f1,Type) and isinstance(f2,Type):
        return f1.merge(f2)
    elif isinstance(f1,Fun) and isinstance(f2,Type):
        var = gensym('v')
        return Fun(var, f1.domain_type, combine_dep_types(f1.body.subst(f1.var,var),f2))
    elif isinstance(f1,Type) and isinstance(f2,Fun):
        var = gensym('v')
        return Fun(var, f2.domain_type, combine_dep_types(f1,f2.body.subst(f2.var,var)))
    elif isinstance(f1,Fun) and isinstance(f2,Fun):
        var1 = gensym('v')
        var2 = gensym('v')
        return Fun(var1, f1.domain_type,
                   Fun(var2, f2.domain_type, combine_dep_types(f1.body.subst(f1.var,var1),f2.body.subst(f2.var,var2))))
    else:
        if ttracing('combine_dep_types'):
            print(show(f1)+' and '+show(f2)+' cannot be combined.')
        return None
Ejemplo n.º 9
0
 def __init__(self,name='',cs={}):
     self.name = name
     if self.name == '': self.name = gensym('T')
     self.comps = Rec(cs)
     self.witness_cache = []
     self.supertype_cache = []
     self.witness_conditions = []
     self.witness_types = []
     self.poss = ''
Ejemplo n.º 10
0
 def __init__(self, name=gensym('BT')):
     ttrtypes.BTypeClass.__init__(self, name)
     self.witness_cache = ([], [])
     self.prob_nonspec = None
     self._query_methods = [
         '_query_witness_cache', '_query_hypobj', '_query_lazyobj',
         '_query_conditions', '_query_oracle', '_query_witness_types',
         '_query_witness_conditions'
     ]
Ejemplo n.º 11
0
def combine_dep_types(f1,f2):
    if isinstance(f1,Type) and isinstance(f2,Type):
        return f1.merge(f2)
    elif isinstance(f1,Fun) and isinstance(f2,Type):
        var = gensym('v')
        return Fun(var, f1.domain_type, combine_dep_types(f1.body.subst(f1.var,var),f2))
    elif isinstance(f1,Type) and isinstance(f2,Fun):
        var = gensym('v')
        return Fun(var, f2.domain_type, combine_dep_types(f1,f2.body.subst(f2.var,var)))
    elif isinstance(f1,Fun) and isinstance(f2,Fun):
        var1 = gensym('v')
        var2 = gensym('v')
        return Fun(var1, f1.domain_type,
                   Fun(var2, f2.domain_type, combine_dep_types(f1.body.subst(f1.var,var1),f2.body.subst(f2.var,var2))))
    else:
        if ttracing('combine_dep_types'):
            print(show(f1)+' and '+show(f2)+' cannot be combined.')
        return None
Ejemplo n.º 12
0
def merge_dep_types(f1,f2):
    if isinstance(f1,Type) and isinstance(f1,Type):
        return f1.merge(f2)
    elif isinstance(f1,Fun) and isinstance(f2,Fun):
        var = gensym('v')
        return Fun(var, f1.domain_type.merge(f2.domain_type),
                   merge_dep_types(f1.body.subst(f1.var,var),f2.body.subst(f2.var,var)))
    else:
        if ttracing('merge_dep_types'):
            print(show(f1)+' and '+show(f2)+' cannot be merged.')
        return None
Ejemplo n.º 13
0
def merge_dep_types(f1,f2):
    if isinstance(f1,Type) and isinstance(f1,Type):
        return f1.merge(f2)
    elif isinstance(f1,Fun) and isinstance(f2,Fun):
        var = gensym('v')
        return Fun(var, f1.domain_type.merge(f2.domain_type),
                   merge_dep_types(f1.body.subst(f1.var,var),f2.body.subst(f2.var,var)))
    else:
        if ttracing('merge_dep_types'):
            print(show(f1)+' and '+show(f2)+' cannot be merged.')
        return None
Ejemplo n.º 14
0
 def rename_iife_param(param: str) -> str:
     """
     If iife param collides with closure, rename it.
     :param param:
     :return:
     """
     self.closure = cast(LambdaAst, self.closure)
     env: Environment = self.closure.env
     var_define: VarDefine = iife_func.env.get(param)
     if param in env.vars:
         param = gensym(param + '$')
     self.closure.iife_params.append(param)
     env.define(param, True)
     # change all references to iife params since we may change param names
     for ref in var_define.refs:
         ref.name = param
     return param
Ejemplo n.º 15
0
 def parser() -> Ast:
     ast = self._maybe_binary(self._parse_atom(), 0)
     # relational operator short-circuit implementation
     if isinstance(ast, BinaryAst):
         # left || right -> (lambda (left) {
         # if left then left else right})(left)
         binary_ast = cast(BinaryAst, ast)
         if binary_ast.operator == '||':
             iife_param = gensym('left')
             ast = CallAst(
                 LambdaAst(
                     '',
                     [iife_param],
                     IfAst(
                         VarAst(iife_param),
                         VarAst(iife_param),
                         binary_ast.right)),
                 [binary_ast.left])
         elif binary_ast.operator == '&&':
             ast = IfAst(binary_ast.left, binary_ast.right, LiteralAst(False))
     return ast
Ejemplo n.º 16
0
    def load_img(self, imgf, reorient2std=False, image_name=None):
        self._finished_plotting = False

        img = nib.load(imgf)

        aff = np.dot(
            get_orig2std(imgf) if reorient2std else np.eye(4),
            img.get_affine())
        tkr_aff = np.dot(
            reorient_orig2std_tkr_mat if reorient2std else np.eye(4),
            get_vox2rasxfm(imgf, stem='vox2ras-tkr'))

        #from nilearn.image.resampling import reorder_img

        #img = reorder_img(uimg, resample='continuous')

        xsz, ysz, zsz = img.shape

        #print 'image coordinate transform', img.get_affine()

        imgd = img.get_data()
        if reorient2std:
            imgd = np.swapaxes(imgd, 1, 2)[:,:,::-1]

        #print 'image size', imgd.shape

        if image_name is None:
            from utils import gensym
            image_name = 'image%s'%gensym()

        self.images[image_name] = (imgd, aff, tkr_aff)
        self.currently_showing_list.append(
            NullInstanceHolder(name=image_name))
        self.currently_showing = self.currently_showing_list[-1]

        self.pins[image_name] = {}
        #self.current_pin = image_name

        self.show_image(image_name)
Ejemplo n.º 17
0
 def create(self):
     a = gensym('_a')
     self.judge(a)
     return a
Ejemplo n.º 18
0
def create_method_rec(self):
    a = gensym('_r')
    self.judge(a)
    return a
Ejemplo n.º 19
0
def create_method_type(self):
    a = gensym('_T')
    self.judge(a)
    return a
Ejemplo n.º 20
0
 def create(self):
     a = gensym('_sigma')
     self.judge(a)
     return(a)
Ejemplo n.º 21
0
 def create(self):
     e = gensym('_e')
     self.judge(e)
     return e
Ejemplo n.º 22
0
def BType(name=gensym('BT'), poss=_M):
    T = BTypeClass(name)
    return add_to_model(T, poss)
Ejemplo n.º 23
0
 def __init__(self, types):
     self.types = types
     #self.depends_on = []
     self.name = gensym('h')
Ejemplo n.º 24
0
def _make_continuation(k: Callable[[Ast], Ast]) -> LambdaAst:
    continuation = gensym("R")
    return LambdaAst('', [continuation], k(VarAst(continuation)))
Ejemplo n.º 25
0
def userjs(file):
    return "userjs/" + utils.gensym(file) + ".js"
Ejemplo n.º 26
0
 def __init__(self,name=gensym('BT'),nu=None):
     ttr.BType.__init__(self,name)
     self.fixed_nu = nu
Ejemplo n.º 27
0
 def create(self):
     a = gensym('_sigma')
     self.judge(a)
     return (a)
Ejemplo n.º 28
0
def create_method_type(self):
    a = gensym('_T')
    self.judge(a)
    return a
Ejemplo n.º 29
0
def create_method_rec(self):
    a = gensym('_r')
    self.judge(a)
    return a
Ejemplo n.º 30
0
 def __init__(self,types):
     self.types = types
     #self.depends_on = []
     self.name = gensym('h')
Ejemplo n.º 31
0
 def create(self):
     a = gensym('_a')
     self.judge(a)
     return a
Ejemplo n.º 32
0
 def __init__(self, name=gensym('BT'), nu=None):
     ttr.BType.__init__(self, name)
     self.fixed_nu = nu
Ejemplo n.º 33
0
 def create(self):
     e = gensym('_e')
     self.judge(e)
     return e