Skip to content

Commit

Permalink
Use double-colon for qualified-name delimiter, and adjust cons and in…
Browse files Browse the repository at this point in the history
…fix-operator definition symbols accordingly
  • Loading branch information
Zoetermeer committed Mar 27, 2017
1 parent 1175d9b commit e71218c
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 420 deletions.
50 changes: 25 additions & 25 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ Basic expressions
Latro supports a few primitive operations on built-in types, such as integer
arithmetic: ``1 + 2``, ``1 * 3 - 2 + 4``.

Lists can be constructed using the right-associative cons operator ``::``
Lists can be constructed using the right-associative cons operator ``@``

.. code:: ocaml
1 :: 2 :: [3, 4, 5] // [1, 2, 3, 4, 5]
1 @ 2 @ [3, 4, 5] // [1, 2, 3, 4, 5]
No language would be complete without variable bindings. We define these using
``let``:
Expand Down Expand Up @@ -191,7 +191,7 @@ Yields the list ``[3, 4, 5]``. We can also use the cons operator to destructure
.. code:: ocaml
let ls = [1, 2, 3, 4, 5]
let x::_ = ls
let x@_ = ls
x
Yields the integer ``1``. Notice also that we can use the wildcard pattern
Expand All @@ -202,7 +202,7 @@ Patterns can be used to do arbitrary traversals on a complex value:
.. code:: ocaml
let ls = [[%(1, 2)], [%(3, 4), %(5, 6)]]
let [[%(x, _)], %(_, y) :: _] = ls
let [[%(x, _)], %(_, y) @ _] = ls
x + y
Produces ``5``.
Expand Down Expand Up @@ -391,8 +391,8 @@ given above, e.g.:

.. code:: ocaml
@(&&)(True, True) = True
@(&&)(_, _) = False
infixl (&&)(True, True) = True
infixl (&&)(_, _) = False
Can only be applied as an infix operator, e.g.:

Expand All @@ -407,8 +407,8 @@ a *precedence assignment*:

.. code:: ocaml
@(&&)(True, True) = True
@(&&)(_, _) = False
infixl (&&)(True, True) = True
infixl (&&)(_, _) = False
precedence && 1
Expand All @@ -418,12 +418,12 @@ highest.) Thus the program:

.. code:: ocaml
@(||)(True, _) = True
@(||)(_, True) = True
@(||)(_, _) = False
infixl (||)(True, _) = True
infixl (||)(_, True) = True
infixl (||)(_, _) = False
@(&&)(True, True) = True
@(&&)(_, _) = False
infixl (&&)(True, True) = True
infixl (&&)(_, _) = False
precedence && 1
precedence || 2
Expand Down Expand Up @@ -494,10 +494,10 @@ We might use this particular ADT to define some useful operations on lists:
| Absent
head([]) = Absent()
head(x::_) = Present(x)
head(x@_) = Present(x)
tail([]) = Absent()
tail(_::xs) = Present(xs)
tail(_@xs) = Present(xs)
head([1, 2, 3]) // Present(1)
tail(["hello", "world"]) // Present(["world"])
Expand Down Expand Up @@ -574,10 +574,10 @@ grouped into modules like so:
len : t -> Int
len("") = 0
len(c::cs) = 1 + len(cs)
len(c@cs) = 1 + len(cs)
}
String.len("hello world") // 11
String::len("hello world") // 11
Note also here we are using a list pattern on strings, which works because
strings are really just a list of Unicode characters.
Expand All @@ -590,12 +590,12 @@ Modules can also be arbitrarily nested:
type t = Char[]
module ExtraStringStuff {
append : t -> t -> t
append(c::cs, b) = c :: append(cs, b)
append(c@cs, b) = c @ append(cs, b)
append(_, b) = b
}
}
StringStuff.ExtraStringStuff.append("hello", " world") // "hello world"
StringStuff::ExtraStringStuff::append("hello", " world") // "hello world"
Submodules can refer to all of the types and/or values defined
in parent modules directly, as the ``ExtraStringStuff`` module
Expand All @@ -611,12 +611,12 @@ they can be referred to without using a qualified module path:
type t = Char[]
module ExtraStringStuff {
append : t -> t -> t
append(c::cs, b) = c :: append(cs, b)
append(c@cs, b) = c @ append(cs, b)
append(_, b) = b
}
}
import StringStuff.ExtraStringStuff
import StringStuff::ExtraStringStuff
append("hello", " world") // "hello world"
Expand Down Expand Up @@ -658,10 +658,10 @@ a module later to add bindings to it.
let bar = 43
}
M.bar + M.foo
M::bar + M::foo
Module names are resolved using *qualified identifiers* or paths, where a
path is a sequence of module names separated by dots (``.``). Resolution applies
path is a sequence of module names separated by double colons (``::``). Resolution applies
to the module-reopening semantics, so that a submodule opening will not extend
some other toplevel module with the same name:

Expand All @@ -677,7 +677,7 @@ some other toplevel module with the same name:
}
}
M.bar + M.foo // ERROR: Unbound identifier 'bar'!
M::bar + M::foo // ERROR: Unbound identifier 'bar'!
This code does not compile because ``bar`` is defined on the module
``N.M``, not ``M``. But if we were to try to define a function
Expand All @@ -694,7 +694,7 @@ directly in ``N`` that refers to ``M``:
let bar = 43
}
f() = M.foo //ERROR: Unbound identifier 'M.foo'!
f() = M::foo //ERROR: Unbound identifier 'M::foo'!
}
We can refer directly to the submodule ``M`` inside ``N``, so here
Expand Down
4 changes: 2 additions & 2 deletions examples/monads/maybe.l
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Maybe {
module MonadInfix {
@(>>=)(Just(x), f) = f(x)
@(>>=)(_, _) = Nothing()
infixl (>>=)(Just(x), f) = f(x)
infixl (>>=)(_, _) = Nothing()

return(x) = Just(x)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/monads/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR

latro ../../lib/Core.l maybe.l tests.l
stack exec -- latroi ../../lib/Core.l maybe.l tests.l
8 changes: 4 additions & 4 deletions examples/monads/tests.l
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Core.List
import Maybe.MonadInfix
import Core::List
import Maybe::MonadInfix

itWorksOnDisparateTypeParamsToMaybe() = {
let result =
Just("hello") >>= fun(str) =
Just(length(str)) >>= fun(len) =
return(len + 1)

IO.println(result)
IO::println(result)
}

itShortCircuitsOnNothing() = {
Expand All @@ -16,7 +16,7 @@ itShortCircuitsOnNothing() = {
Nothing() >>= fun(len) =
return(len + 1)

IO.println(result)
IO::println(result)
}

main(_) = {
Expand Down
20 changes: 10 additions & 10 deletions examples/rope/rope.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//Haskell Rope implementation:
//https://github.com/ptrckbrwn/rope
import Core
import Core.List
import Core::List

module Rope {
type Rope =
| Leaf(String)
| Node(Int, Rope, Rope)

length' : Rope -> Int
length'(Leaf(string)) = List.length(string)
length'(Leaf(string)) = List::length(string)
length'(Node(weight, _, _)) = weight
Expand All @@ -23,9 +23,9 @@ module Rope {
delete : Rope -> Int -> Int -> Rope
delete(Leaf(string), i, j) = {
let %(s1, tmp) = String.splitAt(string, i)
let %(_, s2) = String.splitAt(tmp, j - i)
let weight = List.length(s1 ++ s2)
let %(s1, tmp) = String::splitAt(string, i)
let %(_, s2) = String::splitAt(tmp, j - i)
let weight = List::length(s1 ++ s2)
Node(weight, Leaf(s1), Leaf(s2))
}
Expand Down Expand Up @@ -55,10 +55,10 @@ module Rope {

insert : Rope -> Int -> String -> Rope
insert(Leaf(oldString), n, newString) = {
let %(s1, s3) = String.splitAt(oldString, n)
let %(s1, s3) = String::splitAt(oldString, n)
let s2 = newString
let w1 = List.length(s1 ++ s2 ++ s3)
let w2 = List.length(s1 ++ s2)
let w1 = List::length(s1 ++ s2 ++ s3)
let w2 = List::length(s1 ++ s2)

Node(w1, Node(w2, Leaf(s1), Leaf(s2)), Leaf(s3))
}
Expand All @@ -75,8 +75,8 @@ module Rope {
substring : Rope -> Int -> Int -> Maybe<String>
substring(Leaf(str), i, j) = {
let %(_, tmp) = String.splitAt(str, i)
let %(s, _) = String.splitAt(tmp, j - i)
let %(_, tmp) = String::splitAt(str, i)
let %(s, _) = String::splitAt(tmp, j - i)
Just(s)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rope/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR

latro ../../lib/Core.l rope.l tests.l
stack exec -- latroi ../../lib/Core.l rope.l tests.l
10 changes: 5 additions & 5 deletions examples/rope/tests.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import IO
import Rope

itWrapsTwoRopesByASingleNode() = {
let r1 = Rope.Leaf("hello ")
let r2 = Rope.Leaf("world")
let r1 = Rope::Leaf("hello ")
let r2 = Rope::Leaf("world")

println(Rope.concat'(r1, r2))
println(Rope::concat'(r1, r2))
}

itDeletesRangeForLeaf() = {
let leaf = Rope.Leaf("hello")
let leaf = Rope::Leaf("hello")
println(delete(leaf, 1, 4))
}

Expand Down Expand Up @@ -52,5 +52,5 @@ main(_) = {
stressTests
]

Core.List.each(fun(testFun) = testFun(), tests)
Core::List::each(fun(testFun) = testFun(), tests)
}
Loading

0 comments on commit e71218c

Please sign in to comment.