def create_hypobj(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T,Type): res.addfield(l,T.create_hypobj()) else: depfields.addfield(l,T) return ProcessDepFields(depfields,res,self,'hyp')
def create_hypobj(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T,Type): res.addfield(l,T.create_hypobj()) else: depfields.addfield(l,T) return ProcessDepFields(depfields,res,self,'hyp')
def create(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T, TypeClass): res.addfield(l, T.in_poss(self.poss).create()) else: depfields.addfield(l, T) return ProcessDepFields(depfields, res, self)
def __init__(self, d={}): self.comps = Rec() for item in d.items(): if isinstance(item[1], dict): self.addfield(item[0], RecType(item[1])) else: self.addfield(item[0], item[1]) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [ \ lambda r: isinstance(r, Rec) and RecOfRecType(r,self,self.poss)] self.poss = ''
def __init__(self, T1, T2): self.comps = Rec({'domain': T1, 'range': T2}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [lambda f: isinstance(f,Fun) \ and f.domain_type == self.comps.domain \ and self.comps.range.query(f.app(self.comps.domain.create_hypobj()))]
def __init__(self, T): self.comps = Rec({'base_type': T}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [ lambda l: isinstance(l, list) and forall(l, lambda x: T.query(x)) ]
def __init__(self, pred, args): self.comps = Rec({'pred': pred, 'args': args}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [] self.witness_types = [] self.poss = ''
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 = []
def __init__(self, T, a): self.comps = Rec({'base_type': T, 'obj': a}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [lambda x: x == a and T.query(x),\ lambda x: isinstance(a,LazyObj)\ and show(x) == show(a.eval()) and T.query(x)]
def __init__(self, T1, T2): self.comps = Rec({'left': T1, 'right': T2}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [lambda a: self.comps.left.in_poss(self.poss).query(a), \ lambda a: self.comps.right.in_poss(self.poss).query(a)] self.witness_types = [] self.poss = ''
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 = ''
def __init__(self, list): self.comps = Rec({'types': list}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = \ [lambda s: isinstance(s,TTRString) \ and len(s.items) == len(self.comps.types) \ and forall([x for x in range(len(self.comps.types))], \ lambda i: self.comps.types[i].in_poss(self.poss).query(s.items[i]))] self.poss = ''
def __init__(self, T): self.comps = Rec({'base_type': T}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = \ [lambda s: isinstance(s,TTRString) \ and len(s.items)>0 \ and forall(s.items, lambda a: self.comps.base_type.in_poss(self.poss).query(a))] self.poss = ''
def __init__(self, strT): self.comps = Rec({'base_type': strT}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = \ [lambda s: isinstance(s,TTRString) and s.startswith_recursive(self.comps.base_type)] # and len(s.items)>0 \ # and forall(s.items, # lambda a: self.comps.base_type.in_poss(self.poss).query(a))] self.witness_types = [] self.poss = _M
def __init__(self,d={}): self.comps = Rec() for item in d.items(): if isinstance(item[1], dict): self.addfield(item[0], RecType(item[1])) else: self.addfield(item[0], item[1]) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [ \ lambda r: isinstance(r, Rec) and RecOfRecType(r,self,self.poss)] self.witness_types = [] self.poss = ''
def __init__(self, T): self.comps = Rec({'base_type': T}) self.witness_conditions = [ lambda a, c, oracle: PNeg(self.comps.base_type.query(a, c, oracle)) ]
class RecType(Type): def __init__(self,d={}): self.comps = Rec() for item in d.items(): if isinstance(item[1], dict): self.addfield(item[0], RecType(item[1])) else: self.addfield(item[0], item[1]) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [ \ lambda r: isinstance(r, Rec) and RecOfRecType(r,self,self.poss)] self.witness_types = [] self.poss = '' def in_poss(self,poss): self.poss = poss return self def show(self): s = "" for kvp in self.comps.__dict__.items(): if s == "": s = s + kvp[0] + " : " else: s = s + ", "+kvp[0] + " : " if(isinstance(kvp[1], RecType)): s = s + kvp[1].show() else: s = s + show(kvp[1]) return "{"+s+"}" def to_latex(self,vars): s = "" for kvp in self.comps.__dict__.items(): if s == "": s = s + to_latex(kvp[0]) + " &:& " else: s = s + "\\\\\n"+to_latex(kvp[0]) + " &:& " if(isinstance(kvp[1], RecType)): s = s + kvp[1].to_latex(vars) else: s = s + to_latex(kvp[1],vars) return "\\left[\\begin{array}{lcl}\n"+s+"\n\\end{array}\\right]" def validate(self): if forall(list(self.comps.__dict__.items()),lambda x: CheckField(x,self)) and not self.create_hypobj() == None: return True else: return False def addfield(self, label, value): if label in self.comps.__dict__.keys(): print("\"" +label + "\"" + " is already a label in this record type") else: self.comps.__setattr__(label, value) # def pathvalue(self,path): # if len(path) == 1: # return self.comps.__getattribute__(str(path[0])) # else: # return Rec.pathvalue(self.comps.__getattribute__(path[0]), path[1:]) def pathvalue(self, path): splits=deque(path.split(".")) if (len(splits) == 1): if splits[0] in dir(self.comps): return self.comps.__getattribute__(splits[0]) else: if ttracing('pathvalue'): print(splits[0]+' not a label in '+self.show()) return None else: addr = splits.popleft() if 'pathvalue' not in dir(self.comps.__getattribute__(addr)): if ttracing('pathvalue'): print('No paths into '+show(self.comps.__getattribute__(addr))) return None else: return self.comps.__getattribute__(addr).pathvalue(".".join(splits)) def learn_witness_condition(self,c): logtype(self,c) def learn_witness_type(self,c): logtype_t(self,c) def create(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T,Type): res.addfield(l,T.in_poss(self.poss).create()) else: depfields.addfield(l,T) return ProcessDepFields(depfields,res,self) def create_hypobj(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T,Type): res.addfield(l,T.create_hypobj()) else: depfields.addfield(l,T) return ProcessDepFields(depfields,res,self,'hyp') #Recursive for future use?? def Relabel(self, oldlabel, newlabel, recursive=False): if oldlabel in self.comps.__dict__.keys(): value = self.comps.__dict__[oldlabel] self.comps.__delattr__(oldlabel) self.comps.__setattr__(newlabel, value) return def subst(self,v,a): res = RecType() for l in self.comps.__dict__.keys(): if self.comps.__getattribute__(l) == v: res.addfield(l,a) elif isinstance(self.comps.__getattribute__(l),str): res.addfield(l,self.comps.__getattribute__(l)) else: res.addfield(l,substitute(self.comps.__getattribute__(l),v,a)) #print(show(res)) return res def eval(self): self.comps.eval() return self def merge(self,T): if isinstance(T,RecType): res = RecType({}) SharedLabels = [l for l in LabelsRecType(self) if l in LabelsRecType(T)] OtherLabelsSelf = [l for l in LabelsRecType(self) if l not in LabelsRecType(T)] OtherLabelsT = [l for l in LabelsRecType(T) if l not in LabelsRecType(self)] for label in SharedLabels: T1 = AttValRecType(self,label) T2 = AttValRecType(T,label) if isinstance(T1, tuple) and isinstance(T2, tuple): if T1[1] == T2[1]: f1 = T1[0] f2 = T2[0] res.addfield(label, (merge_dep_types(f1,f2),T1[1])) else: f1 = T1[0] f2 = T2[0] res.addfield(label, (combine_dep_types(f1,f2),T1[1]+T2[1])) elif isinstance(T1,tuple) and isinstance(T2,Type): res.addfield(label, (combine_dep_types(T1[0],T2),T1[1])) elif isinstance(T1,Type) and isinstance(T2,tuple): res.addfield(label, (combine_dep_types(T1,T2[0]),T2[1])) else: res.addfield(label, T1.merge(T2)) for label in OtherLabelsSelf: res.addfield(label,AttValRecType(self,label)) for label in OtherLabelsT: res.addfield(label,AttValRecType(T,label)) return res elif self.subtype_of(T): return self else: return MeetType(self,T) def amerge(self,T): if isinstance(T,RecType): res = RecType({}) SharedLabels = [l for l in LabelsRecType(self) if l in LabelsRecType(T)] OtherLabelsSelf = [l for l in LabelsRecType(self) if l not in LabelsRecType(T)] OtherLabelsT = [l for l in LabelsRecType(T) if l not in LabelsRecType(self)] for label in SharedLabels: T1 = AttValRecType(self,label) T2 = AttValRecType(T,label) if isinstance(T1, tuple) and isinstance(T2, tuple): if T1[1] == T2[1]: f1 = T1[0] f2 = T2[0] if subtype_of_dep_types(f1,f2): res.addfield(label, T1) else: res.addfield(label, T2) else: res.addfield(label, T2) elif isinstance(T1,tuple) and isinstance(T2,Type): res.addfield(label, T2) elif isinstance(T1,Type) and isinstance(T2,tuple): res.addfield(label,T2) elif isinstance(T1,Type) and isinstance(T2,Type): if T1.subtype_of(T2): res.addfield(label, T1) else: res.addfield(label, T1.amerge(T2)) else: res.addfield(label,T2) for label in OtherLabelsSelf: res.addfield(label,AttValRecType(self,label)) for label in OtherLabelsT: res.addfield(label,AttValRecType(T,label)) return res elif self.subtype_of(T): return self else: return T
class RecType(Type): def __init__(self, d={}): self.comps = Rec() for item in d.items(): if isinstance(item[1], dict): self.addfield(item[0], RecType(item[1])) else: self.addfield(item[0], item[1]) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [ \ lambda r: isinstance(r, Rec) and RecOfRecType(r,self,self.poss)] self.witness_types = [] self.poss = '' def in_poss(self, poss): self.poss = poss return self def show(self): s = "" for kvp in self.comps.__dict__.items(): if s == "": s = s + kvp[0] + " : " else: s = s + ", " + kvp[0] + " : " if (isinstance(kvp[1], RecType)): s = s + kvp[1].show() else: s = s + show(kvp[1]) return "{" + s + "}" def to_latex(self, vars): s = "" for kvp in self.comps.__dict__.items(): if s == "": s = s + to_latex(kvp[0]) + " &:& " else: s = s + "\\\\\n" + to_latex(kvp[0]) + " &:& " if (isinstance(kvp[1], RecType)): s = s + kvp[1].to_latex(vars) else: s = s + to_latex(kvp[1], vars) return "\\left[\\begin{array}{lcl}\n" + s + "\n\\end{array}\\right]" def validate(self): if forall(list(self.comps.__dict__.items()), lambda x: CheckField( x, self)) and not self.create_hypobj() == None: return True else: return False def addfield(self, label, value): if label in self.comps.__dict__.keys(): print("\"" + label + "\"" + " is already a label in this record type") else: self.comps.__setattr__(label, value) # def pathvalue(self,path): # if len(path) == 1: # return self.comps.__getattribute__(str(path[0])) # else: # return Rec.pathvalue(self.comps.__getattribute__(path[0]), path[1:]) def pathvalue(self, path): splits = deque(path.split(".")) if (len(splits) == 1): if splits[0] in dir(self.comps): return self.comps.__getattribute__(splits[0]) else: if ttracing('pathvalue'): print(splits[0] + ' not a label in ' + self.show()) return None else: addr = splits.popleft() if 'pathvalue' not in dir(self.comps.__getattribute__(addr)): if ttracing('pathvalue'): print('No paths into ' + show(self.comps.__getattribute__(addr))) return None else: return self.comps.__getattribute__(addr).pathvalue( ".".join(splits)) def learn_witness_condition(self, c): logtype(self, c) def learn_witness_type(self, c): logtype_t(self, c) def create(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T, Type): res.addfield(l, T.in_poss(self.poss).create()) else: depfields.addfield(l, T) return ProcessDepFields(depfields, res, self) def create_hypobj(self): res = Rec() depfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T, Type): res.addfield(l, T.create_hypobj()) else: depfields.addfield(l, T) return ProcessDepFields(depfields, res, self, 'hyp') #Recursive for future use?? def Relabel(self, oldlabel, newlabel, recursive=False): if oldlabel in self.comps.__dict__.keys(): value = self.comps.__dict__[oldlabel] self.comps.__delattr__(oldlabel) self.comps.__setattr__(newlabel, value) return def subst(self, v, a): res = RecType() for l in self.comps.__dict__.keys(): if self.comps.__getattribute__(l) == v: res.addfield(l, a) elif isinstance(self.comps.__getattribute__(l), str): res.addfield(l, self.comps.__getattribute__(l)) else: res.addfield(l, substitute(self.comps.__getattribute__(l), v, a)) #print(show(res)) return res def eval(self): self.comps.eval() return self def merge(self, T): if isinstance(T, RecType): res = RecType({}) SharedLabels = [ l for l in LabelsRecType(self) if l in LabelsRecType(T) ] OtherLabelsSelf = [ l for l in LabelsRecType(self) if l not in LabelsRecType(T) ] OtherLabelsT = [ l for l in LabelsRecType(T) if l not in LabelsRecType(self) ] for label in SharedLabels: T1 = AttValRecType(self, label) T2 = AttValRecType(T, label) if isinstance(T1, tuple) and isinstance(T2, tuple): if T1[1] == T2[1]: f1 = T1[0] f2 = T2[0] res.addfield(label, (merge_dep_types(f1, f2), T1[1])) else: f1 = T1[0] f2 = T2[0] res.addfield( label, (combine_dep_types(f1, f2), T1[1] + T2[1])) elif isinstance(T1, tuple) and isinstance(T2, Type): res.addfield(label, (combine_dep_types(T1[0], T2), T1[1])) elif isinstance(T1, Type) and isinstance(T2, tuple): res.addfield(label, (combine_dep_types(T1, T2[0]), T2[1])) else: res.addfield(label, T1.merge(T2)) for label in OtherLabelsSelf: res.addfield(label, AttValRecType(self, label)) for label in OtherLabelsT: res.addfield(label, AttValRecType(T, label)) return res elif self.subtype_of(T): return self else: return MeetType(self, T) def amerge(self, T): if isinstance(T, RecType): res = RecType({}) SharedLabels = [ l for l in LabelsRecType(self) if l in LabelsRecType(T) ] OtherLabelsSelf = [ l for l in LabelsRecType(self) if l not in LabelsRecType(T) ] OtherLabelsT = [ l for l in LabelsRecType(T) if l not in LabelsRecType(self) ] for label in SharedLabels: T1 = AttValRecType(self, label) T2 = AttValRecType(T, label) if isinstance(T1, tuple) and isinstance(T2, tuple): if T1[1] == T2[1]: f1 = T1[0] f2 = T2[0] if subtype_of_dep_types(f1, f2): res.addfield(label, T1) else: res.addfield(label, T2) else: res.addfield(label, T2) elif isinstance(T1, tuple) and isinstance(T2, Type): res.addfield(label, T2) elif isinstance(T1, Type) and isinstance(T2, tuple): res.addfield(label, T2) elif isinstance(T1, Type) and isinstance(T2, Type): if T1.subtype_of(T2): res.addfield(label, T1) else: res.addfield(label, T1.amerge(T2)) else: res.addfield(label, T2) for label in OtherLabelsSelf: res.addfield(label, AttValRecType(self, label)) for label in OtherLabelsT: res.addfield(label, AttValRecType(T, label)) return res elif self.subtype_of(T): return self else: return T
""" Created on Tue Mar 3 15:05:58 2015 @author: cooper """ from records import Rec from utils import show r = Rec({ 'f': { 'f': { 'ff': 'a', 'gg': 'b' }, 'g': 'c' }, 'g': { 'h': { 'g': 'a', 'h': 'd' } } }) print(show(r)) print(show(r.f)) print(show(r.g.h)) print(show(r.f.f.ff)) print(show(r.subst('a', 'z')))
def __init__(self, T1, T2): self.comps = Rec({'left': T1, 'right': T2}) self.witness_cache = [] self.supertype_cache = [] self.witness_conditions = [lambda a: self.comps.left.query(a) \ and self.comps.right.query(a)]
print(show(Fun('x', Ind, PType(man, ['x'])).app('j'))) print(show(manj)) print(manj.query_nonspec()) M = Possibility('M') manj.in_poss(M) print(Fun('x', Ind, PType(man, ['x'])).app('j').in_poss(M).query_nonspec()) print(show(M)) print(Ty.query(RecType({'x': Ind}))) print(RecType({'x': Ind}).query(Rec({'x': 'j'}))) print(RecType({'x': Ind}).query(Rec({'x': 'j', 'y': 'k', 'z': 'e1'}))) print(RecType({'x': Ind, 'y': Ind}).query(Rec({'x': 'j'}))) #False print( RecTy.query( RecType({ 'x': Ind, 'c': (Fun('v', Ind, PType(man, ['v'])), ['x']) }))) T_man = RecType({'x': Ind, 'c': (Fun('v', Ind, PType(man, ['v'])), ['x'])}) print(T_man.query(Rec({'x': 'j', 'c': '_e1'})))
def sample(self, n=config.sample_size): chart = {} count = -1 nondepfields = RecType() for l in self.comps.__dict__: T = self.comps.__getattribute__(l) if isinstance(T, RecType): count = count + 1 chart[count] = [] vals = T.in_poss(self.poss).sample(n) #print(show(T.in_poss(self.poss))) #print(vals) if count == 0: for r in vals: chart[count].append(Rec({l: r})) #print(chart) else: for r in vals: for rec in chart[count - 1]: combrec = rec.addrec(Rec({l: r})) if combrec: chart[count].append(combrec) else: pass elif isinstance(T, TypeClass): nondepfields.addfield(l, T) else: count = count + 1 chart[count] = [] matches = T[0].matches(self.poss, n) #print('matches: ', show(matches)) for m in matches: for s in m[0].in_poss(self.poss).sample(n): r = Rec({l: s}) #print('r: ',show(r)) #print('T[1]',show(T[1])) for p in T[1]: newrec = Rec({}) newrec.addpath(p, m[1][T[1].index(p)]) #print('newrec:',show(newrec)) r = r.addrec(newrec) #print('r after addrec: ',show(r)) if count == 0: chart[count].append(r) else: for rec in chart[count - 1]: combrec = rec.addrec(r) if combrec: chart[count].append(combrec) count = count + 1 chart[count] = [] #print(show(nondepfields)) if count == 0: newrecs = [] for l in nondepfields.comps.__dict__: matches = nondepfields.comps.__getattribute__(l).in_poss( self.poss).sample(n) if newrecs == []: for i in range(2 * n): m = np.random.choice(matches) newrecs.append(Rec({l: m})) else: for i in range(len(newrecs)): m = np.random.choice(matches) newrecs[i] = newrecs[i].addrec(Rec({l: m})) for r in newrecs: chart[count].append(r) else: for rec in chart[count - 1]: #print([l for l in rec.__dict__],[l for l in nondepfields.comps.__dict__]) if set([l for l in nondepfields.comps.__dict__ ]).issubset(set([l for l in rec.__dict__])): chart[count].append(rec) else: newrecs = [] for l in [ l for l in nondepfields.comps.__dict__ if not l in rec.__dict__ ]: matches = nondepfields.comps.__getattribute__( l).in_poss(self.poss).sample(n) if newrecs == []: for i in range(2 * n): m = np.random.choice(matches) newrecs.append(Rec({l: m})) # for m in matches: # newrecs.append(Rec({l:m})) else: for i in range(len(newrecs)): m = np.random.choice(matches) newrecs[i] = newrecs[i].addrec(Rec({l: m})) # for r in newrecs: # m = np.random.choice(matches) # r.addrec(Rec({l:m})) for r in newrecs: chart[count].append(rec.addrec(r)) #print(show(chart)) res = [] for r in chart[count]: if not any(map(lambda x: equal(x, r), res)): res.append(r) if len(res) <= n: return res else: return list(np.random.choice(res, n, False))