Commit Graph

15 Commits

Author SHA1 Message Date
Veit Heller
e42922e96e
feat: add Introspect.arguments (#1163)
* feat: add Introspect.arguments

* fix: fix struct case of Introspect.arguments
2021-02-01 17:03:38 +01:00
Erik Svedäng
f78fd16a71
refactor: Move code out of Macros.carp into other files (#1014)
* refactor: Move code out of Macros.carp into other files

* fix: Move back some macros needed in --no-core mode

* refactor: Remove weird 'evaluate' macros

* fix: Put back more macros

* fix: Remove transitive loading of Macros.carp

* refactor: Remove ArrayMacros.carp and put 'for' at top of Array.carp instead

* refactor: More splitting up

* refactor: Move back save-docs

* fix: Moved back some stuff

Co-authored-by: Erik Svedang <erik@Eriks-iMac.local>
2020-11-28 12:53:18 +01:00
Scott Olsen
af1d0065e2 Index arguments from 0 in with-copy
Indexing from zero is consistent with the rest of Carp's indexing
behavior, so this should make the function more predictable. I've also
added a note to the docs about this.
2020-08-18 22:52:56 -04:00
scottolsen
3a82e7d5c0 Rename proxy->with-copy, update docs
with-copy better communicates that this macro will produce a function
that performs a copy--it doesn't cover the fact that it creates a
references to an anonymous function...but there's only so much we can
cover in a name.

I've also updated the documentation.
2020-08-10 17:59:26 -04:00
scottolsen
14a3d9b5ab Support introspection on external functions
Like the previous two commits, this commit extends support for
reflection to yet another type, externally registered functions,
allowing us to support calls such as `(arity Int.+)`.
2020-08-10 17:56:28 -04:00
scottolsen
33a7f51c7b Support introspecting primitives
Just as the prior commit added support for capturing the arity of
registered commands, this one captures the arity of registered
primitives, enabling one to make calls such as `(arity defmacro)`, etc.

I have also moved the dummy argument name function into Util.hs since
it's used in both Commands.hs and Primitives.hs.
2020-08-10 17:06:46 -04:00
scottolsen
607d0c0664 Support introspection of commands
Previously, the command xobjs did not capture the arity of their
definitions. After this commit, they do, which enables us to surface
useful information about commands in reflective modules such as
core/Introspection.

Calls such as `(arity Dynamic.+)` and `(arity Dynamic.load)` will work
after this change.
2020-08-10 15:57:55 -04:00
scottolsen
6327f51c73 Add a proxy macro for generating functions for higher-orders
It's a fairly common pattern in Carp to call a higher-order function on
some structure of values, such as an Array. However, these structures,
and their members, all have lifetimes under Carp's memory management
model, which means they expect functions that are mapped over them to
take *a reference to a value* rather than a pure value. Array.reduce is
one example of such a "referential" higher-order, the type of its
function argument is:

```
(Fn [a, (Ref b c)] a)
```

That is, this function takes some pure initial value, then expects to be
called against the members of an array, which are *references* to the
values that are alive throughout the Array's lifetime.

However, one often wants to use a function that operates on pure values
in such contexts, such as +, which forces the programmer to write
anonymous functions that handle copying referenced values to pass them
to the underlying "pure" function:

```
(Array.reduce &(fn [x y] (+ x @y)) 0 &[1 2 3])
```

So, in using some high-order function over some structure in Carp one
usually has to do two things:

1. Wrap the function in a ref
2. Handle copying references into values in order to pass them into some
   simpler function that can also be used outside of memory-bound
   contexts.

The `proxy` macro captures this pattern. It wraps a given function in a
referenced anonymous function and copies an argument of that function at
a designated position before calling the underlying function. For
example, with `proxy`, the above example becomes:

```
(Array.reduce (proxy + 2) 0 &[1 2 3])
```

The macro effectively gives a name to a common pattern--typically it
will only save the programmer a few characters, but it perhaps makes the
act of using a "function that doesn't care about references" in a
reference dominant context more apparent.

One can also use the macro to develop more specialized macros for
certain higher-orders, since these usually dictate where copying must be
performed. For instance, the `Array.reduce` function argument always
expects the referenced value to occur in the second position, thus one
could write:

```
(defmacro reducer [function] (eval (list proxy function 2)))
```

Then the above code becomes even simpler:

```
(Array.reducer (reducer +) 0 &[1 2 3])
```

Which roughly means, "use the + function (which has no concept of
references) in this reference dependent context".

N.B. The examples using `+` won't work as of now due to current bugs
related to calling `arity` directly on an interface--but a synonym for
plus `add` defined as an explicit function will make all the above work
as expected.
2020-08-07 17:12:43 -04:00
scottolsen
383f400a32 Fix length checks, use or 2020-05-27 15:22:42 -04:00
scottolsen
095866e45f Add length checks to struct? and sumtype? for safety 2020-05-27 15:03:10 -04:00
scottolsen
b0d9a34375 Update Introspect to work with the s-expr command 2020-05-27 13:33:43 -04:00
Scott Olsen
9b9aa76b59 Improve introspection support for interfaces
Previously, introspecting an interface only returned `definterface` and
its name. We now return the form corresponding to the interface's types
signature, which enables us to introspect more effectively; e.g.
returning the appropriate arity of an interface.
2020-05-24 17:02:35 -04:00
Scott Olsen
4a29c6a998 Rename s-exp -> s-expr 2020-05-23 15:42:32 -04:00
Scott Olsen
d45a6424eb Fix arity for sumtypes 2020-05-22 01:29:53 -04:00
Scott Olsen
7f095d754c Add Introspect module
The introspect module contains functions that return information about
program bindings based on their s-expressions.
2020-05-22 01:16:55 -04:00