def map_args(tree, args):
    """
    Given a tree and a list of arguments, produce the tree with the arguments
    instead of integers at the leaves of the tree.
    
    E.g. for tree = [[1, 2], [3, 4]] and args = [a, b, c, d] we get
    [[a, b], [c, d]].
    """
    (s, t, a) = tree
    if a[0] == 1:
        return args[0]
    return [map_args((1, t[0], a[1]), args[:a[1][0]]),
            map_args((1, t[1], a[2]), args[a[1][0]:])]