Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  Bool newtype (Michael Alan Dorman)
   2.  Generalised symbolic expressions (Dmitrij Szamozvancev)
   3. Re:  Bool newtype (Michael Alan Dorman)
   4. Re:  Bool newtype (Imants Cekusins)
   5. Re:  Generalised symbolic expressions (Imants Cekusins)
   6. Re:  Bool newtype (Marcin Mrotek)
   7. Re:  Bool newtype (Imants Cekusins)
   8. Re:  Generalised symbolic expressions (Imants Cekusins)


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

Message: 1
Date: Sun, 07 Aug 2016 08:35:49 -0400
From: Michael Alan Dorman <mdor...@jaunder.io>
To: Imants Cekusins <ima...@gmail.com>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Bool newtype
Message-ID: <87mvkog3iy....@jaunder.io>
Content-Type: text/plain

Imants Cekusins <ima...@gmail.com> writes:

> for a Bool-like newtype:
>
> newtype B = B Bool
>
> , is there an easy way to use this newtype B in place of Bool?
>
> e.g.
>
> let b1 = B True
> in if b1 then 1 else 0
>
> ?

Imants,

You can create a function that converts a B to a Bool:

  toBool :: B -> Bool
  toBool B b = b

And then use the result of applying that function to your variable:

  let b1 = B True
  in if toBool b1
     then 1
     else 0

To do anything like this automatically would seem to me to defeat the
purpose of having distinct types, which is to clearly articulate what
values are appropriate in particular contexts.

Mike.


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

Message: 2
Date: Sun, 7 Aug 2016 13:38:05 +0100
From: Dmitrij Szamozvancev <dima.sa...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Generalised symbolic expressions
Message-ID:
        <CANgagRpq4e0GkXFJy_00pyXF=9q0xps-vmtz_kcsdxcoss1...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hello all,

I'm learning about data types and type classes and tried implementing a
simple symbolic expression evaluator, but generalised for non-numeric data
types.

data GenExpr a = Atom a | Sum (GenExpr a) (GenExpr a) | Prod (GenExpr a)
(GenExpr a) | Neg (GenExpr a)

What I am hoping to do is write a general evaluation function which can
some GenExp expression and evaluate it. For example, giving it a GenExp
Integer would evaluate it according to normal arighmetic, GenExp Bool would
be evaluated using Boolean algebra etc. I've declared explicit type classes
for types that can be summed, multiplied and negated:

class HasSum a where (.+.) :: a -> a -> a
class HasProd a where (.*.) :: a -> a -> a
class HasNeg a where (.-) :: a -> a

instance HasSum Integer where (.+.) = (+)
instance HasProd Integer where (.*.) = (*)
instance HasNeg Integer where (.-) = negate

instance HasSum Bool where (.+.) = (||)
instance HasProd Bool where (.*.) = (&&)
instance HasNeg Bool where (.-) = not

-- Evaluate generalised expression
genEval :: (HasSum a, HasProd a, HasNeg a) => GenExpr a -> a
genEval (Atom a) = a
genEval (Sum ge1 ge2) = genEval ge1 .+. genEval ge2
genEval (Prod ge1 ge2) = genEval ge1 .*. genEval ge2
genEval (Neg ge) = (.-) $ genEval ge


However, I would like it to work with types that don't support all three
operations, e.g. natural numbers which don't have a negation operation or
strings that only have concatenation as addition:

instance HasSum Nat where (.+.) = (+)
instance HasProd Nat where (.*.) = (*)

instance HasSum String where (.+.) = (++)

But these wouldn't be suitable inputs for genEval because they don't fulfil
all three type constraints. The only solution I can think of is writing
separate genEvalNat and genEvalString functions, which kind of defeats the
purpose of the generalisation.
Is there a more elegant way to solve this problem?
Thank you in advance!

Dima
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160807/71a59512/attachment-0001.html>

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

