VDM to Isabelle translation - modeling

I am trying to translate a VDM model to Isabelle, but for some reason, what
I do, do not work
The following sample is a VDM function of my model
Dot_Move_invariant: Dot * Dot -> bool
Dot_Move_invariant(d1, d2) ==
d1 < d2 and
let coordinate_1 = Dot_Coord(d1) in
let coordinate_2 = Dot_Coord(d2) in
moving_coordinates_invariant(coordinate_1, coordinate_2);
And the following sample represents my attempt to translate it
definition
Dot_Move_invariant:: "Dot⇒Dot⇒𝔹"
where "Dot_Move_invariant d1 d2 ≡ d1 < d2 ∧ let x = (SOME y. y ∈ Dot_Coord d1) in x ∧ let y = (SOME x. x ∈ Dot_Coord d2 ) "
The error I get is: Inner syntax error⌂
Failed to parse prop

It might be following:
"Dot_Move_invariant d1 d2 ≡ d1 < d2 ∧ let x = (SOME y. y ∈ Dot_Coord d1) in x ∧ let y = (SOME x. x ∈ Dot_Coord d2 ) in y"

Related

Multiplying elements in custom lists in Haskell

I am trying to implement a custom multiplication operation for my custom list data type in Haskell that uses an Int and [Int].
Int is used to reduce integers by modulus division I will refer to as d.
[Int] represents the content of the list
Let's say a and b are two lists that have the same d.
The length of a is w and the length of b is v
The c = a*b is:
c[k] = a[0] * b[k] + a[1] * b[k - 1] + a[2] * b[k - 2] + · · · + a[k] * b[0]
At the end, c[k] is reduced by mod d.
The length of c = w + v - 1
Meaning the index k in c[k] can be larger than the lengths of w and v.
To deal with this I concatenate a list of 0 elements for indices outside the bounds of the original list.
To clarify:
c[0] = (a[0] * b[0]) % d
c[1] = (a[0] * b[1] + a[1] * b[0]) % d
c[2] = (a[0] * b[2] + a[1] * b[1] + a[2] * b[0]) % d
.
.
.
c[w + v - 1]
For example, a = [3,2,4] and b = [7,9,7,2], both have d = 31.
In the code when they are being multiplied they are [3,2,4,0,0,0] and [7,9,7,2,0,0]
In this example, c = a * b = [21, 10, 5, 25, 1, 8]
This is my code:
module Custom where
data CustomList = CustomList Int [Int]
instance Num CustomList where
(CustomList a1 b1) * (CustomList a2 b2) =
if length b1 >= 1 && length b2 >= 1 then do
let llen = (length b1) + (length b2) - 1
--concatenating a list of 0 elements for indices outside the bounds of the original list.
let sub_b1 = llen - (length b1)
let sub_b2 = llen - (length b2)
let zeros_b1 = map (0*) [1..sub_b1]
let zeros_b2 = map (0*) [1..sub_b2]
--matching list lengths
let new_b1 = b1++zeros_b1
let new_b2 = b2++zeros_b2
--trying to mimic a nested for loop
let ans = [ (new_b1 !! x) * (new_b2 !! y) | x <- [0..llen-1], y <- (reverse [0..x]) ]
CustomList (a1) (map (`mod` (a1)) ans)
else do
0
instance Show CustomList where
show (CustomList a b) = "output: " ++ (show b) ++ "\nlength: " ++ (show a)
Output:
*Custom> let a = CustomList 31 [3,2,4]
*Custom> let b = CustomList 31 [7,9,7,2]
Incorrect (What I get)
*Custom> a * b
output: [21,18,14,28,5,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
length: 31
Correct (What I should get)
output: [21, 10, 5, 25, 1, 8]
length: 6
I realize issues in my logic:
The x counter, I need to start from a[0] and end at a[k] for all c[k] calculations but I am starting at a[x].
Answers are not being summed together. For instance, instead of obtaining c[1] = a[0] * b[1] + a[1] * b[0], I obtain c[1] = a[0] * b[1] & c[2] = a[1] * b[0]
I am not sure how to fix it, I have been trying and end up just creating new problems by trying to do.
I am a newbie at Haskell so I would prefer a simple readable way of solving this problem over the more "Haskell" way of doing it.
But any help is appreciated, thanks in advance.
Nice and simple:
data CustomList = CustomList Int [Int] deriving(Show)
instance Num CustomList where
CustomList a1 b1 * CustomList a2 b2 = CustomList a1 (map (`mod` a1) ans)
where ans = map getAnsElem [1..length b1 + length b2 - 1]
getAnsElem k = sum $ zipWith (*) (withLength k b1) (reverse $ withLength k b2)
withLength n xs = take n (xs ++ repeat 0)
Testing it:
λ> CustomList 31 [3,2,4] * CustomList 31 [7,9,7,2]
CustomList 31 [21,10,5,25,1,8]
Explanation:
withLength takes a list and makes it a given length, either by truncating it if it's too long, or padding with zeros if it's too short
zipWith takes 2 lists and goes over them in parallel, using the given function to combine the elements
One reason your approach with a list comprehension failed is because [f x y | x <- xs, y <- ys] takes the Cartesian product of xs and ys instead of zipping them. If you wanted to use a list comprehension instead, you could, but you'd need the ParallelListComp extension, in which case you'd have this:
getAnsElem k = sum [x * y | x <- withLength k b1 | y <- reverse $ withLength k b2]
Note the second | instead of a ,: this is what denotes zipping.

Is this an accurate example of a Haskell Pullback?

I'm still trying to grasp an intuition of pullbacks (from category theory), limits, and universal properties, and I'm not quite catching their usefulness, so maybe you could help shed some insight on that as well as verifying my trivial example?
The following is intentionally verbose, the pullback should be (p, p1, p2), and (q, q1, q2) is one example of a non-universal object to "test" the pullback against to see if things commute properly.
-- MY DIAGRAM, A -> B <- C
type A = Int
type C = Bool
type B = (A, C)
f :: A -> B
f x = (x, True)
g :: C -> B
g x = (1, x)
-- PULLBACK, (p, p1, p2)
type PL = Int
type PR = Bool
type P = (PL, PR)
p = (1, True) :: P
p1 = fst
p2 = snd
-- (g . p2) p == (f . p1) p
-- TEST CASE
type QL = Int
type QR = Bool
type Q = (QL, QR)
q = (152, False) :: Q
q1 :: Q -> A
q1 = ((+) 1) . fst
q2 :: Q -> C
q2 = ((||) True) . snd
u :: Q -> P
u (_, _) = (1, True)
-- (p2 . u == q2) && (p1 . u = q1)
I was just trying to come up with an example that fit the definition, but it doesn't seem particularly useful. When would I "look for" a pull back, or use one?
I'm not sure Haskell functions are the best context
in which to talk about pull-backs.
The pull-back of A -> B and C -> B can be identified with a subset of A x C,
and subset relationships are not directly expressible in Haskell's
type system. In your specific example the pull-back would be
the single element (1, True) because x = 1 and b = True are
the only values for which f(x) = g(b).
Some good "practical" examples of pull-backs may be found
starting on page 41 of Category Theory for Scientists
by David I. Spivak.
Relational joins are the archetypal example of pull-backs
which occur in computer science. The query:
SELECT ...
FROM A, B
WHERE A.x = B.y
selects pairs of rows (a,b) where a is a row from table A
and b is a row from table B and where some function of a
equals some other function of b. In this case the functions
being pulled back are f(a) = a.x and g(b) = b.y.
Another interesting example of a pullback is type unification in type inference. You get type constraints from several places where a variable is used, and you want to find the tightest unifying constraint. I mention this example in my blog.

What GHC optimization is responsible for duplicating case expressions?

Given the following code:
{-# OPTIONS_GHC -funbox-strict-fields #-}
module Test where
data X = X !Int !Int
test (X a b) (X c d) = X (max a c) (max b d)
GHC generates this core when compiling with optimizations (renamed to make reading easier):
test
test =
\ u v ->
case u of x { X y z ->
case v of c { X d e ->
case tagToEnum# (<=# y d) of _ {
False ->
case tagToEnum# (<=# z e) of _ {
False -> x;
True -> X y e
};
True ->
case tagToEnum# (<=# z e) of _ {
False -> X d z;
True -> c
}
}
}
}
Note how GHC has generated in total 4 different code paths. In general, the number of code paths grows exponentially with the number of conditions.
What GHC optimization leads to that behavior? Is there a flag to control this optimization? In my case, this generates huge code bloat, and makes core dumps very hard to read because of deeply nested case expressions.
After some research, I've found that the optimization responsible for this is the so called "case-of-case" transformation, that GHC does presumably in the simplifier, so it cannot be deactivated (since it is necessary for a lot of what GHC does and the simplifier is an integral part of GHC's optimization pipeline).
The following link explains how case of case leads to the duplication: http://lambda.jstolarek.com/2013/01/taking-magic-out-of-ghc-or-tracing-compilation-by-transformation/
In particular, case-of-case turns this:
case (
case C of
B1 -> F1
B2 -> F2
) of
A1 -> E1
A2 -> E2
into the following:
case C of
B1 -> case F1 of
A1 -> E1
A2 -> E2
B2 -> case F2 of
A1 -> E1
A2 -> E2
where the outer case has been duplicated and pushed into the branches.

