Imported function lacks method defined in another module - import

I have a module A that describes a framework. Then, a couple of modules (here just B) that implement the framework. Finally, I want C to use any of the implementations.
However, running the following example:
module A
abstract type Ab end
f(::Ab) = "Not NotImplemented"
export Ab, f
end
module B
using Main.A
struct Bb <: A.Ab end
A.f(::Bb) = "B"
export Bb, f
end
module C
using Main.A
struct Cc
btype :: A.Ab
end
f(model::Cc) = f(model.btype)
function g(model::Cc)
#show f(model)
end
export Cc, g
end
using Main.A, Main.B, Main.C
ex0 = B.Bb()
ex1 = C.Cc(ex0)
g(ex1)
returns an error
ERROR: LoadError: MethodError: no method matching f(::Main.B.Bb)
Closest candidates are:
f(::Main.C.Cc) at C:\Users\tangi\.julia\dev\VariationalInequalitySolver\script.jl:2
and I really don't want to have to import B in the module C to guarantee genericity.

The problem is:
f(model::Cc) = f(model.btype)
in your C module as it creates a new function in module C that has name f.
You need to write:
A.f(model::Cc) = f(model.btype)
which creates a new method for function f from module A.

Related

Julia: ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

I am trying to multiply two array via the Einsum package (uses Meta Programming). I get the following error if I use the #einsum macro with elements of a struct but not if I copy the element beforehand. Can someone explain?
using Einsum
struct MyStruct
a::Array
end
s1 = MyStruct(rand(5, 2))
s2 = MyStruct(rand(6, 2))
# does not work (ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol)
#einsum result[i, j] := s1.a[i, k] * s2.a[j, k]
# does work
s1_a = s1.a
s2_a = s2.a
#einsum result[i, j] := s1_a[i, k] * s2_a[j, k]
The macro #einsum assumes, in the function extractindices which it uses, that the arrays are simple names (i.e. Symbols like s1_a), not expressions like s1.a or function calls or some such thing. It has simply not been written to accomodate indexing of expressions. The package Einsum has not been updated in 4 years, there might be other packages that can achieve this.

Can multiple modules have the same module type? How do I organize them and their interface files?

Currently, I have within the same OCaml file,
blah.ml:
module type blah =
sig
val a : some-type
end
module type X =
sig
val x : some-type
end
module Y : X =
struct
let x = some-def
end
module Z : X =
struct
let x = some-other-def
end
blah.mli looks like this:
module type blah =
sig
val a
end
module type X =
sig
val x : some-type
end
module Y : X
module Z : X
I want X, Y, and Z to be in separate files with separate interfaces. How do I tell in Y.mli and Z.mli that Y and Z have type X?
Any readings for this would also be appreciated. There are a lot of resources talking about modules, interfaces, and functors, but they don't mention interface files for modules that have other modules as types.
You can create x.ml containing the sig, y.ml containing the module, and z.ml containing that module. You don't need to do anything special to tell the compiler that Y : X and Z : X. The compiler infers the module type automatically from the fact that the module conforms to the type i.e. it implements every binding that the module type needs. If Y or Z don't conform, the type error will be shown at the point of use.
If you want to restrict the module type at the point of definition that's also doable, by giving each module an interface file and includeing the required signature there. For example:
(* x.ml *)
module type S = sig
val x : some-type
end
(* y.mli *)
include X.S
(* y.ml *)
let x = some-def
...and so on.
However this often becomes too restrictive as the signature hides too much detail about the types. So in reality you may actually need to add type equality sharing constraints to avoid compile errors where you want to expose more type info. E.g.:
(* y.mli *)
include X.S with type t = int
Often it is easier to not have the explicit interface file at all unless you really need to make some parts of the module private.

how to alias signals from a nested interface in system verilog?

I have a nested interface, something like the pseudo example
interface a();
logic a;
endinterface: a
interface B();
logic b;
a A();
alias b = A.a; // THIS throws an error
endinterface: b
I want to write assertions on interface a from interface B
But it does not allow me to alias the signal. What are other alternatives?
Any suggestions?
Variables and hierarchical references cannot be used in alias statements.
Your alternatives are:
Use assign b = A.a; instead of alias
Just use A.a in your assertion
Declare b using the let construct let b = A.a;
I suggest using the let statement.

