Resolving ATS type error - ats

Preface this with: learning ATS from the ATS web site.
When trying to pull a record out of a list, once I try to reference the record variable I get a type error on x4.a below.
So why does this fail - wouldn't x4 know it has a record?
typedef
abc1_rec = '{a=int, b=int, c=char}
typedef
abc2_rec = '{a=int, b=char, c=string}
val x1 = '{a=1,b=3,c='A'} : abc1_rec // boxed record
val x2 = '{a=1,b='B',c="CAT"} : abc2_rec // boxed record
val a1 = x2.a // =1
val x3 = (x2 :: x1 :: list_nil()) // concat list x2 + x1 + nil
val x4 = x3.head() // record x2
val x5 = x4.a // error - [a] cannot be found
/tmp/patsopt_tcats_lu0auT: 1440(line=77, offs=12) -- 1442(line=77,
offs=14): error(3): [a] cannot be found: the type [S2EVar(779)] is
expected to be a tyrec(record). patsopt(TRANS3): there are [1] errors
in total. exit(ATS): uncaught exception:
_2home_2hwxi_2Research_2ATS_2dPostiats_2src_2pats_error_2esats__FatalErrorExn(1025)

Note x1 and x2 are of different types. They cannot really be put into the
same list. In this case, the type for x3 is list(X, 2) for some T that is assumed to be a subtype of both abc1_rec and abc2_rec; x4 is of the type T, and x4.a gives you a type-error because the typechecker cannot infer that T is a record type.

Related

How do I solve this? Microsoft VBScript runtime error '800a0006' Overflow /Variable.asp, line 27

Here's my Code. I'm making a website that solves for a quadratic formula for my requirement. And it requires to call at least three variables from a form.
<form action="Variable.asp" id="arrays" method="get">
<label for="category">A: </label><br>
<input name="aname" id="aname"><br>
<label for="category">B: </label><br>
<input name="bname" id="bname"><br>
<label for="category">C: </label><br>
<input name="cname" id="cname"><br>
<input type="submit">
</form>
<%
Function solvex1(a,b,c)
x1 = (b * b)
x1 = x1 - (4 * (a * c))
x1 = Sqr(x1)
x1 = -b + x1
x1 = x1 / (2 * a)
x1 = CInt(x1)
x1 = x1
End Function
Function solvex2(a,b,c)
x2 = (b * b)
x2 = x2 - (4 * (a * c))
x2 = Sqr(x2)
x2 = -b - x2
x2 = x2 / (2 * a)
x2 = CInt(x2)
x2 = x2
End Function
Response.Write("Here is your answer: <br>")
Response.Write("X1 = ")
Response.Write(solvex1(Request.Form("aname"), Request.Form("bname"), Request.Form("cname")) & "<br>")
Response.Write("X2 = ")
Response.Write(solvex2(Request.Form("aname"), Request.Form("bname"), Request.Form("cname")) & "<br>")
%>
The main issue is you don’t return anything from your function calls, but you expect to output their value in a Response.Write() call.
Try changing the function definitions to show you want to return the value. Here is the solvex1() function rewritten to return the value of x1:
Function solvex1(a,b,c)
Dim x1
x1 = (b * b)
x1 = x1 - (4 * (a * c))
x1 = Sqr(x1)
x1 = -b + x1
x1 = x1 / (2 * a)
x1 = CInt(x1)
x1 = x1
'Return the value from the function.
solvex1 = x1
End Function
To return a value from a function assign it to the name of the function.
Also, would highly recommend checking the values you pass into the function are valid numeric values before any mathematical calculations are performed.
Useful Links
Return value from a VBScript function
How to indicate if string has numeric value

Semi continuous section in LP format file provided to cbc crashes

I am using version 2.9.9 of cbc in ubuntu 17.10 docker image. My test.lp file has following content:
Maximize
obj: x1 + 2 x2 + 3 x3 + x4
Subject To
c1: - x1 + x2 + x3 + 10 x4 <= 20
c2: x1 - 3 x2 + x3 <= 30
c3: x2 - 3.5 x4 = 0
Bounds
0 <= x1 <= 40
2 <= x4 <= 3
General
x4
Semis
x1 x2 x3
When trying with semis section i get error "terminate called after throwing an instance of 'CoinError?' Aborted"
on mac i get: libc++abi.dylib: terminating with uncaught exception of type CoinError? Abort trap: 6
However if I comment out Semis it works fine. I was hoping that Semis are supported. Am I doing something wrong?
My command is : cbc -presolve on -import test.lp solve solu out.txt
On further analysis i found out when in cbc prompt i type "import test.lp" it fails and shows same error is
The CBC MPS file reader seems not to accept SC bounds either. I think CBC actually supports semi-continuous variables (I tested with a small GAMS model) but it seems difficult to pass it on in an LP or MPS file. As a work around, I would suggest to use binary variables to model semi-continuous behavior:
b * L ≤ x ≤ b * U
b in {0,1}

