about summary refs log blame commit diff stats
path: root/index.html
blob: 98df9a9ee39e76f87d1bce8fec89263209dcc795 (plain) (tree)
1
2
3
4
5
6
7
8
9

                 
                                                                                                                
   

                                                               
   

      
 


                                                                                    
 
   


                                                                                                                  
 
                                                 


                                                                          
                                                                             

                                                                          
                                                                           

                                                                            
                                                                              
                                   


                                                                                                 
                                              
                                                                        
                                                                              
            


                                                                          

                                                                                                                    
                                                                          



                                                                           


                                                                             
                                                                                                                                                 

     



                                                                                                            
                                      
 



                                                                                                                           
                                                                                   
                                                                            
                                                                        
                                              
                                                                        

                                                                          

                                                                                                                          
                                                                       
 
                                                                             
                 
 
                                                                    
                                                       
                                                                              
                                                        
                                                                      
                                                                         
                                                                        
                                                                       
                                                                           
                                                                       
                                                                              
                                                              
                                                                               
                                                 




                                                                            










                                                                               
                                                                         



                                                                             

                                                                                      

                                                                                                    
 


                                                                               
                                                                            





                                                                              

                                                                                    

                                                                           
                                                                           
                                                                        


                                                                                                 


                                                                                      
 













                                                                                                           

                                                                              
 
                                                                                               

                            
                                                                                                                        

                                                                                                 


                                                                              
                                                                                        
                                     




                                                                            
                                                                            

                                                                  
                                                                              
                              
                                                                      

                                                                         
                                                                       

                                                                           
                                                                              
                            




                                                                            
                                                                               
                                                                   
                                           
                                                                             

                                                                                         
                                        












                                                                                                                          
                                                               
 
                                 
 
                                                                    
                                                                            

                                                                            
                                                                        
                           








                                                                                                                                         

                                                                         
 
                                                           
 

                                                                       
                                                                           
                                           
                                                                         
                                                   

                                                                          
                                                            
                                                                           
                                                     
                                                              











                                                                                 
 
                                                                      
                                              
                                                                       
                                                                       

                                                                    




                                                                                                              
 


                                                                         








                                                                                                     
                                                                            
                                     

                                                                               
                                                                                   




                                                                               

                                                                              
                                                                                 

                                                                              



                                                                              
                                                                  
                                                                             
                                      
 

    


              



                                                    

     


                      








                                                                                  
                                                                                        




                                                                             
 
                               
<title>Mu</title>

With apologies to <a href='http://en.wikipedia.org/wiki/Mu_%28negative%29#In_popular_culture'>Robert Pirsig</a>:
<p>
<div style='font-style: italic; margin-left:2em'>
Is it a language, or an operating system, or a virtual machine?
<p>
Mu.
</div>

Read these first: <b><a href='http://akkartik.name/about'>problem statement</a></b>,
<b><a href='http://github.com/akkartik/mu#readme'>trying out Mu</a></b>.
(Mu requires minimal dependencies.)

<p>
Mu's code looks quite alien, requiring editors to be specially configured to
colorize it in a sane manner. So this page provides links to the source files
showing how it currently looks in my <a href='https://github.com/akkartik/mu/blob/master/mu.vim'>custom setup</a>.

<p>Whetting your appetite, some example programs:

