还有更好的C吗?

发布于 2024-09-15 23:02:22 字数 1436 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

也只是曾经 2024-09-22 23:02:22

由于还没有人提出:我认为 ATS 语言 是一个非常好的候选者为了更好的C!特别是因为您喜欢 Haskell 以及具有强类型的函数式编程。请注意,ATS 似乎是专门为系统编程和硬实时应用程序设计的,因为大多数应用程序无需垃圾收集即可完成。

如果您检查点球大战你会发现性能基本上与 C 相当。我认为这令人印象深刻因为现代 C 编译器背后有多年的优化工作,而 ATS 基本上是由一个人开发的。 - - 而提供类似安全功能的其他语言通常会引入开销,ATS 完全在编译时确保一切,从而产生与 C 非常相似的性能特征。

引用该网站:

什么是 ATS?

ATS 是一种静态类型编程语言,它将实现与形式规范统一起来。它配备了一个高度表现力的类型系统,该系统植根于应用类型系统框架,该语言因此而得名。特别是,ATS 中提供了相关类型和线性类型。 ATS (ATS/Anairiats) 当前的实现是用 ATS 本身编写的。它可以与 C/C++ 一样高效(有关具体证据,请参阅计算机语言基准游戏)并支持各种编程范例,包括:

函数式编程。 ATS 的核心是一种基于 eager(又名按值调用)评估的函数式语言,它也可以容纳惰性(又名按需要调用)评估。 ATS 中线性类型的可用性通常使得用它编写的函数程序不仅运行效率高(与 C 相比),而且(内存)占用量也小得惊人(与 C 相比)。

命令式编程。 ATS 中命令式编程的新颖而独特的方法牢固地植根于定理证明的编程范式。 ATS 的类型系统允许许多在其他语言中被认为危险的功能(例如,显式指针算术和显式内存分配/释放)在 ATS 中得到安全支持,使 ATS 成为低级系统编程的可行编程语言。

并发编程。 ATS 配备了多核安全的垃圾收集实现,可以通过使用 pthread 支持多线程编程。用于跟踪和安全操作资源的线性类型的可用性为构建可以利用多核架构的可靠程序提供了有效的方法。

模块化编程。 ATS的模块体系很大程度上受Modula-3的影响,既简单又通用,并且能够有效支持大规模编程。

此外,ATS 包含一个子系统 ATS/LF,支持某种形式的(交互式)定理证明,其中证明被构造为总函数。通过该组件,ATS 提倡采用以程序员为中心的程序验证方法,以语法上交织的方式将编程与定理证明结合起来。此外,该组件可以作为编码演绎系统及其(元)属性的逻辑框架。

Since nobody brought it up yet: I think the ATS language is a very good candidate for a better C! Especially since you enjoy Haskell and thus functional programming with strong types. Note that ATS seems to be specifically designed for systems programming and hard real-time applications as most of it can do without garbage collection.

If you check the shootout you will find that performance is basically on par with C. I think this is quite impressive since modern c compilers have years and years and years of optimization work behind them while ATS is basically developed by one guy. -- while other languages providing similar safety features usually introduce overhead ATS ensures things entirely at compile time and thus yields very similar performance characteristics as C.

To quote the website:

What is ATS?

ATS is a statically typed programming language that unifies implementation with formal specification. It is equipped with a highly expressive type system rooted in the framework Applied Type System, which gives the language its name. In particular, both dependent types and linear types are available in ATS. The current implementation of ATS (ATS/Anairiats) is written in ATS itself. It can be as efficient as C/C++ (see The Computer Language Benchmarks Game for concrete evidence) and supports a variety of programming paradigms that include:

Functional programming. The core of ATS is a functional language based on eager (aka. call-by-value) evaluation, which can also accommodate lazy (aka. call-by-need) evaluation. The availability of linear types in ATS often makes functional programs written in it run not only with surprisingly high efficiency (when compared to C) but also with surprisingly small (memory) footprint (when compared to C as well).

Imperative programming. The novel and unique approach to imperative programming in ATS is firmly rooted in the paradigm of programming with theorem-proving. The type system of ATS allows many features considered dangerous in other languages (e.g., explicit pointer arithmetic and explicit memory allocation/deallocation) to be safely supported in ATS, making ATS a viable programming language for low-level systems programming.

Concurrent programming. ATS, equipped with a multicore-safe implementation of garbage collection, can support multithreaded programming through the use of pthreads. The availability of linear types for tracking and safely manipulating resources provides an effective means to constructing reliable programs that can take advantage of multicore architectures.

Modular programming. The module system of ATS is largely infuenced by that of Modula-3, which is both simple and general as well as effective in supporting large scale programming.

