1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-15 18:13:56 +03:00
juvix/tests/positive/MiniC/MultiModules/Data/Nat.juvix

35 lines
667 B
Plaintext
Raw Normal View History

Support partial application and closure passing in C backend (#190) * Add support for parital application eval/apply * include string.h in libc runtime * Add wasm SimpleFungibleTokenImplicit to tests * Update VP example to new syntax * propagate types from all reachable modules * Change prelude import ordering to workaround minic issue * Pre-declare inductive typedefs in C backend This generates the typedefs corresponding to each inductive type. e.g ``` inductive Bool { .. } ``` is translated to: ``` typedef struct Bool_3_s Bool_3_t; ``` This means that C code can reference these typedefs before they have been fully defined. (all references to inductive types go through these typedefs names). This fixes an issue with the ordering of delcarations when modules are included. * Use common Lib for MiniC tests * libc runtime: flush stdout after writing to it * Adds MiniTicTacToe example using common example lib In MonoJuvixToMiniC we emit the inductive type typedefs before anything else to support includes ordering * Adds tests for mutually recrusive functions * Add golden tests for milestone examples * Example: Remove commented out code * Test error handling behaviour in MiniTicTacToe * Fail clang compilation on warnings * Add test for Nested list types * Add PrettyCode instances for NonEmpty and ConcreteType * Ignore IsImplicit field in Eq and Hashable of TypeApplication This is to workaround a crash in Micro->Mono translation when looking up a concrete type * Fix formatting * hlint fixes * Formatting fixes not reported by local pre-commit * Refactor MonoJuvixToMiniC * Fix shelltest NB: We cannot check the order of the 'Writing' lines because this depends on the order of files returned by the FS which is non-deterministic between systems * Refactor Base to CBuilder * Refactor using applyOnFunStatement Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2022-06-28 11:25:43 +03:00
module Data.Nat;
open import Data.String;
open import Data.Int;
inductive Nat {
zero : Nat;
suc : Nat → Nat;
};
foreign c {
void* natInd(int n, void* zeroCtor, juvix_function_t* sucCtor) {
Add support for built in types (#192) * match inductive definitions * progress towards builtins * more progress towards builtin types * add more builtins * add some errors * add reverse table to builtins * Squashed commit of the following: commit 93333de502d8dd546eb8edf697ca7eef972ea105 Author: Paul Cadman <git@paulcadman.dev> Date: Mon Jun 27 18:21:30 2022 +0100 Use builtin names for match and project functions Add an implementation of nat for the standalone backend commit 868d2098ee57b7acbca84512b6e096650eeeb22d Author: Jan Mas Rovira <janmasrovira@gmail.com> Date: Mon Jun 27 18:15:29 2022 +0200 add builtin information to ClosureInfo commit 32c78aceb19ee4010d66090a3c4e6025621b5c1f Author: Paul Cadman <git@paulcadman.dev> Date: Mon Jun 27 12:52:10 2022 +0100 Refactor BuiltinEnum to sum type of each Builtin commit 55bb72ab12a8fb7d10070c2dee5875482453b7c6 Author: Paul Cadman <git@paulcadman.dev> Date: Fri Jun 24 14:44:28 2022 +0100 Add Builtin information to Mono InfoTable commit a72368f2e3af20baaf44c5e21fa7e6a469cf1ac5 Author: Paul Cadman <git@paulcadman.dev> Date: Fri Jun 24 14:41:51 2022 +0100 Add Bitraversable to Prelude commit afa3153d82a9509b0882e7ca99830040fad9ef65 Author: Paul Cadman <git@paulcadman.dev> Date: Fri Jun 24 14:41:39 2022 +0100 Remove unused import commit ea0b7848fb80970e03a0561be3fb4448486a89a9 Author: Paul Cadman <git@paulcadman.dev> Date: Thu Jun 23 13:54:58 2022 +0100 Use projection functions instead of direct member access * Avoid shadowing C runtime names in foreign block * Fix formatting * Update C names for builtin functions * Add prim_ prefix to builtin C names Implement builtins for standalone and libc backends * Update ormolu action * ci: run all tests for draft PRs Co-authored-by: Paul Cadman <git@paulcadman.dev>
2022-06-28 14:31:31 +03:00
if (n <= 0) return zeroCtor;
return ((void* (*) (juvix_function_t*, void*))sucCtor->fun)(sucCtor, natInd(n - 1, zeroCtor, sucCtor));
Support partial application and closure passing in C backend (#190) * Add support for parital application eval/apply * include string.h in libc runtime * Add wasm SimpleFungibleTokenImplicit to tests * Update VP example to new syntax * propagate types from all reachable modules * Change prelude import ordering to workaround minic issue * Pre-declare inductive typedefs in C backend This generates the typedefs corresponding to each inductive type. e.g ``` inductive Bool { .. } ``` is translated to: ``` typedef struct Bool_3_s Bool_3_t; ``` This means that C code can reference these typedefs before they have been fully defined. (all references to inductive types go through these typedefs names). This fixes an issue with the ordering of delcarations when modules are included. * Use common Lib for MiniC tests * libc runtime: flush stdout after writing to it * Adds MiniTicTacToe example using common example lib In MonoJuvixToMiniC we emit the inductive type typedefs before anything else to support includes ordering * Adds tests for mutually recrusive functions * Add golden tests for milestone examples * Example: Remove commented out code * Test error handling behaviour in MiniTicTacToe * Fail clang compilation on warnings * Add test for Nested list types * Add PrettyCode instances for NonEmpty and ConcreteType * Ignore IsImplicit field in Eq and Hashable of TypeApplication This is to workaround a crash in Micro->Mono translation when looking up a concrete type * Fix formatting * hlint fixes * Formatting fixes not reported by local pre-commit * Refactor MonoJuvixToMiniC * Fix shelltest NB: We cannot check the order of the 'Writing' lines because this depends on the order of files returned by the FS which is non-deterministic between systems * Refactor Base to CBuilder * Refactor using applyOnFunStatement Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2022-06-28 11:25:43 +03:00
\}
};
axiom natInd : Int → Nat → (Nat → Nat) → Nat;
compile natInd {
c ↦ "natInd";
};
natToInt : Nat → Int;
natToInt zero ≔ Int_0;
natToInt (suc n) ≔ Int_1 + (natToInt n);
Support partial application and closure passing in C backend (#190) * Add support for parital application eval/apply * include string.h in libc runtime * Add wasm SimpleFungibleTokenImplicit to tests * Update VP example to new syntax * propagate types from all reachable modules * Change prelude import ordering to workaround minic issue * Pre-declare inductive typedefs in C backend This generates the typedefs corresponding to each inductive type. e.g ``` inductive Bool { .. } ``` is translated to: ``` typedef struct Bool_3_s Bool_3_t; ``` This means that C code can reference these typedefs before they have been fully defined. (all references to inductive types go through these typedefs names). This fixes an issue with the ordering of delcarations when modules are included. * Use common Lib for MiniC tests * libc runtime: flush stdout after writing to it * Adds MiniTicTacToe example using common example lib In MonoJuvixToMiniC we emit the inductive type typedefs before anything else to support includes ordering * Adds tests for mutually recrusive functions * Add golden tests for milestone examples * Example: Remove commented out code * Test error handling behaviour in MiniTicTacToe * Fail clang compilation on warnings * Add test for Nested list types * Add PrettyCode instances for NonEmpty and ConcreteType * Ignore IsImplicit field in Eq and Hashable of TypeApplication This is to workaround a crash in Micro->Mono translation when looking up a concrete type * Fix formatting * hlint fixes * Formatting fixes not reported by local pre-commit * Refactor MonoJuvixToMiniC * Fix shelltest NB: We cannot check the order of the 'Writing' lines because this depends on the order of files returned by the FS which is non-deterministic between systems * Refactor Base to CBuilder * Refactor using applyOnFunStatement Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2022-06-28 11:25:43 +03:00
natToStr : Nat → String;
natToStr n ≔ intToStr (natToInt n);
intToNat : Int → Nat;
intToNat x ≔ natInd x zero suc;
end;