<ul>
<li><a href='html/x.mu.html'>x.mu</a>: a simple program to add two numbers
together. Shows that at bottom Mu is a simple VM bytecode designed to convert
directly to machine code.
<li><a href='html/factorial.mu.html'>factorial.mu</a>: everyone's favorite
example, showing how Mu supports conditionals and loops without any special
syntax, using the special labels '{' and '}'.
<li><a href='html/tangle.mu.html'>tangle.mu</a>: another (contrived) version
of factorial showing Mu's ability to 'tangle' code from multiple places into a
single function or <em>recipe</em>.
<li>simple examples showing off support for concurrency: <a href='html/fork.mu.html'>fork.mu</a>,
<a href='html/channel.mu.html'>channel.mu</a>
<li>simple examples showing off hardware control: <a href='html/display.mu.html'>display.mu</a>,
<a href='html/console.mu.html'>console.mu</a>.
<li><a href='html/screen.mu.html'>screen.mu</a>: example program showing
print primitives that inject a 'screen' <em>dependency</em> which can be faked
for testing.
<li><a href='html/filesystem.mu.html'>filesystem.mu</a>: example program
showing file primitives that inject a 'filesystem' dependency which can be
faked for testing.
<li><a href='html/http-client.mu.html'>http-client.mu</a> and <a href='html/http-server.mu.html'>http-server.mu</a>,
examples of Mu's testable high-level interfaces to the network.
<li><a href='html/static-dispatch.mu.html'>static-dispatch.mu</a>: example
program showing mu's ability to define recipes with headers, and thereby to
allow functions with the same name but arguments of different types to
coexist.
<li><a href='html/counters.mu.html'>counters.mu</a>: lexical scope
<li><a href='html/chessboard.mu.html'>chessboard.mu</a>: a little program for
2 people to play chess, with thorough tests of its behavior including both
screen and keyboard handling.
<li><a href='html/nqueens.mu.html'>nqueens.mu</a>: a solution to the <a href='http://rosettacode.org/wiki/N-queens_problem'>N queens problem</a>.
</ul>

Now a listing of every layer in mu. Recall that you can <a href='http://akkartik.name/post/wart-layers'>stop
loading at any layer and get a valid program to run with a subset of features,
that passes all its tests</a>.

<p><b>Part I</b>: basic infrastructure

<p/><a href='html/000organization.cc.html'>000organization.cc</a>: the basic
skeleton program. Compiles and runs but doesn't do much. Later <em>layers</em>
hook into this skeleton to add functionality. Mu's guarantee: you can <a href='http://youtube.com/watch?v=c8N72t7aScY'>load
features</a> up until any layer, and it will compile and pass all tests until
that point. <a href='http://akkartik.name/post/wart-layers'>More details &rarr;</a>
<br/><a href='html/001help.cc.html'>001help.cc</a>: just a simple test layer
to show how to hook into the skeleton. Also summarizes how to invoke Mu,
behaviors that later layers will be providing.
<br/><a href='html/002test.cc.html'>002test.cc</a>: Mu's minimalist test
harness, relying on a couple of one-liners in the <tt>build</tt> script to
auto-generate lists of tests to run.
<br/><a href='html/003trace.cc.html'>003trace.cc</a>: support for logging
facts about our program, and for <a href='http://akkartik.name/post/tracing-tests'>checking the facts logged in tests</a>.
(<a href='html/003trace.test.cc.html'>tests for the tracing system</a>)

<p><b>Part II</b>: the core Mu virtual machine, designed to compile easily to
machine language.

<p/><a href='html/010vm.cc.html'>010vm.cc</a>: core data structures:
recipes, instructions and <em>reagents</em> (operands).
<br/><a href='html/011load.cc.html'>011load.cc</a>: the textual representation
of recipes and how it's turned into the data structures.
<br/><a href='html/012transform.cc.html'>012transform.cc</a>: after Mu
programs are loaded but before they are run they can be transformed in an
extensible manner akin to lisp macros. Think of this as the core of Mu's
&lsquo;compiler&rsquo; for providing high-level features atop the core.
<br/><a href='html/013update_operation.cc.html'>013update_operation.cc</a>:
our first transform: check for unknown recipes before the program runs.
<br/><a href='html/014literal_string.cc.html'>014literal_string.cc</a>: extend
the loader to support literal strings in various instructions.
<br/><a href='html/015literal_noninteger.cc.html'>015literal_noninteger.cc</a>:
extend the loader to support non-integer numbers.
<br/><a href='html/016dilated_reagent.cc.html'>016dilated_reagent.cc</a>:
allowing whitespace in reagents.
<br/><a href='html/017parse_tree.cc.html'>017parse_tree.cc</a>: a new syntax
for representing complex types as trees using whitespace and parentheses
(s-expressions).
<br/><a href='html/018type_abbreviations.cc.html'>018type_abbreviations.cc</a>:
the core types of Mu are designed to be fully explicit and familiar to
non-programmers at the cost of some verbosity: <tt>number</tt>,
<tt>character</tt>, <tt>boolean</tt>, etc. Once learners get acclimatized,
we can teach them abbreviated forms that are familiar to veteran programmers:
<tt>num</tt>, <tt>char</tt>, <tt>bool</tt>. Mu's facility for type
abbreviations is extensible: learners can abbreviate <tt>number</tt> to
<tt>n</tt> if they so choose, thereby exploring such trade-offs. You can also
create abbreviations suitable for a specific program, like abbreviating
<tt>address:array:address:array:character</tt> to <tt>board</tt> for say a
tic-tac-toe or chess program. Think C's <tt>typedef</tt> statement.
<br/><a href='html/020run.cc.html'>020run.cc</a>: executing Mu recipes by
executing the list of instructions they contain. Future layers will define
more primitive operations that can be used in instructions.
<br/><a href='html/021check_instruction.cc.html'>021check_instruction.cc</a>:
harness for adding per-primitive checks to run before running a program.

