What are the places in which Clojure uses Java primitives as is and uses the boxed versions of the primitives?
As I see here, it is mentioned that Clojure provides full support for primitives. Is there any clear definition on when and where primitives and boxed versions are used in Clojure?
I don't think the rules for when Clojure uses unboxed math can be easily summarized in a few lines (but I may be wrong). But you can use clj-java-decompiler to decompile Clojure code into Java code to get an idea about what goes regarding boxing and unboxing. If I evaluate this code
(ns playground.decompile
(:require [clj-java-decompiler.core :as d]))
(d/decompile
(loop [product 1
counter 10]
(if (= 0 counter)
product
(recur (* product counter)
(dec counter)))))
it prints out this code
// Decompiling class: playground/decompile$fn__7159
package playground;
import clojure.lang.*;
public final class decompile$fn__7159 extends AFunction
{
public static Object invokeStatic() {
long product = 1L;
long multiply;
for (long counter = 10L; 0L != counter; counter = Numbers.dec(counter), product = multiply) {
multiply = Numbers.multiply(product, counter);
}
return Numbers.num(product);
}
#Override
public Object invoke() {
return invokeStatic();
}
}
Here, it looks to me as if the compiler understands that we are using long numbers. You probably have to look from case to case whether boxing happens or not.
You can find some good information on Java interop re primitive types here:
https://clojure.org/reference/java_interop#typehints
You may also be interested in this library for working with primitive arrays:
https://github.com/plumatic/hiphip
Related
I'm refactoring some code and curious if there is a modern C++ feature that allows me to dynamically push all matching declarations into a vector without having to manually type out every parameter name.
For example;
I have the following declarations in my header (instantiated elsewhere);
Fl_Button * button_file_browse;
Fl_Button * button_parse_txt;
Fl_Button * button_parse_xer;
Fl_Button * button_file_browse;
Fl_Button * button_perform_sentiment;
Fl_Button * button_txt_folder_browse;
Then elsewhere I push these all into a vector one-by-one; it's a little painful with tons of widgets. Especially if I add a new Fl_Button I need to go and add it to this piece of code as well.
std::vector<Fl_Button *> vector_fl_button;
vector_fl_button.push_back(button_file_browse);
vector_fl_button.push_back(button_parse_txt);
vector_fl_button.push_back(button_parse_xer);
vector_fl_button.push_back(button_perform_sentiment);
vector_fl_button.push_back(button_txt_folder_browse);
Is there a beautiful C++ feature that allows me to type something as elegant as below?
std::vector<Fl_Button *> vector_fl_button;
vector_fl_button.push_back(button_*); // Pushes all pointers starting with button_
Just to showcase, how simple code generation can be if you have a powerful scripting language at hand. I usually use Common Lisp because I use emacs anyway and so it is all done "in-house". And I usually program in Lisp and only sometimes I regress to my old go-to language C++...
Especially for maintenance, it might pay off and reduce "oversight errors".
Since I use cl-ppcre which is the Lisp regular expression engine, you could even consider to exploit your habits of doing things in your favor.
For example, if you always have 1 header file with your button declarations in your single class in that header, you could just use the header file instead of copy and pasting the button declarations into the lisp code...
The few lines of lisp below just show the basics. And if Lisp is not your thing, I guess you can do the same in APL or Haskell or Perl or Julia or even in Python.
(defparameter *my-buttons*
"Fl_Button * button_file_browse;
Fl_Button * button_parse_txt;
Fl_Button * button_parse_xer;
Fl_Button * button_file_browse;
Fl_Button * button_perform_sentiment;
Fl_Button * button_txt_folder_browse;")
(defparameter *button-decl-pattern*
(ppcre:create-scanner "\\s*Fl_Button\\s*\\*\\s*(button_\\w+);"))
(defun button-names (&optional (decls *my-buttons*))
(with-input-from-string (stream decls)
(loop for line = (read-line stream nil)
while line
collecting
(aref
(nth-value 1 (ppcre:scan-to-strings
*button-decl-pattern*
line))
0))))
(defun vectorize-buttons (buttons)
(with-output-to-string (stream)
(format stream "std::vector<Fl_Button *> vector_fl_button;~%")
(loop for button in buttons do
(format stream
"vector_fl_button.push_back(~A);~%"
button))))
CL-USER> (vectorize-buttons (button-names))
"std::vector<Fl_Button *> vector_fl_button;
vector_fl_button.push_back(button_file_browse);
vector_fl_button.push_back(button_parse_txt);
vector_fl_button.push_back(button_parse_xer);
vector_fl_button.push_back(button_file_browse);
vector_fl_button.push_back(button_perform_sentiment);
vector_fl_button.push_back(button_txt_folder_browse);
"
Is there a beautiful C++ feature that allows me to type something as elegant as below?
std::vector<Fl_Button *> vector_fl_button;
vector_fl_button.push_back(button_*);
No, not built-in. However, FLTK makes it possible to build the vector dynamically. The name of the objects in the C++ code is however lost in compilation so you'll need to find some other criteria.
Fortunately, Fl_Widgets, like Fl_Button have a user_data() function that can return a void* or long (really a void* that you need to cast to a long). So, if you don't want a vector of all Fl_Buttons, you can set user_data when designing the windows. Here I've used fluid to set the user_data value on a button that I like to be included in the vector:
Here's a function to find all widgets of a certain type placed inside another widget (like a Fl_Window) and to apply a unary predicate on the found widgets. If the predicate returns true, it stores the pointer.
widget_funcs.hpp
#ifndef WIDGET_FUNCS_HPP
#define WIDGET_FUNCS_HPP
#include <FL/Fl_Group.H>
#include <algorithm>
#include <iterator>
#include <vector>
template <class T, class UnaryPredicate>
auto find_widgets_of_type(Fl_Widget* w, UnaryPredicate pred) {
std::vector<T*> rv;
// check if "w" is a container class (essentially a dynamic_cast):
Fl_Group* g = w->as_group();
if(g) {
// Copy all the pointers that can be dynamically casted to T*
// and that fulfills the conditions in the predicate function.
std::for_each(g->array(), std::next(g->array(), g->children()),
[&rv,&pred](Fl_Widget* child) {
auto isT = dynamic_cast<T*>(child);
if(isT && pred(isT)) rv.push_back(isT);
});
}
return rv;
}
template <class T> // use this overload if you want all Fl_Buttons
auto find_widgets_of_type(Fl_Widget* w) {
return find_widgets_of_type<T>(w, [](const T*){ return true; });
}
#endif
You can then call the above function with the widget you'd like to get Fl_Buttons from as an argument:
#include "widget_funcs.hpp"
class SomeClass {
public:
SomeClass(Fl_Widget* window) :
vector_fl_button(
find_widgets_of_type<Fl_Button>(window, [](Fl_Button* b) {
return reinterpret_cast<long>(b->user_data()) == 1;
}))
{}
// ...
private:
std::vector<Fl_Button*> vector_fl_button;
};
If you need to find buttons in grandchildren too, you could just make the function recursive.
If you already use user_data for something else, you can replace the predicate in the example above with some other property that is common for all the buttons you'd like to have in the vector.
I'm writing some code that uses Z3 strings to evaluate permissions in ACLs. So far with SMT2 this has been relatively easy. An eg. code of what I'm trying to acheive is:
(declare-const Group String)
(declare-const Resource String)
(define-fun acl1() Bool
(or (and
(= Group "employee")
(str.prefixof "shared/News_" Resource))
(and
(= Group "manager")
(or (str.prefixof "shared/Internal_" Resource)
(str.prefixof "shared/News_" Resource))
)))
(define-fun acl2() Bool
(and (and (str.prefixof "shared/" Resource)
(str.in.re Group re.allchar))
(not (and (str.prefixof "shared/Internal_" Resource)
(= Group "employee")))))
;; perm(acl1) <= perm(acl) iff acl1 => acl2
(define-fun conjecture() Bool
(=> (= acl1 true)
(= acl2 true)))
(assert (not conjecture))
(check-sat)
Reading the z3 c++ bindings, I can't figure out how to stick a z3::function to this yet. So far, assuming that define-fun is just a lisp macro, I have this.
#include <z3++.h>
z3::expr acl1(z3::context& c, z3::expr& G, z3::expr& R)
{
return (((G == c.string_val("employee")) &&
z3::prefixof(c.string_val("shared/News_"), R)) ||
((G == c.string_val("manager")) &&
(z3::prefixof(c.string_val("shared/Internal_"), R) ||
z3::prefixof(c.string_val("shared/News_"), R))));
}
z3::expr acl2(z3::context& c, z3::expr& G, z3::expr& R)
{
return ((z3::prefixof(c.string_val(""), G) &&
z3::prefixof(c.string_val("shared/"), R)) &&
!((G == c.string_val("employee")) &&
(z3::prefixof(c.string_val("shared/Internal"), R))));
}
z3::expr MakeStringFunction(z3::context* c, std::string s) {
z3::sort sort = c->string_sort();
z3::symbol name = c->str_symbol(s.c_str());
return c->constant(name, sort);
}
void acl_eval()
{
z3::context c;
auto Group = MakeStringFunction(&c, "Group");
auto Resource = MakeStringFunction(&c, "Resource");
auto acl1_f = acl1(c, Group, Resource);
auto acl2_f = acl2(c, Group, Resource);
auto conjecture = implies(acl1_f == c.bool_val(true),
acl2_f == c.bool_val(true));
z3::solver s(c);
s.add(!conjecture);
std::cout << s.to_smt2() << std::endl;
switch(s.check()){
case z3::unsat: std::cout<< "Valid Conjecture" << std::endl; break;
case z3::sat: std::cout << "Invalid Conjecture" << std::endl; break;
case z3::unknown: [[fallthrough]]
default:
std::cout << "Unknown" << std::endl;
}
}
int main(){
acl_eval();
return 0;
}
Is this how this is to be done wrt functions in C++ bindings?
while the smt2 code generated by C++ bindings don't exactly look like the other one, I see a whole expr inside an assert with let bindings which kind of does what I want. Additionally, I also want to know if C++ bindings support regex functions like the SMT lib of z3 exposes? I can't find any examples and the docs aren't very clear.
In general, you do not need to create "functions" in SMTLib when you're using the C++ (or any other high-level) API. Instead, you simply write functions in those languages, which generate the required code directly. This does sound confusing at first, but it is the intended use case: SMTLib functions get replaced by functions in the host language. Running them in the host language then produces the necessary syntax trees in the object language; i.e., Z3's internal AST representation. Especially in your case, you do not need any "arguments" passed to these functions, so you shouldn't be creating any at all. So, what you did here is correct.
(Side note: There can be scenarios where you do want to spit out functions in SMTLib. For instance if you want to use uninterpreted functions. Or perhaps you want to use the recursive function definitions, which you cannot really do in the host language. But let's not conflate the matters here. If you do feel you actually do need them, please ask a separate question about that. From your description, I see no reason for them.)
Regarding regular-expression expressions: They're all available in the C++ API, take a look here: https://z3prover.github.io/api/html/z3_09_09_8h_source.html#l03334
In particular, the functions you're looking for are:
in_re: For checking membership
re_full: Regular expression accepting all strings (Somewhat confusingly, SMTLibs allchar is called re_full in the C++ API.)
Hopefully that'll get you started!
I have a global list of items (each with a few properties) in a module of my program. It's immutable and statically defined in the code, so no worries there.
For instance let's say I have vegetables, which are just an alias defining them to an immutable tuple with name (string), code (ubyte) and price (ushort).
I'd like to be able to access those either by name or by code ; so I thought since the list of vegetables is known at compile-time, I could probably construct associative arrays with references to these vegetables (so string=>vegetable and ubyte=>vegetable)
Here's the kind of thing I am trying to achieve :
static struct instructions
{
// list of Veggies
immutable instr[] list = [
Veggie("Potato" , 0xD0, 2),
Veggie("Carrot" , 0xFE, 5),
];
// genByCode and genByName being pure functions that get CTFE'd
// and return the desired associative array
immutable instr[ubyte] byCode = genByCode(list);
immutable instr[string] byName = genByName(list);
// overloaded function returns the right Veggie
instr get(string name) const
{ return byName[name]; }
instr get(ubyte code) const
{ return byCode[code]; }
}
With those generator functions (separated for clarity) of the form
pure instr[ubyte] genByCode(immutable Veggie[] list)
{
instr[ubyte] res;
foreach (i ; list)
res[i.code] = i;
return res;
}
I spent quite some time messing around but I couldn't it to work. Of course it would be trivial to construct at runtime, but clearly it should be possible to do it at compile time.
At first I thought it was an issue of mutability, so I tried marking everything (vegetables and vegetable lists) as immutable (as they should be anyway), but then I ran into issues which I think regard immutable tuples, and feel too lost to keep going.
Could I get help from someone with a clearer overview of the mechanisms at play here ? Thanks !
The data is already there, no need to construct a compile-time associative array.
Just iterate over it statically:
static auto get(int code)(){
static foreach(veggie; list)
static if(veggie.code == code)
return veggie;
}
...
void main(){
writeln(instructions.get!0xD0);
}
It may be slower than access through a hash map, but that's the life of CTFE.
To make sure it evaluates at compile time, you can use this:
template get(int code){
static foreach(veggie; list)
static if(veggie.code == code)
alias get = veggie;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to make a program which can read code from a file (similar to an interpretor but not as complex) store it into a linear list and then execute it when needed.
This is what i want this "interpretor" to know:
variable declaration
array declaration
for, while, if control structures
input and output files (output files can be opened in append mode)
non recursive functions
Because i execute it command by command i can stop/start at a specific number of commands executed which is helpful when trying to do more at the same time (similar to multithreading) and for this reason, i will name the class _MINI_THREAD. Here are the declarations of the struct COMMAND and class _MINI_THREAD:
struct COMMAND
{
unsigned int ID;
char command_text[151];
COMMAND* next;
COMMAND* prev;
};
class _MINI_THREAD
{
public:
void allocate_memory()
{
while (start_of_list == NULL)
start_of_list = new (std::nothrow) COMMAND;
while (end_of_list == NULL)
end_of_list = new (std::nothrow) COMMAND;
start_of_list -> prev = NULL;
start_of_list -> next = end_of_list;
end_of_list -> prev = start_of_list;
end_of_list -> next = NULL;
}
void free_memory()
{
for(COMMAND* i=start_of_list -> next;i!=end_of_list;i=i->next)
delete i -> prev;
delete end_of_list -> prev;
delete end_of_list;
}
bool execute_command(unsigned int number_of_commands)
{
for(unsigned int i=0;i<number_of_commands;i++)
{
/*match id of current command pointed by the cursor with a function from the map*/
if (cursor==end_of_list) return false;
else cursor=cursor->next;
}
return true;
}
bool if_finished()
{
if (cursor==end_of_list)return true;
else return false;
}
unsigned int get_ticks()
{
return ticks_per_loop;
}
void set_ticks(unsigned int ticks)
{
ticks_per_loop = ticks;
}
private:
unsigned int ticks_per_loop;
COMMAND* cursor=NULL;
COMMAND* start_of_list=NULL;
COMMAND* end_of_list=NULL;
};
I also try to keep the syntax of the "invented code" from the source files as close as possible to the c/c++ syntax but sometimes i placed a new parameter because it makes verification a lot easier. Please notice that even the while has a name so that i can manage nested loops faster.
Here is an example i came up with:
Source_file.txt
int a;
input_file fin ("numbers.in");
output_file fout ("numbers.out");
while loop_one ( fin.read(a,int,skipws) )
{
fout.print(a,int);
fout.print(32,char); /*prints a space after each number*/
}
close_input_file fin;
close_output_file fout;
/*This code is supposed to take all numbers from the input file and */
/* move them into the output file */
In the real program the object thread1 of class _MINI_THREAD contains a dinamically allocated list (i will display it as an array for simple understanding)
_MINI_THREAD thread1;
/*read from Source_file.txt each command into thread1 command array*/
thread1.commandarr={
define_integer("a"),
open_input_file("numbers.in",fin),
open_output_file("numbers.out",fout),
define_label_while("loop_one",fin.read()), /*if the condition is false the `cursor` will jump to labe_while_end*/
type_to_file(fout,a,int),
type_to_file(fout,32,char),
label_while_return("loop_one"), /*returns the cursor to the first line after the while declaration*/
label_while_end("loop_one"), /*marks the line after the while return point*/
close_input_file("numbers.in",fin),
close_output_file("numbers.out",fout),
};
/*the cursor is already pointing at the first command (define_integer("a"))*/
/*this will execute commands until the cursor reaches the end_of_list*/
while(thread1.execute_commands(1))NULL;
thread1.free_memory();
Now my problem is actually implementing the IF_CONTROL_STRUCTURE. Because you may want to type if (a==b) or if (foo()) etc... and i don't know how can i test all this stuff.
I somehow managed to make the cursor move accordingly to any structure (while,do ... while,for etc) with the idea of labels but still i cannot check the condition each structure has.
You really want to write some interpreter (probably using some bytecode). Read more about semantics.
Writing well a good interpreter is not a trivial task. Consider using some existing one, e.g. Lua, Guile, Neko, Python, Ocaml, .... and take some time to study their free software implementation.
Otherwise, spend several months reading stuff, notably:
SICP is an absolute must to read (and freely downloadable).
the Dragon Book
Programming Language Pragmatics
Lisp In Small Pieces
about the SECD machine
the GC handbook
Notice that an entire book (at least) is needed to explain how an interpreter works. See also relevant SIGPLAN conferences.
Many (multi-thread friendly) interpreters have some GIL. A genuinely multi-threaded interpreter (without any GIL) is very difficult to design (what exactly would be its REPL ???), and a multi-threaded garbage collector is also very difficult to implement and debug (consider using an existing one, perhaps MPS or Boehm's GC).
So "your simple work" could require several years of full-time work (and could get you a PhD).
a simpler approach
After having read SICP and becoming familiar with some Lisp-like language (probably some Scheme, e.g. thru Guile), you could decide on some simpler approach (basically a tiny Lisp interpreter which you could code in a few hundred lines of C++; not as serious as full-fledged interpreters mentioned before).
You first need to define on paper, at least in English, the syntax and the semantics of your scripting language. Take a strong inspiration from Lisp and its S-expressions. You probably want your scripting language to be homoiconic (so your AST would be values of your languages), and it would have (like Lisp) only expressions (and no statements). So the conditional is ternary like C++ ? :
You would represent the AST of your scripting language as some C++ data structure (probably some class with a few virtual methods). Parsing some script file into an AST (or a sequence of AST, maybe feed to some REPL) is so classical that I won't even explain; you might use some parser generator -improperly called compiler-compilers (like bison or lemon).
You would then at least implement some eval function. It takes two arguments Exp and Env: the first one, Exp, is the AST of the expression to be evaluated, and the second one, Env is some binding environment (defining the binding of local variables of your scripting language, it could be as simple as a stack of mapping from variables to values). And that eval function returns some value. It could be a member function of your AST class (then Exp is this, the receiver ....). Of course ASTs and values of your scripting language are some tagged union (which you might, if so wished, represent as a class hierarchy).
Implementing recursively such an eval in C++ is quite simple. Here is some pseudo code:
eval Exp Env :
if (Exp is some constant) {
return that constant }
if (Exp is a variable Var) {
return the bounded value of that Var in Env }
if (Exp is some primitive binary operator Op /* like + */
with operands Exp1 Exp2) {
compute V1 = eval Exp1 Env
and V2 = Exp2 Env
return the application of Op /* eg addition */ on V1 and V2
}
if (Exp is a conditional If Exp1 Exp2 Exp3) {
compute V1 = eval Exp1 Env
if (V1 is true) {
compute V2 = eval Exp2 Env
return V2
} else { /*V1 is false*/
compute V3 = eval Exp3 Env
return V3
}
}
.... etc....
There are many other cases to consider (e.g. some While, some Let or LetRec which probably would augment Env, primitive operations of different arities, Apply of an arbitrary functional value to some sequence of arguments, etc ...) that are left as an exercise to the reader. Of course some expressions have side effects when evaluated.
Both SICP and Lisp In Small Pieces explain the idea quite well. Read about meta-circular evaluators. Don't code without having read SICP ...
The code chunk in your question is a design error (even the MINI_THREAD thing is a mistake). Take a few weeks to read more, throw your code to the thrash bin, and start again. Be sure to use some version control system (I strongly recommend git).
Of course you want to be able to interpret recursive functions. There are not harder to interpret than non-recursive ones.
PS. I am very interested by your work. Please send me some email, and/or publish your tentative source code.
I was writing some scala at home for fun the other night and thought myself, "wouldn't it be cool if we had this kind of API in C++? - Is it possible?". I started searching for c++, monads and stl collections, but couldn't find anything that seemed to actually increase my productivity :/.
So I set out to implement some proof-of-concept (super inefficient, but at least it works! :)) for what's normally seen in more dedicated functional languages.
auto m = monadic(std::vector<int>{1,2,3});
auto mMapped = m.map([](int x) {return x*x; });
Monadic<std::vector<int>> mFiltered = m.filter([](int x) {return x > 1; });
std::vector<std::string> funList =
monadic(src)
.flatMap([](int x) { return std::vector<int>{1,2,3};} )
.filter([](int x) { return x > 1; })
.map([](int x) { return std::to_string(x).append("_string"); })
.col;
I would really like such a library (but more complete and efficient using move semantics) for my every day c++ code (data management, threading and distributed execution becomes so easy).
The question:
--> Do you know of any such library for C++11 or C++14? <--
The code above was using a very quickly hacked together PoC library I put here https://github.com/GiGurra/cpp_monad (Tested it ok with gcc 4.9.2, VS2015 and some version of clang, can't remember).
The "Monadic" class doesn't contain any specific implementation, it just passes on to whatever map/filter/flatMap free functions are available for the given collection type, for example the map operation is very naively implemented like:
class Monadic {
public:
...
template <typename MapFcn>
auto map(MapFcn mapFcn) { return monadic(cpp_monad::map(col, mapFcn)); }
...
};
// Default naive implementation in unspecialized case
template <typename T_Coll, typename T_MapFcn>
auto map(const T_Coll& src, T_MapFcn fcn) {
std::vector<decltype(fcn(internal::_firstElem(src)))> out;
std::transform(std::begin(src), std::end(src), std::back_inserter(out), fcn);
return out;
};
This way you could replace either the wrapper or the implementation without having to modify the code using the particular API.
Just an idea, perhaps there's something already out there but a lot better.
Take a look at Eric Niebler's Ranges proposal and sample implementation for a similar API that's been proposed for a future C++ standard.
Chandler Carruth's C++Now 2014 talk on Range algorithms is about another attempt to design a more 'functional' style algorithms library for C++.