TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Mars: a hybrid imperative-declarative higher-order language

70 pointsby plesneralmost 11 years ago

9 comments

rayineralmost 11 years ago
Mixing imperative and functional paradigms is a really interesting point in the design space, and has gotten me (begrudgingly) interested in statically-typed languages. I noticed writing Common Lisp that I preferred to write code in a functional style, but occasionally I&#x27;d want to bury an imperative update somewhere. Putting a comment saying: &quot;this should be the only use of SETF in the whole algorithm&quot; isn&#x27;t the most satisfying solution.<p>Languages like Common Lisp and Dylan (and SML) have mixed imperative and functional paradigms in a non-controlled way for awhile, but there is some interesting work going on about how to do it in a more controlled way. Much of this work involves limiting aliasing and mutability, or expressing imperative features an effect system, or some combination of the two.<p>Rust is probably the most mainstream example of the former, but Mezzo is another language exploring this area: <a href="http://protz.github.io/mezzo" rel="nofollow">http:&#x2F;&#x2F;protz.github.io&#x2F;mezzo</a>. The basic idea is that if you&#x27;ve got the unique reference to an object, or a tree of objects, you can modify it without worrying about how it will affect the rest of the program. Koka is a language that focuses more on the latter, expressing the imperative characteristics of a function through an effect system: <a href="http://arxiv.org/pdf/1406.2061.pdf" rel="nofollow">http:&#x2F;&#x2F;arxiv.org&#x2F;pdf&#x2F;1406.2061.pdf</a> (section 2.1). The basic idea here is similar to Java&#x27;s exception specifications. If the set of objects potentially modified by a function must be part of its type, then you can use the type system to keep you from modifying an object, or calling a function that does, in a context where the rest of the program doesn&#x27;t expect that object to be modified.
评论 #8004249 未加载
评论 #8005360 未加载
评论 #8004538 未加载
sparkiealmost 11 years ago
Something that <i>bugs</i> me in Haskell, and which Mars inherits as it is mostly the same semantics - you&#x27;ve taken the effort to create a good type system with optional types and type checking, only to decide to not actually use them for some common operations you&#x27;re performing in the language (namely, head&#x2F;tail).<p><pre><code> ?&gt; Nil.head Runtime Error: List instance has no field &#x27;head&#x27; </code></pre> Such runtime errors could easily be avoided by making head&#x2F;tail return Maybe, and using Nothing in the case of Nil. This hardly complicates code, but it provides the correct behavior, particularly when it comes to doing &quot;records&quot; properly. The Mars docs give an example of how I consider copying Haskell&#x27;s semantics directly without improving them leads to flawed code.<p><pre><code> type Foo: X(u :: Num, v :: Num) Y(v :: Num) </code></pre> There&#x27;s nothing wrong with `v` here, as it is total, but you can easily introduce a runtime error by using `u` incorrectly.<p><pre><code> ?&gt; x = Y(1) ?&gt; x.u Runtime Error: Foo instance has no field &#x27;u&#x27; </code></pre> This is why using records in combination with sum types in Haskell is widely considered a bad idea. It&#x27;s a misfeature as far as I&#x27;m concerned. If given the chance to redo Haskell&#x27;s awful record system, I&#x27;d fix it.<p>The problem can be trivially avoided by making `u` return `Maybe Num` in this example, where it returns Nothing for Y and `Just 1` for X. Ideally, the compiler should check if field names are total over the constructors - if not, it should automatically return an optional type. If the fields are total there&#x27;s no need. I doubt there would be any noticeable performance cost to moving the error to compile time - particularly because you don&#x27;t actually need to use it in internal implementations of map&#x2F;fold and such - you could have an internal function which mimics the current behavior, but don&#x27;t expose it from the module.
评论 #8007482 未加载
评论 #8005275 未加载
shadowmintalmost 11 years ago
Mm. The native library looks difficult to use and primitive (<a href="http://bazaar.launchpad.net/~mgiuca/mars/trunk/view/head:/lib/native.mar" rel="nofollow">http:&#x2F;&#x2F;bazaar.launchpad.net&#x2F;~mgiuca&#x2F;mars&#x2F;trunk&#x2F;view&#x2F;head:&#x2F;li...</a>) making this interesting in a theoretical sense, but not really in any practical sense (to me, at least).
评论 #8004332 未加载
acron0almost 11 years ago
This looks really good. What prevents them from committing this language to the public? Their About (&quot;we don&#x27;t recommend you write real software with it&quot;) is really off-putting for me because I can think of a fantastic use case for this in my current line of work but what does this even mean? I should expect compiler bugs? Conflicted.
评论 #8004625 未加载
评论 #8007516 未加载
adultSwimalmost 11 years ago
Calling functional programming declarative seems like a bit of a stretch to me. I understand the reasoning. Still, when I read the headline I was imagining something pretty different.<p>---<p>The language looks pretty good. I think for most programmers some kind of vanilla ML would work well.
Thizalmost 11 years ago
I hate the double colon, it&#x27;s so unpythonic.<p>I believe the &#x27;as&#x27; keyword would be more smooth:<p><pre><code> def hello(name as String) as String: var greeting as String greeting = &#x27;Hello &#x27;+name return greeting</code></pre>
评论 #8004233 未加载
评论 #8004380 未加载
评论 #8004476 未加载
评论 #8006928 未加载
评论 #8004468 未加载
评论 #8004419 未加载
评论 #8005559 未加载
tormehalmost 11 years ago
Great. I&#x27;ve thought about making a language with similar concepts, but I just can&#x27;t get myself to make a proper stab at it. Not motivated enough. Perhaps scared of failing. I don&#x27;t know.
评论 #8004560 未加载
评论 #8004236 未加载
评论 #8005211 未加载
vfclistsalmost 11 years ago
First time I ever saw on appspot.com 503 over quota message.<p>Are Google that measly with the bandwidth allocations?
评论 #8007432 未加载
sigzeroalmost 11 years ago
Looks a lot like Python.
评论 #8004179 未加载