Commit Graph

46 Commits

Author SHA1 Message Date
Kartik Agaram 4a943d4ed3 5001 - drop the :(scenario) DSL
I've been saying for a while[1][2][3] that adding extra abstractions makes
things harder for newcomers, and adding new notations doubly so. And then
I notice this DSL in my own backyard. Makes me feel like a hypocrite.

[1] https://news.ycombinator.com/item?id=13565743#13570092
[2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning
[3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2

The implementation of the DSL was also highly hacky:

a) It was happening in the tangle/ tool, but was utterly unrelated to tangling
layers.

b) There were several persnickety constraints on the different kinds of
lines and the specific order they were expected in. I kept finding bugs
where the translator would silently do the wrong thing. Or the error messages
sucked, and readers may be stuck looking at the generated code to figure
out what happened. Fixing error messages would require a lot more code,
which is one of my arguments against DSLs in the first place: they may
be easy to implement, but they're hard to design to go with the grain of
the underlying platform. They require lots of iteration. Is that effort
worth prioritizing in this project?

On the other hand, the DSL did make at least some readers' life easier,
the ones who weren't immediately put off by having to learn a strange syntax.
There were fewer quotes to parse, fewer backslash escapes.

Anyway, since there are also people who dislike having to put up with strange
syntaxes, we'll call that consideration a wash and tear this DSL out.

---

This commit was sheer drudgery. Hopefully it won't need to be redone with
a new DSL because I grow sick of backslashes.
2019-03-12 19:14:12 -07:00
Kartik Agaram c442a5ad80 4987 - support `browse_trace` tool in SubX
I've extracted it into a separate binary, independent of my Mu prototype.

I also cleaned up my tracing layer to be a little nicer. Major improvements:

- Realized that incremental tracing really ought to be the default.
  And to minimize printing traces to screen.

- Finally figured out how to combine layers and call stack frames in a
  single dimension of depth. The answer: optimize for the experience of
  `browse_trace`. Instructions occupy a range of depths based on their call
  stack frame, and minor details of an instruction lie one level deeper
  in each case.

Other than that, I spent some time adjusting levels everywhere to make
`browse_trace` useful.
2019-02-25 01:50:53 -08:00
Kartik Agaram a519b78c18 4274
Fix CI.
2018-06-25 13:54:48 -07:00
Kartik Agaram 2caaa7f18f 4272 - type-check variables in non-local spaces
So far we only checked if a single recipe used a variable with multiple
types in any single space. Now we also ensure that the types deduced for
a variable in a space are identical across recipes.
2018-06-25 13:36:27 -07:00
Kartik Agaram b0631dec20 4271 - bugfix unrelated to alloc-ids 2018-06-25 10:59:20 -07:00
Kartik Agaram 23d3a02226 4266 - space for alloc-id in heap allocations
This has taken me almost 6 weeks :(
2018-06-24 09:18:20 -07:00
Kartik Agaram ce9b2b0515 4258 - undo 4257 2018-06-15 22:16:09 -07:00
Kartik Agaram 0edd9b9fc6 4257 - abortive attempt at safe fat pointers
I've been working on this slowly over several weeks, but it's too hard
to support 0 as the null value for addresses. I constantly have to add
exceptions for scalar value corresponding to an address type (now
occupying 2 locations). The final straw is the test for 'reload':

  x:num <- reload text

'reload' returns an address. But there's no way to know that for
arbitrary instructions.

New plan: let's put this off for a bit and first create support for
literals. Then use 'null' instead of '0' for addresses everywhere. Then
it'll be easy to just change what 'null' means.
2018-06-15 22:12:03 -07:00
Kartik K. Agaram 504292f6f1 4106 2017-11-03 18:01:59 -07:00
Kartik K. Agaram aae198a93b 4099
Generalize commit 4089 to arbitrary closures, and not just the current
'space' or call frame. Now we should be treating spaces just like any
other data structure, and reclaiming all addresses inside them when we
need to.

The cost: all spaces must now specify what recipe generated them (so
they know how to interpret the array of locations) using the /names
property.

We can probably make this ergonomic with a little 'type inference'. But
at least things are safe now.
2017-11-01 02:46:41 -07:00
Kartik K. Agaram 514f0e34aa 4089
Clean up how we reclaim local scopes.

It used to work like this (commit 3216):

  1. Update refcounts of products after every instruction, EXCEPT:

      a) when instruction is a non-primitive and the callee starts with
      'local-scope' (because it's already not decremented in 'return')

    OR:

      b) when instruction is primitive 'next-ingredient' or
      'next-ingredient-without-typechecking', and its result is saved to a
      variable in the default space (because it's already incremented at
      the time of the call)

  2. If a function starts with 'local-scope', force it to be reclaimed
  before each return. However, since locals may be returned, *very
  carefully* don't reclaim those. (See the logic in the old `escaping`
  and `should_update_refcount` functions.)

