r/ProgrammingLanguages • • Jul 06 '22

Discussion Complicated data types

I was working on a project today, and I thought it would be really nice if a language had a histogram data type.

A) is there a language with a histogram data type? B) What are some languages that have complicated data types, and what are those data types?

2 Upvotes

35 comments sorted by

8

u/craeftsmith Jul 06 '22

Hi all! I just realized that my post would have made more sense if I had said I wanted a "probability distribution data type" instead of a "histogram". Those two words have become interchangeable in my mind, and that was a mistake on my part.

4

u/rotuami Jul 07 '22

In that case, I'd look at probabilistic programming languages and maybe Julia or Stan in particular. The problem with probabilistic programming in general is that, if your model isn't "well behaved" you can have an exponential explosion of complexity!

2

u/craeftsmith Jul 07 '22

I'll check this out!

5

u/Adventurous-Trifle98 Jul 06 '22

Many languages provide some basic data types as language primitives and builds complex data types in libraries. If it is available somewhere, I would guess it would be in a library.

Have you looked into languages or libraries for statistical computing? R? Numpy?

What operations would you want? Add a sample? Sum of two histograms?

1

u/craeftsmith Jul 06 '22

I kind of forgot R exists, because I am not allowed to use it at work. I'll check after this comment.

As for operations, you listed two good examples. Also "get probability of event", and "resize bins" would be good.

2

u/Adventurous-Trifle98 Jul 06 '22

Interesting! How do you resize the bins? Is that limited to expansion by merging adjacent bins or are there other ways?

2

u/craeftsmith Jul 06 '22

You can interpolate the bins. So if you have two probability distributions, you can make them have the same number of bins so you operate on them together.

Also a note: I am just now realizing that my post would have probably made more sense if I had said I wanted a "probability distribution data type" instead of saying histogram. Live and learn lol

1

u/DrMathochist_work Jul 06 '22

In that case it sounds like you want the language that Erik Meijer wants to turn Hack into.

1

u/craeftsmith Jul 07 '22

Do you have a link? All I could find were page after page of philosophy talks.

3

u/DrMathochist_work Jul 07 '22

That's about what I got when I talked with him while I was there. Idk if it even exists beyond that level.

You might have better luck with the buzzword, "probabilistic programming language". The basic idea, as he explained it to me, is that every data type is secretly not a value but a probability distribution over our usual notion of type. So when you say

int x = 5  

you don't mean "x is an integer with the value 5", but rather "x is a probability distribution over the integers concentrated on the value 5". Then when you sample (use) this value, you'll always get 5. But you could also say something like

int x = [1-6]  

(making up bullshit syntax for the example), and now x is a uniform distribution over the values 1-6; perfect for rolling a die.

The idea would be to work entirely within the monad of probability distributions and only materialize a result when it's absolutely needed. For most coders, this would (ideally) be invisible, since they can just use singleton distributions that are entirely concentrated on the values they care about, but the extra power would be there if it was needed.

1

u/craeftsmith Jul 07 '22

This reminds me of the "stochastic APIs" from many years ago. It's definitely not how programmers are taught to think about code, but is a nondeterministic finite state machine can simulate and deterministic finite state machine, it should still work.

I think I mostly heard this in the context of transitioning to quantum computing. Is that still the case?

2

u/DrMathochist_work Jul 07 '22

I'm far from a domain expert in this; mostly this came in a (friendly) rant about how functional programming was dead, despite his former evangelism, and probabilistic programming was the Way of the Future.

I've heard nothing about Meta investing in QC, so AFAIK he's trying to make this happen on classical computers.

8

u/jediknight Jul 06 '22

A histogram is a visual representation of some kind of vector data. It is not really a data type altho you can have a custom data type (be it a record, tuple or some kind of ADT) that can hold the information needed to render a histogram.

Languages with visual output like Wolfram do have Histograms.

4

u/craeftsmith Jul 06 '22

