1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-01 00:04:58 +03:00

Update validity predicate milestone example to 0.2 syntax (#167)

* Update VP example to new syntax

* Ignore html folders and include assets for html rendering

Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
This commit is contained in:
Paul Cadman 2022-06-15 12:04:14 +01:00 committed by GitHub
parent 117020215c
commit 69a7bf4b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 182 additions and 108 deletions

1
.gitignore vendored
View File

@ -73,3 +73,4 @@ UPDATES-FOR-CHANGELOG.org
docs/md/
_docs
docs/**/*.md
**/html/*

View File

@ -4,25 +4,60 @@ foreign ghc {
import Anoma.Base
};
foreign c {
int readPre(const char *key) {
if (strcmp("change1-key", key)) {
return 100;
\} else if (strcmp("change2-key", key)) {
return 90;
\} else {
return -1;
\}
\}
int readPost(const char *key) {
if (strcmp("change1-key", key)) {
return 90;
\} else if (strcmp("change2-key", key)) {
return 100;
\} else {
return -1;
\}
\}
char* isBalanceKey(const char* s1, const char* s2) {
return "owner-address";
\}
};
import Prelude;
open Prelude;
from-int : Int → Maybe Int;
from-int i ≔ if (i < 0) nothing (just i);
from-string : String → Maybe String;
from-string s ≔ if (s ==String "") nothing (just s);
--------------------------------------------------------------------------------
-- Anoma
--------------------------------------------------------------------------------
axiom readPre : String → Int;
compile readPre {
c ↦ "readPre";
ghc ↦ "readPre";
};
axiom readPost : String → Int;
compile readPost {
c ↦ "readPost";
ghc ↦ "readPost";
};
axiom isBalanceKey : String → String → String;
compile isBalanceKey {
c ↦ "isBalanceKey";
ghc ↦ "isBalanceKey";
};
@ -36,6 +71,6 @@ is-balance-key : String → String → Maybe String;
is-balance-key token key ≔ from-string (isBalanceKey token key);
unwrap-default : Maybe Int → Int;
unwrap-default o ≔ maybe-int 0 o;
unwrap-default ≔ maybe 0 id;
end;

View File

@ -19,9 +19,9 @@ infixr 3 &&;
&& false _ ≔ false;
&& true a ≔ a;
if : (A : Type) → Bool → A → A → A;
if _ true a _ ≔ a;
if _ false _ b ≔ b;
if : {A : Type} → Bool → A → A → A;
if true a _ ≔ a;
if false _ b ≔ b;
--------------------------------------------------------------------------------
-- Backend Booleans
@ -29,28 +29,38 @@ if _ false _ b ≔ b;
axiom BackendBool : Type;
compile BackendBool {
c ↦ "bool";
ghc ↦ "Bool";
};
axiom backend-true : BackendBool;
compile backend-true {
c ↦ "true";
ghc ↦ "True";
};
axiom backend-false : BackendBool;
compile backend-false {
c ↦ "false";
ghc ↦ "False";
};
foreign ghc {
bool :: Bool -> a -> a -> a
bool True x _ = x
bool False _ y = y
};
foreign c {
void* boolInd(bool b, void* a1, void* a2) {
return b ? a1 : a2;
\}
};
axiom bool : BackendBool → Bool → Bool → Bool;
compile bool {
c ↦ "boolInd";
ghc ↦ "bool";
};

View File

@ -3,18 +3,34 @@ module Data.Int;
import Data.Bool;
open Data.Bool;
--------------------------------------------------------------------------------
-- Integers
--------------------------------------------------------------------------------
axiom Int : Type;
compile Int {
c ↦ "int";
ghc ↦ "Int";
};
foreign c {
bool lessThan(int l, int r) {
return l < r;
\}
bool eqInt(int l, int r) {
return l == r;
\}
int plus(int l, int r) {
return l + r;
\}
int minus(int l, int r) {
return l - r;
\}
};
infix 4 <';
axiom <' : Int → Int → BackendBool;
compile <' {
c ↦ "lessThan";
ghc ↦ "(<)";
};
@ -24,6 +40,7 @@ infix 4 <;
axiom eqInt : Int → Int → BackendBool;
compile eqInt {
c ↦ "eqInt";
ghc ↦ "(==)";
};
@ -34,12 +51,14 @@ infix 4 ==Int;
infixl 6 -;
axiom - : Int -> Int -> Int;
compile - {
c ↦ "minus";
ghc ↦ "(-)";
};
infixl 6 +;
axiom + : Int -> Int -> Int;
compile + {
c ↦ "plus";
ghc ↦ "(+)";
};

View File

@ -3,24 +3,18 @@ module Data.List;
import Data.Bool;
open Data.Bool;
import Data.String;
open Data.String;
--------------------------------------------------------------------------------
-- Lists
--------------------------------------------------------------------------------
infixr 5 ∷;
inductive List (A : Type) {
nil : List A;
cons : A → List A → List A;
∷ : A → List A → List A;
};
elem : (A : Type) → (A → A → Bool) → A → List A → Bool;
elem _ _ _ nil ≔ false;
elem A eq s (cons x xs) ≔ eq s x || elem _ eq s xs;
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 A B f z (cons h hs) ≔ foldl _ _ f (f z h) hs;
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;
end;

View File

@ -1,16 +1,21 @@
module Data.Maybe;
--------------------------------------------------------------------------------
-- Maybe
--------------------------------------------------------------------------------
import Data.Int;
open Data.Int;
import Data.Bool;
open Data.Bool;
inductive Maybe (A : Type) {
nothing : Maybe A;
just : A → Maybe A;
};
maybe : (A : Type) → (B : Type) → B → (A → B) → Maybe A → B;
maybe _ _ b _ nothing ≔ b;
maybe _ _ _ f (just x) ≔ f x;
-- from-int : Int → Maybe Int;
-- from-int i ≔ if (i < 0) nothing (just i);
maybe : {A : Type} → {B : Type} → B → (A → B) → Maybe A → B;
maybe b _ nothing ≔ b;
maybe _ f (just x) ≔ f x;
end;

View File

@ -1,12 +1,9 @@
module Data.Pair;
--------------------------------------------------------------------------------
-- Pair
--------------------------------------------------------------------------------
inductive Pair (A : Type) (B : Type) {
mkPair : A → B → Pair A B;
infixr 4 ,;
infixr 2 ×;
inductive × (A : Type) (B : Type) {
, : A → B → A × B;
};
end;

View File

@ -3,17 +3,21 @@ module Data.String;
import Data.Bool;
open Data.Bool;
--------------------------------------------------------------------------------
-- Strings
--------------------------------------------------------------------------------
axiom String : Type;
compile String {
c ↦ "char*";
ghc ↦ "[Char]";
};
foreign c {
bool eqString(char* l, char* r) {
return strcmp(l, r) == 0;
\}
};
axiom eqString : String → String → BackendBool;
compile eqString {
c ↦ "eqString";
ghc ↦ "(==)";
};

View File

@ -1,51 +0,0 @@
module FungibleToken;
import Anoma.Base;
open Anoma.Base;
import Prelude;
open Prelude;
--------------------------------------------------------------------------------
-- Misc
--------------------------------------------------------------------------------
pair-from-optionString : (String → Pair Int Bool) → Maybe String → Pair Int Bool;
pair-from-optionString ≔ maybe String (Pair Int Bool) (mkPair Int Bool 0 false);
check-result : Pair Int Bool → Bool;
check-result (mkPair change all-checked) ≔ (change ==Int 0) && all-checked;
--------------------------------------------------------------------------------
-- Validity Predicate
--------------------------------------------------------------------------------
change-from-key : String → Int;
change-from-key key ≔ unwrap-default (read-post key)
- unwrap-default (read-pre key);
check-vp : List String → String → Int → String → Pair Int Bool;
check-vp verifiers key change owner ≔
if _
-- make sure the spender approved the transaction
(change-from-key key < 0)
(mkPair _ _ (change + (change-from-key key))
(elem _ (==String) owner verifiers))
(mkPair _ _ (change + (change-from-key key)) true);
check-keys : String → List String → Pair Int Bool → String → Pair Int Bool;
check-keys token verifiers (mkPair change is-success) key ≔
if _
is-success
(pair-from-optionString (check-vp verifiers key change)
(is-balance-key token key))
(mkPair _ _ 0 false);
vp : String → List String → List String → Bool;
vp token keys-changed verifiers ≔
check-result
(foldl _ _
(check-keys token verifiers)
(mkPair _ _ 0 true)
keys-changed);
end;

View File

@ -2,19 +2,19 @@ module Prelude;
import Data.Bool;
import Data.Int;
import Data.String;
import Data.List;
import Data.Maybe;
import Data.Pair;
import Data.String;
import System.IO;
open Data.Bool;
open Data.Int;
open Data.List;
open Data.Maybe;
open Data.Pair;
open Data.String;
open System.IO;
open Data.Bool public;
open Data.Int public;
open Data.String public;
open Data.List public;
open Data.Maybe public;
open Data.Pair public;
open System.IO public;
--------------------------------------------------------------------------------
-- Functions
@ -23,7 +23,7 @@ open System.IO;
const : (A : Type) → (B : Type) → A → B → A;
const _ _ a _ ≔ a;
id : (A : Type) → A → A;
id _ a ≔ a;
id : {A : Type} → A → A;
id a ≔ a;
end;

View File

@ -0,0 +1,48 @@
module SimpleFungibleToken;
import Anoma.Base;
open Anoma.Base;
import Prelude;
open Prelude;
--------------------------------------------------------------------------------
-- Misc
--------------------------------------------------------------------------------
pair-from-optionString : (String → Int × Bool) → Maybe String → Int × Bool;
pair-from-optionString ≔ maybe (0 , false);
--------------------------------------------------------------------------------
-- Validity Predicate
--------------------------------------------------------------------------------
change-from-key : String → Int;
change-from-key key ≔ unwrap-default (read-post key) - unwrap-default (read-pre key);
check-vp : List String → String → Int → String → Int × Bool;
check-vp verifiers key change owner ≔
if
(change-from-key key < 0)
-- make sure the spender approved the transaction
(change + (change-from-key key), elem (==String) owner verifiers)
(change + (change-from-key key), true);
check-keys : String → List String → Int × Bool → String → Int × Bool;
check-keys token verifiers (change , is-success) key ≔
if
is-success
(pair-from-optionString (check-vp verifiers key change) (is-balance-key token key))
(0 , false);
check-result : Int × Bool → Bool;
check-result (change , all-checked) ≔ (change ==Int 0) && all-checked;
vp : String → List String → List String → Bool;
vp token keys-changed verifiers ≔
check-result
(foldl
(check-keys token verifiers)
(0 , true)
keys-changed);
end;

View File

@ -1,27 +1,39 @@
module System.IO;
--------------------------------------------------------------------------------
-- Actions
--------------------------------------------------------------------------------
import Data.String;
open Data.String;
import Data.Bool;
open Data.Bool;
axiom Action : Type;
compile Action {
ghc ↦ "IO ()";
c ↦ "int";
ghc ↦ "IO ()";
};
axiom putStr : String → Action;
compile putStr {
c ↦ "putStr";
ghc ↦ "putStr";
};
axiom putStrLn : String → Action;
compile putStrLn {
c ↦ "putStrLn";
ghc ↦ "putStrLn";
};
foreign c {
int sequence(int a, int b) {
return a + b;
\}
};
infixl 1 >>;
axiom >> : Action → Action → Action;
compile >> {
c ↦ "sequence";
ghc ↦ "(>>)";
};

View File

@ -23,15 +23,14 @@ change2-key : String;
change2-key ≔ "change2-key";
verifiers : List String;
verifiers ≔ cons String owner-address (nil String);
verifiers ≔ owner-address ∷ nil;
keys-changed : List String;
keys-changed ≔ cons String change1-key (cons String change2-key (nil String));
keys-changed ≔ change1-key ∷ change2-key ∷ nil;
main : Action;
main ≔
(putStr "VP Status: ")
>> (putStrLn (show-result (vp token keys-changed verifiers)));
end;
end;

View File

@ -14,6 +14,7 @@ github: heliaxdev/minijuvix
extra-source-files:
- README.org
- assets/*
dependencies:
- ansi-terminal == 0.11.*