Context-free grammar and Reversal - reverse

I'm designing a context-free grammar to generate this language:
{ w in {a,b}* | w is of the form uvu^R, where u and v are any strings in {a,b}* }
I would define the first two strings as:
U -> aU | bU | _
V -> aV | bV | _
And then combine those:
S -> UV
But how do I express the reversal as context-free grammar?

You need to make use of the context-free-ness of the grammar (what you're presenting so far is just a regular grammar):
U-> aUa | bUb | a | b | _
Will match things like "ababa" and "aabaa", but not "aabba".
I'll leave it to you to alter this to your needs - but keep in mind that your specified language has the possibility of u being the empty string, hence it generates all strings in {a,b}*.

Related

CUDD: Manipulation of BDDs

I'm working with CUDD C++ interface (https://github.com/ivmai/cudd) but there is almost no information about this library. I would like to know how to remove one variable according to its value.
For example, I have now the next table stored in a bdd:
|-----|-----|-----|
| x1 | x2 | y |
|-----|-----|-----|
| 0 | 0 | 1 |
|-----|-----|-----|
| 0 | 1 | 1 |
|-----|-----|-----|
| 1 | 0 | 1 |
|-----|-----|-----|
| 1 | 1 | 0 |
|-----|-----|-----|
And I want to split the previous table in two separate bdds according to the value of x2 and remove that node afterwards:
If x2 = 0:
|-----|-----|
| x1 | y |
|-----|-----|
| 0 | 1 |
|-----|-----|
| 1 | 1 |
|-----|-----|
If x2 = 1:
|-----|-----|
| x1 | y |
|-----|-----|
| 0 | 1 |
|-----|-----|
| 1 | 0 |
|-----|-----|
Is it possible?
The reason that there is almost no documentation on the C++ interface of the CUDD library is that it is just a wrapper for the C functions, for which there is plenty of documentation.
The C++ wrapper is mainly useful for getting rid of all the Cudd_Ref(...) and Cudd_RecursiveDeref(...) calls that code using the C interface would need to do. Note that you can use the C interface from C++ code as well, if you want.
To do what you want to do, you have to combine the Boolean operators offered by CUDD in a way that you obtain a new Boolean function with the desired properties.
The first step is to restrict s to the x=0 and x=1 case:
BDD s0 = s & !x;
BDD s1 = s & x;
As you noticed, the new BDDs are not (yet) oblivious to the value of the x variable. You want them to be "don't care" w.r.t to the value of x. Since you already know that x is restricted to one particular value in s0 and s1, you can use the existential abstraction operator:
s0 = s0.ExistAbstract(x);
s1 = s1.ExistAbstract(x);
Note that x is used here as a so-called cube (see below).
These are now the BDDs that you want.
Cube explanation: If you abstract from multiple variables at the same time, you should compute such a cube from all the variables to be abstracted from first. A cube is mainly used for representing a set of variables. From mathematical logic, it is known that if you existentially or universally abstract away multiple variables, then the order to abstracting away these variables does not matter. Since the recursive BDD operations in CUDD are implemented over pairs (or triples) of BDDs whenever possible, CUDD internally represents a set of variables as a cube as well, so that an existential abstraction operation can just work on (1) the BDD for which existential abstraction is to be performed, and (2) the BDD representing the set of variables to be abstracted from. The internal representation of a cube as a BDD should not be of relevance to a developer just using CUDD (rather than extending CUDD), except that a BDDD representing a variable can also be used as a cube.
An approach using the Cython bindings to CUDD of the Python package dd is the following, which uses substitution of constant values for variable x2.
import dd.cudd as _bdd
bdd = _bdd.BDD()
bdd.declare('x1', 'x2')
# negated conjunction of the variables x1 and x2
u = bdd.add_expr(r'~ (x1 /\ x2)')
let = dict(x2=False)
v = bdd.let(let, u)
assert v == bdd.true, v
let = dict(x2=True)
w = bdd.let(let, u)
w_ = bdd.add_expr('~ x1')
assert w == w_, (w, w_)
The same code runs in pure Python by changing the import statement to import dd.autoref as _bdd. The pure Python version of dd can be installed with pip install dd. Installation of dd with the module dd.cudd is described here.

MySQL LIKE pattern matching returning empty set when it should be returning values

I have a table structured like below (real data replaced with dummy data):
tableName
+-----------+----------------+
| code | description |
+-----------+----------------+
| A | text here |
| b | blah blah |
| c | sdfsdfsdfsdf |
| d | sdfsfkaljdaklj |
| e | asdkjasdlkjasd |
| f | adskljalkdj |
| g | asdjalkd |
| h | askdjavsd |
+-----------+----------------+
I am trying to do the following command:
SELECT * FROM tableName WHERE description LIKE '__[aeiou]%';
I expect this select statement to return all results from the table where the third character in the description is a, e, i, o, or u.
However, I am getting 0 results for the about query. I know for a fact that valid results which match this pattern (3rd character is a, e, i, o or u) exists in the table.
What is wrong with my query?
Use a regular expression instead of a SQL Server style LIKE pattern:
SELECT *
FROM tableName
WHERE description REGEXP '^..[aeiou].*$';
EDIT:
For those who don't read documentation thoroughly, the documentation says:
The other type of pattern matching provided by MySQL uses extended
regular expressions. When you test for a match for this type of
pattern, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT
RLIKE, which are synonyms).
The following list describes some characteristics of extended regular
expressions:
The list of features, such as [ and ] is in this list. The standard for LIKE has only two wildcards, % and _.
In MySQL the '_' character is a not part of the extended regular expression class and can not be used as such. use RLIKE to use regular expressions