dsolve throwing an error while trying to solve system of ODEs

syms X1(t) X2(t) X3(t) X4(t) X5(t) X6(t) X7(t) X8(t) X9(t) X10(t)
X = [X1; X2; X3; X4; X5; X6; X7; X8; X9; X10];
X0 = X_0;
eqn = diff(X) == -1*A*X;
[X1Sol(t) X2Sol(t) X3Sol(t) X4Sol(t) X5Sol(t) X6Sol(t) X7Sol(t) X8Sol(t) X9Sol(t) X10Sol(t) ] = dsolve(eqn, X0);
Where A is some 10x10 Matrix and X_0 is a column vector with 10 elements whose values are calculated beforehand. When I compile this code Matlab throws me the following error:
Error using mupadengine/feval (line 157) MuPAD error: Error: Invalid
equations. [ode::new]
Error in dsolve>mupadDsolve (line 325) T =
feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 186) sol = mupadDsolve(args, options);
Can anyone tell me what is going on and what is the rectification? I use Matlab R2012a.
I fail to find any error when include X_0 and A in sysms
syms X1(t) X2(t) X3(t) X4(t) X5(t) X6(t) X7(t) X8(t) X9(t) X10(t) X_0 A
X = [X1; X2; X3; X4; X5; X6; X7; X8; X9; X10];
X0 = X_0;
eqn = diff(X) == -1*A*X;
[X1Sol(t) X2Sol(t) X3Sol(t) X4Sol(t) X5Sol(t) X6Sol(t) X7Sol(t) X8Sol(t) X9Sol(t) X10Sol(t) ] = dsolve(eqn,X0)

Print GLPK objective/constraints in human readable format

I'm using GLPK C API for a mixed integer programming problem. Is there some way to print the objective/constraints in human readable format for debugging?
Perhaps the nicest format is the CPLEX LP format. It looks something like this:
Maximize
obj: x1 + 2 x2 + 3 x3 + x4
Subject To
c1: - x1 + x2 + x3 + 10 x4 <= 20
c2: x1 - 3 x2 + x3 <= 30
c3: x2 - 3.5 x4 = 0
Bounds
0 <= x1 <= 40
2 <= x4 <= 3
General
x4
End
You can write your model in this format by calling:
int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname);
See also glp_write_lp — write problem data in CPLEX LP format in the documentation that comes with GLPK.

performance of static member constraint functions

