A new Standard ML dialect written in Java

栏目: IT技术 · 发布时间: 4年前

内容简介:Standard ML interpreter, with relational extensions, implemented in Java(Until version 0.1, Morel was known as smlj.)

A new Standard ML dialect written in Java

Morel

Standard ML interpreter, with relational extensions, implemented in Java

(Until version 0.1, Morel was known as smlj.)

Requirements

Java version 8 or higher.

Get Morel

From Maven

Get Morel from Maven Central :

<dependency>
  <groupId>net.hydromatic</groupId>
  <artifactId>smlj</artifactId>
  <version>0.1.0</version>
</dependency>

Download and build

$ git clone git://github.com/julianhyde/morel.git
$ cd morel
$ ./mvnw install

On Windows, the last line is

> mvnw install

Run the shell

$ ./morel
morel version 0.1.0 (java version "11.0.4", JLine terminal, xterm-256color)
= "Hello, world!";
val it = "Hello, world!" : string
= exit
$

Documentation

Status

Implemented:

  • Literals
  • Variables
  • Comments ( (* block *) and (*) line )
  • let (expression that lets you define local variables and functions)
  • val (including val rec )
  • fun (declare function)
  • Operators: = <> < > <= >= ~ + - * / div mod ^ andalso orelse ::
  • Built-in constants and functions: it true false nil abs not ignore
  • Type derivation
  • fn , function values, and function application
  • if
  • case
  • Primitive, list, tuple and record types
  • Type variables (polymorphism) (except in let and datatype )
  • Enumerated types ( datatype )
  • Tuples and unit, record and list values
  • Patterns (destructuring) in val and case , matching constants, wildcards, lists, records and tuples
  • Basis library functions:
    • Top : abs
    • List : null , length , @ (as prefix at for now), hd , tl , last , getItem , nth , take , drop , rev , concat , revAppend , app , map , mapPartial , find , filter , partition , foldl , foldr , exists , all , tabulate , collate
    • String : maxSize , size , sub , extract , substring , ^ , concat , concatWith , str , implode , explode , map , translate , isPrefix , isSubstring , isSuffix

Not implemented:

  • type
  • local
  • raise , handle
  • exception
  • while
  • References, and operators ! and :=
  • Operators: before o
  • User-defined operators ( infix , infixr )
  • Type annotations in expressions and patterns

Bugs:

  • The @ infix operator to concatenate lists is currently named at and is prefix;
  • Built-in operators do not use the option type;
  • Prevent user from overriding built-in constants and functions: true , false , nil , ref , it , :: ; they should not be reserved
  • Access parameters and variables by offset into a fixed-size array; currently we address them by name, in a map that is copied far too often
  • Runtime should throw when divide by zero
  • Validator should give good user error when it cannot type an expression

Extensions

Morel has a few extensions to Standard ML: postfix labels, implicit labels in record expressions, and relational extensions. Postfix labels and implicit labels are intended to make relational expressions more concise and more similar to SQL but they can be used anywhere in Morel, not just in relational expressions.

Postfix labels

Morel allows '.' for field references. Thus e.deptno is equivalent to #deptno e .

(Postfix labels are implemented as syntactic sugar; both expressions become an application of label #deptno to expression e .)

Because '.' is left-associative, it is a more convenient syntax for chained references. In the standard syntax, e.address.zipcode would be written #zipcode (#address e) .

Implicit labels in record expressions

In standard ML, a record expression is of the form {label1 = exp1, label2 = exp2, ...} ; in Morel, you can omit label = if the expression is an identifier, label application, or field reference.

Thus

{#deptno e, e.name, d}

is short-hand for

{deptno = #deptno e, name = e.name, d = d}

Relational extensions

The from expression (and associated in , where and yield keywords) is a language extension to support relational algebra. It iterates over a list and generates another list.

In a sense, from is syntactic sugar. For example, given emps and depts , relations defined as lists of records as follows

val emps =
  [{id = 100, name = "Fred", deptno = 10},
   {id = 101, name = "Velma", deptno = 20},
   {id = 102, name = "Shaggy", deptno = 30};
   {id = 103, name = "Scooby", deptno = 30}];
val depts =
  [{deptno = 10, name = "Sales"},
   {deptno = 20, name = "Marketing"},
   {deptno = 30, name = "Engineering"},
   {deptno = 40, name = "Support"}];

the expression

from e in emps where e.deptno = 30 yield e.id

is equivalent to standard ML

map (fn e => (#id e)) (filter (fn e => (#deptno e) = 30) emps)

with the where and yield clauses emulating the filter and map higher-order functions without the need for lambdas ( fn ).

Relational expressions are an experiment bringing the features of query languages such as SQL into a functional language. We believe that a little syntactic sugar, backed by a relational query planner, makes ML into a powerful and convenient tool for querying large data sets. Conversely, we want to see how SQL would look if it supported lambdas, function-values, polymorphism, pattern-matching, and removed the syntactic distinction between tables and collection-valued columns.

You can iterate over more than one collection, and therefore generate a join or a cartesian product:

from e in emps, d in depts
  where e.deptno = d.deptno
  yield {e.id, e.deptno, ename = e.name, dname = d.name};

As in any ML expression, you can define functions within a from expression, and those functions can operate on lists. Thus we can implement equivalents of SQL's IN and EXISTS operators:

let
  fun in_ e [] = false
    | in_ e (h :: t) = e = h orelse (in_ e t)
in
  from e in emps
  where in_ e.deptno (from d in depts
                where d.name = "Engineering"
                yield d.deptno)
  yield e.name
end

let
  fun exists [] = false
    | exists hd :: tl = true
in
  from e in emps
  where exists (from d in depts
                where d.deptno = e.deptno
                andalso d.name = "Engineering")
  yield e.name
end

In the second query, note that the sub-query inside the exists is correlated (references the e variable from the enclosing query) and skips the yield clause (because it doesn't matter which columns the sub-query returns, just whether it returns any rows).

More information


以上所述就是小编给大家介绍的《A new Standard ML dialect written in Java》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机科学概论(第7版) (平装)

计算机科学概论(第7版) (平装)

J.Glenn Brookshear / 王保江 / 人民邮电出版社 / 2003-9 / 49.0

《计算机科学概论(第2版)》更新了部分内容,使其更加贴近于计算机科学领域内的最新趋势,这包括了网络安全、开源运动、关联存储、公钥加密、XML、Java和C#等内容。扩充了网络和Internet所覆盖的内容。一个程序用C#语言编写,还有C、C++和Java,作为语言的例子。不过整个方法依旧保持语言的独立。一起来看看 《计算机科学概论(第7版) (平装)》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具