Epipolar line from epipole and image point

I have to determine the epipolar line using this model:
I read some books and the Wikipedia-article. But I cannot figure out, what this means:
l2 = e2 x x2
Where l2 ist the epipolar line in the right/2nd image (red line) and x2 is the image point in the right image of the object x.
My problem: As I assume, the point e2 and x2 are in the right image plane, which means, that the cross product of them (in the formula l2) is perpendicular to the image plane and hence cannot be in the image plane, as the red line is.
What am I understanding wrong?
You can write the equation of a straight line in 2D,
ax + by + c = 0
as a dot product
l . x = 0
where l = [a b c]' is the line and x = [x y 1]' is a point on the line.
So, l and x are orthogonal.
In the second image, both e2 and x2 should lie on the epipolar line l2, meaning
l2 . e2 = 0, l2 . x2 = 0
So, l2 is orthogonal to both e2 and x2. You can find a vector that is orthogonal to both e2 and x2 by taking their cross product. Therefore, we can say l2 = e2 x x2.
You can see that l2 . e2, and l2 . x2 are indeed 0, using triple product properties.
l2 . e2 = e2 . l2 = e2 . (e2 x x2) = x2 . (e2 x e2) = 0
You should not look at the points as 2D points in the image that are crossed, that's the source of your confusion.
The cross is defined using 3D vectors such that
|e2x| |x2x|
l2 = |e2y| X |x2y|
| 1 | | 1 |
and after you get the resulting l2 (3D vector), you should normalize it so that
l2x^2+l2y^2=1