<p/>Various primitive operations: on <a href='html/022arithmetic.cc.html'>numbers</a>,
<a href='html/023boolean.cc.html'>booleans</a>, for <a href='html/024jump.cc.html'>control flow</a>,
and <a href='html/025compare.cc.html'>comparing values</a>.

<p/>Support for <a href='html/026call.cc.html'>defining new recipes</a>. In
Mu calls to functions look just like primitive operations, with the ability to 
<a href='html/027call_ingredient.cc.html'>pass in ingredients</a>, and to 
<a href='html/028call_return.cc.html'>return products</a>. In particular, Mu
supports returning multiple values, and uses this ability far more pervasively
than high-level languages can.

<p/>Support for various data structures: heterogeneous compound types called
<a href='html/030container.cc.html'><em>containers</em></a>, akin to records
or structs, homogeneous <a href='html/032array.cc.html'>arrays</a> of a single
type of value (type <tt>array</tt> conventionally abbreviated as <tt>@</tt>),
and <a href='html/033exclusive_container.cc.html'><em>exclusive containers</em></a>,
akin to C unions but with a tag so each value knows its &lsquo;kind&rsquo;.
Out of these primitive types, Mu builds the usual and growing menagerie of
data structures: <a href='html/064list.mu.html'>linked lists</a> permitting
fast insertion and deletion and unidirectional scanning but slow search;
<a href='html/065duplex_list.mu.html'><em>duplex lists</em></a> that permit
bidirectional scanning; <a href='html/070table.mu.html'>associative arrays or <em>tables</em></a>
for fast insertion, deletion and search using <a href='html/069hash.cc.html'>hash</a>
functions; <a href='html/066stream.mu.html'><em>streams</em></a> for scanning
through strings incrementally; and <a href='html/061text.mu.html'><em>buffers</em></a>
for gradually constructing long strings in a piecemeal fashion.

<p/>Dynamic memory management: Mu supports <a href='html/034address.cc.html'>allocating</a>
space at run-time as pointers or <em>addresses</em>. All Mu instructions can
dereference or <a href='html/035lookup.cc.html'><em>lookup</em></a> addresses
of values in addition to operating on regular values. These addresses are
manually managed like C. However, all allocations are transparently
reference-counted or <a href='html/036refcount.cc.html'><em>refcounted</em></a>,
with every copy of a pointer updating refcounts appropriately. When the
refcount of an allocation drops to zero it is transparently <a href='html/037abandon.cc.html'>reclaimed</a>
and made available to future allocations. By construction it is impossible to
reclaim memory prematurely, while some other part of a program is still
pointing to it. This eliminates a whole class of undefined behavior and
security vulnerabilities that plague C. Compared to Rust, Mu pays some
additional runtime cost in exchange for C-like flexibility (you can copy
addresses around all you like, and write from any copy of an address) and
simpler implementation (no static analysis). Mu by convention abbreviates type
<tt>address</tt> to <tt>&amp;</tt>.

<p/>Support for higher-order recipes that can pass <a href='html/071recipe.cc.html'>recipes</a>
around like any other value.

<p/>Support for running multiple functions concurrently using <a href='html/072scheduler.cc.html'><em>routines</em></a>,
for communicating between routines using <a href='html/075channel.mu.html'><em>channels</em></a>,
and for <a href='html/073wait.cc.html'>synchronizing</a> between routines.
Channels are Mu's only synchronization primitive, queues that can cause the
routine reading or writing from them to stall without taking up CPU resources.
Mu provides safe concurrency by forbidding routines from sharing addresses;
writing to a channel always performs a <a href='html/074deep_copy.cc.html'>deep copy</a>
that preserves all internal aliasing.

