Drasil icon indicating copy to clipboard operation
Drasil copied to clipboard

Specifying size of `Space`'s `Vector`, `Matrix`, and `Array`

Open balacij opened this issue 2 years ago • 11 comments

Related to #3093

When I think of a "vector", I typically think of something with a known, static, length. Do we want to add vector length to the type signature of vectors?

Of course, we might have an area that creates vectors of a desired length, which might mean that we deal with vectors of unknown length at compiled-artifact-runtime.

EDIT: "we need to make sure that both vectors and matrices can have a static or dynamic size." (from this comment)

EDIT 2: this should also be done for arrays (see this comment)

balacij avatar Nov 25 '22 08:11 balacij

Yes, the difficulty here is that we might not know it statically.

What we could do is to have vectors know their length, but have typechecking only error out if it gets things like "2 == 3" (i.e. static dimensions clash) and pass through anything that contains symbols. [We could put out diagnostic information, on demand, on what correctness conditions we're assuming are true]

JacquesCarette avatar Nov 25 '22 16:11 JacquesCarette

I agree that we won't always know the vector length at specification time. Although we will know the dimension of a force vector, position vector etc in a given space, there are cases where the length won't be known until run time. For instance, if we build a sequence of temperatures over a time discretization (t0, t1, t2, ...), the number of discrete values may be an input that we don't know until run time. I'm sure you could use tricks with staging to know this information before the code is executed, but in a regular scientific program, the step size wouldn't be fixed at the requirements (SRS) stage.

I like the idea of checking the size if we know the size but allowing for cases where we don't know it yet. Seems like a nice and clean solution to me.

smiths avatar Nov 25 '22 16:11 smiths

Perfect! I will try to play around with this, but it shouldn't be blocking for #3093.

balacij avatar Nov 25 '22 21:11 balacij

What's the status of this? It seems to be blocking samm82#44. If we constrain the dimensions to integers, the type for compound could be represented as $\mathbb{N}^{118}$; otherwise, if we make it more flexible, it could be represented as $\mathbb{N}^{|E|}$.

samm82 avatar May 29 '23 18:05 samm82

I like $\mathbb{N}^{|E|}$ better. It is self-documenting because the reader doesn't need to figure out where the 118 came from. Also, if they discover a new element, it is still correct. :smile:

smiths avatar May 29 '23 18:05 smiths

No change since the above discussion, unfortunately, @samm82.

balacij avatar May 29 '23 18:05 balacij

No worries! I might take a crack at it then 🤙🏼

samm82 avatar May 29 '23 18:05 samm82

Just a note that I've been working in this on the lengthForVectMat branch, testing it on my fork in the compDefAsVector branch. I'm able to store the dimension of a vector as an integer, but would like to store it as an expression ($|E|$)

image

samm82 avatar Jul 18 '23 15:07 samm82

Can you show me (maybe by linking lines in a comment here) how you're going about the implementation? I have ideas on how to potentially do this.

JacquesCarette avatar Jul 19 '23 17:07 JacquesCarette

Note that I haven't touched Array yet

This is how I've implemented the dimensions for the spaces themselves:

https://github.com/JacquesCarette/Drasil/blob/5ef55a46ccc6eb90f7805f00a5802b6be4bf4b34/code/drasil-lang/lib/Language/Drasil/Space.hs#L39-L41

and here's how I've implemented printing their dimensions in the SRS:

https://github.com/JacquesCarette/Drasil/blob/5ef55a46ccc6eb90f7805f00a5802b6be4bf4b34/code/drasil-printers/lib/Language/Drasil/Printing/Import/Space.hs#L23-L27

I realize this is an imperfect solution (what if we want a 3D matrix?) but figured it was a good place to start. My next step was to use Exprs to define these dimensions in general terms (as discussed above), but I'm wondering if we should be looking at the code generation side of things first (especially in light of #3504).

samm82 avatar Jul 19 '23 21:07 samm82

Let's stick to 1D and 2D for now.

I would be tempted to do

data StagedInt = Static Int | Symbolic Expr

and use Maybe StagedInt. That would let you have 'symbolic' dimensions that could be typechecked to represent an Int.

That's just complex enough that you might not want to inline the printing put have a separate function.

JacquesCarette avatar Jul 20 '23 18:07 JacquesCarette