Send Beginners mailing list submissions to
        [email protected]

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
        [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:  Maybe and Just (Larry Evans)
   2.  Automatic Differentiation (Animesh Saxena)
   3. Re:  Automatic Differentiation (Chadda? Fouch?)
   4. Re:  parser frontend (Konstantine Rybnikov)


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

Message: 1
Date: Fri, 27 Mar 2015 14:57:00 -0500
From: Larry Evans <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe and Just
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8



On 03/27/2015 02:01 PM, Joel Neely wrote:
> Shishir,
> 
> Would I be right to guess that you're coming from an OO background? If
> so, I'd suggest thinking about the difference between declaring a
> constructor and invoking it.
> 
> I suggest that you can read the Haskell declaration
> 
>     data Maybe tp = Just tp | Nothing
> 
> 
> as declaring ?a data type and specifying two constructors that can
> return an instance (in the OO sense of the word) of that data type. But
> when the expression
> 
>     Just "FOO"
> 
> 
> is invoking one of those constructors on a value of type String, so the
> resulting value has type Maybe String.
> 
[snip]
Or, Maybe can be thought of as an abstract class with concrete
subclasses, Just and Nothing.

Or, Maybe can be thought of like a tagged union.  In c++ syntax:

  enum MaybeTag{ JustTag, NothingTag};
  template<typename ValType>
  struct JustType
  {
    ValType my_val;
    JustType(ValType a_val)
    : my_val(a_val)
    {}
  };
  struct NothingType
  {};
  template<typename ValType>
  struct Maybe
  {
  private:
      MaybeTag
    my_tag;
      union possible_values
      { JustType<ValType> just_val;
        NothingType nothing_val;
        possible_values(ValType a_val)
          : just_val(a_val)
          {}
        possible_values()
          : nothing_val()
          {}
      }
    my_val;
    Maybe(ValType a_val)
      : my_tag(JustTag)
      , my_val(a_val)
      {
      }
    Maybe()
      : my_tag(NothingTag)
      , my_val()
      {}
  public:
      MaybeTag
    tag()const
      { return my_tag;
      }
      static Maybe<ValType>
    Just(ValType a_val)
      { return Maybe<ValType>(a_val);
      }
      static Maybe<ValType>
    Nothing()
      { return Maybe<ValType>();
      }
  };

#include <iostream>

int main()
{
  auto mj=Maybe<int>::Just(5);
  std::cout<<"mj.tag="<<mj.tag()<<"\n";
  auto mn=Maybe<int>::Nothing();
  std::cout<<"mn.tag="<<mn.tag()<<"\n";
  return 0;
}

The following is the compile and run of above code:

g++ -std=c++11 tagged_union.cpp -o tagged_union.exe
./tagged_union.exe
mj.tag=0
mn.tag=1




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

Message: 2
Date: Sat, 28 Mar 2015 12:10:02 +0800
From: Animesh Saxena <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Automatic Differentiation
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

I was implementing automatic differentiation in haskell and was able to code 
the calculation part, but I wanted to extend it to show the symbols instead of 
just the final value.
Let me explain and copy/paste the code?

3  data ADif a = ADif a a deriving (Eq)

10 instance Floating x => Floating (ADif x) where
11           pi = ADif pi 0
12           exp    (ADif x x') = ADif (exp    x) (x' * exp x)
13           log    (ADif x x') = ADif (log    x) (x' / x)
14           sqrt   (ADif x x') = ADif (sqrt   x) (x' / (2 * sqrt x))
15           sin    (ADif x x') = ADif (sin    x) (x' * cos x)
16           cos    (ADif x x') = ADif (cos    x) (x' * (- sin x))
?.And so on all the functions


27 instance Num x => Num (ADif x) where
28         ADif x x' + ADif y y' = ADif (x+y) (x'+y')
29         ADif x x' * ADif y y' = ADif (x*y) (y'*x + x'*y)
30         fromInteger x = fromInteger x

let myfunction x = exp (log (sin x))
*Main> myfunction (ADif 2 1)
-0.4161468365471424

I verified that this is the correct solution by hand! (& well mathematica too!)

Anyway now I was hoping to print the actual symbols, so I was googling around 
for extending ?Show? typeclass for floating and Num, kinda similar pattern. Is 
this the right approach? Or I need to rethink the problem?
Basically my aim is to do something like mathematica where if I specify 
D[f[x],x] then I get the answer in symbols. 

-Animesh

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

Message: 3
Date: Sat, 28 Mar 2015 08:19:06 +0100
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Automatic Differentiation
Message-ID:
        <canfjzrz9a1swfnoeeeupswedsox5+vwzhs9ramox_px4srn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sat, Mar 28, 2015 at 5:10 AM, Animesh Saxena <[email protected]>
wrote:

> I was implementing automatic differentiation in haskell and was able to
> code the calculation part, but I wanted to extend it to show the symbols
> instead of just the final value.
>
> Anyway now I was hoping to print the actual symbols, so I was googling
> around for extending ?Show? typeclass for floating and Num, kinda similar
> pattern. Is this the right approach? Or I need to rethink the problem?
> Basically my aim is to do something like mathematica where if I specify
> D[f[x],x] then I get the answer in symbols.
>
>
The idea would be to write a new type that transport an human readable
representation as well as the actual value, then make it a Num, Floating
and so on instance so that you could just use ADif with this type to get a
representation of your action. You can look at simple-reflect
<https://hackage.haskell.org/package/simple-reflect> for a simple
implementation of this idea you can directly use with your ADif. If instead
of a String, you transport an operation tree, you may even simplify your
result and get back almost a symbolic differentiation from your automatic
differentiation !

-- 
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150328/ace00590/attachment-0001.html>

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

Message: 4
Date: Sat, 28 Mar 2015 13:13:55 +0200
From: Konstantine Rybnikov <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] parser frontend
Message-ID:
        <caabahfrpl5dg-v50+tnn5om-oolr8itmwu7_5fzt4upvsy6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Maurizio,