In addition, ATS contains a subsystem ATS/LF that supports a form of (interactive) theorem-proving, where proofs are constructed as total functions. With this component, ATS advocates a programmer-centric approach to program verification that combines programming with theorem-proving in a syntactically intertwined manner. Furthermore, this component can serve as a logical framework for encoding deduction systems and their (meta-)properties.

茶底世界 2024-09-22 23:02:22

Nimrod瓦拉语言?

What about Nimrod or Vala languages ?

海螺姑娘 2024-09-22 23:02:22

Rust

更好的 C 语言的另一个(真正的)候选者是 Rust 编程语言

与其他一些建议不同,(Go、Nimrod、D...)Rust 可以直接与 C 和 C++ 竞争,因为它具有手动内存管理并且不需要垃圾回收(参见 [1])。

Rust 的与众不同之处在于它具有安全的手动内存管理。 (该链接是 pc walton 的博客,他是 Rust 的主要贡献者之一,通常值得一读;)除此之外,这意味着它修复了 空指针的十亿美元错误。这里建议的许多其他语言要么需要垃圾收集 (Go),要么默认打开垃圾收集,并且不提供超出 C++ 提供的安全手动内存管理的设施(Nimrod、D)。

虽然 Rust 有一颗命令式的心,但它确实从函数式语言中借鉴了很多好东西,例如求和类型,又称为标记联合。它还真正关心成为一种安全且面向性能的语言。

[1] 现在有两种主要的指针类型:自有指针(如 C++ 中的 std::unique_ptr,但类型检查器有更好的支持)和托管指针。顾名思义,后者确实需要任务本地垃圾收集,但是 有想法将它们从语言中删除并仅将它们作为库提供。

编辑以反映 @ReneSacs 评论:D 和 Nimrod 中不需要垃圾收集。

Rust

Another (real) candidate for a better C is The Rust Programming Language.

Unlike some other suggestions, (Go, Nimrod, D, ...) Rust can directly compete with C and C++ because it has manual memory management and does not require garbage collection (see [1]).

