aboutsummaryrefslogtreecommitdiff
path: root/entries/funemy
diff options
context:
space:
mode:
authorLily Lin2022-10-26 23:30:06 +0000
committerLily Lin2022-10-26 23:30:06 +0000
commitaf03b3cd051a8a8b1442147136156f271f07ac5c (patch)
tree6239bbd269f8c682fcd2bd1aecd29cc8b2edf6b5 /entries/funemy
parent66fdac7a5ab7040a29fdd6a49cb0123db5ea916a (diff)
parent18d17f9fb578df5f20e689481ed06f44fcf4b80c (diff)
Merge branch 'main' into lily
Diffstat (limited to 'entries/funemy')
-rw-r--r--entries/funemy/.gitignore3
-rw-r--r--entries/funemy/haskell/morphib.hs43
-rw-r--r--entries/funemy/koka/fib.kk12
-rw-r--r--entries/funemy/symbolic/phib.py21
-rwxr-xr-xentries/funemy/z3/z3fib.sh2
-rwxr-xr-xentries/funemy/z3/z4fib.sh40
6 files changed, 120 insertions, 1 deletions
diff --git a/entries/funemy/.gitignore b/entries/funemy/.gitignore
index acb903a..9211bbd 100644
--- a/entries/funemy/.gitignore
+++ b/entries/funemy/.gitignore
@@ -1,3 +1,6 @@
.DS_Store
*.agdai
*.smt2
+**/.koka/*
+*.hi
+*.o
diff --git a/entries/funemy/haskell/morphib.hs b/entries/funemy/haskell/morphib.hs
new file mode 100644
index 0000000..1fae449
--- /dev/null
+++ b/entries/funemy/haskell/morphib.hs
@@ -0,0 +1,43 @@
+-- Fantastic Morphisms
+newtype Free f = Free {unFree :: f (Free f)}
+
+cata :: Functor f => (f a -> a) -> Free f -> a
+cata phi (Free x) = phi $ fmap (cata phi) x
+
+ana :: Functor f => (a -> f a) -> a -> Free f
+ana phi x = Free $ fmap (ana phi) (phi x)
+
+data FibF a =
+ Fib0
+ | Fib1
+ | FibN a a
+ deriving (Eq, Show)
+
+instance Functor FibF where
+ fmap f Fib0 = Fib0
+ fmap f Fib1 = Fib1
+ fmap f (FibN l r) = FibN (f l) (f r)
+
+type Fib a = Free FibF
+
+gen :: Int -> FibF Int
+gen 0 = Fib0
+gen 1 = Fib1
+gen n = FibN (n-1) (n-2)
+
+interp :: FibF Int -> Int
+interp Fib0 = 0
+interp Fib1 = 1
+interp (FibN l r) = l + r
+
+bif :: Int -> Fib Int
+bif = ana gen
+
+fib' :: Fib Int -> Int
+fib' = cata interp
+
+fib :: Int -> Int
+fib = fib' . bif
+
+main :: IO ()
+main = print (fib 14)
diff --git a/entries/funemy/koka/fib.kk b/entries/funemy/koka/fib.kk
new file mode 100644
index 0000000..5b7e66b
--- /dev/null
+++ b/entries/funemy/koka/fib.kk
@@ -0,0 +1,12 @@
+effect fib
+ ctl fib(n : int) : int
+
+fun doFib(inp : int) : div int
+ with ctl fib(n)
+ if n == 0 then 0
+ else if n == 1 then 1
+ else (doFib(n - 1) : int) + doFib(n - 2)
+ fib(inp)
+
+fun main()
+ print(doFib(20))
diff --git a/entries/funemy/symbolic/phib.py b/entries/funemy/symbolic/phib.py
new file mode 100644
index 0000000..c6fb23e
--- /dev/null
+++ b/entries/funemy/symbolic/phib.py
@@ -0,0 +1,21 @@
+from typing import List
+
+def phib(xs: List[int]) -> bool:
+ """
+ Instructions:
+ 1. `pip install crosshair-tool`
+ 2. modify the precondition `pre` to control the length of your fib sequence
+ 3. run `crosshair check phib.py` in your terminal
+
+ pre: len(xs) >= 10
+ post: __return__ != True
+ """
+ if xs[0] != 0:
+ return False
+ if len(xs) > 1:
+ if xs[1] != 1:
+ return False
+ for i in range(2,len(xs)):
+ if xs[i] != xs[i-1] + xs[i-2]:
+ return False
+ return True
diff --git a/entries/funemy/z3/z3fib.sh b/entries/funemy/z3/z3fib.sh
index 07ab418..e96226f 100755
--- a/entries/funemy/z3/z3fib.sh
+++ b/entries/funemy/z3/z3fib.sh
@@ -15,7 +15,7 @@ else
fi
if [ "$1" -lt "0" ]; then
- echo "Argument must be larger than 0."
+ echo "Argument must be larger or equal to 0."
exit 1
fi
diff --git a/entries/funemy/z3/z4fib.sh b/entries/funemy/z3/z4fib.sh
new file mode 100755
index 0000000..f7a1e48
--- /dev/null
+++ b/entries/funemy/z3/z4fib.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+# z3 fib, but better
+
+# Instructions:
+# 1. having z3 installed and put under your $PATH
+# 2. making sure z4fib.sh is executable, by `chmod +x z4fib.sh`
+# 3. running as `./z4fib.sh [length of the fib sequence]`
+# 4. having fun :)
+
+if [ -e fib.smt2 ]
+then
+ rm -f fib.smt2
+ touch fib.smt2
+else
+ touch fib.smt2
+fi
+
+if [ "$1" -lt "0" ]; then
+ echo "Argument must be larger or equal to 0."
+ exit 1
+fi
+
+echo "(declare-const fib (Seq Int))" >> fib.smt2
+echo "" >> fib.smt2
+echo "(assert" >> fib.smt2
+echo " (and" >> fib.smt2
+echo " (= (seq.len fib) $1)" >> fib.smt2
+echo " (= (seq.nth fib 0) 0)" >> fib.smt2
+echo " (= (seq.nth fib 1) 1)" >> fib.smt2
+echo " (forall ((i Int))" >> fib.smt2
+echo " (=> (and (> i 1) (< i (seq.len fib)))" >> fib.smt2
+echo " (= (seq.nth fib i)" >> fib.smt2
+echo " (+ (seq.nth fib (- i 1))" >> fib.smt2
+echo " (seq.nth fib (- i 2))))))))" >> fib.smt2
+echo "" >> fib.smt2
+
+echo "(check-sat)" >> fib.smt2
+echo "(get-model)" >> fib.smt2
+
+z3 fib.smt2