def test_lt_(): assert Z(1).__lt__(Z(0)) == Bool.false() assert Z(1).__lt__(Z(-1)) == Bool.false() assert Z(-1).__lt__(Z(-2)) == Bool.false() assert Z(0).__lt__(Z(0)) == Bool.false() assert Z(-1).__lt__(Z(-1)) == Bool.false()
def test_has(): my_map = Map({1: 1, -2: -2, 0: 0}) assert my_map.has(Z(1)) == Bool.true() assert my_map.has(Z(-2)) == Bool.true() assert my_map.has(Z(-200)) == Bool.false() assert Map.empty().has(Z(-200)) == Bool.false()
def __lt__(self: Z, other: Z) -> Z: if (self == Z.cons(pos=var.x1, neg=var.x2)) and (other == Z.cons( pos=var.y1, neg=var.y2)): # simply the oposite of __gt__ if var.x1 < var.y1: return Bool.true() elif var.x2 > var.y2: return Bool.true() else: return Bool.false()
def __gt__(self: Z, other: Z) -> Z: if (self == Z.cons(pos=var.x1, neg=var.x2)) and (other == Z.cons( pos=var.y1, neg=var.y2)): # if the pos part is greater # it's true if var.x1 > var.y1: return Bool.true() # same thing if the neg part is smaller elif var.x2 < var.y2: return Bool.true() else: return Bool.false()
def has(my_map: Map, key: Z) -> Map: # same reasonnement as get_value, # but we return true/false instead of value # if the map is empty , it's end if my_map == Map.empty(): return Bool.false() elif my_map == Map.add(my_map=var.map, key=var.key, value=var.value): if var.key == key: return Bool.true() return (var.map).has(key)
def __lt__(self: Nat, other: Nat) -> Bool: # 0 < 0 = false if (self == Nat.zero()) and (other == Nat.zero()): return Bool.false() # 0 < suc(y) = true if (self == Nat.zero()) and (other == Nat.suc(var.y)): return Bool.true() # suc(x) < 0 = false if (self == Nat.suc(var.x)) and (other == Nat.zero()): return Bool.false() # suc(x) < suc(y) = x < y if (self == Nat.suc(var.x)) and (other == Nat.suc(var.y)): return var.x < var.y
def eval_instr(prog: Prog) -> Prog: # assign statement if car(prog.block) == Instr.i_assign(varName=var.name, expr=var.expr): literal = Expr.eval_expr(expr=var.expr, context=prog.context) return prog.where( context = add(c=prog.context, k=var.name, v=literal), func_list = prog.func_list, block = prog.block) # if statement if car(prog.block) == Instr.i_if(cond=var.cond, b_then=var.b_then, b_else=var.b_else): bool_literal = Expr.eval_expr(expr=var.cond, context=prog.context) if bool_literal == Bool.true(): return prog.where( context = prog.context, func_list = prog.func_list, block = prog.block) if bool_literal == Bool.false(): return prog.where( context = prog.context, func_list = prog.func_list, block = prog.block) # while statement if car(prog.block) == Instr.i_while(cond=var.cond, block=var.block): bool_literal = eval_expr(expr=var.cond, context=prog.context) if bool_literal == Bool.true(): return prog.where( context = prog.context, func_list = prog.func_list, block = prog.block) if bool_literal == Bool.false(): return prog.where( context = prog.context, func_list = prog.func_list, block = prog.block) # expr statement if car(prog.block) == Inst.i_expr(var.expr): Expr.eval_expr(expr=var.expr, head=prog.context) return prog.where( context = prog.context, func_list = prog.func_list, block = prog.block) pass
def __le__(self: Nat, other: Nat) -> Bool: if self == other: return Bool.true() return self < other