What sets Rust apart is that it has safe manual memory management. (The link is to pc walton's blog, one of Rusts main contributors and generally worth a read ;) Among other things, this means it fixes the billion dollar mistake of nullpointers. Many of the other languages suggested here either require garbage collection (Go) or have garbage collection turned on by default and do not provide facilities for safe manual memory management beyond what C++ provides (Nimrod, D).

While Rust has an imperative heart, it does borrow a lot of nice things from functional languages, for example sum types aka tagged unions. It is also really concerned with being a safe and performance oriented language.

[1] Right now there are two main pointer types owned pointers (like std::unique_ptr in C++ but with better support from the typechecker) and managed pointers. As the name suggests the latter do require task-local garbage collection, but there are thoughts to remove them from the language and only provide them as a library.

EDITED to reflect @ReneSacs comment: Garbage collection is not required in D and Nimrod.

你的笑 2024-09-22 23:02:22

我对 Haskell 不太了解,但如果你想要一个强大的类型系统,请看看 Ada。它大量用于航空航天应用的嵌入式系统。 SIGADA moto 是“我们信任强类型”。但是,如果您必须执行 Windows/Linux 类型的设备驱动程序,那么它就没有多大用处。

它不那么受欢迎的几个原因:

  • 冗长的语法 - 旨在读取而不是编写
  • 编译器历来昂贵
  • 与 DOD 和设计委员会的关系,程序员似乎对此感到不满

我认为事实是大多数程序员不喜欢强类型系统。

I don't know much about Haskell, but if you want a strong type system, take a look at Ada. It is heavily used in embedded systems for aerospace applications. The SIGADA moto is "In strong typing we trust." It won't be of much use, however, if you have to do Windows/Linux type device drivers.

A few reasons it is not so popular:

  • verbose syntax -- designed to be read, not written
  • compilers were historically expensive
  • the relationship to DOD and design committees, which programmers seem to knock

I think the truth is that most programmers don't like strong type systems.

森林散布 2024-09-22 23:02:22

Nim(以前的 Nimrod)拥有强大的类型系统,包含概念和简单的泛型。它还具有带有模板和宏的广泛编译时机制。它还具有简单的 C FFI 以及您期望从系统编程语言获得的所有低级功能,因此您可以编写您自己的例如,内核。

目前它编译为 C,因此您可以在任何运行 GCC 的地方使用它。如果您只想使用 Nim 作为更好的 C,您可以通过 --os:standalone 编译器开关来实现,它为您提供了一个基本的标准库,与操作系统无关。

例如,要编译为 AVR 微控制器,您可以使用:

nim c --cpu:avr --os:standalone --deadCodeElim:on --genScript x.nim

Nim 有一个软实时 GC,您可以在其中指定它的运行时间以及最大暂停时间(以微秒为单位)。如果实在无力承担GC,可以完全禁用它(--gc:none编译器开关),只使用像C那样的手动内存管理,丢失大部分标准库,但仍然保留更加理智和强大的类型系统。

此外,标记指针是一项计划中的功能,例如,它可以确保您不会将内核级指针与用户级指针混合在一起。

Nim (former Nimrod) has a powerful type system, with concepts and easy generics. It also features extensive compile time mechanisms with templates and macros. It also has easy C FFI and all the low level features that you expect from a system programming language, so you can write your own kernel, for example.

Currently it compiles to C, so you can use it everywhere GCC runs, for example. If you only want to use Nim as better C, you can do it via the --os:standalone compiler switch, that gives you a bare bones standard library, with no OS ties.

For example, to compile to an AVR micro-controller you can use:

nim c --cpu:avr --os:standalone --deadCodeElim:on --genScript x.nim

Nim has a soft real-time GC where you can specify when it runs and the max pause time in microseconds. If you really can't afford the GC, you can disable it completely (--gc:none compiler switch) and use only manual memory management like C, losing most of the standard library, but still retaining the much saner and powerful type system.

Also, tagged pointers are a planned feature, that ensure you don't mix kernel level pointers with user level pointers, for example.

拥抱我好吗 2024-09-22 23:02:22

D 可能会提供您想要的东西。它具有非常丰富的类型系统,但如果需要,您仍然可以控制内存布局。它有像 C 一样不受限制的指针。它是垃圾收集器,但您不会被迫使用垃圾收集器,并且如果您确实愿意,可以编写自己的内存管理代码。

但是,我不确定您可以在多大程度上将类型丰富性与您想要使用的低级方法混合在一起。

如果您找到适合您需求的产品,请告诉我们。

D might offer what you want. It has a very rich type system, but you can still control memory layout if you need to. It has unrestricted pointers like C. It’s garbage collected, but you aren’t forced to use the garbage collector and you can write your own memory management code if you really want.

However, I’m not sure to what extent you can mix the type richness with the low-level approach you want to use.

Let us know if you find something that suits your needs.

高冷爸爸 2024-09-22 23:02:22

我不确定 Cyclone 处于什么状态,但这为标准 C 提供了更多安全性。D 在某种程度上也可以被认为是“更好的 C”,但它的它的裂脑在标准库中的地位不是很清楚。

我选择的“更好的 C”语言是 OOC。虽然还很年轻,但是很有趣。它为您提供了 OO,但没有 C++ 的杀手级复杂性。它使您可以轻松访问 C 接口(您可以“覆盖”C 结构并在调用外部库时正常使用它们/以这种方式控制内存布局)。它默认使用 GC,但如果您确实不需要它,可以将其关闭(但这意味着您不能再使用标准库集合而不会泄漏)。

另一条评论提到了 Ada,我忘记了,但这提醒了我:有 Oberon,它应该是一种安全的(呃)语言,但也包含垃圾收集机制。

I'm not sure what state Cyclone is in, but that provided more safety for standard C. D can be also considered a "better C" to some extent, but its status is not very clear with its split-brain in standard library.

My language of choice as a "better C" is OOC. It's still young, but it's quite interesting. It gives you the OO without C++'s killer complexity. It gives you easy access to C interfaces (you can "cover" C structs and use them normally when calling external libraries / control the memory layout this way). It uses GC by default, but you can turn it off if you really don't want it (but that means you cannot use the standard library collections anymore without leaking).

The other comment mentioned Ada which I forgot about, but that reminded me: there's Oberon, which is supposed to be a safe(-er) language, but that also contains garbage collection mechanisms.

一笑百媚生 2024-09-22 23:02:22

您可能还想查看BitC。它是一门严肃的语言,而不是一个玩具,但它还没有准备好,并且可能不会及时准备好对您有任何用处。

尽管如此,BitC 的一个具体设计目标是支持与 Haskell 风格的类型系统结合的低级开发。它最初是为了支持 Coyotos 微内核的开发而设计的。我认为 Coyotos 已经被消灭了,但 BitC 显然仍在开发中。

You might also want to look at BitC. It’s a serious language and not a toy, but it isn’t ready yet and probably won’t be ready in time to be of any use to you.

Nonetheless, a specific design goal of BitC is to support low-level development in conjunction with a Haskell-style type system. It was originally designed to support development of the Coyotos microkernel. I think that Coyotos was killed off, but BitC is still apparently being developed.

蓝颜夕 2024-09-22 23:02:22

C++ 无法解决这个问题。而且我无论如何也不想使用C++。我发现 OO 类和模板很令人头疼。

克服这种态度。只需使用C++。您可以从用 C++ 编写 C 语言开始,然后逐渐转向更好的风格。

C++ doesn't solve this. And I don't want to use C++ anyways. I find OO classes and templates to be a headache.

Get over this attitude. Just use C++. You can start with coding C in C++ and keep gradually moving to better style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文