Is there any way in Standard ML to make a functor output a structure which has all of the functionality of the passed in structure, plus any new functionality.
In a similar way, is it possible to do multiple ascription? In the case of the above it would be immediately useful because you could ascribe the output of the functor to both the signature of the original structure and another signature which specifies the new functionality.
I understand the implications of doing such a thing, and why it might be a bad idea. Currently I've just been keeping a copy of the passed in structure within the functor output - but this means you have a long chain of "Foo.Bar.func" to access the base functionality.
Thanks
Say I wanted to make a signature for "TestUp". Is there any way to do this without duplicating the contents of the "TEST" into a new signature?
If I understand your question correctly then you are looking for the include keyword, which will include the definition of a previous signature into a new and thus extending the signature with the previous definitions.
signature TEST_EXT =
sig
include TEST
val beep1 : meep -> unit
end
functor TestUp_EXT(T : TEST) : TEST_EXT =
struct
open T
fun localFun s = beep (10, s)
val beep1 = localFun
end
structure Test2_EXT = TestUp_EXT (Test);
Test2_EXT.beep (5, "EXT: Hi");
Test2_EXT.beep1 "Hi";
print (Int.toString (Test2.rand ()) ^ "\n");
(* This will fail as the signature doesn't define this function,
however as seen the function can easily be used within the functor as
expected *)
(* Test2_EXT.localFun "Hi"; *)
You can use open to bring the contents of a structure into the current scope. If used inside another structure (or functor), it'll do what I believe it is you want.
An example can be seen here:
signature TEST =
sig
type meep;
val beep : int * meep -> unit;
end;
structure Test : TEST =
struct
type meep = string
fun beep (0, _) = ()
| beep (n, s) = (print (s^"\n"); beep (n-1, s));
end;
functor TestUp (T : TEST) =
struct
open T
fun rand () = 4
end;
structure Test2 = TestUp (Test);
Test.beep (5, "Hello");
Test2.beep (5, "Hi");
print (
Int.toString (Test2.rand ()) ^ "\n"
);
Related
The TMB objective functions are seemingly defined in one function block that is saved to a <name>.cpp file. Then, after compiling the file, each objective function is accessed by loading with the command dyn.load(dynlib(<name>)).
Is it possible to store more than one objective function in each .cpp file? For example, the following two objective functions are very similar to each other, but at the moment need to be saved to different files:
// TMB Tutorial but with fixed variance
#include <TMB.hpp> // Links in the TMB libraries
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_VECTOR(x); // Data vector transmitted from R
PARAMETER(mu); // Parameter value transmitted from R
Type sigma = 1.0;
Type f; // Declare the "objective function" (neg. log. likelihood)
f = -sum(dnorm(x,mu,sigma,true)); // Use R-style call to normal density
return f;
}
and
// TMB Tutorial
#include <TMB.hpp> // Links in the TMB libraries
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_VECTOR(x); // Data vector transmitted from R
PARAMETER(mu); // Parameter value transmitted from R
PARAMETER(sigma); //
Type f; // Declare the "objective function" (neg. log. likelihood)
f = -sum(dnorm(x,mu,sigma,true)); // Use R-style call to normal density
return f;
}
The "map" argument to MakeADFun() allows you to fix parameters at specific values.
In this example, we only need to compile/load the latter template. First, we'll write the template to a file, compile, and load the resulting DLL.
library(TMB)
file_conn <- file('test.cpp')
writeLines("
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_VECTOR(x);
PARAMETER(mu);
PARAMETER(sigma);
Type f;
f = -sum(dnorm(x,mu,sigma,true));
return f;
}
", file_conn)
close(file_conn)
compile('test.cpp')
dyn.load(dynlib('test'))
We can use the same DLL to fit models with and without a varying sigma.
n <- 100
x <- rnorm(n = n, mean = 0, sd = 1)
f1 <- MakeADFun(data = list(x = x),
parameters = list(mu = 0, sigma = 1),
DLL = 'test')
f2 <- MakeADFun(data = list(x = x),
parameters = list(mu = 0, sigma = 1),
DLL = 'test',
map = list(sigma = factor(NA)))
opt1 <- do.call('optim', f1)
opt2 <- do.call('optim', f2)
When using "map", the specified parameter(s) (sigma in this case) is fixed at the value given in "parameters".
Post-optimization, we do a sanity check -- the mus ought to be nearly identical.
> opt1$par
mu sigma
0.08300554 1.07926521
> opt2$par
mu
0.08300712
Turning random effects on and off is a little more difficult. An example of that is given here, where you can use CppAD::Variable() to check whether to decrement the negative log-likelihood or not.
For unalike objective functions (not subsets of one another), you could pass a DATA_INTEGER or DATA_STRING into the template, e.g. like they did in glmmTMB here, and pick the objective function depending on the value of that DATA_*.
I just wanted to make clear on what #alexforrence meant by
For unalike objective functions (not subsets of one another), you could pass a DATA_INTEGER or DATA_STRING into the template, e.g. like they did in glmmTMB here, and pick the objective function depending on the value of that DATA_*
It turns out there is a code snippet on the TMB github that covers this scenario, which I duplicate here:
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_STRING(model_type);
if (model_type == "model1") {
#include "model1.h"
} else
if (model_type == "model2") {
#include "model2.h"
} else {
error ("Unknown model type")
}
return 0;
}
That is, pass in a string telling the objective function which model to choose, and then include the text for that function stored in a separate .h file.
I'd like to unit test fun1 without calling fun2.
let fun2() =
// Some complex function with lots of dependencies.
1
let fun1() =
fun2() * 2
What the best way to break the dependency between the two functions?
I’ve tried a couple of different ways, but they just add clutter.
Pass fun2 into fun1
let fun1(fun2) =
fun2() * 2
Convert to a class and override
type FunClass() =
abstract member fun2 : unit -> int
default x.fun2() = 1
member x.fun1() =
x.fun2() * 2
type FunClassMock() =
override member x.fun2() = 1
Use stategy pattern
type Fun1Class(fun2Class) =
member x.fun1() =
fun2Class.fun2() * 2
Use a variable
let fun2Imp() =
1
let mutable fun2 = fun2Imp
let fun1() =
fun2() * 2
Is there a cleaner way?
It depends on your usage, but you could do something like this:
let fun2() =
// Some complex function with lots of dependencies.
1
let createFun1 fun2 =
fun () -> fun2() * 2
let fun1 = createFun1 fun2
This is also useful for unit testing since you can test fun1 by simply passing a simple function in for fun2.
It isn't very flexible, but a compiler directive would work.
let fun2() =
#if TESTING
1
#else
// Some complex function with lots of dependencies.
#endif
Defining fun2 in separate modules and opening the needed module is another option.
module Impl =
let fun2() =
// Some complex function with lots of dependencies.
module Testing =
let fun2() = 1
Any way you do it (that I know of, at least) is going to "add clutter". How about something like this?
let fun1() =
fun1_impl(fun2)
let fun1_impl(fun2) =
fun2() * 2
Then, in regular code, use fun1, and in your tests, use fun1_impl.
In all generality, the argument passing seems the cleanest.
The clutter effect might not be tackled technically, but semantically : it comes from the fact that it seems a bit arbitrary, from the lack of meaning attached to "function1".
Is there may be a higher, more coherent, level that might be more meaningful in your code ?
Consider the following code:
module ftwr;
import std.regex;
import std.stdio;
import std.conv;
import std.traits;
S consume (S) (ref S data, Regex ! ( Unqual!(typeof(S.init[0])) ) rg)
{
writeln (typeid(Unqual!(typeof(S.init[0]))));
auto m = match(data, rg);
return m.hit;
}
void main()
{
auto data = "binary large object";
auto rx = regex(".*");
consume (data, rx); // this line is mentioned in the error message
}
Now, I expect the compiler to infer that consume is to be instantiated as
string consume!(string)(string, Regex!(char))
but that doesn't seem to happen. The errors are as follows:
func_template_with_regex.d(24): Error: template ftwr.consume(S) does not match any function template declaration
func_template_with_regex.d(24): Error: template ftwr.consume(S) cannot deduce template function from argument types !()(string,Regex!(char))
and I see that the parameter types are correct... I've tried some variations of function signature, like:
S consume (S) (Regex ! ( Unqual!(typeof(S.init[0])) ) rg, ref S data)
which doesn't compile also (the idea was to change the order of arguments), and
immutable(S)[] consume (S) (Regex ! ( S ) rg, ref immutable(S)[] data)
which compiles and infers the types alright. If I specify the type explicitly in the call, i.e.
consume!string(data, rx);
it also compiles and the debug writeln prints char, just as expected. Am I missing something in the inference rules, or I've just hit a bug in the compiler?
Oh yes:
$ dmd -v
DMD64 D Compiler v2.053
...
I can't say if it's a bug, but here's a workaround which doesn't force you to specify the type or change the order of arguments. Change consume's signature to:
S consume (S, U) (ref S data, Regex!U rg) if (is(U == Unqual!(typeof(S.init[0]))))
I have been tinkering lately with fully integrating continuous testing into my Matlab development cycle and have run across a problem I don't know how to get around. As almost all users know, Matlab kindly hides sub-functions within an M-file from the view of any functions outside that M-file. A toy example can be seen below:
function [things] = myfunc(data)
[stuff] = mysubfunc(data)
things = mean(stuff);
end
I want to perform unit testing on subfunc itself. This is, AFAIK, impossible because I cannot call it from any external function.
I'm currently using Matlab xUnit by Steve Eddins and cannot get around this issue. The easy solution -- splitting subfunc out to its own M-file -- is not acceptable in practice because I will have numerous small functions I want to test and don't want to pollute my filesystem with a separate M-file for each one. What can I do to write and perform easy unit tests without making new files for each function I want to test?
What you need to do in general is get function handles to your subfunctions from within the primary function and pass them outside the function where you can unit test them. One way to do this is to modify your primary function such that, given a particular set of input arguments (i.e. no inputs, some flag value for an argument, etc.), it will return the function handles you need.
For example, you can add a few lines of code to the beginning of your function so that it returns all of the subfunction handles when no input is specified:
function things = myfunc(data)
if nargin == 0 % If data is not specified...
things = {#mysubfunc #myothersubfunc}; % Return a cell array of
% function handles
return % Return from the function
end
% The normal processing for myfunc...
stuff = mysubfunc(data);
things = mean(stuff);
end
function mysubfunc
% One subfunction
end
function myothersubfunc
% Another subfunction
end
Or, if you prefer specifying an input flag (to avoid any confusion associated with accidentally calling the function with no inputs as Jonas mentions in his comment), you could return the subfunction handles when the input argument data is a particular character string. For example, you could change the input checking logic in the above code to this:
if ischar(data) && strcmp(data, '-getSubHandles')
I have a pretty hacky way to do this. Not perfect but at least it's possible.
function [things] = myfunc(data)
global TESTING
if TESTING == 1
unittests()
else
[stuff] = mysubfunc(data);
things = mean(stuff);
end
end
function unittests()
%%Test one
tdata = 1;
assert(mysubfunc(tdata) == 3)
end
function [stuff] = mysubfunc(data)
stuff = data + 1;
end
Then at the prompt this will do the trick:
>> global TESTING; TESTING = 1; myfunc(1)
??? Error using ==> myfunc>unittests at 19
Assertion failed.
Error in ==> myfunc at 6
unittests()
>> TESTING = 0; myfunc(1)
ans =
2
>>
Have you used the new-style classes? You could turn that function in to a static method on a utility class. Then you could either turn the subfunctions in to other static methods, or turn the subfunctions in to local functions to the class, and give the class a static method that returns the handles to them.
classdef fooUtil
methods (Static)
function [things] = myfunc(data)
[stuff] = mysubfunc(data);
things = mean(stuff);
end
function out = getLocalFunctionHandlesForTesting()
onlyAllowThisInsideUnitTest();
out.mysubfunc = #mysubfunc;
out.sub2 = #sub2;
end
end
end
% Functions local to the class
function out = mysubfunc(x)
out = x .* 2; % example dummy logic
end
function sub2()
% ...
end
function onlyAllowThisInsideUnitTest()
%ONLYALLOWTHISINSIDEUNITTEST Make sure prod code does not depend on this encapsulation-breaking feature
isUnitTestRunning = true; % This should actually be some call to xUnit to find out if a test is active
assert(isUnitTestRunning, 'private function handles can only be grabbed for unit testing');
end
If you use the classdef style syntax, all these functions, and any other methods, can all go in a single fooUtil.m file; no filesystem clutter. Or, instead of exposing the private stuff, you could write the test code inside the class.
I think the unit testing purists will say you shouldn't be doing this at all, because you should be testing against the public interface of an object, and if you need to test the subparts they should be factored out to something else that presents them in its public interface. This argues in favor of making them all public static methods and testing directly against them, forgetting about exposing private functions with function handles.
classdef fooUtil
methods (Static)
function [things] = myfunc(data)
[stuff] = fooUtil.mysubfunc(data);
things = mean(stuff);
end
function out = mysubfunc(x)
out = x .* 2; % example dummy logic
end
function sub2()
% ...
end
end
end
I use a method that mirrors the way GUIDE use to generate its entry methods. Granted it's biased towards GUIs...
Foo.m
function varargout=foo(varargin)
if nargin > 1 && ischar(varargin{1}) && ~strncmp( varargin{1},'--',2)
if nargout > 0
varargout = feval( varargin{:} );
else
feval = ( varargout{:} );
else
init();
end
This allows you to do the following
% Calls bar in foo passing 10 and 1
foo('bar', 10, 1)
I have inherited a fairly large codebase, 90% C++, and I need to get up to speed on it quickly. There are hundreds of .cc files in a wide directory tree structure.
It's fairly complex, and has no logging. In order to figure out how some major subsystems work, I want to insert a function call into every function.
E.g., given a .cc file full of stuff like this:
void A::foo(int a, int b) {
// ...
}
void A::bar() {
// ...
}
void B::bleh(const string& in) {
// ...
}
I'd like to get this:
void A::foo(int a, int b) {
LOG(debug) << "A::foo() called.";
// ...
}
void A::bar() {
LOG(debug) << "A::bar() called.";
// ...
}
void B::bleh(const string& in) {
LOG(debug) << "B::bleh() called.";
// ...
}
This can be done via python script, CMD script, power shell script, etc. If there is a way to make VS do it, great. Whatever works. Doesn't have to be pretty, I'm not checking any of this in.
Also, it doesn't necessarily need to get everything. E.g. nested classes, implementations in header files, etc.
Had something similar for adding profiling code using Macros in VS, here's the code
(this also groups everything under a single "undo" command and lists all of the changes in its own output window)
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module Module1
Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
Dim window As Window
Dim outputWindow As OutputWindow
Dim outputWindowPane As OutputWindowPane
window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
If show Then window.Visible = True
outputWindow = window.Object
Try
outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
End Try
outputWindowPane.Activate()
Return outputWindowPane
End Function
Const ToInsert As String = "/* Inserted text :D */"
Sub AddProfilingToFunction(ByVal func As CodeFunction2)
Dim editPoint As EditPoint2 = func.StartPoint.CreateEditPoint()
While editPoint.GetText(1) <> "{"
editPoint.CharRight()
End While
editPoint.CharRight()
editPoint.InsertNewLine(1)
Dim insertStartLine As Integer = editPoint.Line
Dim insertStartChar As Integer = editPoint.LineCharOffset
editPoint.Insert(ToInsert)
GetOutputWindowPane("Macro Inserted Code").OutputString( _
editPoint.Parent.Parent.FullName & _
"(" & insertStartLine & "," & insertStartChar & _
") : Inserted Code """ & ToInsert & """" & vbCrLf)
End Sub
Sub AddProfilingToProject(ByVal proj As Project)
If Not proj.CodeModel() Is Nothing Then
Dim EventTitle As String = "Add Profiling to project '" & proj.Name & "'"
GetOutputWindowPane("Macro Inserted Code").OutputString("Add Profiling to project '" & proj.Name & "'" & vbCrLf)
DTE.UndoContext.Open(EventTitle)
Try
Dim allNames As String = ""
For i As Integer = 1 To proj.CodeModel().CodeElements.Count()
If proj.CodeModel().CodeElements.Item(i).Kind = vsCMElement.vsCMElementFunction Then
AddProfilingToFunction(proj.CodeModel().CodeElements.Item(i))
End If
Next
Finally
DTE.UndoContext.Close()
End Try
GetOutputWindowPane("Macro Inserted Code").OutputString(vbCrLf)
End If
End Sub
Sub AddProfilingToSolution()
GetOutputWindowPane("Macro Inserted Code").Clear()
If Not DTE.Solution Is Nothing And DTE.Solution.IsOpen() Then
For i As Integer = 1 To DTE.Solution.Projects.Count()
AddProfilingToProject(DTE.Solution.Projects.Item(i))
Next
End If
End Sub
End Module
P.S
Remember to change the "Const ToInsert As String = ..." to the code you actually want to be inserted
Since you're using Visual C++, and it seems you only need the name of the function called, it might be possible to automate this further using the following command-line switches to cl.exe:
/Gh: Enable _penter function call
/GH: Enable _pexit function call
Basically, providing these switches means that the compiler will automatically inject calls to functions named _penter() and _pexit() whenever any function begins or ends. You can then provide a separately-compiled module that implements these two functions, which either (a) calls some helper library such as DbgHelp to determine the name of the called function, or (b) just grabs the return address from the stack and prints it verbatim -- afterwards, write a script to transform these addresses into function names by looking at e.g. the linker map file produced if you pass /link /MAP:mymapfile.txt to cl.exe.
Of course, you'll need to put your _penter() and _pexit() in a separate module with /Gh and /GH turned off to avoid infinite recursion! :)
I did it some years ago in VS.
Regex will help you.
BTW, it not nesasary to insert different string. You can add the same string like:
LOG(debug) << __FUNCTION__ << " called.";
EDIT
something like this regexp (valid for VS only):
(void|char|int):b+:i\:\::i\([^(]*\):b*\{
You should extend the regexp depending of your needs.
A run-time profiler will kind of give you that information: it will tell what subroutines were called from each routine, and how many times (but not, in what sequence).
Have you considered running the code within a debugger, and simply stepping through the entire application (or otherwise setting a breakpoint on the code you're interested in and just stepping through that)? I find that to sometimes be a useful technique when faced with a large legacy code base that I didn't write.
Alternatively, if you're compiling in the VS world, consider taking a look at the /Gh and
/GH switches to cl.exe. They seem to allow you to hook function entry/exit and call some other routine. I've never used them before, but they seem to directly address your need.