However, this approach had issues. We needed two separate commands for
'local-scope' (reclaim locals on exit) and 'new-default-space'
(programmer takes charge of reclaiming locals). The hard-coded
reclamation duplicated refcounting logic. In addition to adding
complexity, this implementation failed to work if a function overwrites
default-space after setting up a local-scope (the old default-space is
leaked). It also fails in the presence of continuations. Calling a
continuation more than once was guaranteed to corrupt memory (commit
3986).

After this commit, reclaiming local scopes now works like this:

  Update refcounts of products for every PRIMITIVE instruction.
  For non-primitive instructions, all the work happens in the `return`
  instruction:
    increment refcount of ingredients to `return`
      (unless -- one last bit of ugliness -- they aren't saved in the
      caller)
    decrement the refcount of the default-space
      use existing infrastructure for reclaiming as necessary
      if reclaiming default-space, first decrement refcount of each
      local
        again, use existing infrastructure for reclaiming as necessary

This commit (finally!) completes the bulk[1] of step 2 of the plan in
commit 3991. It was very hard until I gave up trying to tweak the
existing implementation and just test-drove layer 43 from scratch.

[1] There's still potential for memory corruption if we abuse
`default-space`. I should probably try to add warnings about that at
some point (todo in layer 45).
2017-10-22 23:48:03 -07:00
Kartik K. Agaram 6c96a437ce 3522 2016-10-19 22:10:35 -07:00
Kartik K. Agaram 5813bcfc7d 3520
Drop a few debug prints. Hopefully now we need never duplicate trace
statements and can instead just dump them to screen.

I'll soon need the ability to selectively dump traces for a specific
label.
2016-10-18 10:44:40 -07:00
Kartik K. Agaram 897ae8c185 3394 2016-09-17 14:53:00 -07:00
Kartik K. Agaram 0f2781f8a2 3393 2016-09-17 14:43:13 -07:00
Kartik K. Agaram a0331a9b0e 3390 2016-09-17 13:00:39 -07:00
Kartik K. Agaram 760f683f27 3389 2016-09-17 12:55:10 -07:00
Kartik K. Agaram 192d59d3bb 3380
One more place we were missing expanding type abbreviations: inside
container definitions.
2016-09-17 00:43:20 -07:00
Kartik K. Agaram af023b323b 3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).

This commit does several things at once that I couldn't come up with a
clean way to unpack:

  A. It moves to a new representation for type trees without changing
  the actual definition of the `type_tree` struct.

  B. It adds unit tests for our type metadata precomputation, so that
  errors there show up early and in a simpler setting rather than dying
  when we try to load Mu code.

  C. It fixes a bug, guarding against infinite loops when precomputing
  metadata for recursive shape-shifting containers. To do this it uses a
  dumb way of comparing type_trees, comparing their string
  representations instead. That is likely incredibly inefficient.

Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.

== more notes and details

I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:

              o
            / | \
         left val right

..then the type_tree (a b c) used to look like this (before this
commit):

      o
      | \
      a   o
          | \
          b   o
              | \
              c   null

..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):

      o
    /   \
   o      o
   |    /   \
   a   o      o
       |    /   \
       b   o      null
           |
           c

The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).

Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).

