1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-15 10:03:22 +03:00
juvix/tests/positive/MiniC/HigherOrder/Input.juvix
2023-01-16 18:15:25 +00:00

143 lines
2.5 KiB
Plaintext

module Input;
--------------------------------------------------------------------------------
-- Strings
--------------------------------------------------------------------------------
builtin string axiom String : Type;
compile String {
c ↦ "char*";
};
--------------------------------------------------------------------------------
-- IO
--------------------------------------------------------------------------------
axiom Action : Type;
compile Action {
c ↦ "int";
};
foreign c {
int sequence(int a, int b) {
return a + b;
\}
};
infixl 1 >>;
axiom >> : Action → Action → Action;
compile >> {
c ↦ "sequence";
};
axiom put-str : String → Action;
compile put-str {
c ↦ "putStr";
};
axiom put-str-ln : String → Action;
compile put-str-ln {
c ↦ "putStrLn";
};
--------------------------------------------------------------------------------
-- Integers
--------------------------------------------------------------------------------
axiom Int : Type;
compile Int {
c ↦ "int";
};
axiom Int_0 : Int;
compile Int_0 {
c ↦ "0";
};
axiom Int_1 : Int;
compile Int_1 {
c ↦ "1";
};
foreign c {
int plus(int l, int r) {
return l + r;
\}
};
infixl 6 +int;
axiom +int : Int -> Int -> Int;
compile +int {
ghc ↦ "(+)";
c ↦ "plus";
};
axiom to-str : Int → String;
compile to-str {
c ↦ "intToStr";
};
--------------------------------------------------------------------------------
-- Natural Numbers
--------------------------------------------------------------------------------
type Nat :=
zero : Nat
| suc : Nat → Nat;
infixl 6 +;
+ : Nat → Nat → Nat;
+ zero n := n;
+ (suc m) n := suc (m + n);
to-int : Nat → Int;
to-int zero := Int_0;
to-int (suc n) := Int_1 +int (to-int n);
nat-to-str : Nat → String;
nat-to-str n := to-str (to-int n);
one : Nat;
one := suc zero;
two : Nat;
two := suc one;
three : Nat;
three := suc two;
--------------------------------------------------------------------------------
-- Lists
--------------------------------------------------------------------------------
type ListNat :=
null : ListNat |
cons : Nat → ListNat → ListNat;
foldl : (Nat → Nat → Nat) → Nat → ListNat → Nat;
foldl _ z null := z;
foldl f z (cons n hs) := foldl f (f z n) hs;
l : ListNat;
l := cons one (cons two null);
sum : ListNat → Nat;
sum l := foldl (+) zero l;
sum-is-three : Action;
sum-is-three := put-str "list sum is: "
>> put-str-ln (nat-to-str (sum l));
main : Action;
main := sum-is-three
end;