Commit Graph

66 Commits

Author SHA1 Message Date
Scott Olsen
d82e8a5a3f
refactor: add type candidates and template generators (#1361)
* refactor: add type candidates for validation

This commit adds a new module and type, the TypeCandidate, which
represents a potentially valid or invalid type. We use it as the
input for both type validation routines and type binding generation. The
type also allows us to unify the structure of sum types and product
types in an xobj agnostic way, paving the way for future simplification
of binding generation for type definitions.

This commit also removes SumtypeCase.hs, since it's no longer needed.

* refactor: add template generators; update type templates

This commit builds on the TypeCandidate data structure further by
providing "template generators" that work on candidates. Using
generators, templates for type functions ("methods") can be written
almost completely declaratively. Generators also remove some of the
typical boilerplate involved in creating templates from lists of tokens
and enable us to unify several of the generic and concrete templates for
types.

Generators can act on type candidates or their fields (for
field-specific functions). In general, this approach makes the
generation of type templates more structured. A type candidate now
contains all the information a generator needs to create appropriate
templates, thus it is a single and well-defined input for validation and
generation of user defined types.

This commit also updates the Deftype templates to use template
generators.

* refactor: use template generators for sumtype templates
2021-12-20 15:41:14 +01:00
Scott Olsen
380945bf32
feat: add box type (#1358)
* feat: add box templates and box type

This commit adds an implementation of Boxes, memory manged heap
allocated values.

Boxes are implemented as C pointers, with no additional structure but
are treated as structs in Carp. To facilitate this, we need to add them
as a clause to our special type emissions (TypesToC) as they'd otherwise
be emitted like other struct types.

Co-authored-by: Veit Heller <veit@veitheller.de>

* fix: slight memory management fix for Box

Make sure we free the box!

* test: add tests for box (including memory checks)

* Revert "fix: Ignore clang nitpick"

This reverts commit 70ec6d46d4.

* fix: update example/functor.carp

Now that a builtin type named Box exists, the definitions in this file
cause a conflict. I've renamed the "Box" type in the functor example to
remove the conflict.

* feat: add Box.peek

Box.peek allows users to transform a reference to a box into a a
reference to the box's contained value. The returned reference will have
the same lifetime as the box. This function allows callers to manipulate
the value in a box without re-allocation, for example:

```clojure
(deftype Num [val Int])

(let-do [box (Box.init (Num.init 0))]
  (Num.set-val! (Box.peek &box) 1)
  @(Num.val (Box.peek &box)))
```

This commit also includes tests for Box.peek.

Co-authored-by: TimDeve <TimDeve@users.noreply.github.com>

Co-authored-by: Veit Heller <veit@veitheller.de>
Co-authored-by: Erik Svedäng <erik@coherence.io>
Co-authored-by: TimDeve <TimDeve@users.noreply.github.com>
2021-11-30 10:35:22 +01:00
Erik Svedäng
b745b1fcdb build: 0.5.3 2021-09-17 13:54:26 +02:00
Erik Svedäng
5cc691be56 build: 0.5.2 2021-09-08 19:59:00 +02:00
Erik Svedäng
3b429541a3
refactor: Clean up memory management functions (#1240)
* refactor: Mid-refactor save point.

* feat: Code compiles

* refactor: Remove unused imports

* refactor: Move functions out of massive `manageMemory` block

* refactor: Move out even more functions from `manageMemory`

* refactor: Made most patterns match on "head form" of each s-expression

e.g. (if a b c) matches on 'if', 'a', 'b' and 'c'

* refactor: Use the pattern synonyms in Memory

* refactor: Remove a little cruft

* refactor: whenOK function

* refactor: Use 'whenRight' functions to avoid directly matching on Either

* docs: Comment the 'getConcretizedPath' function

* refactor: Move functionFinding-functions into Polymorphism module
2021-06-16 21:41:58 +02:00
Scott Olsen
4f7905d85b
refactor: move form validation into a separate module (#1233) 2021-06-08 07:39:06 +02:00
Erik Svedäng
99c4284f27 build: Release 0.5.1 2021-06-06 20:55:54 +02:00
Scott Olsen
e1943b29a9
Refactor: clean up Env module, store type environments in modules (#1207)
* refactor: major environment mgmt refactor

This big refactor primarily changes two things in terms of behavior:

1. Stores a SymPath on concretely named (non-generic) struct types;
   before we stored a string.
2. The SymPath mentioned in (1.) designates where the struct is stored
   in the current environment chain. Modules now carry a local type
   environment in addition to their local value environments. Any types
   defined in the module are added to this environment rather than the
   global type environment.

To resolve a type such as `Foo.Bar` we now do the following:

- Search the *global value environment* for the Foo module.
- Get the type environment stored in the Foo module.
- Search for Bar in the Foo module's type environment.

Additionally, this commit eliminates the Lookup module entirely and
refactors the Env module to handle all aspects of environment management
in hopefully a more reusable fashion.

I also took the opportunity to refactor primitiveDeftype in Primitives
and qualifySym in Qualify, both of which were hefty functions that I
found difficult to grok and needed refactoring anyway as a result of
lookup changes (lookups now return an Either instead of a Maybe).

Subsequent commits will clean up and clarify this work further.

This does include one minor regression. Namely, an implementation of
`hash` in core/Color that was maximally generic now needs type casting.

* refactor: clean up recent Env changes

This commit removes some redundant functions, unifies some logic, and
renames some routines across the Env module in efforts to make it
cleaner. Call sites have been updated accordingly.

* chore: format code with ormolu

* fix: update lookup tests

Changes references to renamed functions in the Env module.

* refactor: style + additional improvements from eriksvedang@

- Rename arrayTy -> arrayTyA in ArrayTemplates.hs to disambiguate.
- Add maybeId util function.
- Remove commented code.
- Refactor a few functions for readability.

* fix: fix type inference regression

Recent commits introduced one minor regression whereby an instance of
type inference in core/Color.carp no longer worked and required
explicit type annotation. The problem ultimately had to do with
qualification:

- Prior to the recent changes, type inference worked because the call in
  question was qualified to Color.Id.get-tag, fixing the type.
- Failing to copy over a local envs Use modules to function envs
  resulted in finding more than just Color.Id.get-tag for this instance.

We now copy use modules over to function envs generated during
qualification to ensure we resolve to Use'd definitions before more
general cases.

Similarly, I made a small change to primitiveUse to support contextual
use calls (e.g. the `(use Id)` in Color.carp, which really means `(use
Color.Id)`)

* chore: Update some clarificatory comments

* chore: fix inline comment
2021-05-19 19:20:48 +02:00
Scott Olsen
3ab7e229ae
refactor: Move evaluation errors into a separate module (#1158) 2021-03-09 19:24:02 +01:00
Erik Svedäng
528228373f build: Release v0.5.0 2021-02-01 19:22:42 +01:00
jacereda
b45b52b568
Add Dynamic.hash (#1069)
* feat: Add Dynamic.hash

* fix: Hash should be a Long
2020-12-16 15:53:55 +01:00
Scott Olsen
ee0aa59c28
Primitive refactors (#1070)
* refactor: move primitive errors; refactor primtiveInfo

This commit is the first in what will hopefully be a series of helpful
primitive refactors. To start, we:

- Move some inline `evalError` strings into a `PrimitiveError` module,
  (similar to the `TypeError`/`Types` module relationship
- Add `Reifiable` instances for String and Int types to take these
  types to their XObj representation.
- Add info utility functions for converting Info data to an XObj
- Refactor the `info` primitive:

  - Use monadic combinators + `maybe` instead of nested cases.
  - Use helper lookup functions that take a *context*--nearly *all*
    lookup calls currently extract some env, typically without doing
    anything to it, to pass it to lookup. This is a sign the boundary is
    incorrect and lookups should take the context instead--this will allow
    us to eliminate a ton of local `globalEnv`, `typeEnv`, etc. bindings.
  - Don't print hidden bindings
  - Indent printed meta information.
  - Color bindings blue

* chore: format code

* refactor: improve names for lookups that take a context

* feat: print hidden binders when calling info

Someone calling info might be interested in hidden binders as well, for
debugging purposes, etc. To enable this, we provide a version of show
for binders that prints hidden binders.

I've also made the printing of meta values in info more generic.
2020-12-15 21:38:55 +01:00
jacereda
ccd9af500e
Address incomplete patterns (#1062)
* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Unused error

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Address incomplete patterns

* chore: Remove flag present only in newer ghc
2020-12-09 06:19:28 +01:00
Erik Svedäng
b1aaa83b6a
refactor: Make Lookup module more focused and DRY (#1054)
* refactor: Move constraints test to own module, create TestLookup module

* refactor: Extracted 'Env' module

* refactor: Extracted 'TypePredicates' module

* test: First, very simple test

* refactor: Extracted 'Managed' module

* refactor: Add 'lookupBinder' function that doesn't return an Env (often what we want)

* refactor: Move out more stuff from Lookup

* refactor: Use new 'lookupBinder' in tons of places (avoids tuple)

* refactor: Got rid of monolithic 'recursiveLookupInternal'

* refactor: Avoid boolean blindness

* refactor: Better names for some lookup functions

* refactor: More logical order in Lookup.hs

* style: Use correct version of ormolu (0.1.4.1)

* refactor: Slightly more consistent naming

* refactor: Address @scolsen:s feedback
2020-12-07 07:06:32 +01:00
Scott Olsen
09fdd80f94
refactor: improve readability of interface functions (#1053)
* refactor: improve readability of interface functions

Also refactors the `define` function for readability.

The old definitions of these functions were quite unwieldy and difficult
to read. This refactor attempts to make the monadic contexts we're
juggling (often 3, Maybe, IO, Either) easier to spot.

* refactor: Add context env updaters; refactor prims

This commit contains a few more cleanups of the interface and define
functions in Primitives. It also defines a new context module for
holding functions that update context environments.
2020-12-05 21:00:28 +01:00
jacereda
2a94f67db8
Address name shadowing (#1032)
* Rename ty to xobjTy.

* Rename info to xobjInfo.

* Rename obj to xobjObj.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Address name shadowing.

* Remove some primes.
2020-12-01 00:11:01 +01:00
Erik Svedäng
49b2c1ea10
Update CarpHask.cabal 2020-11-26 08:26:51 +01:00
jacereda
cb39a6a0c3
Address unused matches. (#1019) 2020-11-25 22:12:57 +01:00
jacereda
f6386c6b70
Address unused local bind. (#1018)
* Address unused local bind.

* Add TODO about possible bug.
2020-11-25 09:19:15 +01:00
jacereda
d742c24097
Compile with -Wall. (#1011)
* Prepare for -Wall.

* Address unused imports.

* Address missing signatures.

* Address unused do bind.

* Address type defaults.

* Address orphans.

* Switch to nixos-20.09.
2020-11-24 14:06:42 +01:00
Erik Svedäng
793bf19ede
docs: Remove a lot of duplicated information and refer to webpages instead (#988)
* docs: Remove a lot of duplicated information and refer to webpages instead

* fix: Remove line about (help), it does not make sense anymore

* fix: Add open-browser to nix config

Co-authored-by: Erik Svedang <erik@Eriks-iMac.local>
2020-11-23 09:58:06 +01:00
scottolsen
25839de02d Enhance type reflection; get types of values, get kinds
Extends Carp's support for type reflection by returning types for
values as well as bindings.

`type` now also returns a valid Carp expression/s-expression and so its
output can be used as input to dynamic functions and macros (prior to
this commit, `type` printed the type to the REPL but did not return a
meaningful expression in Carp).

Here are a few illustrations of the behavior:

```
(def x 1)
;; type now returns an s-expression/symbol
(type x)
=> Int
;; It also works on values
(type 1)
=> Int
(type 2b)
=> Byte
(type "foo")
=> (Ref String <StaticLifetime>)
;; It works on more complex values as well
(type Maybe)
=> Module
(type Maybe.Just)
(Fn [a] (Maybe a) <StaticLifetime>)
;; reports honestly about polymorphism
(type (Maybe.Nothing))
=> (Maybe a)
(type (Pair.init 1 2))
=> (Pair Int Int)
;; What about the type of types?
(type (type 2))
=> Type
;; Or the type of types of types?
(type (type (type 2)))
=> ()
;; One more time!
(type (type (type (type 2))))
=> ()
;; so, () is the fixpoint of type, and is reached after two applications
(type zero)
;; the type of an interface is all of its implementations
=> (((Fn [] (Array a) <StaticLifetime>) (Fn [] Bool <StaticLifetime>) (Fn
[] Byte <StaticLifetime>) (Fn [] Char <StaticLifetime>) (Fn [] Double
<StaticLifetime>) (Fn [] Float <StaticLifetime>) (Fn [] Int
<StaticLifetime>) (Fn [] Int16 <StaticLifetime>) (Fn [] Int32
<StaticLifetime>) (Fn [] Int64 <StaticLifetime>) (Fn [] Int8
<StaticLifetime>) (Fn [] Long <StaticLifetime>) (Fn [] (Maybe a)
<StaticLifetime>) (Fn [] (Pair a b) <StaticLifetime>) (Fn [] (Quadruple
a b c d) <StaticLifetime>) (Fn [] String <StaticLifetime>) (Fn []
(Triple a b c) <StaticLifetime>) (Fn [] Uint16 <StaticLifetime>) (Fn []
Uint32 <StaticLifetime>) (Fn [] Uint64 <StaticLifetime>) (Fn [] Uint8
<StaticLifetime>)))
```

As shown in the example above, this change also includes a cosmetic
update to the representation of lifetime variables, which are surrounded
in <> to distinguish them from type variables.

This commit also adds a new `kind` primitive that reports on the kind of
a binding or value:

```
(def x 3)
(kind x)
=> Base
(kind 2)
=> Base
(kind Maybe.Just)
=> Higher
(kind (Maybe.Just 2))
=> Higher
```

`kind` and `type` both support interactive development in the repl, for
example, a user can rely on `kind` to check the kind of a type they plan
on using in an interface that demands a higher-kinded argument.

Likewise, they both also support developing macros based on type
information.
2020-10-02 17:48:58 -04:00
scottolsen
5955df79c8 Move interface registration functions into Interfaces.hs
This PR moves interface handling code out of Primitives.hs and into its
own module. The actual primitives user's rely on to define and implement
interfaces still live in Primitives.hs, but now all the
registration/internal context mgmt code related to interfaces lives in
its own module.

Hopefully this will make it easier to modify and extend!
2020-06-10 01:02:13 -04:00
scottolsen
6ae3fc3008 Add Meta module
The Meta module does two things:

- Collects functions related to working with Carp MetaData in one place
- Hides the underlying implementation of MetaData
  MetaData is backed by `Data.Map` previously, we'd alter MetaData by
  getting the underlying map directly and calling Map functions to modify
  the contents. The functions in the Meta module encapsulate this--now
  callers may manipulate MetaData without having to know anything about
  the underlying Map implementation. This should make it easier to swap
  out the underlying representation in the future as well.
2020-06-09 14:28:07 -04:00
Jorge Acereda
6656d96791 Add pkgconfigflags. 2020-06-01 21:34:12 +02:00
Erik Svedäng
69ffac4097
Merge pull request #832 from Vertmo/cleanup-types
Separated Types utilities into SymPath.hs and TypesToC.hs
2020-05-24 11:49:14 +02:00
Erik Svedäng
ea372b1728
Merge pull request #827 from jacereda/cmdline-eval
Command line switches to evaluate code
2020-05-24 11:45:44 +02:00
Basile Pesin
8de9a416cb Separated Types utilities into SymPath.hs and TypesToC.hs 2020-05-24 11:08:17 +02:00
Scott Olsen
fbc226ce2f Move Info into its own module
Unlike many of the other datatypes in Obj.hs, Info does not introduce
mutually recursive dependencies. Obj.hs is quite large and contains a
lot of different functionality, so I think it helps to move anything
that we can separate out into its own module in order to start
simplifying the Obj.hs and its responsibilities.
2020-05-23 13:09:53 -04:00
Jorge Acereda
bd59af2a1e Switch resolver. 2020-05-21 20:44:43 +02:00
Jorge Acereda
dba9ef53c3 Switch option parsing to optparse-applicative, remove tcc dependency. 2020-05-21 20:06:07 +02:00
Erik Svedäng
979077b388 Some cleanup and start of tests for Static Array. 2020-04-29 10:39:15 +02:00
Erik Svedäng
9853fba9d6 Progress, but can't use the ref to the StaticArray at all since it's
referring to something that's not "alive" according to memory system.
2020-04-23 09:43:32 +02:00
hellerve
70cc9b5848 eval: add primitives 2020-04-17 11:43:09 +02:00
Jorge Acereda
28104b0fb2 Path module. 2019-10-09 12:18:30 +02:00
Erik Svedäng
4ceb187a06 Bump to version 0.3.0. 2019-07-04 10:34:03 +02:00
Erik Svedäng
32f14cd9e3 Work in progress... 2019-03-26 10:23:27 +01:00
hellerve
6716edf2af compiler: better error messages 2019-03-12 19:11:12 +01:00
Randy Valis
a0d4525723 Merge branch 'master' of https://github.com/xran-deex/Carp 2019-03-03 17:30:37 -05:00
Randy Valis
b1c78ef216 Modifications to compile on Windows 2019-03-03 17:23:13 -05:00
Erik Svedäng
475b7389ea Validation for sumtype cases. 2019-02-20 15:25:00 +01:00
Erik Svedäng
ab9ee1246e A beginning of 'str' and 'prn' for sumtypes. 2019-01-23 20:26:42 +01:00
Erik Svedäng
4745217bd7 Sumtype module. 2018-10-30 12:55:17 +01:00
hellerve
8341b7e026 compiler: add suport for markdown in docstrings 2018-10-22 15:58:57 -04:00
hellerve
498af1ad45 compiler: only use colored errors on ttys 2018-10-19 08:33:26 -04:00
Erik Svedäng
14f03252a8 Created a 'ToTemplate' module to avoid putting too much stuff into Concretize. 2018-08-30 13:07:39 +02:00
hellerve
e175d57fb9 docs: change to pretty rendering 2018-08-07 09:53:13 +02:00
Erik Svedäng
8993ce38b3 RenderDocs.hs 2018-03-27 06:27:52 +02:00
Erik Svedäng
ac6afff264 Started working on 'relative-include' macro - not working yet though. 2018-03-12 16:30:36 +01:00
Erik Svedäng
9857dd64db Remove deps. 2018-02-27 16:00:55 +01:00