morloc: A multi-lingual, typed, workflow language

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

See GitHub README https://github.com/morloc-project/morloc#readme


[Skip to Readme]

Properties

Versions 0.33.0, 0.33.0
Change log ChangeLog.md
Dependencies aeson (>=1.2.4.0 && <1.3), base (>=4.7 && <5), bytestring (>=0.10.8.2 && <0.11), containers (>=0.5.10.2 && <0.6), directory (>=1.3.0.2 && <1.4), docopt (>=0.7.0.5 && <0.8), extra (>=1.6.5 && <1.7), filepath (>=1.4.1.2 && <1.5), haskell-src-meta (>=0.8.0.2 && <0.9), megaparsec (>=6.4.1 && <6.5), morloc, mtl (>=2.2.2 && <2.3), parsec (>=3.1.13.0 && <3.2), partial-order (>=0.1.2.1 && <0.2), pretty-simple (>=2.1.0.0 && <2.2), prettyprinter (>=1.2.0.1 && <1.3), prettyprinter-ansi-terminal (>=1.1.1.2 && <1.2), process (>=1.6.1.0 && <1.7), raw-strings-qq (>=1.1 && <1.2), safe (>=0.3.17 && <0.4), scientific (>=0.3.5.3 && <0.4), template-haskell (>=2.12.0.0 && <2.13), text (>=1.2.3.0 && <1.3), unordered-containers (>=0.2.9.0 && <0.3), yaml (>=0.8.29 && <0.9) [details]
License GPL-3.0-only
Copyright 2020 Zebulun Arendsee
Author Zebulun Arendsee
Maintainer zbwrbz@gmail.com
Category Language, Compiler, Code Generation
Home page https://github.com/morloc-project/morloc
Bug tracker https://github.com/morloc-project/morloc/issues
Source repo head: git clone https://github.com/morloc-project/morloc
Uploaded by arendsee at 2020-11-06T07:05:42Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for morloc-0.33.0

[back to package description]

experimental travis build status github release license: GPL v3 DOI

morloc is a functional programming language where functions are imported from foreign languages and unified under a common type system. The compiler generates the code needed to compose functions across languages and also to direct automation of mundane tasks such as data validation, type/format conversions, data caching, distributed computing, and file reading/writing. The endgame is to develop morloc into a query language that returns optimized programs from an infinite library of functions and compositions of functions.

See the manual for more information.

If you want to get straight to playing with code, go through the steps in the installation section and then go to the project in demo/01_sequence_analysis.

Status

This project is under active development with no stability guarantees until the v1.0 release. Pull requests, issue reports, and private messages are very welcome.

Installation

Compile and install the package (requires the Haskell utility stack):

git clone https://github.com/morloc-project/morloc
cd morloc
stack install --fast

morloc also depends on the JSON::XS perl module from CPAN, which can be installed as follows:

export PERL_MM_USE_DEFAULT=1
export PERL_CANARY_STABILITY_NOPROMPT=1
sudo perl -MCPAN -e 'install JSON::XS' 

For Python support, you need to download the pymorlocinternals library from PyPi:

pip install pymorlocinternals
# or on Mac:
pip3 install pymorlocinternals

For R support, you need to install the rmorlocinternals library from github, in an R session, run:

R> install.packages("devtools")
R> devtools::install_github("morloc-project/rmorlocinternals")

C++ support currently requires a GNU compiler that supports C++11.

morloc modules can be installed from the morloc library with the commands such as:

morloc install cppbase
morloc install pybase
morloc install rbase
morloc install math

The morloc install commands will install the modules in the $HOME/.morloc/lib folder.

Last of all, if you are working in vim, you can install morloc syntax highlighting as follows:

mkdir -p ~/.vim/syntax/
mkdir -p ~/.vim/ftdetect/
cp vim-syntax/loc.vim ~/.vim/syntax/
echo 'au BufRead,BufNewFile *.loc set filetype=loc' > ~/.vim/ftdetect/loc.vim

Getting Started

export hello
hello = "Hello World"

The "export" keyword exports the variable "hello" from the module.

Paste this into a file (e.g. "hello.loc") and then it can be imported by other morloc modules or directly compiled into a program where every exported term is a subcommand.

