-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
a788478
commit 40287ef
Showing
76 changed files
with
2,312 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Data.List; | ||
|
||
import Data.Bool; | ||
open Data.Bool; | ||
|
||
infixr 5 ∷; | ||
inductive List (A : Type) { | ||
nil : List A; | ||
∷ : A → List A → List A; | ||
}; | ||
|
||
elem : {A : Type} → (A → A → Bool) → A → List A → Bool; | ||
elem _ _ nil ≔ false; | ||
elem eq s (x ∷ xs) ≔ eq s x || elem eq s xs; | ||
|
||
foldl : {A : Type} → {B : Type} → (B → A → B) → B → List A → B; | ||
foldl f z nil ≔ z; | ||
foldl f z (h ∷ hs) ≔ foldl f (f z h) hs; | ||
|
||
map : {A : Type} → {B : Type} → (A → B) → List A → List B; | ||
map f nil ≔ nil; | ||
map f (h ∷ hs) ≔ f h ∷ map f hs; | ||
|
||
null : {A : Type} → List A → Bool; | ||
null nil ≔ true; | ||
null _ ≔ false; | ||
|
||
head : {A : Type} → List A → A; | ||
head (x ∷ _) ≔ x; | ||
|
||
tail : {A : Type} → List A → List A; | ||
tail (_ ∷ xs) ≔ xs; | ||
|
||
terminating | ||
transpose : {A : Type} → List (List A) → List (List A); | ||
transpose (nil ∷ _) ≔ nil; | ||
transpose xss ≔ (map head xss) ∷ (transpose (map tail xss)); | ||
|
||
concatList : {A : Type} → List A → List A → List A; | ||
concatList nil xss ≔ xss; | ||
concatList (x ∷ xs) xss ≔ x ∷ (concatList xs xss); | ||
|
||
flatten : {A : Type} → List (List A) → List A; | ||
flatten ≔ foldl concatList nil; | ||
|
||
prependToAll : {A : Type} → A → List A → List A; | ||
prependToAll _ nil ≔ nil; | ||
prependToAll sep (x ∷ xs) ≔ sep ∷ x ∷ prependToAll sep xs; | ||
|
||
intersperse : {A : Type} → A → List A → List A; | ||
intersperse _ nil ≔ nil; | ||
intersperse sep (x ∷ xs) ≔ x ∷ prependToAll sep xs; | ||
|
||
any : {A : Type} → (A → Bool) → List A → Bool; | ||
any f xs ≔ foldl (||) false (map f xs); | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
module Data.Nat; | ||
|
||
import Data.Bool; | ||
open Data.Bool; | ||
|
||
import Data.Int; | ||
open Data.Int; | ||
|
||
import Data.String; | ||
open Data.String; | ||
|
||
inductive Nat { | ||
zero : Nat; | ||
suc : Nat → Nat; | ||
}; | ||
|
||
==Nat : Nat → Nat → Bool; | ||
==Nat zero zero ≔ true; | ||
==Nat (suc m) (suc n) ≔ ==Nat m n; | ||
==Nat _ _ ≔ false; | ||
|
||
one : Nat; | ||
one ≔ suc zero; | ||
|
||
two : Nat; | ||
two ≔ suc one; | ||
|
||
three : Nat; | ||
three ≔ suc two; | ||
|
||
four : Nat; | ||
four ≔ suc three; | ||
|
||
five : Nat; | ||
five ≔ suc four; | ||
|
||
six : Nat; | ||
six ≔ suc five; | ||
|
||
seven : Nat; | ||
seven ≔ suc six; | ||
|
||
eight : Nat; | ||
eight ≔ suc seven; | ||
|
||
nine : Nat; | ||
nine ≔ suc eight; | ||
|
||
ten : Nat; | ||
ten ≔ suc nine; | ||
|
||
plusNat : Nat → Nat → Nat; | ||
plusNat zero n ≔ n; | ||
plusNat (suc m) n ≔ suc (plusNat m n); | ||
|
||
natToInt : Nat → Int; | ||
natToInt zero ≔ 0; | ||
natToInt (suc n) ≔ 1 + (natToInt n); | ||
|
||
natToStr : Nat → String; | ||
natToStr n ≔ intToStr (natToInt n); | ||
|
||
infix 4 <Nat; | ||
<Nat : Nat → Nat → Bool; | ||
<Nat zero _ ≔ true; | ||
<Nat _ zero ≔ false; | ||
<Nat (suc m) (suc n) ≔ m <Nat n; | ||
|
||
infix 4 >Nat; | ||
>Nat : Nat → Nat → Bool; | ||
>Nat _ zero ≔ true; | ||
>Nat zero _ ≔ false; | ||
>Nat (suc m) (suc n) ≔ m >Nat n; | ||
|
||
infix 4 <=Nat; | ||
<=Nat : Nat → Nat → Bool; | ||
<=Nat l r ≔ (l <Nat r) || (==Nat l r); | ||
|
||
infix 4 >=Nat; | ||
>=Nat : Nat → Nat → Bool; | ||
>=Nat l r ≔ (l >Nat r) || (==Nat l r); | ||
|
||
foreign c { | ||
void* natInd(int n, void* a1, minijuvix_function_t* a2) { | ||
if (n <= 0) return a1; | ||
return ((void* (*) (minijuvix_function_t*, void*))a2->fun)(a2, natInd(n - 1, a1, a2)); | ||
\} | ||
}; | ||
|
||
axiom natInd : Int → Nat → (Nat → Nat) → Nat; | ||
compile natInd { | ||
c ↦ "natInd"; | ||
}; | ||
|
||
from-backendNat : Int → Nat; | ||
from-backendNat bb ≔ natInd bb zero suc; | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Prelude; | ||
|
||
import Data.Bool; | ||
import Data.Int; | ||
import Data.Nat; | ||
import Data.String; | ||
import Data.List; | ||
import Data.Maybe; | ||
import Data.Pair; | ||
import System.IO; | ||
|
||
open Data.Bool public; | ||
open Data.Int public; | ||
open Data.Nat public; | ||
open Data.String public; | ||
open Data.List public; | ||
open Data.Maybe public; | ||
open Data.Pair public; | ||
open System.IO public; | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Functions | ||
-------------------------------------------------------------------------------- | ||
|
||
axiom int-to-str : Int → String; | ||
|
||
compile int-to-str { | ||
c ↦ "intToStr"; | ||
}; | ||
|
||
const : (A : Type) → (B : Type) → A → B → A; | ||
const _ _ a _ ≔ a; | ||
|
||
id : {A : Type} → A → A; | ||
id a ≔ a; | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../Lib/Data |
Oops, something went wrong.