muro-lang.dev / docs
Haskell
muro emit path.muro --backend haskell checks the book and writes <stem>.hs beside it. The file is one module. It needs only the base package. ghc -Wall -Werror accepts it on GHC 9.4 through 9.10. The module is Safe. It does not use unsafePerformIO, unsafeCoerce, unsafeInterleaveIO, or Debug.Trace.
muro emit half_ok.muro --backend haskell
cp half_ok.hs HalfOk.hsmodule Main (main) where
import qualified HalfOk
main :: IO ()
main = print (HalfOk.half 4)ghc -Wall -Werror -O0 -o half Main.hs
./half
That prints 2. The file on disk is named after the stem (half_ok.hs). The module is the stem in UpperCamel (HalfOk), split on characters that are not letters or digits. A stem that starts with a digit is prefixed with Muro. ghc -c half_ok.hs compiles the file. An import looks the module up as HalfOk.hs, so copy or rename the file before another module imports it.
Muro.emit_haskell/2 writes the same file and returns {:ok, [path]}. This backend is not a muro gen kind. A manifest does not name it.
What is emitted
Spec and evidence are omitted. An erased argument, an erased field, and a type index are omitted: no parameter, no field, and no unit placeholder. A run is a function. run internal is compiled and left out of the export list, and so is a generated step. A book with no run and no data is <file> has no run to emit. A book of data alone is the types.
The first line is -- Code generated by muro <version>. DO NOT EDIT. The language line is Safe, NoImplicitPrelude, BangPatterns, StrictData, and NoGeneralizedNewtypeDeriving. GHC2021 turns on GeneralizedNewtypeDeriving, and Safe rejects that extension, so the module turns it off. Imports are qualified (Prelude as MuroP, Numeric.Natural as MuroNat, Data.Word as MuroWord, Data.Int as MuroInt, and Data.Bits as MuroBits). The emitter records each qualifier with the fragment that needs it (a declaration, an instance, a helper, or a run), and includes a qualifier from a helper when that helper is itself used. A fragment that is not emitted does not contribute imports, so a dropped Muro__Rnf instance cannot pull in an unused Prelude. A string or bytes literal that happens to mention MuroNat. does not pull in an unused import.
| Muro | Haskell |
|---|---|
Nat |
Numeric.Natural.Natural |
Unit |
() |
WBool |
Bool |
U8, U32, U64 |
Word8, Word32, Word64 |
I64 |
Int64 |
String |
String |
Bytes, Slice |
Muro__Bytes, a strict list of Word8 |
Opt a |
Muro__Opt a, payload lazy |
a × b |
Muro__Pair a b, both fields lazy |
Stream a |
Muro__Stream a, head and tail lazy |
| a function |
-> |
data |
data, fields strict unless the field is a stream |
| an atom |
one constructor of data Atom |
Nat is arbitrary precision. Subtraction is truncated: m - n is 0 when n is greater than m, not the Natural exception. The library pred, plus, max, min, minus, and compare lower to those operations when the body is the one the library pins. A user plus with that body is the same operation. suc adds one.
U8, U32, U64, and I64 follow Muro.Word. Arithmetic wraps. A shift by a Nat at or past the width is 0, and an arithmetic right shift of a negative I64 by that much is -1. Division and remainder by zero are Muro__None. So is I64 division or remainder of the minimum by -1. Checked addition, subtraction, and multiplication are Muro__Opt. quot and rem truncate toward zero.
String length is the number of Char values, as a Natural, which is muro’s scalar length. The whole spine is forced, because a bang pattern only forces the first cons. Bytes and Slice are the same strict list. A string becomes UTF-8 when a byte operation reads it. Bool and, or, xor, and not force their arguments. Haskell’s && is lazy, and muro’s cost model is not.
A data declaration keeps its type parameters. A value index is dropped, the way the other pure backends drop a parameter whose domain is not Type. A Type parameter is kept when a value parameter precedes it, so Foo (n : Nat) (A : Type) is Foo a. A constructor field that is a runtime Nat stays, so Fin still carries that number and Vec A n is Vec a. Eq and Show are derived when no field is a function or a stream, including through a nested type. Muro__Stream does not derive them. Muro__Pair, Muro__Opt, and Muro__Bytes do. A user data type with no constructors is emitted under its own name with an empty constructor list, for example data Void. That is distinct from the builtin Empty, which is Muro__Empty. The unreachable branch of an indexed match, which the checker skips because the index cannot match, is muro__abort. That binding diverges and has the result type. It is not a call of error, and a reached branch does not call it.
A lambda is a lambda. A closure captures the values it uses. Application is ordinary application.
Strictness and codata
Forcing matches evaluation. Every value that is not a stream is forced where Muro.IR.Eval would force it, including when the layout is unknown. An unknown or opaque layout is forced at least to weak head normal form. When the Haskell type can carry an instance without a constraint on a type parameter, that same value is deep-forced by muro__deep. The class is Muro__Rnf. muro__rnf reduces a value to () in place, and muro__deep is that reduction followed by the original value. Each equation sequences the non-codata fields and recurses on the last recursive field in tail position, so forcing a long spine does not build stack. A type parameter is forced to weak head normal form: the class would need Muro__Rnf a, and a constructor such as nothing does not determine a. A signature carries no Muro__Rnf constraint. A stream stays lazy only when the layout is positively a stream. An unknown layout is forced. A stream field is not walked.
Arguments and let bindings are banged, so a recursive accumulator does not build a chain of thunks. The result of a tail call is not wrapped. Data fields are strict under StrictData, except a stream field, which is marked lazy (~).
A guarded stream is an ordinary lazy value. Building it forces the seed and the captures and does not run the first step. The step runs when the head or the tail is demanded. Muro__Pair and Muro__Opt mark their fields lazy too, so a stream can sit in them. Evaluation still forces both halves of a pair, and the payload of Some, when the value is built. Those forces are bindings outside the constructor, and Muro__Rnf walks the fields. Opening a pair or an option forces a non-stream component again. A string is forced down its spine, because a bang stops at the first cons. The same force is applied to a constructor argument, including a string field whose element type is a parameter. Observing take n terminates. A stream parameter is still banged, which forces one constructor, not the tail.
Builtin Empty is data Muro__Empty = Muro__Empty. matchEmpty is the diverging binding muro__abort. GHC reports that black hole as <<loop>>.
Affinity
The module does not enable LinearTypes. A GHC linear argument is used exactly once. Muro’s affine grade means at most once, including not at all. The checker already rejected a second use. The generated comment in the module says this. Session typestate is not part of this backend.
Names
Values start with a lowercase letter. Types and constructors start with an uppercase letter. The spelling is the one Muro.Emit.Name.encode/1 uses, which is injective: z doubles, _ is zu, - is zd, and ' is zq. size is sizze. foo' is foozq. len-one is lenzdone.
A value that is a Haskell keyword, or one of forall, mdo, rec, proc, family, pattern, and the other words this backend reserves, gains _k. case is case_k. A value whose encoding starts with an uppercase letter is lowercased and gains _c, so True is true_c and does not clash with the boolean. A type whose encoding starts with a lowercase letter is capitalized and gains a trailing '. A type variable is lowercase, so A is a_c.
Constructors share one module namespace. Each is the type’s Haskell name, an underscore, and the constructor body: Tree.leaf is Tree_Leaf' and Bush.twig is Bush_Twig'.
Claim order is the reserved qualifiers and helpers (MuroP, MuroNat, MuroWord, MuroInt, MuroBits, and the runtime helpers), then types, then constructors, then runs, then local binders against the names already in scope. A second claim of the same spelling is _2, then _3. data MuroP is the type MuroP_2. A parameter named m inside plus is m_2 when a run named m was claimed first. An unused binder is !_, except a binder written _ that the body uses, which is claimed (zu).
What is refused
A meeting that marks a party may fail is refused first, with c:mayfail and may fail is checked and not emitted, the same text as the other backends that refuse it.
A book that declares a session, a choreography, a keyed family, a temporal formula, a conformance, or a meeting is refused as a whole. The message names the file, the line, the column, the kind, and the name: examples/ping.muro:5:1: haskell: session ping is not emitted.
An F32 or machine run (F32, Tensor, addt, packI) is haskell: machine in <name>. A host or IO effect is haskell: host in <run>. A use of an erased binding is haskell: erased in <run>. A term this image cannot lower is haskell: unsupported in <run>, or haskell: opaque in <run> when a type variable was not recovered. The message is located when the declaration has a position. A book with no run and no data is <file> has no run to emit. The file is not written.
Source on GitHub
· fetched from murolang/muro manual/