News for hackers. Stuff that matters.
Re:Futhark by Example by ethanlipson on Saturday May 16, 2026 @06:42AM (#48158922)
Futhark is really such a great idea. I'm not convinced that dependent types are worth the cognitive overhead in general, but it's definitely worth it to include the length as part of the type information for dynamic arrays, e.g.:

  concat(Vec<T, n>, Vec<T, m>) -> Vec<T, n+m>
  matmul(Mat<T, n, m>, Mat<T, m, l>) -> Mat<T, n, l>
  head(Vec<T, n+1>) -> (T, Vec<T, n>)
This would have saved me so much headache debugging CUDA kernels and numpy!! I wish it were a first-class feature in those frameworks, and even general-purpose languages, but alas.
Re:Futhark by Example by VorpalWay on Saturday May 16, 2026 @08:02AM (#48159418)
You can do this with templates in C++ and generics in Rust I'm pretty sure. I think the Eigen C++ library supports this. (I have yet to do a linear algebra heavy Rust project, so I can't speak to the options that exist there.)
Re:Futhark by Example by otabdeveloper4 on Saturday May 16, 2026 @08:20AM (#48159533)
Yeah, C++ arrays are literally that.
Re:Futhark by Example by alpinisme on Saturday May 16, 2026 @08:46AM (#48159773)
Arrays are not dynamically sized though (handling runtime sizes) and don’t have efficient append/concat. The point of the dependent types is that you can have the type system track that concat creates an M+N length vector, sort preserves length (and adds a sorted guarantee that slice preserves), etc. Sure you can do a lot with templates, but that’s advanced templates not just “C++ arrays” in a throwaway “literally that” way.
Re:Futhark by Example by ethanlipson on Saturday May 16, 2026 @09:37AM (#48160179)
I'm talking about cases where the array size is not known at compile time. For example, say the user passes in a list of numbers as command line arguments. Then we have

  argv: Vec<String, argc>
If I want to map these to ints, then I'd like a compile-time guarantee that the resulting array

  nums: Vec<Int, argc>
is the same length as argv. Lean and Idris can do this, but AFAIK no commonly used languages can. But unlike general dependent types, these are not hard to wrap one's head around and would save a lot of frustration, in my experience.
Re:Futhark by Example by itishappy on Saturday May 16, 2026 @10:27AM (#48160560)
Here they are in Futhark:

    val concat [n] [m] 't : (xs: [n]t) -> (ys: [m]t) -> *[n + m]t
    val matmul [n] [m] [l] 't : (xs: [n][m]t) -> (ys: [m][l]t) -> *[n][l]t
    val head [n] 't : (x: [n]t) -> t
And here's the pathological case (length cannot be determined at compile time):

    val filter [n] 'a : (p: a -> bool) -> (as: [n]a) -> *[]a
Other pathological cases include conditionals and loops.
Re:Futhark by Example by ainch on Saturday May 16, 2026 @01:21PM (#48162062)
The Pyrefly type checker is starting to work on this kind of shape hinting - so far it only works on Torch but I believe the plan is for it to work with other array packages (eg. JAX, NumPy)

https://pyrefly.org/en/docs/tensor-shapes/#how-it-works

Re:Futhark by Example by mathisfun123 on Saturday May 16, 2026 @02:17PM (#48162470)
Shape functions and shape analysis are basically mundane infra in almost every ML compiler/language/DSL.

https://mlir.llvm.org/docs/Dialects/ShapeDialect/

Re:Futhark by Example by ainch on Saturday May 16, 2026 @10:16PM (#48165515)
I didn't know that, thanks for sharing. It makes sense, but then it also makes me wonder why none of the deep learning libraries (Torch, Jax/NNX, Eigen etc...) make this information available. Instead, ML people all have their own schemes for tracking shape information, like commenting '# (b, n, t)' on every line, or suffixing shapes to variable names - and in my experience it's a common source of bugs.
Re:Futhark by Example by mathisfun123 on Saturday May 16, 2026 @10:42PM (#48165635)
> Torch, Jax

Both of these "make it available". Just because people don't know how to use/find them doesn't mean they're not "available".

> Eigen

This is not an ML anything, it's a linear algebra library.

> like commenting '# (b, n, t)' on every line, or suffixing shapes to variable names

There's a difference between tracking shapes in the compiler and specifying shapes in the model.

Re:Futhark by Example by ainch on Sunday May 17, 2026 @04:44AM (#48167128)
I think we're talking at cross purposes. The reason I'm excited about the Pyrefly work is that it leverages the type system to infer array shapes statically, which makes it simpler to reason about them when you're writing the code and catch bugs. The fact that people have developed these janky approaches to shape tracking suggests that there's a gap to be filled.

Jax and Torch don't do that statically. They obviously have to do it at runtime, but that doesn't address this particular issue. I mention Eigen because array shape hinting is generally useful for any linalg library, not purely for ML applications.

Re:Futhark by Example by mathisfun123 on Sunday May 17, 2026 @11:24AM (#48169762)
> Jax and Torch don't do that statically. They obviously have to do it at runtime, but that doesn't address this particular issue.

You don't understand what you're talking about.

Jax is explicitly mentioned in your pyrefly link as having a parallel (but slightly weaker) system. In addition Jax is built on stablehlo which uses shape dialect, which is part of the compiler (and therefore statically known).

Torch has a symbolic shape inference system that I helped build:

https://github.com/pytorch/pytorch/blob/main/torch/fx/experi...

> The fact that people have developed these janky approaches to shape tracking suggests that there's a gap to be filled.

I have already said it: the fact that people do not know how to use the tools does not mean the tools are lacking - it means the users are unsophisticated. Let me put it this way: almost everyone that is employed to work with these tools is aware of these features and therefore eschews those kinds of comment strings.

Re:Futhark by Example by ainch on Sunday May 17, 2026 @09:01PM (#48174604)
Thanks for the reply - you're clearly much more experienced with the internals here, but I believe we're still talking at cross-purposes. I believe you're talking about compilers like jax.jit or torch.compile performing symbolic shape inference. I'm talking about the ergonomics of tracking shape information while writing Python code that calls these libraries. I don't use Torch much, so I'll just comment on the Jax side.

> Jax is explicitly mentioned in your pyrefly link as having a parallel (but slightly weaker) system

Jaxtyping is limited to runtime-only checks (which might as well be assert statements), and doesn't infer shapes based on operations. I'm interested in Pyrefly because I've run into the limitations of Jaxtyping in my own usage.

> Jax is built on stablehlo which uses shape dialect, which is part of the compiler (and therefore statically known).

It's true that JAX does shape inference when it compiles down to HLO - but that isn't available to the Python typing system. The Pyrefly development is addressing that, so you get static analysis before even running anything, or without having to add eval_shape calls all over your codebase. I think that's helpful, and will catch bugs. When I say Jax does inference at runtime, I mean that you have to run for the jit compiler to kick in - you don't get feedback as you edit.

> the fact that people do not know how to use the tools does not mean the tools are lacking... almost everyone that is employed to work with these tools is aware of these features and therefore eschews those kinds of comment strings.

The examples I took are from Andrej Karpathy and Noam Shazeer - maybe the disconnect is that they're more on the research side. Perhaps only unsophisticated users rely on these hacks - but as one such user I'm very excited that Pyrefly is addressing a problem I have. I suspect part of the misunderstanding that's evolved here is that these tools serve audiences with different needs.

Re:Futhark by Example by imtringued on Monday May 18, 2026 @02:10AM (#48176094)
Every single comment you've posted was unhelpful and demeaning and you've constantly had to backtrack every single time you've had to respond. This is extremely poor form and I see zero goodwill here.

ainch doesn't care about the inference engine knowing the shapes. This is obvious. He has been talking about developer ergonomics and you've basically said "only idiots don't know where the ergonomics features are", while linking to a file which explicitly states that it is a experimental/private API that is only needed in niche situations which is basically doubling down on having poor ergonomics.

If you can't understand the problem as a developer working on those features and you had to link to the source you've written rather than the docs, you're basically admitting that you're the problem.

Re:Futhark by Example by mathisfun123 on Monday May 18, 2026 @09:45AM (#48179850)
This is the whiniest response I've ever seen on hn. Congrats.

> you had to link to the source you've written rather than the docs, you're basically admitting that you're the problem.

God forbid i expect people using complex tools to know how they actually work.

Edit: there are two categories of devs out there - those that expect code to be a fully perfected product delivered to them on a silver platter (only requiring docs, which must be perfect btw), and those which understand all code is merely a suggestion. For the latter a code pointer is more than enough. I leave you to infer which are more productive.

Re:Futhark by Example by nestorD on Saturday May 16, 2026 @10:49PM (#48165671)
See also jaxtyping which, contrary to what its name might imply, covers JAX/PyTorch/NumPy/MLX/TensorFlow arrays and tensors.

https://docs.kidger.site/jaxtyping/

Re:Futhark by Example by ainch on Sunday May 17, 2026 @04:47AM (#48167143)
I use jaxtyping as documentation, but the fact it can only be used for runtime checking (in a slightly clunky manner) and can't infer shapes based on ops really limits its utility imo.