I'm trying to learn static member constraints in F#. From reading Tomas Petricek's blog post, I understand that writing an inline function that "uses only operations that are themselves written using static member constraints" will make my function work correctly for all numeric types that satisfy those constraints. This question indicates that inline works somewhat similarly to c++ templates, so I wasn't expecting any performance difference between these two functions:
let MultiplyTyped (A : double[,]) (B : double[,]) =
let rA, cA = (Array2D.length1 A) - 1, (Array2D.length2 A) - 1
let cB = (Array2D.length2 B) - 1
let C = Array2D.zeroCreate<double> (Array2D.length1 A) (Array2D.length2 B)
for i = 0 to rA do
for k = 0 to cA do
for j = 0 to cB do
C.[i,j] <- C.[i,j] + A.[i,k] * B.[k,j]
C
let inline MultiplyGeneric (A : 'T[,]) (B : 'T[,]) =
let rA, cA = Array2D.length1 A - 1, Array2D.length2 A - 1
let cB = Array2D.length2 B - 1
let C = Array2D.zeroCreate<'T> (Array2D.length1 A) (Array2D.length2 B)
for i = 0 to rA do
for k = 0 to cA do
for j = 0 to cB do
C.[i,j] <- C.[i,j] + A.[i,k] * B.[k,j]
C
Nevertheless, to multiply two 1024 x 1024 matrixes, MultiplyTyped completes in an average of 2550 ms on my machine, whereas MultiplyGeneric takes about 5150 ms. I originally thought that zeroCreate was at fault in the generic version, but changing that line to the one below didn't make a difference.
let C = Array2D.init<'T> (Array2D.length1 A) (Array2D.length2 B) (fun i j -> LanguagePrimitives.GenericZero)
Is there something I'm missing here to make MultiplyGeneric perform the same as MultiplyTyped? Or is this expected?
edit: I should mention that this is VS2010, F# 2.0, Win7 64bit, release build. Platform target is x64 (to test larger matrices) - this makes a difference: x86 produces similar results for the two functions.
Bonus question: the type inferred for MultiplyGeneric is the following:
val inline MultiplyGeneric :
^T [,] -> ^T [,] -> ^T [,]
when ( ^T or ^a) : (static member ( + ) : ^T * ^a -> ^T) and
^T : (static member ( * ) : ^T * ^T -> ^a)
Where does the ^a type come from?
edit 2: here's my testing code:
let r = new System.Random()
let A = Array2D.init 1024 1024 (fun i j -> r.NextDouble())
let B = Array2D.init 1024 1024 (fun i j -> r.NextDouble())
let test f =
let sw = System.Diagnostics.Stopwatch.StartNew()
f() |> ignore
sw.Stop()
printfn "%A" sw.ElapsedMilliseconds
for i = 1 to 5 do
test (fun () -> MultiplyTyped A B)
for i = 1 to 5 do
test (fun () -> MultiplyGeneric A B)
Good question. I'll answer the easy part first: the ^a is just part of the natural generalization process. Imagine you had a type like this:
type T = | T with
static member (+)(T, i:int) = T
static member (*)(T, T) = 0
Then you can still use your MultiplyGeneric function with arrays of this type: multiplying elements of A and B will give you ints, but that's okay because you can still add them to elements of C and get back values of type T to store back into C.
As to your performance question, I'm afraid I don't have a great explanation. Your basic understanding is right - using MultiplyGeneric with double[,] arguments should be equivalent to using MultiplyTyped. If you use ildasm to look at the IL the compiler generates for the following F# code:
let arr = Array2D.zeroCreate 1024 1024
let f1 = MultiplyTyped arr
let f2 = MultiplyGeneric arr
let timer = System.Diagnostics.Stopwatch()
timer.Start()
f1 arr |> ignore
printfn "%A" timer.Elapsed
timer.Restart()
f2 arr |> ignore
printfn "%A" timer.Elapsed
then you can see that the compiler really does generate identical code for each of them, putting the inlined code for MultipyGeneric into an internal static function. The only difference that I see in the generated code is in the names of locals, and when running from the command line I get roughly equal elapsed times. However, running from FSI I see a difference similar to what you've reported.
It's not clear to me why this would be. As I see it there are two possibilities:
FSI's code generation may be doing something slightly different than the static compiler
The CLR's JIT compiler may be treat code generated at runtime slightly differently from compiled code. For instance, as I mentioned my code above using MultiplyGeneric actually results in an internal method that contains the inlined body. Perhaps the CLR's JIT handles the difference between public and internal methods differently when they are generated at runtime than when they are in statically compiled code.
I'd like to see your benchmarks. I don't get the same results (VS 2012 F# 3.0 Win 7 64-bit).
let m = Array2D.init 1024 1024 (fun i j -> float i * float j)
let test f =
let sw = System.Diagnostics.Stopwatch.StartNew()
f() |> ignore
sw.Stop()
printfn "%A" sw.Elapsed
test (fun () -> MultiplyTyped m m)
> 00:00:09.6013188
test (fun () -> MultiplyGeneric m m)
> 00:00:09.1686885
Decompiling with Reflector, the functions look identical.
Regarding your last question, the least restrictive constraint is inferred. In this line
C.[i,j] <- C.[i,j] + A.[i,k] * B.[k,j]
because the result type of A.[i,k] * B.[k,j] is unspecified, and is passed immediately to (+), an extra type could be involved. If you want to tighten the constraint you can replace that line with
let temp : 'T = A.[i,k] * B.[k,j]
C.[i,j] <- C.[i,j] + temp
That will change the signature to
val inline MultiplyGeneric :
A: ^T [,] -> B: ^T [,] -> ^T [,]
when ^T : (static member ( * ) : ^T * ^T -> ^T) and
^T : (static member ( + ) : ^T * ^T -> ^T)
EDIT
Using your test, here's the output:
//MultiplyTyped
00:00:09.9904615
00:00:09.5489653
00:00:10.0562346
00:00:09.7023183
00:00:09.5123992
//MultiplyGeneric
00:00:09.1320273
00:00:08.8195283
00:00:08.8523408
00:00:09.2496603
00:00:09.2950196
Here's the same test on ideone (with a few minor changes to stay within the time limit: 512x512 matrix and one test iteration). It runs F# 2.0 and produced similar results.

Resources