Git Product home page Git Product logo

elchemy's Introduction

Quick install

npm install -g elchemy

What is it?

Elchemy lets you write simple, fast and quality type safe code while leveraging both the Elm's safety and Elixir's ecosystem

In case of any questions about the project feel free to submit them in Issues with Q&A label

Features

  • Type inference: Powerful type inference means you rarely have to annotate types. Everything gets checked for you by the compiler
  • Easy and type-safe interop: You can call Elixir/Erlang without any extra boiler-plate. All the calls you make are checked in terms of type-safety as thoroughly as possible based on Elixir's typespecs.
  • All the best of Elm and Elixir: Elchemy inherits what's best in Elm - type safety, inference and extreme expressiveness, but also what's best in Elixir - Doc-tests, tooling and obviously the entire BEAM platform.
  • Nearly no runtime errors - Elchemy's type system eliminates almost all runtime errors. With a shrinking set of edge cases, your entire Elchemy codebase is safe. Elixir parts of the codebase are the only ones to be a suspect in cases of runtime errors happening.
  • Beautiful and fully readable output - The produced code is idiomatic, performant and can be easily read and analyzed without taking a single look at the original source.

Maturity of the project

  • Parser - 99% of Elm's syntax (see elm-ast)
  • Compiler - 90% (Sophisticated incremental compilation. No support for Windows yet though (#287) and also big reliance on unix tools (#288)
  • Elchemy-core - 95% ( Everything covered except side effects and JSON Decoders)
  • Interop with Elixir - 90% - Purity tests (#162) and handling of macro-heavy libraries (#276) to go
  • Ideology - 70% - We've got a pretty solid idea of where Elchemy is going
  • Documentation - 80% - There are two tutorials and a complete Gitbook documentation. Few entrance level tutorials though, this project tries to change it.
  • Elchemy-effects - 20% - You can't and shouldn't write anything with side-effects in Elchemy yet. We're working on finding the best solution for effects that would fit both Elm's and Elixir's community (see #297 for more info)
  • Elchemy-core for Erlang VM - 5% - Everything for os related tasks like filesystem, OTP goodies etc are yet to be done
  • Elchemy type checker - 20% - Self-hosted elchemy type inference algorithm, written by @wende and inspired by @Bogdanp.

Usage

Prerequisites

Installation in an existing Elixir project

Install elchemy globally with:

npm install -g elchemy

Then, in the root directory of your project do:

elchemy init

And follow the instructions.

elchemy will find all *.elm files specified in elchemy_path and compile it into corresponding *.ex files in lib directory.

You can override output directory specifying elixirc_paths.

Installation as a standalone

npm install -g elchemy

Usage

elchemy compile source_dir output_dir

Recommended editors setup

Build from source

git clone https://github.com/wende/elchemy.git
cd elchemy
make compile
./elchemy compile source_dir output_dir

and

make dev

In order to launch and test the web demo.

Troubleshooting

If something doesn't work, try

npm install -g elchemy
elchemy clean
elchemy init
mix test

first

FAQ

Why would I want to use that?

  • You like types
  • But even more you prefer compile-time errors over run-time error
  • You prefer add b c = b + c over defp add(a, b), do: b + c
  • You like curry
  • You think failing fast is cool, but not as cool as not failing at all

Why wouldn't I want to use that?

  • Your project relies on die-hard battle tested libraries, and you despise any versions starting with 0
  • You're afraid that when you learn what Monad is your mustache will grow, and eyesight weaken

Can I use it in already existing Elixir project?

You can, but nice and dandy compile tools are still on their way

Will my employer notice I'm having an affair with Elchemy?

The output files of Elchemy treat the code readability as a first class citizen. The code is meant to be properly indented, the comments aren't omitted, and the code is optimized as hard as it can ( f.i case clauses reduce to function overloads)

When will Elchemy become 1.0.0?

Once it's done, so as it is supposed to be in the so called semantic versioning. 😇

Can I contribute?

Definitely. Yes. Please do. 💕

How are types represented?

You're a nosy one, aren't you? 😄 Elchemy represents all type constructors as snake cased atoms, and all type applications as tuples. Which means that MyType 42 "Forty two" Error in Elchemy equals to {:my_type, 42, "Forty Two", :error} in Elixir.

Can I use already existing Elm libraries with Elchemy?

As long as they don't use any Native modules, Ports or Elm runtime they can be safely imported and used

Can I use already existing Elixir libraries with Elchemy?

Yes. You can do an ffi call to any function in any module. Whether it's Elixir module, Erlang module, or even a macro you can include it in your code. Ffi calls are a treated specially in Elchemy, and they get generated test to analyze the types based on @specs, so that you don't compromise type safety for using Elixir code. In order to increase readability it's advised not to use ffi calls if not necessary and always document and doctest them.

But what about out of function macros? Like tests and use Module?

Unfortunately you can't write any macros with do..end blocks yet. You can write any out of function code using an elixir inline code with:

{- ex
  *code_here*
-}

But it is a last resort solution and shouldn't ever be abused.

Can I define an Elixir macro in Elchemy?

So you want to write an Elm-like code, that will manipulate Elixir code, which generates an Elixir code that manipulates Elixir code? How about no?

Do I need to have Elm installed to compile my .elm files with Elchemy?

Elchemy uses Elm to typecheck your program. It is possible to use it without Elm on your machine, while it's not advised.

Contributor credits:

Inspiration:

Contributing Guide

Targeted values:

  • Fully readable and indented elixir code generated from compilation
  • Seamless and stress less interop with existing Elixir code, preferably with magically working type safety
  • Full integration with entire elm syntax for editors and compilers magic

elchemy's People

Contributors

baransu avatar colinbankier avatar gitter-badger avatar justgage avatar kevgathuku avatar latachz avatar littlestudent avatar matsubara0507 avatar nathanielknight avatar shalokshalom avatar wende avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elchemy's Issues

Smart type aliases

Add:

Example:

type State a = List a | Map a
fun : State a

->

@spec fun(list(a) | %{})

Typing failure

So, there appears to be a slight typing failure. :-)

Example:

module Stack exposing (..)

type alias TestRecord =
  { int : int
  , float : float
  }

blah : TestRecord
blah =
  42

blorp =
  blah.x

->

# Compiled using Elmchemy v0.0.17
defmodule Stack do
  use Elmchemy

  @spec blah :: %{int: any, float: any}
  curry blah/0
  def blah() do
    42
  end

  curry blorp/0
  def blorp() do
    blah.x
  end

end

As an example, my MLElixir project (which puts a strongly-typed ML face on Elixir, it is not a released project, just something I screw around with for fun, its errors are also very low level and not prettied up at all) compiles the similar code as the above Elm:

iex> import MLElixir
MLElixir
iex> defmlmodule Stack do
...>   type test_record = %{
...>     int: integer,
...>     float: float,
...>   }
...>
...>   let blah | test_record = 42
...>
...>   let blorp = blah.x
...> end

Gives this:

** (throw) {:NO_TYPE_RESOLUTION, %TypedElixir.Type.Const{const: :integer, meta: %{values: '*'}}, %TypedElixir.Type.Const{const: :integer, meta: %{values: '*'}}, %TypedElixir.Type.Record{labels: [int: %TypedElixir.Type.Const{const: :integer, meta: %{}}, float: %TypedElixir.Type.Const{const: :float, meta: %{}}], meta: %{}}}

Which is entirely expected, but currently the above elm code is working in elmchamy when it absolutely should not.

Looking forward to this being developed out. :-)

Add support for receive function

How to process messages is still up to a debate:

Temporary solution:

Example:

case receive of
  Msg a -> 1

->

receive do
  {:msg, a} -> 1

But since it's impure, unidiomatic, and totally forbids a case of after block it should be discussed and redeveloped after

Make tuples in lists work correctly

Add:

Example:

[(1,1), (1,2)]

->

[{1,1}, {1, 2}]

instead of

[1,1, {1, 2}]

The cause of that is that Lists and Tuples are implemented on binary operator , instead of being parsed as an entity

Change defcurry to def and curry/arity

Change defcurry to something nicer like for example:

a b c d = 10

->

curry a/3
def a(b, c, d), do: 10

Or automatically generate curried functions in @before_compile macro

Add support for module metacode

Example

meta = [
  "use GenServer",
  "defstruct [:something, :else]
]

->

use Genserver
defstruct [:field1, :field2, field3: "default")

Add support for all autoimported Elm standard functions

From https://github.com/elm-lang/core#default-imports :

  • import Basics exposing (..) #25
  • import List exposing ( List, (::) ) #60
  • import Maybe exposing ( Maybe( Just, Nothing ) ) #61
  • import Result exposing ( Result( Ok, Err ) ) #62
  • import String #63
  • import Tuple #64
  • import Debug #65
  • import Platform exposing ( Program )
  • import Platform.Cmd exposing ( Cmd, (!) )
  • import Platform.Sub exposing ( Sub )

Example:

a >> b >> c

->

fn arg -> arg |> a |> b |> c end  

Write tests for already existing features

  • Code

    • Tuples
    • Atoms
    • Type Applications / Tuples
    • Records
    • Strings, Integers, Lists, Floats
    • Function Declarations
    • Function Applications
    • Module definition, function extraction, and elixir module meta code
    • Typespecs and type aliases ( #13 )
  • Standard Library

    • - String module
    • - Basics module
    • - List module
    • - Maybe module

Add support for simple Structs

Add: Simple structs (more than one field is not yet support in ast parser)

Example:

point = {x = 1}
{ point | x = 2 }

->

point = %{x => 1}
%{ point | x => 2 }

Add support for function documentation

Add:

Example:

{-| my function that is awesome -}
function : Awesome
function = Awesome

->

@doc "my function that is awesome"
@spec function :: :awesome
def function(), do: :awesome

This is an AST job because elm-ast sees no difference between block and regular comments

Support returning unfinished functions

Right now specs behave incorrectly when we define a function without taking all of the parameters

Example:

someFun : Int -> String
someFun = 
  to_string

->

@spec some_fun(integer) :: String.t
def some_fun() do
  to_string
end

While it's perfectly fine syntax used very often to make default arguments

Add support for Elm doctests (cause they're cool)

That would make testing with Elmchemy much easier

Example:

{-| Extract the first element of a list.

    head [1,2,3] == Just 1
    head [] == Nothing
-}
head : List a -> Maybe a
head list =
  case list of
    x :: xs ->
      Just x

    [] ->
      Nothing

->

@doc """
 Extract the first element of a list.

    iex> head.([1,2,3])
    1
    iex> head.([])
    nil
"""
@spec head : List a -> Maybe a
def head([x | xs]) do
  x
end
def head([]) do
  nil
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.