<p><b>Part III</b>: transforms to make Mu a little more expressive, and give
it some of the benefits of a high-level language.

<p/><a href='html/040brace.cc.html'>040brace.cc</a> and
<a href='html/041jump_target.cc.html'>041jump_target.cc</a>: how Mu provides
structured goto-less programming without introducing the syntax of
conditionals and loops other languages require.
<br/><a href='html/042name.cc.html'>042name.cc</a>: how Mu transforms variable
names to raw memory addresses.
<br/><a href='html/043space.cc.html'>043space.cc</a>: how variables in
different routines are isolated from each other using <em>spaces</em>. Mu
&lsquo;local variables&rsquo; are allocated on the heap.
<br/><a href='html/044space_surround.cc.html'>044space_surround.cc</a>:
Chaining spaces together to accomodate variables with varying lifetimes and
ownership properties.
<br/><a href='html/045closure_name.cc.html'>045closure_name.cc</a>: how spaces
can implement lexical scope.
<br/><a href='html/046global.cc.html'>046global.cc</a>: experimental support
for 'global' variables that are always available inside a single routine. Mu
has no variables that are available transparently across routines, and this
might go away as well. Global variables are currently not checked as
rigorously as the rest of the type system.
<br/><a href='html/047check_type_by_name.cc.html'>047check_type_by_name.cc</a>:
a transform to deduce missing types in instructions on the basis of
previous instructions in the same function.
<br/><a href='html/050scenario.cc.html'>050scenario.cc</a>: Mu's first syntax
&mdash; not for code but for tests. (<a href='html/051scenario_test.mu.html'>example</a>)
<br/><a href='html/052tangle.cc.html'>052tangle.cc</a>: support for layers in
Mu programs. They've been so good to us.
<br/><a href='html/053recipe_header.cc.html'>053recipe_header.cc</a>:
a new syntax for summarizing the number and types of ingredients and products
a function expects at the top next to its name, in a <em>header</em>.
<br/><a href='html/054static_dispatch.cc.html'>054static_dispatch.cc</a>:
allowing multiple variants of a function to coexist as long as each has a
unique header.
<br/>Support for generic or <em>shape-shifting</em> <a href='html/055shape_shifting_container.cc.html'>data structures</a>
and <a href='html/056shape_shifting_recipe.cc.html'>recipes</a>, containing
wildcard type ingredients that start with an &lsquo;_&rsquo;. Everytime you
use a shape-shifting recipe with a new set of &lsquo;concrete&rsquo; types
for its type ingredients, it creates a new variant of the recipe for you
matching those types.
<br/><a href='html/057immutable.cc.html'>057immutable.cc</a>, a static analysis to
ensure that functions never modify anything but their products.

<p><b>Part IV</b>: Miscellaneous.

<p/><a href='html/061text.mu.html'>061text.mu</a>: strings in Mu are
bounds-checked rather than null-terminated. They're also unicode-aware (code
points only; no control characters, no combining characters, no
normalization). Mu recipes that take strings can take literal strings thanks
to a <a href='html/060rewrite_literal_string.cc.html'>transform</a> that
allocates them on the heap.
<br/><a href='html/062convert_ingredients_to_text.cc.html'>062convert_ingredients_to_text.cc</a>:
a convenience transform primarily intended to provide the illusion of dynamic
typing when adding to the trace. The <span style='font-family:courier,fixed'>stash</span>
command can print any number of ingredients of any type into the trace. Its
default output format is fairly simplistic, but it can be overridden for
arbitrary types by defining a variant of the <a href='html/058to_text.cc.html'><span style='font-family:courier,fixed'>to-text</span></a>
function with an ingredient of the appropriate type. For example, see
<a href='html/064list.mu.html'>064list.mu</a> which defines how we trace
lists.
<br/><a href='html/067random.cc.html'>067random.cc</a>: a random-number
generator with a <a href='html/068random.mu.html'>testable</a> interface.

<p><b>Part V</b>: Primitives for interfacing with hardware.

