From 5a157d66b046a3061f92b47a02dee97e34d7df37 Mon Sep 17 00:00:00 2001 From: tA Date: Wed, 17 Jul 2019 12:28:00 +1200 Subject: [PATCH] first commit --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ week2.hs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 README.md create mode 100644 week2.hs diff --git a/README.md b/README.md new file mode 100644 index 0000000..c58c52c --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# I'm so sorry + +Hoon101's Week 2 Assignment in Haskell + +> Create a gate that takes a noun, checks if its a cell, and if not, checks if the atom is odd or even + +## Running + +in `ghci`, load the file: + +``` +:load week2.hs +``` + +you can then run the program using the form + +`:t solution (nil :: X)` + +where `X` is either an atom or a cell, using the following syntax: + +`Atom`: Using Church Encoding with `(S n)` and `Z`, ie `(S (S (S Z)))` is the atom `3` + +`Cell`: Using `Cons x xs` and `Nil`, ie `(Cons (S Z) (Cons Z Z))` is the cell `[1 [0 0]]` + +## Example + +``` +*Main> :load week2.hs +[1 of 1] Compiling Main ( week2.hs, interpreted ) +Ok, modules loaded: Main. + +*Main> :t solution (nil :: (S (S (S (S Z))))) +solution (nil :: (S (S (S (S Z))))) + :: IsEvenAtom + +*Main> :t solution (nil :: (S (S (S (S (S Z)))))) +solution (nil :: (S (S (S (S (S Z)))))) + :: IsOddAtom + +*Main> :t solution (nil :: (Cons (Cons (S Z) (S (S Z))) (Cons (S (S (S Z))) (S (S (S (S Z))))))) +solution (nil :: (Cons (Cons (S Z) (S (S Z))) (Cons (S (S (S Z))) (S (S (S (S Z))))))) + :: IsCell +``` diff --git a/week2.hs b/week2.hs new file mode 100644 index 0000000..a68bad0 --- /dev/null +++ b/week2.hs @@ -0,0 +1,61 @@ +{-# OPTIONS_GHC -fno-warn-missing-methods #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE FunctionalDependencies #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE UndecidableInstances #-} + +nil = undefined + +data True +data False + +data S n +data Z + +data Nil +data Cons h t + +data IsCell +data IsOddAtom +data IsEvenAtom + +class Not t b | t -> b +instance Not True False +instance Not False True + +class And a b r | a b -> r +instance And True True True +instance And True False False +instance And False True False +instance And False False False + +class If c t f r | c t f -> r +instance If True t f t +instance If False t f f + +class IsEven a b | a -> b +instance IsEven Z True +instance IsEven (S Z) False +instance (IsEven n r) + => IsEven (S (S n)) r +instance IsEven (Cons xs x) False + +class Wutpam t b | t -> b +instance Wutpam Z True +instance Wutpam (S n) True +instance Wutpam Nil False +instance Wutpam (Cons x xs) False + +class Wutket t b | t -> b +instance (Wutpam t b, Not b r) + => Wutket t r + +class Solution n r | n -> r + where solution :: n -> r +instance ( Wutket n c + , Not c a + , IsEven n m + , If m IsEvenAtom IsOddAtom e + , If c IsCell e o + ) + => Solution n o where solution = nil