Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  problem with type (Brandon Allbery)
   2. Re:  Simplify (normalize) symbolic polynom-like   expression
      (Daniel Hlynskyi)
   3. Re:  problem with type (Morel Pisum)
   4.  lambda notation vs function-argument notation    in GHCi
      (Morel Pisum)
   5. Re:  Simplify (normalize) symbolic polynom-like   expression
      (Chadda? Fouch?)
   6. Re:  Simplify (normalize) symbolic polynom-like   expression
      (Chadda? Fouch?)
   7. Re:  Simplify (normalize) symbolic polynom-like   expression
      (Daniel Hlynskyi)


----------------------------------------------------------------------

Message: 1
Date: Sat, 16 Jun 2012 20:46:07 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] problem with type
To: miro <[email protected]>
Cc: [email protected]
Message-ID:
        <cakfcl4xjygslxcun3zypaznuq_w3vrnsotmbboracjwqftq...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sat, Jun 16, 2012 at 8:39 PM, miro <[email protected]> wrote:

>  Am a bit stuck here,... please, what is wrong with this?
>
> checkNode :: String -> [String] -> Bool
> checkNode s nodes =
>    [s == node | node <- nodes ]
>

This is going to return a *list* of Bools, one per String in the original
list.

-- 
brandon s allbery                                      [email protected]
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120616/17293864/attachment-0001.htm>

------------------------------

Message: 2
Date: Sun, 17 Jun 2012 08:32:34 +0300
From: Daniel Hlynskyi <[email protected]>
Subject: Re: [Haskell-beginners] Simplify (normalize) symbolic
        polynom-like    expression
To: M?t? Kov?cs <[email protected]>
Cc: [email protected]
Message-ID:
        <canzg+ydukoyszfhezcst7fr12patnuruaki9hm6wjgk6qzg...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Not for equality testing, only for prettyprinting. So I want some
simple way to apply all the rules with smallest number of expression
tree traverses

As for ipoly, it looks like CAS. I will use Maxima as CAS and I don't
want to do extra Maxima call for something, that can be easily
(expecting) done in Haskell.

2012/6/17 M?t? Kov?cs <[email protected]>:
> Hi Daniel,
>
> It depends on what you want to use the normalized / canonical form for.
>
> If it's to reduce semantic equivalence testing to simple syntactic
> equality, e.g.
> (A == B) = (canonize(A) == canonize(B)),
> then you could just use the fully expanded form, which isn't really
> simplification. :)
>
> I'm doing something similar here (for polynomial expressions over an
> inner product space):
> https://github.com/mkovacs/ipoly/blob/master/Poly.hs
>
> Cheers,
> M?t?
>
> On Sat, Jun 16, 2012 at 2:17 PM, Daniel Hlynskyi <[email protected]> 
> wrote:
>> Hello.
>>
>> I am new to typefull programming, so I've got a question.
>> I have a simple mathematical expression (addition, product and
>> exponentiation only):
>>
>>> data Expr =?I Int -- integer constant
>>> ? ? ? ? ? | V -- symbolic variable
>>> ? ? ? ? ? | Sum [Expr]
>>> ? ? ? ? ? | Prod [Expr]
>>> ? ? ? ? ? | Pow Expr Expr
>>
>> What I want is normalize\simplify this expression. Eg `Prod [Pow V (I
>> 0), Pow V (I 1)] ` must be simplified to just `V`. What techniques
>> should I use to write `normalize` function?
>> Simplification rules are quite simple:
>>
>>> normalize (Sum [a]) = normalize a
>>> normalize (Sum xs) | (I 0) `elem` xs = map nomalize . Sum $ filter (/= I 0) 
>>> xs
>>> ? ? ? ? ? ? ? ? ? ? ? ? ?| otherwise = map normalize xs
>>> normalize (Prod xs) | (I 0) `elem` xs = I 0
>>> normalize (Prod xs) | (I 1) `elem` xs = map nomalize . Prod $ filter (/= I 
>>> 1) xs
>>> ? ? ? ? ? ? ? ? ? ? ? ? ?| otherwise = map normalize xs
>>> normalize (Pow a (I 0)) = I 1
>>> normalize (Pow a (I 1)) = normalize a
>>
>> and so on. But rules like theese cannot simplify some expressions, for
>> example, `Prod [Pow V (I 0), Pow V (I 1)] `.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners



------------------------------

Message: 3
Date: Sun, 17 Jun 2012 09:08:06 +0200
From: Morel Pisum <[email protected]>
Subject: Re: [Haskell-beginners] problem with type
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

What you want is probably

checkNode :: String -> [String] -> Bool
checkNode s nodes = and (map (==s) nodes)

which returns True iff all nodes equal s.


> Am a bit stuck here,... please, what is wrong with this?
>
> checkNode :: String -> [String] -> Bool
> checkNode s nodes =
>    [s == node | node <- nodes ]
>
>
> src/me.hs:3:4:
>     Couldn't match expected type `Bool' with actual type `[t0]'
> [1 of 1] Compiling Main             ( src/me.hs, interpreted )
>     In the expression: [s == node | node <- nodes]
> Failed, modules loaded: none.
>     In an equation for `checkNode':
>         checkNode s nodes = [s == node | node <- nodes]
>
> thanks,
> Miro




