Extract to an OCaml file by using "Recursive Extraction" in Coq - coq

I want to extract a function foo in Coq to an OCaml file. Because my real function have to use the Recursive Extraction, when I run a program it output the extracted OCaml code on the cmd. But I would like to output it to a file, for example: foo.ml
Recursive Extraction foo.
When I tried:
Recursive Extraction "foo.ml" foo.
or Recursive Extraction foo "foo.ml"
I got an error: Syntax error: "." expected after [vernac:command] (in [vernac_aux]).
My question is: Am I able to extract the function foo to a file by using the Recursive Extraction syntax? Thank you for your help.

According to the manual (http://coq.inria.fr/distrib/V8.4/refman/Reference-Manual027.html), Extraction "foo.ml" foo will extract foo and recursively all its dependencies into foo.ml, i.e. you don't need Recursive in that case, it is only used for extraction on stdout.

Related

How to apply MATLAB addpath to non static string?

dir1 = '/tmp1';
dir2 = '/tmp2'
If we do the following
addpath [dir1 dir2];
MATLAB takes '[dir1 dir2]' as the path name to add. We can do the following
eval(sprintf(...
'addpath %s;', ...
[dir1 dir2]));
I was wondering if there is any better way. Thank you,
A very simple way to achieve what you want is to call:
addpath(fullfile(dir1,dir2));
Fullfile will take care of adjusting the string to be a proper folder name (under both Windows and Unix) as in:
fullfile('foo','bar') % returns foo/bar
fullfile('foo/','bar') % returns foo/bar
To add files recursively just do:
pathsToAdd = genpath(fullfile(dir1,dir2));
addpath(pathsToAdd);
The general issue that you are having is that MATLAB has two ways of calling commands. The first does not use an explicit function call function() but rather just multiple inputs on the command line separated by a space:
addpath directory1 directory2
As you know this will add both directory1 and directory2 to the path.
What is happening here is that MATLAB converts all of the inputs to strings implicitly and passes them to the addpath function. The explicit equivalent is
addpath('directory1', 'directory2')
As you can see, internally MATLAB calls addpath like a normal function with input parameters, and as such you can pass it variables rather than string literals:
dir1 = 'directory1';
dir2 = 'directory2';
addpath(dir1, dir2);
This is why you are seeing an issue with:
addpath [dir1 dir2]
Because, as written, [dir1 dir2] is convert to a string (implicitly) since it was passed using the function parameter1 parameter2 syntax rather than the explicit function syntax.
Also, be careful because [dir1, dir2] doesn't do what you think it does. What it actually does it appends the strings dir1 and dir2 and would result in:
[dir1, dir2]
/tmp1/tmp2
If that is what you expect, then use fullfile rather than basic horizontal concatenation to ensure you have the proper file separators, etc.
addpath(fullfile(dir1, dir2));
You will actually see the implicit syntax in many MATLAB functions that accept only strings as input parameters. It is important to know, though, that you can always use the explicit function call function() instead to pass input strings which may be stored in variables.

CoffeeScript: calculate parse tree (like coffee -n) in a program

Is there a way to compute the CoffeeScript parse tree of a program (provided as a string) inside CoffeeScript without calling an external program?
For example, let's say I have a string 'square=(n)->n*n' inside a CoffeeScript program. I want to get the same output as storing this string in a file square.coffee and calling on the command line coffee -n square.coffee --- but without creating another process:
Block
Assign
Value "square"
Code
Param "n"
Block
Op *
Value "n"
Value "n"
Please, provide with your solution a link to documentation how to interpret the resulting data structure.
Just look in the source: the -n flag invokes (require 'coffee-script).nodes. The result is a syntax tree which corresponds to grammar.coffee and would be interpreted with nodes.coffee.
So this:
(require 'coffee-script').nodes 'square = (n)->n*n'
Will give you a syntax tree. Before you print it, you could use its toString method to get the same output as the coffee CLI.
For the filesystem operations, just use node's readFile or readFileSync from the fs library:
{readFileSync} = require 'fs'
{nodes} = require 'coffee-script'
nodes readFileSync('squares.coffee').toString()

inline iPython call to system

I really like to use system shell commands in iPython. But I was wondering if it is possible to loop over the returned values from a call to e.g. !ls. This works:
files = !ls ./*_subcell_cooc.txt
for f in files:
print f
But this does not:
for f in ( !ls ./*_subcell_cooc.txt):
print f
Error is:
File "<ipython-input-1-df2bc72907d7>", line 5
for f in ( !ls $ROOT/*_subcell_cooc.txt):
^
SyntaxError: invalid syntax
No it is not possible, the syntax var = !something is special cased in IPython. It is not valid python syntax, and we will not extend for loops and so on to work with it.
You can do assignment as you show in your first example, but using glob,os and other real python module to do that will be more robust, not much harder, and also work outside of IPython...
For the anecdote Guido was really not happy with IPython half-shell syntax when he saw it last time at SciPy2013.
(Also it uppercase I in IPython please.)

Matlab executables, passing variable [duplicate]

This question already has answers here:
How can I pass command line arguments to a standalone MATLAB executable running on Linux/Unix?
(3 answers)
Closed 9 years ago.
How do I use deploytool to get an executable file from a .m function and use it?
say, I have a .m names foo, here is the code:
function product = foo(array_a,array_b)
product = array_a.*array_b
end
now I use deploytool to generate a foo.exe, how can I use it with the same workspace vars, AKA array_a and array_b?
Regards
I got your code to work by just supplying the executable file with variables.
I first ran mbuild -setup. I have your file, called foo2.m:
function product = foo(array_a,array_b)
if ischar(array_a)
array_a = str2num(array_a);
end
if ischar(array_b)
array_b = str2num(array_b);
end
product = array_a.*array_b
end
The only difference is I ensured that the input are processed as numbers, not strings. Then, I compile:
mcc -mv -R -singleCompThread -N -p optim -p stats foo2.m
(A good explanation of this command is here: MCC example. I used the link to help me get it working.)
Then, just execute the function.
./run_foo2.sh /usr/local/MATLAB/R2011a/ 1 2
....
product =
2
Make sure you specify the location of the compiler libraries as the first argument, then array_a and array_b as the 2nd and 3rd arguments.
I first got an error when I tried to run the executable: error while loading shared libraries: libmwmclmcrrt.so.7.15: cannot open shared object file. I fixed this by finding the library file path (using find . -name "libmwmclmcrrt.so*"). I then corrected the library path I was supplying as the first argument when I called the executable.
You can use eval to convert strings to other data types, such as arrays. See here for more details.
Also, pcode could be another way, if you want to protect your source code.

read function from text file in matlab

I'm going to read some function from a Unicode text file in matlab and calculate there answer with my own variables. first i use fopen to read the text file, then what should i do to convert each line of that text file to a function? for example the func.txt contains:
(x^2)-3y
sin(x+z)+(y^6)
and i need to write an m.file which read func.txt and process that like this:
function func1[x,y] = (x^2)-3y
function func2[x,y,z] = sin(x+z)+(y^6)
Preamble: if your final aim is to use those functions in matlab (i.e. evaluate them for some values of x,y,...), I would rather suggest the following approach that looks more robust to me.
In principle, in fact, you don't need to manipulate the file funct.txt to evaluate the functions defined therein.
First problem: each line of your file funct.txt must define an inline function.
Say that the first function (i.e., the first line) of the file funct.txt has been copied into a string str,
str = '(x^2)-3y',
you can obtain a function from it using the command inline:
f1 = inline(str,'x','y');
which gives to you (matlab output)
f1 =
Inline function:
f1(x,y) = (x^2)-3y.
Now you can use f1 just calling it as f1(x,y), for whatever values x,y.
Second problem: you have to parse your file funct.txt to obtain the strings str containing the definitions of your functions. That's easier, you may want to consider the function fgets.
Third problem: the functions in funct.txt may depend on 2,3 (or more?) independent variables. As far as I know there is no easy way to parse the string to discover it. Thus, you may want to define each inline function as depending on all your independent variables, i.e.
f1 = inline('(x^2)-3y','x','y','z');
the variable z will play no active role, by the way. Nonetheless, you need to specify a third dummy parameter when you call f1.