cols.append(v) rows.append(tuple(cols)) return linda.Value(tuple(rows)) def valueToGrid(val, memo): g = Grid() for x in range(9): for y in range(9): if val[x][y].sum_pos == 1: v = None else: v = int(val[x][y]) g.grid[x][y] = v return g gridtype = linda.Type("grid :: ((int + Nil) * 9) * 9;", Grid, gridToValue, valueToGrid) def master(): grid = Grid() # Create an empty grid work_ts = linda.TupleSpace() linda.uts._out(("work_ts", work_ts) func = linda.loadCodeFromFile("difficulty_func.min") linda.uts._out(("difficulty_func", func)) start = time.time() linda.uts._out((empty, grid)) tup = linda.uts._in((gridtype, ))
def squareToValue(sq, memo): if sq.val is None: v = linda.Nil() v.sum_pos = 0 else: v = linda.Value(sq.val) v.sum_pos = 1 return v def valueToSquare(val, memo): if val.isNil(): return Square(None) else: return Square(int(val)) sqtype = linda.Type("square :: Nil + int;", Square, squareToValue, valueToSquare) class Row: def __init__(self, row): self.row = row def rowToValue(row, memo): return linda.Value(row.row) def valueToRow(val, memo): return Row(list(val)) rowtype = linda.Type("row :: square * 9;", Row, rowToValue, valueToRow) class Grid: def __init__(self, rows):
#!/usr/bin/python import linda linda.connect() t = linda.Type("a :: (long * long);") v = linda.Value((1, 2), t) linda.uts._out((v, )) print linda.uts._in((t, ))
#!/usr/bin/python import linda linda.connect() t1 = linda.Type("t1 :: int * (int * int);") t2 = linda.Type("t2 :: (int * int) * int;") t3 = linda.Type("t3 :: int * int * int;") v = linda.Value((1, (2, 3)), t1) linda.uts._out((v, )) print linda.uts._rd((t1, )) print linda.uts._rd((t2, )) print linda.uts._in((t3, ))
self.other = None def __str__(self): return "<Test %s>" % (str(self.other is self)) def convertTo(val, memo): v = linda.Ptr(None, type) memo.register(val, v) v.ptr = memo.getValue(val.other) return v def convertFrom(val, memo): t = Test() memo.register(val, t) t.other = val.ptr return t type = linda.Type("Test :: ptr(Test);", Test, convertTo, convertFrom) v = Test() v.other = v linda.uts._out((v, )) tup = linda.uts._in((Test, )) print tup print id(tup[0]) print id(tup[0].other)
#!/usr/bin/python import linda linda.connect() t = linda.Type("a :: ((int * int) * bool);") v = linda.Value(((1, 1), True), t) linda.uts._out((v, )) t2 = linda.Type("b :: int * int;") t3 = linda.Type("c :: (b * bool);") print linda.uts._in((t3, ))
double :: int -> int; double x = 2 * x; """) print f(2) linda.uts._out((f, )) (nf, ) = linda.uts._rd((f.type, )) print nf(4) (nf, ) = linda.uts._in((f, )) print "func2" f2 = linda.Function(""" double2 :: (int * 1) -> (int * 1); double2 x = 2 * x[0]; """) print f2((6, )) linda.uts._out((f2, )) (nf2, ) = linda.uts._rd((f2.type, )) print nf2((8, )) t = linda.Type("f2 :: (int * ()) -> (int * ());") (nf2, ) = linda.uts._in((t, )) print nf2((10, ()))
#!/usr/bin/python import linda linda.connect() t1 = linda.Type("t1 :: int + string;") t2 = linda.Type("t2 :: string + int;") v = linda.Sum(1, 0, t1) linda.uts._out((v, )) r = linda.uts._rd((t1, )) print r, r[0].sum_pos r = linda.uts._rd((t2, )) print r, r[0].sum_pos
#!/usr/bin/python import linda linda.connect() type = linda.Type("inttype :: int;") value = linda.Value(1, type) linda.uts._out((value, )) print linda.uts._rd((value, )) print linda.uts._rd((type, ))
#!/usr/bin/python import linda linda.connect() t = linda.Type("a :: int;") v = linda.Value(1, t) linda.uts._out((v, )) print linda.uts._in((t, ))
#!/usr/bin/python import linda linda.connect() type = linda.Type("intlist :: Nil + (int * intlist);") print type value = linda.Value(None, type) value.setSumPos(0) linda.uts._out((value, )) value = linda.Value(None) value.setSumPos(0) value = linda.Value((3, value)) value.setSumPos(1) value = linda.Value((2, value)) value.setSumPos(1) value = linda.Value((1, value), type) value.setSumPos(1) # end result is (1, (2, (3, None))) linda.uts._out((value, )) print linda.uts._in((type, )) print linda.uts._in((type, ))
#!/usr/bin/python import linda linda.connect() t1 = linda.Type("t1 :: int * int * int;") t2 = linda.Type("t2 :: int * (int * int);") print len(t1), len(t2) v = linda.Value((1, 2, 3), t1) print v linda.uts._out((v, )) print linda.uts._in((t2, ))[0]
#!/usr/bin/python import linda linda.connect() linda.uts._out((1, )) print linda.uts._rd((int, )) t = linda.Type("t :: long;") print linda.uts._in((t, ))
#!/usr/bin/python import linda linda.connect() ptr1 = linda.Type("ptr1 :: int * ptr(ptr2);") ptr2 = linda.Type("ptr2 :: int;") value2 = linda.Value(1, ptr2) value = linda.Value((1, linda.Ptr(value2)), ptr1) linda.uts._out((value, )) print linda.uts._in((value, ))
#!/usr/bin/python import linda linda.connect() type1 = linda.Type("intlist :: Nil + (int * floatlist);") type2 = linda.Type("floatlist :: Nil + (float * intlist);") value = linda.Value(None, type1) value.setSumPos(0) linda.uts._out((value, )) value = linda.Value(None) value.setSumPos(0) value = linda.Value((3, value)) value.setSumPos(1) value = linda.Value((2.0, value)) value.setSumPos(1) value = linda.Value((1, value), type1) value.setSumPos(1) # end result is (1, (2, (3, None))) linda.uts._out((value, )) print linda.uts._in((type1, )) print linda.uts._in((type1, ))
#!/usr/bin/python import linda linda.connect() class IntPair: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return "<IntPair %i %i>" % (self.a, self.b) def IntPairToValue(pair, memo): return linda.Value((pair.a, pair.b)) def ValueToIntPair(v, memo): pair = IntPair(int(v[0]), int(v[1])) # memo.register(v, pair) return pair t = linda.Type("intpair :: (long * long);", IntPair, IntPairToValue, ValueToIntPair) linda.uts._out((IntPair(1, 2), )) print linda.uts._in((IntPair, ))