Ejemplo n.º 1
0
def dumps_cover(
        cover, f, care, fol,
        latex=False,
        show_dom=False,
        show_limits=False,
        comment=True):
    """Return disjunction of orthotopes in `cover`, one per line.

    @param latex: use `pf.sty` commands
    @param show_dom: if `care` implies type hints,
        then conjoin type hints (`fol.vars[var]['dom']`)
    @param show_limits: conjoin limits of  bitfield values
    @param comment: if `True`, then list support of `f`, `cover`

    @rtype: `str`
    """
    prm = lat.setup_aux_vars(f, care, fol)
    c = list()
    if show_limits:
        r = tyh._list_limits(prm.x_vars, fol.vars)
        c.extend(r)
    show_dom = show_dom and _care_implies_type_hints(f, care, fol)
    if show_dom:
        r = tyh._list_type_hints(prm.x_vars, fol.vars)
        c.extend(r)
    else:
        log.info(
            'type hints omitted')
    r = lat.list_expr(
        cover, prm, fol, use_dom=show_dom, latex=latex)
    s = stx.vertical_op(r, op='or', latex=latex, spacing=2)
    c.append(s)
    if care != fol.true:
        c.append('care expression')
    s = stx.vertical_op(c, op='and', latex=latex)
    f_vars = fol.support(f)
    care_vars = fol.support(care)
    s_comment = (
        '(* `f` depends on:  {f_vars} *)\n'
        '(* `care` depends on:  {care_vars} *)\n'
        '(* The minimal cover is: *)').format(
            f_vars=_comma_sorted(f_vars),
            care_vars=_comma_sorted(care_vars))
    if comment:
        s = '{comment}\n{s}'.format(comment=s_comment, s=s)
    # could add option to find minimal cover for care too
    # postcondition
    r = lat.list_expr(
        cover, prm, fol, use_dom=show_dom)
    r = stx.disj(r)
    g = fol.add_expr(r)
    # ensure that `g` equals `f` inside `care`
    # `g` can be arbitrary outside of `care`
    assert (g & care) == (f & care), r
    return s
Ejemplo n.º 2
0
def dumps_cover(
        cover, f, care, fol,
        latex=False,
        show_dom=False,
        show_limits=False):
    """Return disjunction of orthotopes in `cover`, one per line.

    @param latex: use `pf.sty` commands
    @param show_dom: if `care` implies type hints,
        then conjoin type hints (`fol.vars[var]['dom']`)
    @param show_limits: conjoin limits of  bitfield values

    @rtype: `str`
    """
    prm = lat.setup_aux_vars(f, care, fol)
    c = list()
    if show_limits:
        r = tyh._list_limits(prm.x_vars, fol.vars)
        c.extend(r)
    show_dom = show_dom and _care_implies_type_hints(f, care, fol)
    if show_dom:
        r = tyh._list_type_hints(prm.x_vars, fol.vars)
        c.extend(r)
    else:
        log.info(
            'type hints omitted (care does not imply them)')
    r = lat.list_expr(
        cover, prm, fol, use_dom=show_dom, latex=latex)
    s = stx.vertical_op(r, op='or', latex=latex)
    c.append(s)
    n_expr = len(r)
    if care != fol.true:
        c.append('care expression')
    s = stx.vertical_op(c, op='and', latex=latex)
    f_vars = fol.support(f)
    care_vars = fol.support(care)
    s = (
        '(* `f` depends on:  {f_vars} *)\n'
        '(* `care` depends on:  {care_vars} *)\n'
        '(* The minimal cover is: *)\n{s}').format(
            f_vars=_comma_sorted(f_vars),
            care_vars=_comma_sorted(care_vars),
            s=s)
    # could add option to find minimal cover for care too
    # postcondition
    r = lat.list_expr(
        cover, prm, fol, use_dom=show_dom)
    r = stx.disj(r)
    g = fol.add_expr(r)
    # ensure that `g` equals `f` inside `care`
    # `g` can be arbitrary outside of `care`
    assert (g & care) == (f & care), r
    return s