morloc make hello.loc

This will generate a single file named "nexus.pl". The nexus is the executable script that the user will interact with. For this simple example, it is the only generated file. It is currently written in Perl.

Calling "nexus.pl" with no arguemtns or with the -h flag, will print a help message:

$ ./nexus.pl -h
The following commands are exported:
  hello
    return: Str

The return: Str phrases states that hello returns a string value.

The command hello can be called as shown below:

$ ./nexus.pl hello
Hello World

Composing C++ Functions

The following code uses only C++ functions (fold, map, add and mul).

import cppbase (fold, map, add, mul)

export square;
export sumOfSquares;

square x = mul x x;

sumOfSquares xs = fold add 0 (map square xs);

If this script is pasted into the file "example-1.loc", it can be compiled as follows:

morloc install cppbase
morloc make example-1.loc

The install command clones the cppbase repo from github repo into the local directory ~/.morloc/lib. The morloc make command will generate a file named nexus.pl, which is an executable interface to the exported functions.

You can see typed usage information for the exported functions with the -h flag:

$ ./nexus.pl -h
The following commands are exported:
  square
    param 1: Num
    return: Num
  sumOfSquares
    param 1: [Num]
    return: Num

Then you can call the exported functions (arguments are in JSON format):

$ ./nexus.pl sumOfSquares '[1,2,3]'
14

The nexus.pl executable dispatches the command to the compiled C++ program, pool-cpp.out.

Language interop

morloc can compose functions across languages. For example:

import math (fibonacci);
import rbase (plotVectorPDF, ints2reals);

export fibplot

fibplot n = plotVectorPDF (ints2reals (fibonacci n)) "fibonacci-plot.pdf";

The fibplot function calculates Fibonacci numbers using a C++ function and plots it using an R function. The R function plotPDF is a perfectly normal R function with no extra boilerplate:

plotPDF <- function(x, filename){
  pdf(filename)
  plot(x)
  dev.off()
}

The Morloc Type System

The first level of the morloc type system is basically System F extended across languages. A given function will have a general type as well as a specialized type for each language it is implemented in.

The map function has the types

map :: (a -> b) -> [a] -> [b]
map Cpp :: (a -> b) -> "std::vector<$1>" a -> "std::vector<$1>" b
map Python3 :: (a -> b) -> list a -> list b

The general signature looks almost the same as the Haskell equivalent (except that morloc universal quantification is currently explicit). The list type constructors for C++ are very literally "type constructors" in that they are used to create syntactically correct C++ type strings. If the type variable a is inferred to be int, for example, then the C++ type std::vector<int> will be used in the generated code. The same occurs in the python type constructors list, except here the same Python type is generated regardless of the type of a.

The following example is available in examples/rmsWithTypes.loc:

import cppbase (fold, map, add, mul)

export square;
export sumOfSquares;

square x = mul x x;

sumOfSquares xs = fold add 0 (map square xs);

This example cannot be compiled since none of the functions are imported or sourced, but it can be typechecked:

morloc typecheck examples/rmsWithTypes.loc
add :: Num -> Num -> Num;
add Cpp :: double -> double -> double;

mul :: Num -> Num -> Num;
mul Cpp :: double -> double -> double;

fold     :: (b -> a -> b) -> b -> [a] -> b;
fold Cpp :: (b -> a -> b) -> b -> "std::vector<$1>" a -> b;

map :: (a -> b) -> [a] -> [b];
map Cpp :: (a -> b) -> "std::vector<$1>" a
                    -> "std::vector<$1>" b;

square x = mul x x;
sumOfSquares xs = fold add 0 (map square xs);

The typechecker associates each sub-expression of the program with a set of types. The specific type information in mul is sufficient to infer concrete types for every other C++ function in the program. The inferred C++ type of sumOfSquares is

"std::vector<$1>" double -> double

The general type for this expression is also inferred as:

List Num -> Num

The concrete type of mul is currently written as a binary function of doubles. Ideally this function should accept any numbers (e.g., an int and a double). I intend to add this functionallity eventually, perhaps with a Haskell-style typeclass system.