cling

129 views
Skip to first unread message

Ben Goodrich

unread,
Oct 15, 2012, 6:19:27 PM10/15/12
to stan...@googlegroups.com
I recently discovered cling

http://root.cern.ch/drupal/content/cling
http://root.cern.ch/drupal/content/what-cling

cling is an interactive C++ interpreter, which they claim can handle anything that clang handles. I think cling could be used sort-of like a debugger for the C++ code that stanc generates.

Ben



Ben Goodrich

unread,
Aug 28, 2016, 8:55:42 PM8/28/16
to stan development mailing list

https://www.youtube.com/watch?v=g7-tskP0OzI

/opt/cling/bin$ ./stan.sh # just loads math.hpp with correct includes
****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ double y = 1.3;
[cling]$ stan::math::var mu = 0.5, sigma = 1.2;
[cling]$ stan::math::var lp = 0;
[cling]$ lp -= 0.5 * log(2 * stan::math::pi());
[cling]$ lp -= log(sigma);
[cling]$ lp -= 0.5 * pow((y - mu) / sigma, 2);
[cling]$ std::cout << "f(mu, sigma) = " << lp.val() << std::endl;
f(mu, sigma) = -1.32348
[cling]$ lp.val() // simpler alternative since cling is an interpreter
(double) -1.32348
[cling]$ lp.grad();
[cling]$ std::cout << " d.f / d.mu = " << mu.adj() << std::endl << " d.f / d.sigma = " << sigma.adj() << std::endl;
 d.f / d.mu = 0.555556
 d.f / d.sigma = -0.462963
[cling]$ .q

But you need a login to play with it.

Ben

Allen Riddell

unread,
Aug 29, 2016, 5:00:13 PM8/29/16
to stan...@googlegroups.com
If this sort of thing is possible perhaps we can bundle this (or
something similar) into the executable and just interpret (transpiled)
Stan program code? It would certainly be great to skip that compilation
step.

Or will this just never work with code involving templates?


On 08/28/2016 08:55 PM, Ben Goodrich wrote:
> On Monday, October 15, 2012 at 6:19:27 PM UTC-4, Ben Goodrich wrote:
>
> I recently discovered cling
>
> http://root.cern.ch/drupal/content/cling
> <http://root.cern.ch/drupal/content/cling>
> http://root.cern.ch/drupal/content/what-cling
> --
> You received this message because you are subscribed to the Google
> Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to stan-dev+u...@googlegroups.com
> <mailto:stan-dev+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Ben Goodrich

unread,
Aug 29, 2016, 6:37:04 PM8/29/16
to stan development mailing list
On Monday, August 29, 2016 at 5:00:13 PM UTC-4, riddell6 wrote:
If this sort of thing is possible perhaps we can bundle this (or
something similar) into the executable and just interpret (transpiled)
Stan program code? It would certainly be great to skip that compilation
step.

Or will this just never work with code involving templates?

It can parse whatever clang can parse because it uses clang to parse it. The Stan Math Library certainly has a lot of templating that it handles fine:

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ stan::math::log_sum_exp(2, 3)
(boost::math::tools::promote_args<int, int, float, float, float, float>::type) 3.31326
[cling]$ stan::math::var two(2);
[cling]$ stan::math::log_sum_exp(two, 3)
(boost::math::tools::promote_args<int, stan::math::var, float, float, float, float>::type) @0x696b840
[cling]$ stan::math::log_sum_exp(two, 3).val()
(double) 3.31326
[cling]$ .q

But bundling it seems like a tall order since it uses a modified LLVM / clang and LLVM / clang is huge.

Ben

Reply all
Reply to author
Forward
0 new messages