Layers I got repeatedly stuck at:

  1. the transform for precomputing type sizes (layer 30)
  2. type-checks on merge instructions (layer 31)
  3. the transform for precomputing address offsets in types (layer 36)
  4. replace operations in supporting shape-shifting recipes (layer 55)

After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.

Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.

So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.

== lessons learned

There's several levels of hygiene when it comes to layers:

1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.

2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.

3. A layer is introducing features that are tested only in later layers.

4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)

Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.

Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.

Definitely avoid layer 4 at any time.

== minor lessons

Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).

Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.

What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-09 18:32:52 -07:00
Kartik K. Agaram a9a2f7db59 3108 2016-07-10 21:47:24 -07:00
Kartik K. Agaram 9dcbec398c 2990
Standardize quotes around reagents in error messages.

I'm still sure there's issues. For example, the messages when
type-checking 'copy'. I'm not putting quotes around them because in
layer 60 I end up creating dilated reagents, and then it's a bit much to
have quotes and (two kinds of) brackets. But I'm sure I'm doing that
somewhere..
2016-05-20 22:11:34 -07:00
Kartik K. Agaram 2655ae726e 2974
Fix CI failure.
2016-05-18 17:05:22 -07:00
Kartik K. Agaram b0bf5321de 2864 - replace all address:shared with just address
Now that we no longer have non-shared addresses, we can just always
track refcounts for all addresses.

Phew!
2016-04-24 11:54:30 -07:00
Kartik K. Agaram b24eb4766a 2773 - switch to 'int'
This should eradicate the issue of 2771.
2016-03-13 20:26:47 -07:00
Kartik K. Agaram 1ead356219 2735 - define recipes using 'def'
I'm dropping all mention of 'recipe' terminology from the Readme. That
way I hope to avoid further bike-shedding discussions while I very
slowly decide on the right terminology with my students.

I could be smarter in my error messages and use 'recipe' when code uses
it and 'function' otherwise. But what about other words like ingredient?
It would all add complexity that I'm not yet sure is worthwhile. But I
do want separate experiences for veteran programmers reading about Mu on
github and for people learning programming using Mu.
2016-03-08 01:46:47 -08:00
Kartik K. Agaram 1e38eee5db 2718 - stop crashing on unknown space
I'm going to stop wasting precious first-line characters on 'bugfix:'.
It's going to be all bugfixes for a while I think.
2016-02-26 21:50:54 -08:00
Kartik K. Agaram 1b76245c63 2712 2016-02-26 13:04:55 -08:00
Kartik K. Agaram d0e29245f9 2707 2016-02-25 20:47:42 -08:00
Kartik K. Agaram f51e9f63b4 2701 - turn some warnings into errors
I really have only one warning left: when somebody redefines a function.
I think I'm going to just turn that into an error as well and drop the
notion of warnings altogether. Anytime we find something wrong we stop
running the program. This is a place where hygiene is justified.
2016-02-25 07:46:56 -08:00
Kartik K. Agaram f22250a174 2680
Delete all the [] that has crept in since 2377 in November.
2016-02-20 21:58:48 -08:00
Kartik K. Agaram 9f95c7451b 2685
Stack of plans for cleaning up replace_type_ingredients() and a couple
of other things, from main problem to subproblems:

  include type names in the type_tree rather than in the separate properties vector
  make type_tree and string_tree real cons cells, with separate leaf nodes
  redo the vocabulary for dumping various objects:
    do we really need to_string and debug_string?
    can we have a version with *all* information?
    can we have to_string not call debug_string?

This commit nibbles at the edges of the final task, switching from
member method syntax to global function like almost everything else. I'm
mostly using methods just for STL in this project.
2016-02-19 13:42:45 -08:00
Kartik K. Agaram 3e5f435842 2631 2016-02-06 09:29:36 -08:00
Kartik K. Agaram 455fbac64f 2576 - distinguish allocated addresses from others
This is the one major refinement on the C programming model I'm planning
to introduce in mu. Instead of Rust's menagerie of pointer types and
static checking, I want to introduce just one new type, and use it to
perform ref-counting at runtime.

So far all we're doing is updating new's interface. The actual
ref-counting implementation is next.