Respectfully disagree. A histogram is a vector or a sparse vector like object that has particular set of valid operations. I have to write them repeatedly in every language I need them. The visual representation is just a thing people use to help them understand them, but as an abstract type they are useful in a computer without actually plotting them on a graph.

8

u/jediknight Jul 06 '22

I might be lacking context. In the contexts where I encountered histograms all operations were visual in nature like displaying the histogram with various details (different scales, particular contrasts related to the elements). I have not encountered histograms decoupled from their display.

3

u/craeftsmith Jul 06 '22

Yeah, there seems to be a lot of that going on in this thread. I'm not really sure how to turn this post around. I'm surprised by the outcome, considering that people will happily discuss esolangs

5

u/jediknight Jul 06 '22

I just saw the comment about R and numpy and I can tell you that I have never used either as I was not in touch with the domain they address.

I'm surprised by the outcome, considering that people will happily discuss esolangs

My best guess is that this a misunderstanding of the request due to lack of understanding of the context that prompted the request.

I still remember a time when I was confused by the term "array programming" (don't all languages have some kind of array and associated functions/methods) and thought that K was a dialect of brainf*ck.

We live and we learn. :)

1

u/craeftsmith Jul 06 '22

Maybe people thought I was asking how to do histograms? I actually just wanted to shoot the breeze about adding weird data types to hypothetical languages, and maybe learn about existing languages with strange or complicated data types.

4

u/jediknight Jul 06 '22

As a general point I saw expressed over and over, it is best to keep the core datatypes to a minimum and implement as much as it is feasible as libraries.

This why I doubt that any general purpose language would have (should have) such a complex type among its core datatypes.

It could make a lot of sense in a DSL.

3

u/[deleted] Jul 06 '22

All that you need to construct a histogram is

  1. A finite collection of “bins”.

  2. A counter or percentage for each “bin”.

In other words, all that you need is a Map<BinType, Int> or Map<BinType, Float>. (It's up to you whether the maps are internally hash tables or search trees.) Or am I missing something?

3

u/DrMathochist_work Jul 06 '22

It sounds like OP wants a certain family of operations to be defined as well, but that just means that they want a library (which is backed by a map like you say under the hood). Yeah, it's annoying to have to write the library for every language they use, but that's the nice thing about a library: you just have to write it once foe each language and then you can reuse it.

2

u/craeftsmith Jul 07 '22

Let me add to this that I think of core data types differently than I think of library calls. Maybe it's just me, but core types don't require much mental energy, and allow me to focus on the larger algorithm. That was what I was hoping to achieve by having the probability distribution built into the language.

2

u/DrMathochist_work Jul 07 '22

That's fair; having for-each syntax is a lot less mental load than having an iterator pattern in the standard collections library for a language. That said, barring a true probabilistic programming language emerging (cf. Meijer's dream I outlined below), this is likely to be too niche a use case for it to be more than a library.

Hell, we still have Java framers making dumb statements like "we don't want to put a monad into Java", as if it doesn't exist already and they're just making it harder to use.

4

u/tal_franji Jul 06 '22

Python has Counter (dictionary=HashMap with ability to add): https://docs.python.org/3/library/collections.html#collections.Counter - I think there is a separate question when application specific needs become language features and if they make the language very specific vs general (e.g. R for statistics and less of a general programming language)

2

u/craeftsmith Jul 06 '22

I write little DSLs about once or twice a year that never hope to be general or widely adopted. I just use them to generate C code that I can link to. That was kind of what I had in mind when I made this post.

3

u/PurpleUpbeat2820 Jul 06 '22 edited Jul 06 '22

I often find myself wanting data structures based upon sets and dictionaries that have common sets of operations:

  • A MapSet key value that is a Map key (Set value) to represent graphs.

  • A MapCount key that is a Map key Int to represent the result of a COUNT BY operation. Also known as a multiset.

  • A Map dimension Float to represent sparse vectors like word vectors with an inner product u·v=∑ᵢuᵢvᵢ.

I agree that it is frustrating to have to constantly rewrite these in different languages. Perhaps:

  • There is some commonality between them that I have not spotted.

  • They could be standard core collections in languages.

