Skip to content
vic

szktty/bran

A strongly-typed language with type inference running on Erlang VM, influenced by OCaml.

szktty/bran.json
{
"createdAt": "2014-11-16T12:32:21Z",
"defaultBranch": "develop",
"description": "A strongly-typed language with type inference running on Erlang VM, influenced by OCaml.",
"fullName": "szktty/bran",
"homepage": null,
"language": "OCaml",
"name": "bran",
"pushedAt": "2014-12-25T15:53:15Z",
"stargazersCount": 100,
"topics": [],
"updatedAt": "2024-11-05T00:45:11Z",
"url": "https://github.com/szktty/bran"
}

Circle CI

A strongly-typed language with type inference running on Erlang VM, influenced by OCaml.

  • Erlang/OTP 17.3
  • OCaml 4.02.1
  • Menhir 20140422
  • Spotlib 2.5.0
  • OMake 0.9.8.6-0.rc1
  • OUnit2 2.0.0
$ omake
  1. Execute make at liberl directory to compile Erlang sources used by compiled Bran modules.
$ cd liberl
$ make
  1. Copy this directory or create a symbolic link to installation destination.
$ ln -s /home/yourname/bran /opt/local/bran
  1. Add path of the bin directory to command line path. (environment variable PATH, etc.)
# .bashrc, etc.
export PATH=/opt/local/bran/bin:$PATH
  1. Set environment variable BRAN_LIBS to the lib directory including signature files of Bran.
# .bashrc, etc.
export BRAN_LIBS=/opt/local/bran/lib
  1. Add path of the liberl/ebin directory, including Erlang modules, to environment variable ERL_LIBS.
# .bashrc, etc.
export ERL_LIBS=/opt/local/bran/liberl/ebin:$ERL_LIBS
  1. OK, let’s try bran command.
$ bran
# compile
$ ./bran fib.br
# use as Erlang module
$ erl
...
1> fib:fib(10).
89
  • Data types
    • Map
    • Option
    • Reference
    • Exception
    • Binary
    • Process
  • Type system
    • Records
    • Variants
    • Polymorphic type
  • Syntax
    • Pattern matching
    • Exception handling
    • Labeled arguments and optional arguments
    • Message passing
  • Compilation
    • Optimization
    • Executable file generation (escript)
  • Library
    • Standard library
    • Obj module
    • OTP
  • Tools
    • Interactive shell
    • Source browsing support
    • Build tool support (rebar)
  • Build
    • Installation
    • Using packaging tools (OPAM, etc.)
  • .bri (interface file)
  • .br (implementation file)
  • unit
  • bool
  • int
  • float
  • char
  • string
  • list (empty lists must be specified type. for example: ([] : int list))
  • bitstring
# comment
@atom
@Atom
@atom_ok
@Atom_ok
@"atom"
@"a t o m"
@"*a+t-o/m!?* :-@"
  • On Bran, String is not a list of character type data. string and char list are different types.
  • Escape sequences are the same as one of Erlang.
"string"
"42"
"hello, world!\n"
"\052"
"\x00\xff"
  • Character literals are single quote characters.
'0'
'a'
'Z'
'\n'
'\052'
'\xff'

Basically bitstring syntax is the same as one of Erlang. See Bit Syntax Expressions.

<<42>>
<<"abc">>
<<1,17,42:16>>
<<123/int-native>>
<<123/unsigned-big-integer>>
<"abc"/utf8>>
  • Erlang’s base#value is base r value (ex. 16rff). Because # is used by comment in Bran.
  • Integer has any number of digits.
42
042 # -> 42
2r101
16r1f
2.3
2.3e3
2.3e-3
7.000e+00
[]
[1]
[1, 2, 3]
[1, 2, 3,]
(1, "a")
[||]
[|1|]
[|1, 2, 3|]
[|1, 2, 3,|]
var x = 1
# Top level
def [rec] f x ... = [block]
# Local
def [rec] f x ... = [block] in ...
# Circular reference
def [rec] f x ... = ...
and f x ... = ...

Top level definition starts at start of line.

def f x y = x + y

In function definition, definition must be indented and end with in. Indent size must be spaces more than one.

def f x =
def f' x' =
...
in
f' x

signature (.bri):

def f : int -> int -> int
f x y # Call function "f" with "x" and "y" arguments
f x $ g y z # => f x (g y z) Haskell's "$"

.bri:

external print_bool : bool -> unit = "bran_lib_pervasives:print_bool"

use Erlang.eval : string -> string.

var result = Erlang.eval "ok."

Conditional expression syntax is:

if [bool-exp] then [block] end
if [bool-exp] then [block] else [block] end
if [bool-exp] then [simple-exp] else [simple-exp]

end” can be omitted when blocks have only an simple expression (literals, field access, array access, type constructor with no arguments and expression with parentheses).

Examples:

if x then
print_string "true"
true
end
if x then
print_string "true"
true
else
print_string "false"
false
end
if x then true else false
if x then true
else false
if x then
true
else
false
for i = 1 to max do
...
end
try [exp] with [pattern] end

Example:

try
...
with
| Not_found -> ...
| e -> raise e
end
perform
x1 <- action1
x2 <- action2
action3 x1 x2
return x1
end