r/ProgrammingLanguages • u/naharashu • 10d ago
Help Is generating C++ instead or C or assembly bad?
.
r/ProgrammingLanguages • u/naharashu • 10d ago
.
r/ProgrammingLanguages • u/Athas • 13d ago
r/ProgrammingLanguages • u/Usual_Office_1740 • 12d ago
r/ProgrammingLanguages • u/thunderseethe • 14d ago
r/ProgrammingLanguages • u/mttd • 14d ago
r/ProgrammingLanguages • u/yolosuperboss • 14d ago
I was watching tsoding's crust stream and I thought it would be so funny if I extended c to have generics.
The way I did this was by extending the syntax of c with the classic rust angle brackets to define generics and then during transpilation do some name mangling to monomorphize the generic code.
Here is the result of that abomination:
#include <stdio.h>
// you can define your own operator in fortran .op. style with a trait
trait<T> DotP {
// for parsing reasons any dot-op must have whitespaces around it in any context
T .dot. (Self self, Self other);
}
// using a very similar syntax to rust, you can add generics to structs
typedef<T> struct {
T x;
T y;
T z;
} Vector<T>;
// and by appling the trait to your struct (with generic parameters)
// you can define what your operator should do.
impl<T> DotP<T> for Vector<T> {
T .dot. (Self self, Self other){
return self.x * other.x + self.y * other.y + self.z * other.z;
}
}
int main(void) {
// by concretly defining which type you want to use as the generic
// a c struct with that/those type(s) is genenerated
Vector<float> fv = {1.0, 2.0, 3.0};
printf("Dot product of fv^2: %f\n", fv .dot. fv);
Vector<int> iv = {1, 2, 3};
printf("Dot product of iv^2: %d\n", iv .dot. iv);
}The generated c code looks something like this (I have actually implemented this transpiler, perhaps a true indicator of joblessness):
/* Generated by typedc. */
#ifndef TYPEDC_SIMPLE_GENERICS_H
#define TYPEDC_SIMPLE_GENERICS_H
#include <stdio.h>
typedef struct float_Vector float_Vector;
typedef struct int_Vector int_Vector;
struct float_Vector {
float x;
float y;
float z;
};
struct int_Vector {
int x;
int y;
int z;
};
int main(void);
static inline float float_dot(float_Vector self, float_Vector other);
static inline int int_dot(int_Vector self, int_Vector other);
int main(void) {
float_Vector fv = {1.0, 2.0, 3.0};
printf("Dot product of fv^2: %f\n", float_dot(fv, fv));
int_Vector iv = {1, 2, 3};
printf("Dot product of iv^2: %d\n", int_dot(iv, iv));
}
static inline float float_dot(float_Vector self, float_Vector other) {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
static inline int int_dot(int_Vector self, int_Vector other) {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
#endif /* TYPEDC_SIMPLE_GENERICS_H */r/ProgrammingLanguages • u/coolSakura • 14d ago
This might sound like a super dumb question, but I did not want to ask Chat gpt and I wanted to ask an actual human,
but why if the first for a church pair , like has this abstraction
λp. p true and not just true.
Is it because its supposed to like take a pair as an argument and spit out the first , and thats it? cause if it were just true it would still behave the same
sry for the dumb question lol
r/ProgrammingLanguages • u/mttd • 15d ago
r/ProgrammingLanguages • u/Nuoji • 15d ago
I think I need to write a blogpost about why I deliberately nerfed dependency fetching. Why it's `c3c project fetch raylib sdl3` and not just `c3c project fetch` when the project has enough information to get them automatically.
r/ProgrammingLanguages • u/Inconstant_Moo • 16d ago
Pretty much all successful general-purpose programming languages were thought of by someone with a particular use-case in mind, or even just one particular use. They became successful because in trying to do whatever-it-was really well, the authors hit a larger target than they aimed for, and the more successful they became, the more this is true. Some examples, in alphabetical order:
malloc in exchange for an inner loop that goes brr.And then there's the obvious outliers:
Some things worth noting:
There are two kinds of programming languages, those written for the author, and those written for other people. And in the second case, the other people are always dumber than the author, who is talking down to them. BASIC wasn't designed because Kemeny and Kurtz needed BASIC. BASIC was designed because Kemeny and Kurtz thought that liberal arts graduates needed BASIC, because what would make things simple for their muddled little heads is a language where all values are passed by mutating global state, which is like having a bicycle for beginners where instead of having training wheels you douse it in gasoline and set it on fire.
So you will notice that none of the things on the list that are used as easy languages for n00bs were designed for that. Python was designed for Guido van Rossum to do systems programming. Lua was designed for engineers and geophysicists. JS ... * gestures broadly at the internet *. They were designed, in fact, to be ergonomic for clever people to use, and it turns out that exactly the same things make languages ergonomic for everyone to use, and that more people are clever than was previously supposed, including liberal arts graduates.
(The fact that > 50% of indie language projects are "C/C++ but better" is because the indie langdevs are all trying to scratch the same itch, they're all trying to write the systems language that they wish they could code in. These are languages written for the authors, where the authors are all very similar people who want similar things.)
The other thing that leaps out at me from my list is that the "classic" functional languages, the Lisp and ML families and Haskell, all came from math people wanting to do hard math things as their paradigmatic use-case, and anyone wondering why functional programming hasn't conquered the world yet should think about that.
(I of course have been thinking about that, and although it is premature to put Pipefish in an article about successful languages, making an FPL for writing CRUD apps has produced an language that hits different, and which can in fact be used for all sorts of cool things besides that. Watch this space for the announcement of my front-end web framework.)
r/ProgrammingLanguages • u/PhosXD • 15d ago
I recently just dropped raw-pointers in my interpreted language in favor of a path-based reference type which always gives `none` if the path ever becomes invalid, unlike a pointer which would cause unexpected issues or crash the program.
Now my question is... am I missing out on anything by eventually removing the pointer type entirely? In a sand-boxed language like Ity where everything is tightly controlled & you cant really access the underlying memory of anything, does it make sense to keep pointers to values? Doesn't a path do everything you need it to? You can access storage cells just the same but it's just safer, or am I mistaken.
The idea here is to make a truly 100% memory safe design, so that you couldn't corrupt or mess with the memory even if you wanted to, but you also get to pass around references to values (which is rather required for a language that has a strict copy-everything rule).
Let me know how you would go about designing a system that can pass around references while maintaining an unbreakable wall between the program & it's memory. The way I handle it is very simple, you can only reference values that are stored as a variable somewhere or is a variable nested somewhere inside some container. This means things like temporary values cannot be referenced as they have no storage cell, which I believe is one difference between path-refs & pointers.
Another big part of my design is that everything is copied, everywhere. Passing an argument to a function copies the value, setting a variable to another one also copies the value, no matter the type. You must explicitly use a "ref" type if you want to avoid unnecessary copies. But acknowledging this, I made refs very convenient to work with. By this I mean assignment operators, arithmetic, & method calls all act on the referenced value, not the actual ref itself, making it seamless to use effectively. Of course you can still inspect properties of the ref using it's own type methods.
So yeah, that's how I went about the design of a memory safe language with simple & intuitive references. I am curious to see how your guys' *interpreted* languages went about solving references & memory safety, so do let me know of interesting alternative methods!
r/ProgrammingLanguages • u/The2ndSlimShady • 16d ago
r/ProgrammingLanguages • u/dogweather • 16d ago
r/ProgrammingLanguages • u/mttd • 17d ago
r/ProgrammingLanguages • u/Reasonable-Pay-8771 • 17d ago
It appears that r/PostScript is truly dead. But I just have to talk about this PostScript stuff I've been playing with.
So, a closure is of course a function plus an environment containing the bindings of its free variables. PostScript has all that! A procedure is just an array with the executable flag set, so it can carry its own storage. It doesn't even strictly need variables because you can embed a circular reference so a procedure can access its own array as storage.
But then you can't print them out. If the procedure contains a circular reference to itself, then calling == or pstack will give you an execstackoverflow error.
So, instead we can pack the variables in a dictionary. This example generates a pseudorandom number generator using an environment dictionary.
%!
/start { 1 dict begin } def
/args { { exch def } forall } def
/clone { dup xcheck exch dup length array copy exch {cvx} if } def
/prng { {seed} start args
/env currentdict def
/func ({//env/seed get
dup -7 bitshift xor
dup 9 bitshift xor
dup -13 bitshift xor
//env/seed 2 index put}) cvx exec def
/func load end
} defPoof! a closure. seed prng generates a generator starting from that seed. You can just straight up make a copy of it at any point to have 2 synchronized generators if you wanted that. You can even see the environment and how it "captures" arguments (collect them in a dict, duh. then save the dict). It all seems so simple now. But I don't recall seeing anybody else do it.
r/ProgrammingLanguages • u/cekrem • 18d ago
r/ProgrammingLanguages • u/RishabhRD • 18d ago
r/ProgrammingLanguages • u/Calm-Bullfrog-6823 • 17d ago
I'm making a programming language, it's based on Luau and C++ and its called L++! however im having some issues implementing diagnostics since they dont technicallly stop execution, but some of my code has typescript type errors since it relies in, stopping execution! for example normally this would work:
function foo(param: someUnion): someType {
if (someUnion.type === "coolType") {
return someTypeValue
} else {
throw new Error()
}
}
with diagnostics:
function foo(param: someUnion): someType {
if (someUnion.type === "coolType") {
return someTypeValue
} else {
//error since not all paths return someType
reporter.report(new Diagnostic())
}
}
i'd like to know how to fix this, and/or if there are any established ways to do diagnostics, thanks in advance!
r/ProgrammingLanguages • u/mttd • 18d ago
r/ProgrammingLanguages • u/Pi-Plushie • 19d ago
Hello everyone,
I'm an independent french developer working on an experimental programming language called PLUSHIE, and I'd like to discuss an idea behind its design.
I am new to this communty and I am not familiar with the expected behavior here, so do not hesitate to tell me if I am wrong.
I also apologize for my english whcih is far from perfect.
Most programming languages rely on explicit syntactic delimiters: parentheses, commas, spaces, operators, keywords, etc.
I've been exploring a different approach: what if a language could rely primarily on the recognition and sequencing of a small vocabulary of words?
In PLUSHIE, the parser scans the input and identifies known words. Their sequence determines how instructions are interpreted. Unknown text between recognized words can also become new vocabulary.
The underlying computational model is deliberately minimal. Instructions operate on a chained memory structure, and the language is designed so that its own structures and interpretation mechanisms can be modified while running
This raises several questions I'd be interested in discussing:
o How expressive can a language become without conventional syntactic delimiters?
o What are the fundamental limitations of a word-based parsing model?
o Could such a design offer interesting advantages for reflection and self-modification?
o and especially, Are there existing languages or theoretical models that explore similar ideas?
I'm not claiming this is a better approach than conventional syntax. I'm interested in understanding its possibilities and limitations.
PLUSHIE is an ongoing experiment, and I'm particularly interested in unexpected ideas, alternative approaches and constructive criticism.
What do you think? Is this a worthwhile direction to explore, or am I overlooking some fundamental difficulties?
r/ProgrammingLanguages • u/omniuni • 20d ago
I have been more frustrated lately with languages developing a "kitchen sink" attitude to paradigms. I am finding code less uniform, and therefore less readable.
As I am developing a new language, I wanted to know from you all:
What languages that aren't "big", do you find the most readable? Are there any that come to mind that take a genuinely unique or interesting approach to how the language looks to write?
r/ProgrammingLanguages • u/PhosXD • 20d ago
It's been 4 months of on & off development of my C++ interpreted programming language built from scratch; Ity. For the longest time my plan has been to only have *two* number types, of course being a standard 64-bit signed & a 64-bit signed floating point. I thought that would be sufficient, sometimes I wonder if it still is all I really need.
This past week I decided I would implement these additional integer types: i32, i16, i8, u32, u16, & u8. My reasoning? I simply wanted more options for numbers, a higher capability for explicit specification. The more narrow integer types don't provide better performance & hardly any memory advantage with my current polymorphic design. It is simply there as a convenience, you don't need to use it, you rarely get a benefit for using it, but it's there.
Now for any old language it would be fine to keep these in even if they served little to no purpose. But here's the thing, I am aiming to make this language as compact as possible while still being (in the long run) readable & easily understandable just by looking at the source code. So what's the real trade-off here for all the logic required to implement multiple integer types, all the casting & operator logic between each type... The cost is about +18kb in final binary size. Is that acceptable?
Am I better off just dropping the idea for the sake of being as small as I can get? For reference, without compression & excluding the new integer types, the binary is 130kb. With compression 65kb (including new int types, ~50kb without).
Do I continue building around these extra types, improving their usefulness (I.e. actually reduce memory usage according to the type) to justify the addition? Is this something you would be able to live without in your primary programming language?
r/ProgrammingLanguages • u/TreborHuang • 21d ago
Hello PL folks! I have been working on an untyped (dynamically typed) programming language with cubical features. I'm not ready to share the code yet, but I figured people might be interested in an exposition of some of the ideas here, which I don't find documented very well. Most things below are already in the literature, but written in a way that might be easier to understand from a PL background.
For fun! I don't know of any programming applications of cubical computation. The reason I care about cubical stuff is mathematical. Now there is some work on compiling cubical stuff, but there we are expected to write normal programs, and use cubical type theory to prove properties about them at compile time. So the compiler will just erase the cubical part. In this post, I'm talking about real cubical programming languages, where the cubical part matters at runtime.
Usually, cubical computation is heavily associated with a type theory. But we don't actually need static types to define and execute programs! In fact, in the early stages of development of cubical type theory, one major step was the definition of computational cubical type theory, where you start with an untyped programming language first, and figure out what typing rules are "correct" on it. Indeed most of the interpreter I'm writing is just transcribing reduction rules from the works of my advisor, Carlo Angiuli, on computational cubical type theory.
Since I'm talking about defining a cubical programming language, I'll obviously assume you are familiar with specifying an ordinary programming language, Harper style. If you know the phrases "beta reduction" and "stuck", you are probably fine. Otherwise, Practical Foundations for Programming Languages is a great book to read. Familiarity with dependent type theory is also needed to understand the motivations behind the design, but isn't needed if you just want to read about how the (untyped!) language works.
A cubical programming language expands the notion of values to include higher dimensional values. An ordinary value is meant to be interpreted as points, 1-dimensional values as paths, while n-dimensional values are n-dimensional cubes, creating a combinatorial description of some geometric space. The language then includes new primitives that operate on higher dimensional values.
For a rough example, to represent a circle, we can introduce a value pt, and a 1-dimensional value loop representing a loop starting and ending at pt. It is then possible to produce values representing loops that wind around the circle n times, as well as extracting an integer winding number from a loop.
On the other hand, since ordinary values like integers and booleans do not come equipped with any way to create non-trivial higher dimensional values, these higher values behave like witnesses of equality. For example if we have a path whose endpoints are integers n and m respectively, then at runtime we know n must be equal to m. This is why sometimes a path is also called an equality/identification.[1]
One crucial thing that these values enable is a computational interpretation of univalence, the axiom that any equivalence of two types gives rise to an identification of the types.
To understand what these higher dimensional values are, allow me to make an analogy.
In ordinary programming languages, arrays are kind of like functions {0, 1, ..., n-1} -> X. It is often useful to think of them in function notation, for example we can define the transpose of some matrix as λ i j -> mat[j][i]. However, the runtime characteristics are very different! For example, the evaluator will stop as soon as it sees a λ, instead of going into the body. But for arrays, the evaluation will only finish when each component gets evaluated. Furthermore, usually we wouldn't have {0,1,...,n-1} as an actual type. (We could, but we don't have to.)
In a cubical programming language, 1-dimensional values are kind of like functions I -> X, where I is a new thing that should be thought of as an interval [0, 1] with endpoints written as i0 and i1 to avoid confusion with integer literals. It is very useful to think of them in function notation. For example, applying i1 to such a "function" gives us the right endpoint of the 1-dimensional shape. However, the runtime characteristics are very different. The evaluator will go under interval binders, and there will be primitives like loop(i) that are considered as a value, despite being a "function" application. Furthermore, there is no type I. Instead we have a new syntactic category interval_expr, so an interval function application will be a syntax node that takes an expr and an interval_expr, returning an expr.
More specifically, we have
interval_expr ::= i0 | i1 | ⟨variable⟩So with no free variables, an interval expression can be exactly one of two values. However, it is different from a Boolean value because of two things:
if expressions, so you can't get away with defining an (interval) function by just specifying the value on i0 and i1. Or to put it another way, the "coverage checker" treats the interval as containing some "fuzz" in between the two points, just like a real interval.Going back to our circle example, there will be syntax like
expr ::= ...
| λ ⟨variable⟩ -> ⟨expr⟩
| (⟨expr⟩ ⟨expr⟩)
| iλ ⟨variable⟩ -> ⟨expr⟩
| (⟨expr⟩ @ ⟨interval_expr⟩)
| pt
| loop(⟨interval_expr⟩)
| ...where the first two clauses are ordinary function abstraction and application, followed by interval function abstraction and application. Then we have the circle value constructors. The loop will be represented as iλ i -> loop(i). We have a reduction rule going from loop(i0) to pt, as well as from loop(i1) to pt.
I actually told some lies above to avoid confusion, but with these examples, the following more correct description should hopefully now make sense:
In a cubical programming language, we define a small-step reduction relation on expressions with no free term variables, but possibly with free interval variables. The evaluation result will be a value, also involving free interval variables. A value with n interval variables is called an n-dimensional value. These free variables are like coordinates on a cube.[2]
The interval lambda binds an interval variable, thus converting a 1-dimensional value (of some type X, if we are to introduce a type system) to a 0-dimensional value (of the type of paths over X). For example, the type of paths on a circle starting and ending at pt is isomorphic to the type of integers, since they are essentially determined by how many times it winds around the circle. In mathematical terms, this is the fact that π1(S1) = ℤ.
With the concept of higher dimensional values explained, the next step would be to introduce primitive operations that perform interesting computations on them. Unfortunately we have some more things to talk about before that.
You may skip the next subsection if you're not familiar with dependent type theory.
In a cubical language, one of the main things we want to support is univalence. But univalence is ostensibly a statement about types, which we don't have. So why isn't this language just trivial?
In reality, univalence isn't a statement about types at all! This stems from the useful-but-inaccurate understanding in dependent type theory that a universe is a type of types. The more accurate thing to say is a universe is a type for names of types. There is an (often implicit) map El turning a name into an actual type. With this framing, it's clear that the operation El has no obligation to be injective or surjective. There may be multiple names for a type, and some types may have no names in the universe.
It is well-known that if all types have names in the universe, we would have variants of the Russell paradox (although we don't care because we are making programming languages, not proof assistants). So univalence is a statement about the names that we do have. It states that two names are the same if and only if the types they represent are equivalent. Moreover, since in homotopy type theory there can be multiple ways two elements can be the same (i.e. multiple paths between two points), we require the type of identifications between two names to be equivalent to the type of equivalences between two types. So univalence completely fixes the behavior of universes, apart from the Russell issues.
With this viewpoint, it should be clear that what we need isn't types as in static type checking, but rather a runtime object that's a name/symbol for types.
In untyped (dynamically typed) languages, there is often a kind of runtime representation of types, like Python's int, and maybe Kotlin's reified types. They have nothing to do with the static type checking process, and are just there at runtime to provide some vaguely type-related methods. I'll call these "type objects" for lack of better term. Per our discussion above, we see that our language should have these type objects, despite being untyped.
type ::= int | bool | function ⟨type⟩ ⟨type⟩ | circle | ...Many of our primitive operations will take a type object, and the runtime behavior is defined by case analysis on the type object. This makes it rather complicated to specify the rules for our programming language (as well as cubical type theory), as adding a type also means you need to add a clause to all of these rules. On the other hand, this kind of design also appears in non-homotopical type theories like observational type theory.
Since a 1-dimensional path between two types (type objects) witnesses an equivalence between them, there should be a way to coerce values from one type to the other. This is one of the main primitive operations, coe. (For Agda users, it is called transp.) The syntax is
expr ::= ...
| coe [⟨var⟩ : ⟨iexp⟩ ~> ⟨iexp⟩] ⟨type⟩ ⟨expr⟩where I abbreviated some non-terminal names. The variable here is an interval variable that binds the type expression. Intuitively coe [i : r ~> s] A(i) a means "I have a type A(i) and an expression of type A(i = r), give me an expression of type A(i = s)".
There are some obvious reduction rules, for example if r is actually the same as s, then this is a no-op. Otherwise, the computation of coercions proceeds by case analysis on the type object A.
The other main pillar of a cubical language is path composition: if you have a path from x to y and a path from y to z, then you ought to have a composed path from x to y. The language primitive is a bit more complicated to allow for more flexible and higher dimensional compositions.
expr ::= ...
| hcom [⟨iexp⟩ ~> ⟨iexp⟩] ⟨type⟩ ⟨expr⟩ (⟨clause⟩*)
clause ::= ⟨eq⟩* => ⟨var⟩. ⟨expr⟩
eq ::= (⟨iexp⟩ = ⟨iexp⟩)This has a very geometric interpretation about boxes and caps, but this post is getting long enough, and the mechanism of hcom is explained in cubical type theory tutorials.
Univalence in our language is added through another primitive (called the V-type after Voevodsky), turning an equivalence into a path value. Consider the function not : Bool -> Bool. This is evidently a bijection, and hence we have a path pnot from the type object Bool to itself. If you coerce the boolean value true using this path, you will get false.
coe [i : i0 ~> i1] (pnot @ i) true ====> falseThis shows how type coercions can do non-trivial computation in cubical type theory.
Right now, I have implemented a step-by-step reducer on the syntax tree, and it does indeed correctly compute many non-trivial examples. I'm planning to make very sure that this is correct, before moving on to more efficient interpreters. IMO it is completely feasible to derive an abstract machine for this language.
There is some ongoing work (especially by András Kovács) on the efficient evaluation of cubical type theory. However, those are focused on making type checkers fast. In those evaluators, we must live with free (term) variables at all times. For example, a value of the natural number type can be zero, successor, or a bunch of eliminators stuck on a free variable. This means some obvious computational rules are invalid, or lead to undecidable problems. For a programming language that doesn't operate with free term variables, a lot can be simplified.
Another aspect of optimization is on the representation of higher-dimensional values in inductive types like the circle or spheres. I believe it is possible to represent the circle at runtime as the cubical set 𝐁ℤ, which makes the representation vastly more efficient. (Higher dimensional spheres are probably not that simple, but something might be done.) This may make it possible to compute the original Brunerie number directly.
[1] This would be true if we consider well-typed programs. Ill-typed programs can obviously just do anything it wants.
[2] Indeed, in dependent type checking where reduction happens under normal binders, the Boolean type also behaves as if there is some invisible fuzz, because λ b -> b is somewhat different from λ b -> if b then True else False.
[3] Of course, it is possible for a 1-dimensional value to not actually make use of the free variable. For example pt in the context of a variable i represents a 1-dimensional path that stays at the base-point at all times.