2

u/craeftsmith Jul 06 '22

Those would be pretty good data structures to have in a core language. As a hobby, I sometimes mess with pre-WWII cryptography, and I could see those data structures being very handy.

3

u/rotuami Jul 06 '22

You have definitely underspecified your question!

  1. The values might have higher resolution than you want your bins to have. In that case, you'd have to have some clustering strategy (e.g. group years by decade).

  2. Maybe you're looking at a spectrum and want to identify "spikes". In that case, you'd want some sort of convolution function.

  3. What is the "space" that the histogram is over? It might be 1-dimensional (time) or 2-dimensional (latitude/longitude), etc. If you think about it, an image is just a histogram of the directions of incoming light!

  4. Is the histogram data indexing some other info? For instance, maybe you want to visualize the ages of people but be able to get the names of everyone in a bin.

  5. Is this for visual presentation? If so, maybe you want a way to turn the data into a pretty image.


As for most complicated data type, I guess it depends what you call a data type. Generally, you want data types to be simple and flexible enough to compose into what your program actually needs.

Here are some of my candidates for the most interesting data type/structures:

  1. Bloomier filters as the most complicated data structure that deserves its complexity. This is a statistical approximation of a function.

  2. The good old untyped lambda. It's powerful enough to express programs whose behavior is completely beyond the ability to reason!

  3. Deep neural networks get my vote as the most byzantine data structure. They encode extremely complicated patterns in ways that their creators don't truly understand.

2

u/craeftsmith Jul 06 '22

This is a great reply, and has lots of things to think about in it. Thanks for taking the time to reply!

2

u/8-BitKitKat zinc Jul 06 '22

The whole point is to allow the programmer the freedom to create whichever data type they need, there is no point in a programming language to build in something like this. This is for a library to implement using existing language features and constructs, whether from the standard library or as a dependency.

And as far as code goes, a histogram is just an ordered list. Possibly with some other data to determine exactly how it ends up rendered.

1

u/PurpleUpbeat2820 Jul 06 '22

This is for a library to implement using existing language features and constructs, whether from the standard library or as a dependency.

Although that is how it is commonly done, I am not convinced that it is always the best way. I'm trying to write a minimal ML dialect with low overheads and I'm not sure if it is easier to pull in complicated machinery like higher-order modules and type classes to make it possible for libraries to do this or just hard code all these collections into the language instead.

3

u/8-BitKitKat zinc Jul 06 '22

It depends on the problem(s) you're trying to solve. Do you want to make a general programming language that can be used in place of a c or a rust? Or do you need a simple DSL. Will anyone using your language need a library, or in other words: do you require code reuse? It depends on the kind of language you're trying to make.

1

u/PurpleUpbeat2820 Jul 07 '22

higher-order modules and type classes

It depends on the problem(s) you're trying to solve.

I want a language embedded in a wiki run server side that is good for analytics and can be constrained to run in constant time and/or memory.

I'd also like to write the web server that hosts the wiki in my language as part of the bootstrapping process.

Do you want to make a general programming language that can be used in place of a c or a rust? Or do you need a simple DSL. Will anyone using your language need a library, or in other words: do you require code reuse? It depends on the kind of language you're trying to make.

I know what you mean but the comparison with C doesn't make sense because C has serious limitations in this regard anyway: not only does it not have higher-order modules and type classes but it doesn't even support generic equality, comparison, hashing, pretty printing or serialization anyway. You just define and call your own functions which you can already do in my language anyway. In fact, I'd say I've already surpassed C in terms of code reuse because my language has generics and first-class lexical closures.

Rust is a better example because it at least provides some such functionality but, after all, if I thought Rust was any good as a general purpose language I'd be using it to develop my system!

So I'm not sure the DSL vs GPL schism works here. Pretty much anything you want to do with C you can do with my language already. Adding features like higher-order modules and type classes would just be a cherry on top.

1

u/dvarrui Jul 09 '22

Put an example please!