Haskell - Types, Enums, and Functions

Good morning everyone,
Here's what I'm working on today, and the issue I'm running in to:
--A
data Row = A | B | C | D | E | F | G | H | I | J deriving (Enum, Ord, Show, Bounded, Eq, Read)
data Column = One | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten deriving (Enum, Ord, Show, Bounded, Eq, Read)
--B
data Address = Address Row Column deriving (Show, Read, Eq)
Then a few lines later I get to the problem child:
toAddress r c = Address(toEnum r, toEnum c)
I need to feed Address a Row and Column, but I need to turn r and c into Row and Column (not Ints)
Obviously toAddress is not structured correctly to carry out this task. The requirement is as follows:
Write a function toAddress that takes in a row and column, each in [0
− 9]. Construct an Address and return it. Use toEnum to index into
your Row and Column enums lists.
Does anyone have any suggestions on how to accomplish what I'm going for here?
Thank you!
You got the syntax wrong.
A function application of a function f :: A -> B -> C in haskell looks like this f a b and not f(a,b). f(a,b) still is correct syntax but not what you want: it passes only one parameter to the function (i.e. the tuple consisting of a and b).
So the correct implementation of toAddress looks like this:
toAddress r c = Address (toEnum r) (toEnum c)

How to get first alphanumeric character after the first occurance of a set of possible symbols using MYSQL REGEX

I'm having diffficulty with a regular expression.
I have a MYSQL table with ABC notated tunes in it that look a bit like this:
X: 1
T: Spórt
M: 6/8
L: 1/8
R: jig
K: Dmaj
|:AdF ~A3 | GBE ~G3 | AdF ~A3 |
GBE cde | AdF ~A3 | GBE ~G3 |
cdc A2G | EAA D3 :|
I want to make a search function in MYSQL that will list tunes in order by their starting note. In this case I need to return A
Most tunes begin with either : a bar-line |, a repeat bar-line |:or no bar-line (which means I have to match the first character on the first line that has bar-lines in it)
Any suggestions of what regex would do this? I find regexpressions extremely confusing!
Try: \|:?([A-Ga-g])
\| matches |
:? matches : if there is one
([A-Ga-g]) Gets a note.

Haskell operator vs function precedence