Message: 3
Date: Sun, 07 Aug 2016 08:49:33 -0400
From: Michael Alan Dorman <mdor...@jaunder.io>
To: Imants Cekusins <ima...@gmail.com>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Bool newtype
Message-ID:
        <87popku4ki.fsf@aching.i-did-not-set--mail-host-address--so-tickle-me>
Content-Type: text/plain

Imants Cekusins <ima...@gmail.com> writes:

> for a Bool-like newtype:
>
> newtype B = B Bool
>
> , is there an easy way to use this newtype B in place of Bool?
>
> e.g.
>
> let b1 = B True
> in if b1 then 1 else 0
>
> ?

Imants,

You can create a function that converts a B to a Bool:

  toBool :: B -> Bool
  toBool B b = b

And then use the result of applying that function to your variable:

  let b1 = B True
  in if toBool b1
     then 1
     else 0

To do anything like this automatically would seem to me to defeat the
purpose of having distinct types, which is to clearly articulate what
values are appropriate in particular contexts.

Mike.


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

Message: 4
Date: Sun, 7 Aug 2016 15:13:26 +0200
From: Imants Cekusins <ima...@gmail.com>
To: Michael Alan Dorman <mdor...@jaunder.io>,  The Haskell-Beginners
        Mailing List - Discussion of primarily beginner-level topics related
        to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Bool newtype
Message-ID:
        <CAP1qinbjNgcfYsC-VW1Y4dY=a8PrB-fA1dnsLoVfNpqGYG=_...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

True: distinct types help. In this case a function expects a couple bool
args so  the newtypes help ensure that correct args are passed in correct
order.

However when working with vals within a function, it may be handy to be
able to deduce bool from the newtype, at least to be used with if. This may
introduce complexity so not suggesting / requesting anything. Just
thought:  something might be already available.

For example, num-like newtypes can be added, compared as are, without the
need to extract the num. Num newtypes conveniently can be derived with
"deriving".

A function you suggest is good and it works. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160807/fee6d450/attachment-0001.html>

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

Message: 5
Date: Sun, 7 Aug 2016 15:41:10 +0200
From: Imants Cekusins <ima...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Generalised symbolic expressions
Message-ID:
        <cap1qinybvnudelmceewteswjuga1y+h55pe9nyo61qsop-i...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Dima,

is data GenExpr necessary?
​
would HasSum, HasProd, HasNeg classes & instances not be enough?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160807/43cfc2a5/attachment-0001.html>

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

Message: 6
Date: Sun, 7 Aug 2016 16:49:39 +0200
From: Marcin Mrotek <marcin.jan.mro...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Bool newtype
Message-ID:
        <cajcfpzmrrriw1cvrcr9snmynxpxjvfetinx1piak0_hmhf1...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hello,

That kind of a function can be also declared automatically, as in:

newtype B = B { toBool :: Bool }

Best regards,
Marcin Mrotek


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

Message: 7
Date: Sun, 7 Aug 2016 16:59:38 +0200
From: Imants Cekusins <ima...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Bool newtype
Message-ID:
        <cap1qinzcu9fu3_dx_uem+eq-yuntpmm_edjc5+hacsnn2dk...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

> newtype B = B { toBool :: Bool }

yep, this works well. toBool b1 is easy enough ;)

I wondered if there was an easy (with deriving etc) way to test B as it is.
If there isn't - no biggie.


​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160807/69acef45/attachment-0001.html>

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

Message: 8
Date: Sun, 7 Aug 2016 17:13:18 +0200
From: Imants Cekusins <ima...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Generalised symbolic expressions
Message-ID:
        <cap1qina1enxrem5wfbumaavdh1wv+hae2a1bgjbfx8g+xsr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

> writing separate genEvalNat and genEvalString functions

.. here is another possibility:

data GenExpr_nat a = ...
data GenExpr_string a = ...


class GenEval exp a where
genEval ::exp a -> a

instance GenEval GenExpr_nat a where
...

instance GenEval GenExpr_string a where
...
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160807/ea25d0db/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 98, Issue 7
****************************************

Reply via email to