Ejemplo n.º 3
0
def test_vertical_op():
    c = ['a', 'b', 'c']
    s = stx.vertical_op(c)
    s_ = ' /\ a\n /\ b\n /\ c'
    assert s == s_, (s, s_)
    c = ['/\ a\n/\ b\n/\ c', 'd', 'e']
    s = stx.vertical_op(c)
    s_ = ' /\ /\ a\n    /\ b\n    /\ c\n /\ d\n /\ e'
    assert s == s_, (s, s_)
    c = ['/\ a\n/\ b\n/\ c', 'd', 'e']
    s = stx.vertical_op(c, op='or')
    s_ = ' \/ /\ a\n    /\ b\n    /\ c\n \/ d\n \/ e'
    assert s == s_, (s, s_)
    c = ['e']
    s = stx.vertical_op(c)
    s_ = c[0]
    assert s == s_, (s, s_)
    c = ['/\ a\n/\ b\n/\ c']
    s = stx.vertical_op(c)
    s_ = c[0]
    assert s == s_, (s, s_)
    c = ['a', 'b', 'c']
    s = stx.vertical_op(c, op='and', latex=True)
    c = [s, 'd', 'e']
    s = stx.vertical_op(c, op='or', latex=True)
    s_ = r'''\begin{disj}
    \begin{conj}
        a \\
        b \\
        c
    \end{conj} \\
    d \\
    e
\end{disj}'''
    assert s == s_, (s, s_)
Ejemplo n.º 4
0
def list_expr(
        cover, prm, fol,
        simple=False,
        use_dom=False, latex=False):
    """Return `list` of `str`, each an orthotope in `cover`.

    @param simple: if `True`, then return expression
        that can be parsed by `fol.add_expr`.
    @param use_dom: omit conjuncts that contain dom of var
        assumes that `|= care => type_hints`
    """
    px = prm._px
    xp = _map_parameters_to_vars(px)
    support = fol.support(cover)
    keys = {xp[k] for k in support}
    keys = natsort.natsorted(keys)
    c = _orthotopes_iter(cover, fol)
    r = list()
    for product in c:
        w = list()
        for x in keys:
            a = px[x]['a']
            b = px[x]['b']
            a = product[a]
            b = product[b]
            tyh._check_type_hint(a, b, fol.vars[x], x)
            # can `x` be ignored ?
            if use_dom:
                dom = fol.vars[x]['dom']
                a, b = tyh._clip_subrange((a, b), dom, x)
            if a is None and b is None:
                continue
            if a == b:
                s = '({x} = {a})'
            elif simple:
                s = '({a} <= {x}) /\ ({x} <= {b})'
            else:
                # precise even in absence of limits/dom
                s = '({x} \in {a} .. {b})'
            s = s.format(x=x, a=a, b=b)
            w.append(s)
        # conjoin as one triplet per line
        lines = w
        n_tail = len(lines) % 3
        tail = lines[-n_tail:]
        lines = lines[:-n_tail]
        i = iter(lines)
        triplets = list(zip(i, i, i))
        lines = [' /\ '.join(t) for t in triplets]
        lines.append(' /\ '.join(tail))
        s = stx.vertical_op(lines, latex=latex, op='and')
        r.append(s)
    r = natsort.natsorted(r)  # reproducible vertical listing
    return r
Ejemplo n.º 5
0
def list_expr(cover, prm, fol, simple=False, use_dom=False, latex=False):
    """Return `list` of `str`, each an orthotope in `cover`.

    @param simple: if `True`, then return expression
        that can be parsed by `fol.add_expr`.
    @param use_dom: omit conjuncts that contain dom of var
        assumes that `|= care => type_hints`
    """
    px = prm._px
    xp = _map_parameters_to_vars(px)
    support = fol.support(cover)
    keys = {xp[k] for k in support}
    keys = natsort.natsorted(keys)
    c = _orthotopes_iter(cover, fol)
    r = list()
    for product in c:
        w = list()
        for x in keys:
            a = px[x]['a']
            b = px[x]['b']
            a = product[a]
            b = product[b]
            tyh._check_type_hint(a, b, fol.vars[x], x)
            # can `x` be ignored ?
            if use_dom:
                dom = fol.vars[x]['dom']
                a, b = tyh._clip_subrange((a, b), dom, x)
            if a is None and b is None:
                continue
            if a == b:
                s = '({x} = {a})'
            elif simple:
                s = '({a} <= {x}) /\ ({x} <= {b})'
            else:
                # precise even in absence of limits/dom
                s = '({x} \in {a} .. {b})'
            s = s.format(x=x, a=a, b=b)
            w.append(s)
        # conjoin as one triplet per line
        lines = w
        n_tail = len(lines) % 3
        tail = lines[-n_tail:]
        lines = lines[:-n_tail]
        i = iter(lines)
        triplets = list(zip(i, i, i))
        lines = [' /\ '.join(t) for t in triplets]
        lines.append(' /\ '.join(tail))
        s = stx.vertical_op(lines, latex=latex, op='and')
        r.append(s)
    r = natsort.natsorted(r)  # reproducible vertical listing
    return r