It's not completely clear how your compiler knows which files depend on
which. Does it read files one by one and "updates" dependency list? What
about situation when toplevel construct is split in two files, how does
this single-file API handle it?

On Fri, Mar 27, 2015 at 3:38 PM, Maurizio Vitale <[email protected]> wrote:

> G'day,
>   I'm trying to decide how to architect the frontend of a compiler I'm
> using as an excuse for learning Haskell.
>
>   Suppose we have a language (SystemVerilog) that has the notion of
> multi-file compilation units.
> This means that specific subset of files form separate scopes for certain
> things.
> For instance if we represent with lists of lists compilation units:
> [[a,b,c], [d], [e,f]]
> a macro definition in file b, would affect the rest of the file and file
> c, but wouldn't have effect on files d,e or a.
>
> Furthermore, if a toplevel construct is split between two files, say c and
> d, the compiler should treat the original compilation unit specification as
> if it was:
> [[a,b,(c,d)][e,f]], where (c,d) means we're compiling the logical
> concatenation of c and d.
> If (c,d) is also incomplete, (c,d,e) should be tried and so on.
>
> Now in well designed systems, all files can actually be compiled in
> parallel and so I'd like to optimize for that case and recompile files
> (either because we need side effects from files before them or because
> they're incomplete. So I'm not to concerned if wasted work is done for the
> degenerate cases.
>
> Any suggestion on how to go about this?
> Le's assume that parsing a file is done with a function:
> p :: file -> Either Error (ast, [defs], [use]) where the [defs] are things
> that might affects files down the line and [use] are things that, if
> defined by some prior file, cause recompilation.
>
> I'm interested in both a sequential and parallel solution and it would be
> sweet if they where similar, with the proper abstractions.
>
> Thanks a lot for any insight/ideas,
>
>   Maurizio
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150328/a7a0657a/attachment-0001.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 81, Issue 70
*****************************************

Reply via email to