<p/><a href='html/080display.cc.html'>080display.cc</a>: primitives for
accessing the keyboard and screen.
<br/><a href='html/081print.mu.html'>081print.mu</a>: helpers that can swap
the real screen with fake ones for testing.
<br/><a href='html/082scenario_screen.cc.html'>082scenario_screen.cc</a>:
writing tests that check what is printed to screen.
(<a href='html/083scenario_screen_test.mu.html'>examples</a>)
<br/><a href='html/084console.mu.html'>084console.mu</a>: helpers that can
swap the real keyboard and mouse with fake ones for testing.
<br/><a href='html/085scenario_console.cc.html'>085scenario_console.cc</a>:
writing tests for keyboard and mouse using the fakes.
(<a href='html/086scenario_console_test.mu.html'>examples</a>)
<br/><a href='html/087file.cc.html'>087file.cc</a>: primitives for accessing
the file system.
<br/><a href='html/088file.mu.html'>088file.mu</a>: helpers that permit
swapping a fake filesystem or <tt>resources</tt> object for testing.
<br/><a href='html/089scenario_filesystem.cc.html'>089scenario_filesystem.cc</a>:
writing tests for filesystem using the fakes.
(<a href='html/090scenario_filesystem_test.mu.html'>examples</a>)
<br/><a href='html/091socket.cc.html'>091socket.cc</a>: primitives for
accessing the network.
<br/><a href='html/092socket.mu.html'>092socket.mu</a>: helpers for the
network. In Mu you create a fake network &lsquo;neighborhood&rsquo; the same
way you create a fake local file system.

<p/><a href='html/029tools.cc.html'>029tools.cc</a>: various primitive
operations to help with testing and debugging.
<br/><a href='html/100trace_browser.cc.html'>100trace_browser.cc</a>: a
zoomable UI for inspecting traces generated by Mu programs. Allows both
scanning a high-level view and drilling down into selective details.

<p><b>Part VI</b>: An environment that watches programmers as they
manually test their code, and turns these interactive sessions into reproducible
test scenarios. The <a href='https://github.com/akkartik/mu/blob/master/edit/Readme.md'>readme for the app</a>
contains instructions for running it. Stop loading after each of these layers
to get a working version with just fewer features. 

<p/><a href='html/edit/001-editor.mu.html'>edit/001-editor.mu</a>: data
structures for a text editor widget. Load just this layer to see just the
rendering and line-wrapping at work.
<br/><a href='html/edit/002-typing.mu.html'>edit/002-typing.mu</a>: support
for moving the cursor anywhere with the mouse and typing text in there.
<br/><a href='html/edit/003-shortcuts.mu.html'>edit/003-shortcuts.mu</a>:
support for various keyboard shortcuts for manipulating text you've typed in.
<br/><a href='html/edit/004-programming-environment.mu.html'>edit/004-programming-environment.mu</a>:
combining two text editor widgets, one on the left, one on the right.
<br/><a href='html/edit/005-sandbox.mu.html'>edit/005-sandbox.mu</a>: support
for running mu code in the right-hand widget using code from the left, and
displaying results in a <em>sandbox</em> below on the right. You can have
multiple sandboxes, and hit F4 to rerun them all at any time with the latest
version of the code on the left side.
<br/><a href='html/edit/006-sandbox-copy.mu.html'>edit/006-sandbox-edit.mu</a>:
click on the 'copy' button in each sandbox to duplicate its contents.
<br/><pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: Exclusive containers contain exactly one of a fixed number of 'variants'
//: of different types.
//:
//: They also implicitly contain a tag describing precisely which variant is
//: currently stored in them.

:(before "End Mu Types Initialization")
//: We'll use this container as a running example, with two number elements.
{
type_ordinal tmp = put(Type_ordinal, "number-or-point", Next_type_ordinal++);
get_or_insert(Type, tmp);  // initialize
get(Type, tmp).kind = EXCLUSIVE_CONTAINER;
get(Type, tmp).name = "number-or-point";
get(Type, tmp).elements.push_back(reagent("i:number"));
get(Type, tmp).elements.push_back(reagent("p:point"));
}

//: Tests in this layer often explicitly set up memory before reading it as an
//: array. Don't do this in general. I'm tagging exceptions with /raw to keep
//: checks in future layers from flagging them.
:(scenario copy_exclusive_container)
# Copying exclusive containers copies all their contents and an extra location for the tag.
def main [
  1:num <- copy 1  # 'point' variant
  2:num <- copy 34
  3:num <- copy 35
  4:number-or-point <- copy 1:number-or-point/unsafe
]
+mem: storing 1 in location 4
+mem: storing 34 in location 5
+mem: storing 35 in location 6