How to numerically calculate value of complex function given derivative of this function in Haskell?

Given:
Haskell
Complex-valued function df/dz defined on complex plane U (let's say z is a Complex Double).
Point z1 from the U on which df/dz is defined.
Question:
How to get value of function f(z) for which df/dz is a derivative, in point z1?
I. e. how to restore value of original function given only it's derivative, assuming complex plane?
This question is somewhat related to my previous question about calculating integrals of complex functions, but they are about different things. Here I am interested not in calculating some scalar value, but in finding the origin function given it's derivative. It's essentially calculating the indefinite integral of this derivative.
(Runge–Kutta in Haskell)
You can use some numeric solver like Runge-Kutta
-- define 4th order Runge-Kutta map (RK4)
rk4 :: Floating a => (a -> a) -> a -> a -> a
rk4 f h x = x + (1/6) * (k1 + 2*k2 + 2*k3 + k4)
where k1 = h * f (x)
k2 = h * f (x + 0.5*k1)
k3 = h * f (x + 0.5*k2)
k4 = h * f (x + k3)
in that case function signature is Floating but you can use RealFloat instead (you can use runge-kutta in complex).
Complete example:
Prelude> import Data.Complex
Prelude Data.Complex> let rk4 f h x = x + (1/6) * (k1 + 2*k2 + 2*k3 + k4) where {k1 = h * f(x);k2 = h * f (x + 0.5*k1);k3 = h * f (x + 0.5*k2);k4 = h * f (x + k3)}
Prelude Data.Complex> let f z = 2 * z
Prelude Data.Complex> rk4 f (0.1 :+ 0.2) (0.3 :+ 1.2)
(-0.2334199999999999) :+ 1.4925599999999999
Prelude Data.Complex>
On the other hand, #leftaroundabout suggest extend that behavior to VectorSpace (great! of course! :D )

Resources