The Quipper System

QuipperLib.ClassicalOptim.AlgExp

Description

This module contains an efficient representation of algebraic boolean formulas.

Synopsis

# Auxiliary functions

mapOfSet :: Ord a => Set a -> Map a Int Source #

Build the characteristic function of a set.

setOfMap :: Ord a => Map a Int -> Set a Source #

Get the set of elements whose images are odd.

split_even :: [a] -> ([a], [a]) Source #

Split a list in the middle.

# Expressions

type Exp = Set IntSet Source #

The type of algebraic boolean expressions.

We represent boolean expressions using "and" and "xor" as the primitive connectives. Equivalently, we can regard booleans as the elements of the two-element field F2, with operations "*" (times) and "+" (plus).

An algebraic expression x1*x2*x3 + y1*y2*y3 + z1*z2 is encoded as {{x1,x2,x3},{y1,y2,y3},{z1,z2}}.

In particular, {} == False == 0 and {{}} == True == 1.

listOfExp :: Exp -> [[Int]] Source #

Turn an Exp into a list of lists.

expOfList :: [[Int]] -> Exp Source #

Turn a list of lists into an Exp.

exp_and :: Exp -> Exp -> Exp Source #

The conjunction of two expression.

exp_xor :: Exp -> Exp -> Exp Source #

The xor of two expressions.

The expression "False".

The expression "True".

The negation of an expression.

The expression xn.

# Properties of expressions

The important property of expressions is that two formulas have the same truth table iff they are syntactically equal. This makes the equality test of wires theoretically straightforward.

The following automated tests check this property, using the Test.QuickCheck library.

## Truth tables

A valuation on a set of variables is a map from variables to booleans. This can be thought of as a row in a truth table. A truth table is a map from valuations to booleans, but we just represent this as a list of booleans, listed in lexicographically increasing order of valuations.

vars_of_exp :: Exp -> [Int] Source #

Get the variables used in an expression.

Evaluate the expression with respect to the given valuation. A valuation is a map from variables to booleans, i.e., a row in a truth table.

valuations_of_vars :: [Int] -> [Map Int Bool] Source #

Construct the list of all 2n valuations for a given list of n variables.

truth_table_of_exp :: [Int] -> Exp -> [Bool] Source #

Build the truth table for the given expression, on the given list of variables. The truth table is returned as a list of booleans in lexicographic order of valuations. For example, if

 1 2 | exp
F F | f1
F T | f2
T F | f3
T T | f4

then the output of the function is [f1,f2,f3,f4].

exp_of_truth_table :: Int -> [Bool] -> Exp Source #

Return an expression realizing the given truth table. Uses variables starting with the given number.

## Quick-checking

twoExp :: Integral a => a -> Int Source #

Compute 2n.

genBoolList :: Integral a => a -> Gen [Bool] Source #

Generate a list of Bool.

test_args :: Args Source #

Arguments for QuickCheck.

test_truth1 :: Int -> IO () Source #

First test: truth table to expression to truth table is the identity.

genIntList :: [Int] -> Int -> Gen [Int] Source #

Generate a random list of Ints.

genExp :: [Int] -> Gen Exp Source #

Generate a random expression out of the given variables.

test_truth2 :: Int -> IO () Source #

Second test: expression to truth table to expression is the identity.

# Orphan instances

 # MethodsshowsPrec :: Int -> Exp -> ShowS #show :: Exp -> String #showList :: [Exp] -> ShowS #