using evalin to evaluate a function in the base workspace

I am trying to allow a function to have access to the base workspace using the evalin function, but I am having trouble. Here is a simple example:
My main code:
A = 1;
B = 2
evalin('base','[ C ] = FUN(B)');
C
My Function:
function [C ] = FUN( B )
C = A + B;
end
My error:
Undefined function or variable 'A'.
Error in FUN (line 4)
C = A + B;
Error in Test (line 4)
evalin('base','[ C ] = FUN(B)');
So, the function is not being evaluated in the base workspace because it does not know what the value of A is.
Can anyone suggest something? I have a lot of variables that I need to access in several functions and I don't want to pass them and I don't want to use global variables.
Thanks!
From the evalin documentation,
evalin(ws, expression) executes expression, a string containing any valid MATLABĀ® expression, in the context of the workspace ws. ws can have a value of 'base' or 'caller' to denote the MATLAB base workspace or the workspace of the caller function.
So the line of code
evalin('base','[ C ] = FUN(B)');
evaluates only the line of code
[ C ] = FUN(B)
in the context of the base workspace. It does not evaluate the body of the function within the context of the base workspace. So the error that you are observing makes sense.
Is there a particular reason why you don't want to pass the variables in to the function? Why do you have several variables in the base (?) workspace, or do you just have several variables within a main function?
If the latter, you could use nested functions to have access to the variables declared in the caller (function) workspace. For example, suppose you have a main function like
function main()
A = 1;
B = 2;
C = FUN();
function [C] = FUN()
C = A + B;
end
end
The function FUN has access to both A and B and so you don't have to pass in any arguments.
An alternative to passing in several different inputs, is to just pass in a structure that has different fields that your function can access at will. Using the above example, we could do the following
function main()
A = 1;
B = 2;
data.A = A;
data.B = B;
C = FUN(data);
end
function [C] = FUN(data)
C = data.A + data.B;
end
In this case, the function FUN can be a function within its own file or declared after main. Again, we only pass in one argument that has all the data that the function needed.
Actually Evalin function is used to take data from base workspace:
Syntax is :
evalin('base','variable')
Evalin function is used in the function .
For example see the below function
function [out1 out2 out3]=main_fun(in1,in2)
out1=in1+in2;
out2=in1-in2;
in3=evalin('base','in3');
in4=evalin('base','in4');
out3=in3+in4;
end
Here out3 value will have the sum of in3 and in4 from workspace.
out1 and out2 will have the sum and difference of in1 and in2 from current function workspace.

invoking superclass constructor

I read this documentation page about how to invoke superclass constructor from a child class. The syntax they mention is this:
obj = obj#MySuperClass(SuperClassArguments);
I'm wondering what the purpose of # symbol in the above syntax is. Is the # symbol just a meaningless place occupier in the syntax or does the # symbol represent the function handle symbol in MATLAB?
If I use:
obj = MySuperClass(SuperClassArguments);
instead of
obj = obj#MySuperClass(SuperClassArguments);
it still works fine. So what is the purpose of using # symbol?
1) no this has nothing to do with function handles, this is the syntax used to call the superclass constructor
2) you could try it and see for yourself. Here is an example:
A.m
classdef A < handle
properties
a = 1
end
methods
function obj = A()
disp('A ctor')
end
end
end
B.m
classdef B < A
properties
b = 2
end
methods
function obj = B()
obj = obj#A(); %# correct way
%#obj = A(); %# wrong way
disp('B ctor')
end
end
end
With the correct syntax, we get:
>> b = B()
A ctor
B ctor
b =
B with properties:
b: 2
a: 1
If you use the commented line instead of the first one, you get the following error:
>> clear classes
>> b = B()
A ctor
A ctor
B ctor
When constructing an instance of class 'B', the constructor must preserve the class of the returned
object.
Error in B (line 8)
disp('B ctor')