:(before "End size_of(type) Cases")
if (t.kind == EXCLUSIVE_CONTAINER) {
  // Compute size_of Exclusive Container
  return get(Container_metadata, type).size;
}
:(before "End compute_container_sizes Atom Cases")
if (info.kind == EXCLUSIVE_CONTAINER) {
  compute_exclusive_container_sizes(info, type, pending_metadata);
}

:(code)
void compute_exclusive_container_sizes(const type_info& exclusive_container_info, const type_tree* full_type, set<type_tree>& pending_metadata) {
  // size of an exclusive container is the size of its largest variant
  // (So, like containers, it can only contain arrays if they're static and
  // include their length in the type.)
  container_metadata metadata;
  for (int i = 0; i < SIZE(exclusive_container_info.elements); ++i) {
    reagent/*copy*/ element = exclusive_container_info.elements.at(i);
    // Compute Exclusive Container Size(element, full_type)
    compute_container_sizes(element.type, pending_metadata);
    int variant_size = size_of(element);
    if (variant_size > metadata.size) metadata.size = variant_size;
  }
  // ...+1 for its tag.
  ++metadata.size;
  Container_metadata.push_back(pair<type_tree*, container_metadata>(new type_tree(*full_type), metadata));
}

//:: To access variants of an exclusive container, use 'maybe-convert'.
//: It always returns an address (so that you can modify it) or null (to
//: signal that the conversion failed (because the container contains a
//: different variant).

//: 'maybe-convert' requires a literal in ingredient 1. We'll use a synonym
//: called 'variant'.
:(before "End Mu Types Initialization")
put(Type_ordinal, "variant", 0);

:(scenario maybe_convert)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:point, 22:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
# boolean
+mem: storing 1 in location 22
# point
+mem: storing 35 in location 20
+mem: storing 36 in location 21

:(scenario maybe_convert_fail)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:num, 21:bool <- maybe-convert 12:number-or-point/unsafe, 0:variant
]
# boolean
+mem: storing 0 in location 21
# number: no write

