def map(func, dict): _cv = dict res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBEmpty_elm_builtin res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'color'), (Var, 'key'), (Var, 'value'), (Var, 'left'), (Var, 'right')) if res is not None: color = res['color'] key = res['key'] value = res['value'] left = res['left'] right = res['right'] return RBNode_elm_builtin(color, key, (func(key, value)), (map(func, left)), (map(func, right)))
def stepState(*args): rKey, rValue, (list, result) = args _cv = list res = patternMatch(_cv, PList ) if res is not None: return (list, rightStep(rKey, rValue, result)) res = patternMatch(_cv, PCons, ((Var, 'lKey'), (Var, 'lValue')), (Var, 'rest') ) if res is not None: lKey = res['lKey'] lValue = res['lValue'] rest = res['rest'] if lKey < rKey: return stepState(rKey, rValue, (rest, leftStep(lKey, lValue, result))) else: if lKey > rKey: return (list, rightStep(rKey, rValue, result)) else: return (rest, bothStep(lKey, lValue, rValue, result))
def removeHelpPrepEQGT(targetKey, dict, color, key, value, left, right): _cv = left res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'lK'), (Var, 'lV'), (Var, 'lLeft'), (Var, 'lRight')) if res is not None: lK = res['lK'] lV = res['lV'] lLeft = res['lLeft'] lRight = res['lRight'] return RBNode_elm_builtin(color, lK, lV, lLeft, (RBNode_elm_builtin(Red, key, value, lRight, right))) _cv = right res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Black), Any, Any, (Nested, [ (Variant, RBNode_elm_builtin), (Val, Black), Any, Any, Any, Any]), Any) if res is not None: return moveRedRight(dict) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Black), Any, Any, (Val, RBEmpty_elm_builtin), Any) if res is not None: return moveRedRight(dict) return dict
def removeHelpEQGT(targetKey, dict): _cv = dict res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'color'), (Var, 'key'), (Var, 'value'), (Var, 'left'), (Var, 'right')) if res is not None: color = res['color'] key = res['key'] value = res['value'] left = res['left'] right = res['right'] if targetKey == key: _cv = getMin(right) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), Any, (Var, 'minKey'), (Var, 'minValue'), Any, Any) if res is not None: minKey = res['minKey'] minValue = res['minValue'] return balance(color, minKey, minValue, left, (removeMin(right))) res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBEmpty_elm_builtin else: return balance(color, key, value, left, (removeHelp(targetKey, right))) res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBEmpty_elm_builtin
def member(key, dict): _cv = get(key, dict) res = patternMatch(_cv, (Variant, Just), Any) if res is not None: return True res = patternMatch(_cv, (Variant, Nothing)) if res is not None: return False
def update(targetKey, alter, dictionary): _cv = alter((get(targetKey, dictionary))) res = patternMatch(_cv, (Variant, Just), (Var, 'value')) if res is not None: value = res['value'] return insert(targetKey, value, dictionary) res = patternMatch(_cv, (Variant, Nothing)) if res is not None: return remove(targetKey, dictionary)
def insertHelp(key, value, dict): _cv = dict res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBNode_elm_builtin(Red, key, value, RBEmpty_elm_builtin, RBEmpty_elm_builtin) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'nColor'), (Var, 'nKey'), (Var, 'nValue'), (Var, 'nLeft'), (Var, 'nRight')) if res is not None: nColor = res['nColor'] nKey = res['nKey'] nValue = res['nValue'] nLeft = res['nLeft'] nRight = res['nRight'] _cv = compare(key, nKey) res = patternMatch(_cv, (Variant, LT)) if res is not None: return balance(nColor, nKey, nValue, (insertHelp(key, value, nLeft)), nRight) res = patternMatch(_cv, (Variant, EQ)) if res is not None: return RBNode_elm_builtin(nColor, nKey, value, nLeft, nRight) res = patternMatch(_cv, (Variant, GT)) if res is not None: return balance(nColor, nKey, nValue, nLeft, (insertHelp(key, value, nRight)))
def get(targetKey, dict): _cv = dict res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return Nothing res = patternMatch(_cv, (Variant, RBNode_elm_builtin), Any, (Var, 'key'), (Var, 'value'), (Var, 'left'), (Var, 'right')) if res is not None: key = res['key'] value = res['value'] left = res['left'] right = res['right'] _cv = compare(targetKey, key) res = patternMatch(_cv, (Variant, LT)) if res is not None: return get(targetKey, left) res = patternMatch(_cv, (Variant, EQ)) if res is not None: return Just(value) res = patternMatch(_cv, (Variant, GT)) if res is not None: return get(targetKey, right)
def isEmpty(dict): _cv = dict res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return True res = patternMatch(_cv, (Variant, RBNode_elm_builtin), Any, Any, Any, Any, Any) if res is not None: return False
def sizeHelp(n, dict): _cv = dict res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return n res = patternMatch(_cv, (Variant, RBNode_elm_builtin), Any, Any, Any, (Var, 'left'), (Var, 'right')) if res is not None: left = res['left'] right = res['right'] return sizeHelp((sizeHelp((n + 1), right)), left)
def maxDepth(dct): res = patternMatch(dct, (Variant, Dict.RBNode_elm_builtin), Any, Any, Any, (Var, 'left'), (Var, 'right'), ) if res is None: return 0 return 1 + max(maxDepth(res['left']), maxDepth(res['right']))
def foldr(func, acc, t): _cv = t res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return acc res = patternMatch(_cv, (Variant, RBNode_elm_builtin), Any, (Var, 'key'), (Var, 'value'), (Var, 'left'), (Var, 'right')) if res is not None: key = res['key'] value = res['value'] left = res['left'] right = res['right'] return foldr(func, (func(key, value, (foldr(func, acc, right)))), left)
def remove(key, dict): _cv = removeHelp(key, dict) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'k'), (Var, 'v'), (Var, 'l'), (Var, 'r')) if res is not None: k = res['k'] v = res['v'] l = res['l'] r = res['r'] return RBNode_elm_builtin(Black, k, v, l, r) x = _cv return x
def getMin(dict): _cv = dict res = patternMatch(_cv, (Variant, RBNode_elm_builtin), Any, Any, Any, (AsVar, 'left', ( (Variant, RBNode_elm_builtin), Any, Any, Any, Any, Any)), Any) if res is not None: left = res['left'] return getMin(left) return dict
def testLists(): lst0 = List.empty lst1 = List.toElm([1]) lst2 = List.toElm([1, 2, 3]) tupList = List.toElm([(1, 10), (2, 20)]) assertEqual(True, patternMatch(lst0, PList)) assertEqual(None, patternMatch(lst1, PList)) assertEqual(None, patternMatch(lst0, PCons, Any, Any)) assertEqual(True, patternMatch(lst1, PCons, Any, Any)) assertEqual(True, patternMatch(lst2, PCons, Any, Any)) # good vals assertEqual(True, patternMatch(lst1, PCons, (Val, 1), Any)) assertEqual(True, patternMatch(lst2, PCons, (Val, 1), Any)) # bad vals assertEqual(None, patternMatch(lst1, PCons, (Val, 5), Any)) assertEqual(None, patternMatch(lst2, PCons, (Val, 5), Any)) # capture res = patternMatch(lst2, PCons, (Var, 'h'), (Var, 'r')) assertEqual(res['h'], 1) assertEqual(list(res['r']), [2, 3]) # tups res = patternMatch(tupList, PCons, ((Var, 't1'), (Var, 't2')), (Var, 'r')) assertEqual(res['t1'], 1) assertEqual(res['t2'], 10) assertEqual(list(res['r']), [(2, 20)]) res = patternMatch(tupList, PCons, ((Val, 5), (Var, 't2')), (Var, 'r')) assertEqual(None, res) res = patternMatch( tupList, PCons, (Any, Any), (Var, 'r'), ) assertEqual(res, dict(r=[(2, 20)]))
def balance(color, key, value, left, right): _cv = right res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'rK'), (Var, 'rV'), (Var, 'rLeft'), (Var, 'rRight')) if res is not None: rK = res['rK'] rV = res['rV'] rLeft = res['rLeft'] rRight = res['rRight'] _cv = left res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'lK'), (Var, 'lV'), (Var, 'lLeft'), (Var, 'lRight')) if res is not None: lK = res['lK'] lV = res['lV'] lLeft = res['lLeft'] lRight = res['lRight'] return RBNode_elm_builtin(Red, key, value, (RBNode_elm_builtin(Black, lK, lV, lLeft, lRight)), (RBNode_elm_builtin(Black, rK, rV, rLeft, rRight))) return RBNode_elm_builtin(color, rK, rV, (RBNode_elm_builtin(Red, key, value, left, rLeft)), rRight) _cv = left res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'lK'), (Var, 'lV'), (Nested, [ (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'llK'), (Var, 'llV'), (Var, 'llLeft'), (Var, 'llRight')]), (Var, 'lRight')) if res is not None: lK = res['lK'] lV = res['lV'] llK = res['llK'] llV = res['llV'] llLeft = res['llLeft'] llRight = res['llRight'] lRight = res['lRight'] return RBNode_elm_builtin(Red, lK, lV, (RBNode_elm_builtin(Black, llK, llV, llLeft, llRight)), (RBNode_elm_builtin(Black, key, value, lRight, right))) return RBNode_elm_builtin(color, key, value, left, right)
def removeHelp(targetKey, dict): _cv = dict res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBEmpty_elm_builtin res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'color'), (Var, 'key'), (Var, 'value'), (Var, 'left'), (Var, 'right')) if res is not None: color = res['color'] key = res['key'] value = res['value'] left = res['left'] right = res['right'] if targetKey < key: _cv = left res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Black), Any, Any, (Var, 'lLeft'), Any) if res is not None: lLeft = res['lLeft'] _cv = lLeft res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), Any, Any, Any, Any) if res is not None: return RBNode_elm_builtin(color, key, value, (removeHelp(targetKey, left)), right) _cv = moveRedLeft(dict) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'nColor'), (Var, 'nKey'), (Var, 'nValue'), (Var, 'nLeft'), (Var, 'nRight')) if res is not None: nColor = res['nColor'] nKey = res['nKey'] nValue = res['nValue'] nLeft = res['nLeft'] nRight = res['nRight'] return balance(nColor, nKey, nValue, (removeHelp(targetKey, nLeft)), nRight) res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBEmpty_elm_builtin return RBNode_elm_builtin(color, key, value, (removeHelp(targetKey, left)), right) else: return removeHelpEQGT(targetKey, (removeHelpPrepEQGT(targetKey, dict, color, key, value, left, right)))
def testCustomTypes(): Number = CustomType('Number', 'Zero', OneDigit=1, TwoDigit=2) Zero = Number.Zero OneDigit = Number.OneDigit TwoDigit = Number.TwoDigit zero = Number.Zero one = Number.OneDigit(1) twelve = Number.TwoDigit(1, 2) v1 = (Val, 1) v2 = (Val, 2) v3 = (Val, 3) v4 = (Val, 4) x = (Var, 'x') y = (Var, 'y') # exact, precise matches assertEqual(True, patternMatch(zero, (Variant, Zero))) assertEqual(True, patternMatch(one, (Variant, OneDigit), v1)) assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), v1, v2)) # bad vtypes assertNone(patternMatch(one, (Variant, Zero))) assertNone(patternMatch(twelve, (Variant, OneDigit), v1)) assertNone(patternMatch(zero, (Variant, TwoDigit), v1, v2)) # good vtypes but wrong values assertNone(patternMatch(one, (Variant, OneDigit), v4)) assertNone(patternMatch(twelve, (Variant, TwoDigit), v1, v3)) # easy matches with Any assertEqual(True, patternMatch(one, (Variant, OneDigit), Any)) assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), Any, Any)) # partial matches with Any assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), v1, Any)) assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), Any, v2)) # mismatches with Any assertNone(patternMatch(twelve, (Variant, TwoDigit), v4, Any)) assertNone(patternMatch(twelve, (Variant, TwoDigit), Any, v4)) # capture matches assertEqual(patternMatch(one, (Variant, OneDigit), x), dict(x=1)) assertEqual(patternMatch(twelve, (Variant, TwoDigit), x, y), dict(x=1, y=2)) # partial capture matches assertEqual(patternMatch(twelve, (Variant, TwoDigit), x, Any), dict(x=1)) assertEqual(patternMatch(twelve, (Variant, TwoDigit), x, v2), dict(x=1)) assertEqual(patternMatch(twelve, (Variant, TwoDigit), Any, y), dict(y=2)) assertEqual(patternMatch(twelve, (Variant, TwoDigit), v1, y), dict(y=2)) # more failures assertNone(patternMatch(twelve, (Variant, TwoDigit), x, v4)) assertNone(patternMatch(twelve, (Variant, TwoDigit), v4, x)) assertNone(patternMatch(zero, (Variant, TwoDigit), x, y)) Tree = CustomType('Tree', 'Empty', Node=3) rrNode = Tree.Node(3, Tree.Empty, Tree.Empty) rNode = Tree.Node(2, Tree.Empty, rrNode) tNode = Tree.Node(1, Tree.Empty, rNode) res = patternMatch(tNode, (Variant, Tree.Node), (Var, 'n'), Any, ( AsVar, 'rChild', ((Variant, Tree.Node), (Var, 'rn'), Any, Any), )) assertEqual(res['n'], 1) assertEqual(res['rChild'], rNode) assertEqual(res['rn'], 2)
def moveRedRight(dict): _cv = dict res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'clr'), (Var, 'k'), (Var, 'v'), (Nested, [ (Variant, RBNode_elm_builtin), (Var, 'lClr'), (Var, 'lK'), (Var, 'lV'), (Nested, [ (Variant, RBNode_elm_builtin), (Val, Red), (Var, 'llK'), (Var, 'llV'), (Var, 'llLeft'), (Var, 'llRight')]), (Var, 'lRight')]), (Nested, [ (Variant, RBNode_elm_builtin), (Var, 'rClr'), (Var, 'rK'), (Var, 'rV'), (Var, 'rLeft'), (Var, 'rRight')])) if res is not None: clr = res['clr'] k = res['k'] v = res['v'] lClr = res['lClr'] lK = res['lK'] lV = res['lV'] llK = res['llK'] llV = res['llV'] llLeft = res['llLeft'] llRight = res['llRight'] lRight = res['lRight'] rClr = res['rClr'] rK = res['rK'] rV = res['rV'] rLeft = res['rLeft'] rRight = res['rRight'] return RBNode_elm_builtin(Red, lK, lV, (RBNode_elm_builtin(Black, llK, llV, llLeft, llRight)), (RBNode_elm_builtin(Black, k, v, lRight, (RBNode_elm_builtin(Red, rK, rV, rLeft, rRight))))) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'clr'), (Var, 'k'), (Var, 'v'), (Nested, [ (Variant, RBNode_elm_builtin), (Var, 'lClr'), (Var, 'lK'), (Var, 'lV'), (Var, 'lLeft'), (Var, 'lRight')]), (Nested, [ (Variant, RBNode_elm_builtin), (Var, 'rClr'), (Var, 'rK'), (Var, 'rV'), (Var, 'rLeft'), (Var, 'rRight')])) if res is not None: clr = res['clr'] k = res['k'] v = res['v'] lClr = res['lClr'] lK = res['lK'] lV = res['lV'] lLeft = res['lLeft'] lRight = res['lRight'] rClr = res['rClr'] rK = res['rK'] rV = res['rV'] rLeft = res['rLeft'] rRight = res['rRight'] _cv = clr res = patternMatch(_cv, (Variant, Black)) if res is not None: return RBNode_elm_builtin(Black, k, v, (RBNode_elm_builtin(Red, lK, lV, lLeft, lRight)), (RBNode_elm_builtin(Red, rK, rV, rLeft, rRight))) res = patternMatch(_cv, (Variant, Red)) if res is not None: return RBNode_elm_builtin(Black, k, v, (RBNode_elm_builtin(Red, lK, lV, lLeft, lRight)), (RBNode_elm_builtin(Red, rK, rV, rLeft, rRight))) return dict
def removeMin(dict): _cv = dict res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'color'), (Var, 'key'), (Var, 'value'), (AsVar, 'left', ( (Variant, RBNode_elm_builtin), (Var, 'lColor'), Any, Any, (Var, 'lLeft'), Any)), (Var, 'right')) if res is not None: color = res['color'] key = res['key'] value = res['value'] left = res['left'] lColor = res['lColor'] lLeft = res['lLeft'] right = res['right'] _cv = lColor res = patternMatch(_cv, (Variant, Black)) if res is not None: _cv = lLeft res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Val, Red), Any, Any, Any, Any) if res is not None: return RBNode_elm_builtin(color, key, value, (removeMin(left)), right) _cv = moveRedLeft(dict) res = patternMatch(_cv, (Variant, RBNode_elm_builtin), (Var, 'nColor'), (Var, 'nKey'), (Var, 'nValue'), (Var, 'nLeft'), (Var, 'nRight')) if res is not None: nColor = res['nColor'] nKey = res['nKey'] nValue = res['nValue'] nLeft = res['nLeft'] nRight = res['nRight'] return balance(nColor, nKey, nValue, (removeMin(nLeft)), nRight) res = patternMatch(_cv, (Variant, RBEmpty_elm_builtin)) if res is not None: return RBEmpty_elm_builtin return RBNode_elm_builtin(color, key, value, (removeMin(left)), right) return RBEmpty_elm_builtin