One implication: I might sometimes need duplicate implementations for a
recipe with allocated vs vanilla addresses of the same type. So far it
seems I can get away with just always passing in allocated addresses;
the situations when you want to pass an unallocated address to a recipe
should be few and far between.
2016-01-19 23:18:03 -08:00
Kartik K. Agaram e31cb59df9 2571
Reorganize layers in preparation for a better way to manage heap
allocations without ever risking use-after-free errors.
2016-01-19 14:26:35 -08:00
Kartik K. Agaram f59ccc4652 1768 2015-07-13 14:34:58 -07:00
Kartik K. Agaram 363be37f3f 1702 - experiment: start using 'ordinal' in names
It comes up pretty early in the codebase, but hopefully won't come up
in the mu level until we get to higher-order recipes. Potentially
intimidating name, but such prime real estate with no confusing
overloadings in other projects!
2015-07-04 09:40:50 -07:00
Kartik K. Agaram a91c1c2a28 1599 2015-06-19 16:34:11 -07:00
Kartik K. Agaram ab6ed192b7 1585 2015-06-17 12:39:09 -07:00
Kartik K. Agaram b2757892d5 1458
While pushing out color support in fake screens I realized I've been
complecting the special-case of a special-case to transform
literal-string arguments for 'new'. As a result I hadn't been catching
bad habits like giving its arg the wrong type. Now we have cleaner
separation of the two variants of 'new', a few more checks, and better
error messages when we mis-call it.

Aside: I've added a third goto target. Sliding into spaghetti? Keep an
eye on it.

This goto might become a common pattern: a layer hooking into a previous
one to prevent it from happening. In this case new on literal-strings
prevents the transform for new from triggering.
2015-05-25 21:36:46 -07:00
Kartik K. Agaram ac0e9db526 1391 - avoid unsigned integers 2015-05-17 02:22:41 -07:00
Kartik K. Agaram 0f125d5ff1 1387 2015-05-16 21:24:21 -07:00
Kartik K. Agaram 5497090aa1 1363 - rename 'integer' to 'number'
..now that we support non-integers.
2015-05-13 10:03:26 -07:00
Kartik K. Agaram 5798a11bf0 1343 2015-05-11 10:04:16 -07:00
Kartik K. Agaram 05d177737c 1299 - stop using [] in any vector
Useful check:

  $ grep "[^ '\"]\[[^\"]" *.cc \
    |perl -pwe 's/\Wargv\[|\WTests\[|\Wframe\[|\WMemory\[|\WName\[|\WSurrounding_space\[|\WRecipe\[|\WType\[|\WRecipe_number\[|\WType_number\[|\WBefore_fragments\[|\WAfter_fragments\[//g' \
    |perl -pwe 's/\Wargv\[|\WTests\[|\Wframe\[|\WMemory\[|\WName\[|\WSurrounding_space\[|\WRecipe\[|\WType\[|\WRecipe_number\[|\WType_number\[|\WBefore_fragments\[|\WAfter_fragments\[//g' \
    |grep '[^ ]\['
2015-05-07 15:49:40 -07:00
Kartik K. Agaram 0487a30e70 1298 - better ingredient/product handling
All primitives now always write to all their products. If a product is
not used that's fine, but if an instruction seems to expect too many
products mu will complain.

In the process, many primitives can operate on more than two ingredients
where it seems intuitive. You can add or divide more than two numbers
together, copy or negate multiple corresponding locations, etc.

There's one remaining bit of ugliness. Some instructions like
get/get-address, index/index-address, wait-for-location, these can
unnecessarily load values from memory when they don't need to.

Useful vim commands:
  %s/ingredients\[\([^\]]*\)\]/ingredients.at(\1)/gc
  %s/products\[\([^\]]*\)\]/products.at(\1)/gc
  .,$s/\[\(.\)]/.at(\1)/gc
2015-05-07 15:29:13 -07:00
Kartik K. Agaram b96af395b9 1276 - make C++ version the default
I've tried to update the Readme, but there are at least a couple of issues.
2015-05-05 21:17:24 -07:00