:(before "End Primitive Recipe Declarations")
MAYBE_CONVERT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "maybe-convert", MAYBE_CONVERT);
:(before "End Primitive Recipe Checks")
case MAYBE_CONVERT: {
  const recipe& caller = get(Recipe, r);
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 ingredients in '" << inst.original_string << "'\n" << end();
    break;
  }
  reagent/*copy*/ base = inst.ingredients.at(0);
  // Update MAYBE_CONVERT base in Check
  if (!base.type) {
    raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
    break;
  }
  const type_tree* base_type = base.type;
  // Update MAYBE_CONVERT base_type in Check
  if (!base_type->atom || base_type->value == 0 || !contains_key(Type, base_type->value) || get(Type, base_type->value).kind != EXCLUSIVE_CONTAINER) {
    raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
    break;
  }
  if (!is_literal(inst.ingredients.at(1))) {
    raise << maybe(caller.name) << "second ingredient of 'maybe-convert' should have type 'variant', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
    break;
  }
  if (inst.products.empty()) break;
  if (SIZE(inst.products) != 2) {
    raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 products in '" << inst.original_string << "'\n" << end();
    break;
  }
  reagent/*copy*/ product = inst.products.at(0);
  // Update MAYBE_CONVERT product in Check
  reagent& offset = inst.ingredients.at(1);
  populate_value(offset);
  if (offset.value >= SIZE(get(Type, base_type->value).elements)) {
    raise << maybe(caller.name) << "invalid tag " << offset.value << " in '" << inst.original_string << '\n' << end();
    break;
  }
  const reagent& variant = variant_type(base, offset.value);
  if (!types_coercible(product, variant)) {
    raise << maybe(caller.name) << "'maybe-convert " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should write to " << to_string(variant.type) << " but '" << product.name << "' has type " << to_string(product.type) << '\n' << end();
    break;
  }
  reagent/*copy*/ status = inst.products.at(1);
  // Update MAYBE_CONVERT status in Check
  if (!is_mu_boolean(status)) {
    raise << maybe(get(Recipe, r).name) << "second product yielded by 'maybe-convert' should be a boolean, but tried to write to '" << inst.products.at(1).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case MAYBE_CONVERT: {
  reagent/*copy*/ base = current_instruction().ingredients.at(0);
  // Update MAYBE_CONVERT base in Run
  int base_address = base.value;
  if (base_address == 0) {
    raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
    break;
  }
  int tag = current_instruction().ingredients.at(1).value;
  reagent/*copy*/ product = current_instruction().products.at(0);
  // Update MAYBE_CONVERT product in Run
  reagent/*copy*/ status = current_instruction().products.at(1);
  // Update MAYBE_CONVERT status in Run
  // optimization: directly write results to only update first product when necessary
  if (tag == static_cast<int>(get_or_insert(Memory, base_address))) {
    const reagent& variant = variant_type(base, tag);
    trace(9999, "mem") << "storing 1 in location " << status.value << end();
    put(Memory, status.value, 1);
    if (!is_dummy(product)) {
      // Write Memory in Successful MAYBE_CONVERT in Run
      for (int i = 0; i < size_of(variant); ++i) {
        double val = get_or_insert(Memory, base_address+/*skip tag*/1+i);
        trace(9999, "mem") << "storing " << no_scientific(val) << " in location " << product.value+i << end();
        put(Memory, product.value+i, val);
      }
    }
  }
  else {
    trace(9999, "mem") << "storing 0 in location " << status.value << end();
    put(Memory, status.value, 0);
  }
  goto finish_instruction;
}

:(code)
const reagent variant_type(const reagent& base, int tag) {
  return variant_type(base.type, tag);
}

const reagent variant_type(const type_tree* type, int tag) {
  assert(tag >= 0);
  const type_tree* root_type = type->atom ? type : type->left;
  assert(contains_key(Type, root_type->value));
  assert(!get(Type, root_type->value).name.empty());
  const type_info& info = get(Type, root_type->value);
  assert(info.kind == EXCLUSIVE_CONTAINER);
  reagent/*copy*/ element = info.elements.at(tag);
  // End variant_type Special-cases
  return element;
}

:(scenario maybe_convert_product_type_mismatch)
% Hide_errors = true;
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:num, 21:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
+error: main: 'maybe-convert 12:number-or-point/unsafe, 1:variant' should write to point but '20' has type number

:(scenario maybe_convert_dummy_product)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  _, 21:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
$error: 0

//:: Allow exclusive containers to be defined in mu code.

:(scenario exclusive_container)
exclusive-container foo [
  x:num
  y:num
]
+parse: --- defining exclusive-container foo
+parse: element: {x: "number"}
+parse: element: {y: "number"}

:(before "End Command Handlers")
else if (command == "exclusive-container") {
  insert_container(command, EXCLUSIVE_CONTAINER, in);
}

//: arrays are disallowed inside exclusive containers unless their length is
//: fixed in advance

:(scenario exclusive_container_contains_array)
exclusive-container foo [
  x:array:num:3
]
$error: 0

:(scenario exclusive_container_disallows_dynamic_array_element)
% Hide_errors = true;
exclusive-container foo [
  x:array:num
]
+error: container 'foo' cannot determine size of element 'x'

//:: To construct exclusive containers out of variant types, use 'merge'.
:(scenario lift_to_exclusive_container)
exclusive-container foo [
  x:num
  y:num
]
def main [
  1:num <- copy 34
  2:foo <- merge 0/x, 1:num  # tag must be a literal when merging exclusive containers
  4:foo <- merge 1/y, 1:num
]
+mem: storing 0 in location 2
+mem: storing 34 in location 3
+mem: storing 1 in location 4
+mem: storing 34 in location 5

//: type-checking for 'merge' on exclusive containers

:(scenario merge_handles_exclusive_container)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  z:num
]
def main [
  1:foo <- merge 0/x, 34
]
+mem: storing 0 in location 1
+mem: storing 34 in location 2
$error: 0

:(scenario merge_requires_literal_tag_for_exclusive_container)
% Hide_errors = true;
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  z:num
]
def main [
  1:num <- copy 0
  2:foo <- merge 1:num, 34
]
+error: main: ingredient 0 of 'merge' should be a literal, for the tag of exclusive-container 'foo' in '2:foo <- merge 1:num, 34'

