comparison.md 1.24 KB
Newer Older
Nicolas Lenz's avatar
Nicolas Lenz committed
1 2
# Comparisons

Nicolas Lenz's avatar
Nicolas Lenz committed
3
This compares some stuff in Haskell wtih how it could look in Katrin. This is meant to emphasize how Katrin could cut back on verbosity.
Nicolas Lenz's avatar
Nicolas Lenz committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

### Simple function with pattern matching

```
length :: [a] -> Int
length [] = 0
length (x:xs) = 1 + length xs
```

```
length : List a -> Nat0
    [] => 0
    (x::xs) => 1 + length xs
```

### Records

```
data Person = Person {personName :: String, personAge :: Int}  -- prefixing because of name collisions

peter :: Person
Nicolas Lenz's avatar
Nicolas Lenz committed
25
peter = Person {personName = "Peter", personAge = 65}
Nicolas Lenz's avatar
Nicolas Lenz committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

showName :: Person -> String
showName person = "The name is " ++ name person ++ "."
```

```
Person : Type
    Name : Person -> String  -- no prefixing necessary
    Age : Person -> Nat0

peter : Person
    Name = "Peter"
    Age = 65

showName : Person -> String
    person => "The name is " + person.Name + "."
```
Nicolas Lenz's avatar
Nicolas Lenz committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

### Traits

```
class Showable (a :: *) where
    show :: a -> String

instance Showable Person where
    show p = "The name is " ++ personName p

f :: Showable a => a -> IO ()
f x = putStrLn $ "Show: " ++ show x
```

```
Nicolas Lenz's avatar
Nicolas Lenz committed
58
trait (a : Type) : Showable
Nicolas Lenz's avatar
Nicolas Lenz committed
59 60 61 62 63 64 65 66
    show : a -> String

Person : Showable
    show p = "The name is " + p.Name

f : (a : Showable, m : MonadTerminal) => a -> m ()
    x => print $ "Show: " + show x
```