Browse Source

first commit

master
Thorn Avery 4 years ago
commit
5a157d66b0
2 changed files with 104 additions and 0 deletions
  1. +43
    -0
      README.md
  2. +61
    -0
      week2.hs

+ 43
- 0
README.md View File

@@ -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
```

+ 61
- 0
week2.hs View File

@@ -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

Loading…
Cancel
Save