------------------------------

Message: 4
Date: Sun, 17 Jun 2012 09:13:28 +0200
From: Morel Pisum <[email protected]>
Subject: [Haskell-beginners] lambda notation vs function-argument
        notation        in GHCi
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Let's define a function in GHCi:

> Prelude> let f s n = and (map (==s) n)
> Prelude> :t f
> f :: Eq a => a -> [a] -> Bool

This is fine. But when I define this function using lambda notation, I
get this

> Prelude> let f = \s n -> and (map (==s) n)
> Prelude> :t f
> f :: () -> [()] -> Bool

which is really weird.

Why does this happen?



------------------------------

Message: 5
Date: Sun, 17 Jun 2012 09:36:01 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Simplify (normalize) symbolic
        polynom-like    expression
To: Daniel Hlynskyi <[email protected]>
Cc: [email protected]
Message-ID:
        <canfjzrbexwnxnns7pjofpyeclprj2m5na+jbtb4adzouncg...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Jun 16, 2012 at 11:17 PM, Daniel Hlynskyi
<[email protected]> wrote:
> Hello.
> Simplification rules are quite simple:
>
>> normalize (Sum [a]) = normalize a
>> normalize (Sum xs) | (I 0) `elem` xs = map nomalize . Sum $ filter (/= I 0) 
>> xs
>> ? ? ? ? ? ? ? ? ? ? ? ? ?| otherwise = map normalize xs
>> normalize (Prod xs) | (I 0) `elem` xs = I 0
>> normalize (Prod xs) | (I 1) `elem` xs = map nomalize . Prod $ filter (/= I 
>> 1) xs
>> ? ? ? ? ? ? ? ? ? ? ? ? ?| otherwise = map normalize xs
>> normalize (Pow a (I 0)) = I 1
>> normalize (Pow a (I 1)) = normalize a
>
> and so on. But rules like theese cannot simplify some expressions, for
> example, `Prod [Pow V (I 0), Pow V (I 1)] `.

It's because you're doing it in the wrong direction : in a tree always
normalize leaves before you try to normalize branches.

normalize (Sum xs) = case sort . filter (/= I 0) . map normalize $ xs of
  [] -> I 0
  [a] -> a
  ys -> sumPrefix ys
    where
      sumPrefix (I n : I m : ys) = sumPrefix $ I (n+m) : ys
      sumPrefix ys = ys

See how I normalize the elements of xs before anything else.

Note that this isn't quite the right way to represent a poly if you
want to get to a canonical representation.

-- 
Jeda?



------------------------------

Message: 6
Date: Sun, 17 Jun 2012 09:37:54 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Simplify (normalize) symbolic
        polynom-like    expression
To: Daniel Hlynskyi <[email protected]>
Cc: [email protected]
Message-ID:
        <CANfjZRbWJH5h0=omvymeu_lv7qvzk2zd85szvtc6znsnh-4...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sun, Jun 17, 2012 at 9:36 AM, Chadda? Fouch?
<[email protected]> wrote:
> normalize (Sum xs) = case sort . filter (/= I 0) . map normalize $ xs of
> ?[] -> I 0
> ?[a] -> a
> ?ys -> sumPrefix ys
> ? ?where
> ? ? ?sumPrefix (I n : I m : ys) = sumPrefix $ I (n+m) : ys
> ? ? ?sumPrefix ys = ys

Sorry I did this a bit too fast...

> normalize (Sum xs) = case sumPrefix . sort . filter (/= I 0) . map normalize 
> $ xs of
>  [] -> I 0
>  [a] -> a
>  ys -> Sum ys



------------------------------

Message: 7
Date: Sun, 17 Jun 2012 11:30:37 +0300
From: Daniel Hlynskyi <[email protected]>
Subject: Re: [Haskell-beginners] Simplify (normalize) symbolic
        polynom-like    expression
To: Chadda? Fouch? <[email protected]>
Cc: [email protected]
Message-ID:
        <canzg+yfaw07jvjt1s5akof+ucxnmy-hwowuuldnd5tr1-wp...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Thanks, that worked!

> Note that this isn't quite the right way to represent a poly if you
want to get to a canonical representation.

Can you expand a bit more on this? It is not classic poly, but
combination of symbolic vars,sums,products and exponentiation
operations (in fact, auto-random-generated)

2012/6/17 Chadda? Fouch? <[email protected]>:
> On Sun, Jun 17, 2012 at 9:36 AM, Chadda? Fouch?
> <[email protected]> wrote:
>> normalize (Sum xs) = case sort . filter (/= I 0) . map normalize $ xs of
>> ?[] -> I 0
>> ?[a] -> a
>> ?ys -> sumPrefix ys
>> ? ?where
>> ? ? ?sumPrefix (I n : I m : ys) = sumPrefix $ I (n+m) : ys
>> ? ? ?sumPrefix ys = ys
>
> Sorry I did this a bit too fast...
>
>> normalize (Sum xs) = case sumPrefix . sort . filter (/= I 0) . map normalize 
>> $ xs of
>> ?[] -> I 0
>> ?[a] -> a
>> ?ys -> Sum ys



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 48, Issue 22
*****************************************

Reply via email to