From ca55bc860d4aaa42ec57ed5ed39cb69f913f31e7 Mon Sep 17 00:00:00 2001 From: pimjager Date: Thu, 23 Jun 2016 12:35:23 +0200 Subject: [PATCH] Examples --- examples/demo/map.spl | 15 +++++++ examples/demo/maybe.spl | 44 ++++++++++++++++++ examples/demo/passBy.spl | 18 ++++++++ examples/demo/peano.spl | 53 ++++++++++++++++++++++ examples/demo/printparse.spl | 87 ++++++++++++++++++++++++++++++++++++ examples/higher.spl | 12 ++--- examples/passBy.spl | 12 ++++- gen.icl | 2 +- 8 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 examples/demo/map.spl create mode 100644 examples/demo/maybe.spl create mode 100644 examples/demo/passBy.spl create mode 100644 examples/demo/peano.spl create mode 100644 examples/demo/printparse.spl diff --git a/examples/demo/map.spl b/examples/demo/map.spl new file mode 100644 index 0000000..ef6cf38 --- /dev/null +++ b/examples/demo/map.spl @@ -0,0 +1,15 @@ +map(f, xs) :: (a -> b) -> [a] -> [b] { + if(isEmpty(xs)) { return []; } + else { + return f(xs.hd) : map(f, xs.tl); + } +} + +printI(i) :: Int -> Void { + print(i); +} + +main() { + var xs = map(\x-> x+1, [1,2,3,4,5]); + map(printI, xs); +} \ No newline at end of file diff --git a/examples/demo/maybe.spl b/examples/demo/maybe.spl new file mode 100644 index 0000000..a4b9416 --- /dev/null +++ b/examples/demo/maybe.spl @@ -0,0 +1,44 @@ +//poor mans maybe +//(1, a) = Just a +//(0, _) = Nothing + +pure(a) :: a -> (Int, a) { + return (1, a); +} + +//note, ma is edited by reference! +fmap(f, ma) :: (a -> b) -> (Int, a) -> (Int, b) { + if (ma.fst == 0) { return ma; } + else { + ma.snd = f(ma.snd); + return ma; + } +} + +bind(ma, f) :: (Int, a) -> (a -> (Int, b)) -> (Int, b) { + if (ma.fst == 0) { return ma; } + else { + return f(ma.snd); + } +} + +main() { + + var j = pure(True); + var f = \b->!b; + var n = (0, False); //ugly that we need the additional parameter + + var jf = fmap(\b->!b, j); + var nf = fmap(\b->!b, n); + + var fma = \b -> pure(!b); + + var bjf = bind(j, fma); //note is true because ma is passed by reference + bar bnf = bind(n, fma); + + print("fmap Just false: ", jf.fst, jf.snd, "\n"); + print("fmap Nothing: ", nf.fst, "\n"); + + print("bind Just True: ", bjf.fst, bjf.snd, "\n"); + print("bind Nothing: ", bnf.fst, "\n"); +} \ No newline at end of file diff --git a/examples/demo/passBy.spl b/examples/demo/passBy.spl new file mode 100644 index 0000000..74acb29 --- /dev/null +++ b/examples/demo/passBy.spl @@ -0,0 +1,18 @@ +modif(str) :: [Char] -> Void { + str.tl.tl.hd = 'z'; +} + +modifAtom(x) :: Int -> Void { + x = 4; +} + +main() { + var s = "bar"; + var y = 1; + print("before modif: ", s, "\n"); + modif(s); + print("after modif: ",s, "\n"); + print("before modifAtom: ", y, "\n"); + modifAtom(y); + print("after modifAtom: ", y); +} \ No newline at end of file diff --git a/examples/demo/peano.spl b/examples/demo/peano.spl new file mode 100644 index 0000000..f8e8c70 --- /dev/null +++ b/examples/demo/peano.spl @@ -0,0 +1,53 @@ +ackerman(m, n){ + if(m ==0){ + return n+1; + } else { + if(m>0 && n == 0){ + return ackerman(m-1, 1); + } else { + return ackerman(m-1, ackerman(m, n-1)); + } + } +} + +hyper(n, a, b){ + if(n == 0){ return b + 1; + } else { if(b == 0 && n == 1){ return a; + } else { if(b == 0 && n == 2){ return 0; + } else { if(b == 0 && n >= 3){ return 1; + } else { return hyper(n-1, a, hyper(n, a, b - 1)); + }}}} +} + +main(){ + print("s(3)=", hyper(0, 3, 3)); + print("3+3=", hyper(1, 3, 3)); + print("3*3=", hyper(2, 3, 3)); + print("3^4=", hyper(3, 3, 4)); + print("2|3=", hyper(4, 2, 3)); + + print("a(0, 0)=", ackerman(0, 0)); + print("a(0, 1)=", ackerman(0, 1)); + print("a(0, 2)=", ackerman(0, 2)); + print("a(0, 3)=", ackerman(0, 3)); + + print("a(1, 0)=", ackerman(1, 0)); + print("a(1, 1)=", ackerman(1, 1)); + print("a(1, 2)=", ackerman(1, 2)); + print("a(1, 3)=", ackerman(1, 3)); + + print("a(2, 0)=", ackerman(2, 0)); + print("a(2, 1)=", ackerman(2, 1)); + print("a(2, 2)=", ackerman(2, 2)); + print("a(2, 3)=", ackerman(2, 3)); + + print("a(3, 0)=", ackerman(3, 0)); + print("a(3, 1)=", ackerman(3, 1)); + print("a(3, 2)=", ackerman(3, 2)); + print("a(3, 3)=", ackerman(3, 3)); + + print("a(4, 0)=", ackerman(4, 0)); +/* print("a(4, 1)=", ackerman(4, 1)); + print("a(4, 2)=", ackerman(4, 2)); + print("a(4, 3)=", ackerman(4, 3));*/ +} diff --git a/examples/demo/printparse.spl b/examples/demo/printparse.spl new file mode 100644 index 0000000..68de2fb --- /dev/null +++ b/examples/demo/printparse.spl @@ -0,0 +1,87 @@ +toInt(x) :: Char -> Int{ + if(x == '0'){ return 0; + } else { if(x == '1'){ return 1; + } else { if(x == '2'){ return 2; + } else { if(x == '3'){ return 3; + } else { if(x == '4'){ return 4; + } else { if(x == '5'){ return 5; + } else { if(x == '6'){ return 6; + } else { if(x == '7'){ return 7; + } else { if(x == '8'){ return 8; + } else { if(x == '9'){ return 9; + } else { return -1; }}}}}}}}}} +} + +toChar(x) :: Int -> Char{ + if(x == 0){ return '0'; + } else { if(x == 1){ return '1'; + } else { if(x == 2){ return '2'; + } else { if(x == 3){ return '3'; + } else { if(x == 4){ return '4'; + } else { if(x == 5){ return '5'; + } else { if(x == 6){ return '6'; + } else { if(x == 7){ return '7'; + } else { if(x == 8){ return '8'; + } else { if(x == 9){ return '9'; + } else { return '0'; }}}}}}}}}} +} + +isDigit(x) :: Char -> Bool { + return toInt(x) != -1; +} + +strToInt(x) :: [Char] -> Int { + [Char] xs = x; + var i = 0; + var m = 1; + if(!isEmpty(x)){ + if(xs.hd == '-'){ + xs = xs.tl; + m = -1; + } + + while(!isEmpty(xs)){ + i = i*10 + toInt(xs.hd); + xs = xs.tl; + } + } + return i*m; +} + +printList(p, l) :: (a -> Void) -> [a] -> Void { + print('['); + if(!isEmpty(l)){ + p(l.hd); + l = l.tl; + } + while(!isEmpty(l)){ + print(", "); + p(l.hd); + l = l.tl; + } + print("]\n"); +} + +printInt(x) { + [Char] l = []; + if(x < 0){ + print('-'); + x = -x; + } + while(x > 0){ + l = toChar(x % 10) : l; + x = x / 10; + } + print(l); +} + +printBool(b) :: Bool -> Void { + print(b); +} + +main(){ + var num = "-1234"; + + printList(printInt, [32, 4, strToInt(num)]); + printList(printBool, [True, False, True, True]); +} diff --git a/examples/higher.spl b/examples/higher.spl index c1ad6d6..1b6f1e6 100644 --- a/examples/higher.spl +++ b/examples/higher.spl @@ -57,13 +57,13 @@ main(){ var f = giveF(); - var plus = \x y->x+y; + //var g = map(pl, [1,2,3]); + //var is = mapF(\f->f(1), g); + //var is = map(\f->(ap(f, 1)), g); + //var p = g.hd; + //var q = p(1); - var g = map(plus, [1,2,3]); - var is = mapF(\f->f(1), g); - //var is = map(\f->f(1), g); - var p = g.hd; - var q = p(1); + //print(q); //print("is.hd", is.hd); diff --git a/examples/passBy.spl b/examples/passBy.spl index d463ed1..74acb29 100644 --- a/examples/passBy.spl +++ b/examples/passBy.spl @@ -2,9 +2,17 @@ modif(str) :: [Char] -> Void { str.tl.tl.hd = 'z'; } +modifAtom(x) :: Int -> Void { + x = 4; +} + main() { var s = "bar"; - print(s, "\n"); + var y = 1; + print("before modif: ", s, "\n"); modif(s); - print(s, "\n"); + print("after modif: ",s, "\n"); + print("before modifAtom: ", y, "\n"); + modifAtom(y); + print("after modifAtom: ", y); } \ No newline at end of file diff --git a/gen.icl b/gen.icl index 25a4e6f..87e4f8e 100644 --- a/gen.icl +++ b/gen.icl @@ -359,9 +359,9 @@ followFs [FieldTl:fs] = tell [Instr "lda" [Lit 0] ""] followFs [FieldFst:fs] = tell [Instr "ldc" [Lit 1] "select fst" ,Instr "sub" [] "select fst"] >>| followFs fs +followFs [FieldSnd] = tell [] followFs [FieldSnd:fs] = tell [Instr "lda" [Lit 0] ""] >>| followFs fs -followFs [FieldSnd] = tell [] foldVarDecl :: Int VarDecl -> Gen Int foldVarDecl x (VarDecl _ mt k e) = g e -- 2.20.1