:(scenario merge_handles_exclusive_container_inside_exclusive_container)
exclusive-container foo [
  x:num
  y:bar
]
exclusive-container bar [
  a:num
  b:num
]
def main [
  1:num <- copy 0
  2:bar <- merge 0/a, 34
  4:foo <- merge 1/y, 2:bar
]
+mem: storing 0 in location 5
+mem: storing 34 in location 6
$error: 0

:(before "End check_merge_call Cases")
case EXCLUSIVE_CONTAINER: {
  assert(state.data.top().container_element_index == 0);
  trace(9999, "transform") << "checking exclusive container " << to_string(container) << " vs ingredient " << ingredient_index << end();
  // easy case: exact match
  if (types_strictly_match(container, inst.ingredients.at(ingredient_index)))
    return;
  if (!is_literal(ingredients.at(ingredient_index))) {
    raise << maybe(caller.name) << "ingredient " << ingredient_index << " of 'merge' should be a literal, for the tag of exclusive-container '" << container_info.name << "' in '" << inst.original_string << "'\n" << end();
    return;
  }
  reagent/*copy*/ ingredient = ingredients.at(ingredient_index);  // unnecessary copy just to keep this function from modifying caller
  populate_value(ingredient);
  if (ingredient.value >= SIZE(container_info.elements)) {
    raise << maybe(caller.name) << "invalid tag at " << ingredient_index << " for '" << container_info.name << "' in '" << inst.original_string << "'\n" << end();
    return;
  }
  const reagent& variant = variant_type(container, ingredient.value);
  trace(9999, "transform") << "tag: " << ingredient.value << end();
  // replace union with its variant
  state.data.pop();
  state.data.push(merge_check_point(variant, 0));
  ++ingredient_index;
  break;
}

:(scenario merge_check_container_containing_exclusive_container)
container foo [
  x:num
  y:bar
]
exclusive-container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 23, 1/y, 34
]
+mem: storing 23 in location 1
+mem: storing 1 in location 2
+mem: storing 34 in location 3
$error: 0

:(scenario merge_check_container_containing_exclusive_container_2)
% Hide_errors = true;
container foo [
  x:num
  y:bar
]
exclusive-container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 23, 1/y, 34, 35
]
+error: main: too many ingredients in '1:foo <- merge 23, 1/y, 34, 35'

:(scenario merge_check_exclusive_container_containing_container)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 1/y, 23, 34
]
+mem: storing 1 in location 1
+mem: storing 23 in location 2
+mem: storing 34 in location 3
$error: 0

:(scenario merge_check_exclusive_container_containing_container_2)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 0/x, 23
]
$error: 0

:(scenario merge_check_exclusive_container_containing_container_3)
% Hide_errors = true;
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 1/y, 23
]
+error: main: too few ingredients in '1:foo <- merge 1/y, 23'

:(scenario merge_check_exclusive_container_containing_container_4)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  a:num
  b:num
]
def main [
  1:bar <- merge 23, 24
  3:foo <- merge 1/y, 1:bar
]
$error: 0

//: Since the different variants of an exclusive-container might have
//: different sizes, relax the size mismatch check for 'merge' instructions.
:(before "End size_mismatch(x) Cases")
if (current_step_index() < SIZE(Current_routine->steps())
    && current_instruction().operation == MERGE
    && !current_instruction().products.empty()
    && current_instruction().products.at(0).type) {
  reagent/*copy*/ x = current_instruction().products.at(0);
  // Update size_mismatch Check for MERGE(x)
  const type_tree* root_type = x.type->atom ? x.type : x.type->left;
  assert(root_type->atom);
  if (get(Type, root_type->value).kind == EXCLUSIVE_CONTAINER)
    return size_of(x) < SIZE(data);
}

:(scenario merge_exclusive_container_with_mismatched_sizes)
container foo [
  x:num
  y:num
]
exclusive-container bar [
  x:num
  y:foo
]
def main [
  1:num <- copy 34
  2:num <- copy 35
  3:bar <- merge 0/x, 1:num
  6:bar <- merge 1/foo, 1:num, 2:num
]
+mem: storing 0 in location 3
+mem: storing 34 in location 4
# bar is always 3 large so location 5 is skipped
+mem: storing 1 in location 6
+mem: storing 34 in location 7
+mem: storing 35 in location 8