I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code
list = map foo $ xs
can be rewritten as
list = (map foo) $ (xs)
and will eventually be
list = map foo xs
My question used to be, why the first formulation would not be rewritten as
list = (map foo $) xs
since function precedence is always higher than operator precedence, but I think that I have found the answer: operators are simply not allowed to be arguments of functions (except of course, if you surround them with parentheses). Is this right? If so, I find it odd, that there is no mention of this mechanic/rule in RWH or Learn you a Haskell, or any of the other places that I have searched. So if you know a place, where the rule is stated, please link to it.
-- edit: Thank you for your quick answers. I think my confusion came from thinking that an operator literal would somehow evaluate to something, that could get consumed by a function as an argument. It helped me to remember, that an infix operator can be mechanically translated to a prefix functions. Doing this to the first formulation yields
($) (map foo) (xs)
where there is no doubt that ($) is the consuming function, and since the two formulations are equivalent, then the $ literal in the first formulation cannot be consumed by map.
Firstly, application (whitespace) is the highest precedence "operator".
Secondly, in Haskell, there's really no distinction between operators and functions, other than that operators are infix by default, while functions aren't. You can convert functions to infix with backticks
2 `f` x
and convert operators to prefix with parens:
(+) 2 3
So, your question is a bit confused.
Now, specific functions and operators will have declared precedence, which you can find in GHCi with ":info":
Prelude> :info ($)
($) :: (a -> b) -> a -> b -- Defined in GHC.Base
infixr 0 $
Prelude> :info (+)
class (Eq a, Show a) => Num a where
(+) :: a -> a -> a
infixl 6 +
Showing both precedence and associativity.
You are correct. This rule is part of the Haskell syntax defined by the Haskell Report. In particular note in Section 3, Expressions, that the argument to function application (an fexp) must be an aexp. An aexp allows operators as part of sections, and also within a parenthesized expression, but not bare operators.
In map foo $ xs, the Haskell syntax means that this is parsed as two expressions which are applied to the binary operator $. As sepp2k notes, the syntax (map foo $) is a left section and has a different meaning.
I have to confess I've never thought much about this and actually had to look it up in the Report to see why operators have the behavior they do.
In addition to the information provided by other answers already, note that different operators can have different precedences over other operators, as well as being left-/right- or non-associative.
You can find these properties for the Prelude operators in the Haskell 98 Report fixity section.
+--------+----------------------+-----------------------+-------------------+
| Prec- | Left associative | Non-associative | Right associative |
| edence | operators | operators | operators |
+--------+----------------------+-----------------------+-------------------+
| 9 | !! | | . |
| 8 | | | ^, ^^, ** |
| 7 | *, /, `div`, | | |
| | `mod`, `rem`, `quot` | | |
| 6 | +, - | | |
| 5 | | | :, ++ |
| 4 | | ==, /=, <, <=, >, >=, | |
| | | `elem`, `notElem` | |
| 3 | | | && |
| 2 | | | || |
| 1 | >>, >>= | | |
| 0 | | | $, $!, `seq` |
+--------+----------------------+-----------------------+-------------------+
Any operator lacking a fixity declaration is assumed to be left associative with precedence 9.
Remember, function application has highest precedence (think of precedence 10 compared to the other precedences in the table) [1].
The difference is that infix operators get placed between their arguments, so this
list = map foo $ xs
can be rewritten in prefix form as
list = ($) (map foo) xs
which, by the definition of the $ operator, is simply
list = (map foo) xs
Operators can be passed as function arguments if you surround them with parenthesis (i.e. map foo ($) xs, which would indeed be passed as (map foo ($)) xs). However if you do not surround them with parenthesis, you are correct that they cannot be passed as argument (or assigned to variables).
Also note that the syntax (someValue $) (where $ could be any operator) actually means something different: it is equivalent to \x -> someValue $ x, i.e. it partially applies the operator to its left operand (which in case of $ is a noop of course). Likewise ($ x) partially applies the operator to the right operand. So map ($ x) [f, g, h] would evaluate to [f x, g x, h x].