Change in priorities
This commit is contained in:
72
examples/vs_haskell/typeclasses.hs
Normal file
72
examples/vs_haskell/typeclasses.hs
Normal file
@@ -0,0 +1,72 @@
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE InstanceSigs #-}
|
||||
{-# LANGUAGE BlockArguments #-}
|
||||
import Prelude((>>=), Maybe( Just, Nothing ), return, fmap)
|
||||
import Debug.Trace
|
||||
|
||||
-- 1
|
||||
class Add l r o where
|
||||
add :: l -> r -> o
|
||||
(+) :: l -> r -> o
|
||||
(+) = add
|
||||
|
||||
-- 2
|
||||
class Mappable c where
|
||||
map :: (i -> o) -> c i -> c o
|
||||
|
||||
-- 3
|
||||
class Mappable c => Zippable c where
|
||||
zip :: (l -> r -> o) -> c l -> c r -> c o
|
||||
|
||||
|
||||
-- 4
|
||||
class Default t where
|
||||
def :: t
|
||||
|
||||
-- 5
|
||||
instance (Zippable c, Add l r o)
|
||||
=> Add (c l) (c r) (c o) where
|
||||
add :: (Zippable c, Add l r o) => c l -> c r -> c o
|
||||
add = zip add
|
||||
|
||||
|
||||
|
||||
-- 6
|
||||
-- newtype List t = List (Maybe (t, List t))
|
||||
|
||||
-- instance Mappable List where
|
||||
-- map :: (i -> o) -> List i -> List o
|
||||
-- map f (List o) = List (fmap (\(h, t) -> (f h, map f t)) o)
|
||||
|
||||
-- instance Zippable List where
|
||||
-- zip :: (l -> r -> o) -> List l -> List r -> List o
|
||||
-- zip f (List l) (List r) = List do
|
||||
-- (lh, lt) <- l
|
||||
-- (rh, rt) <- r
|
||||
-- return (f lh rh, zip f lt rt)
|
||||
|
||||
-- instance Add (List e) (List e) (List e) where
|
||||
-- add (List l) (List r) = List case l of
|
||||
-- Just (head, tail) -> Just (head, add tail r)
|
||||
-- Nothing -> r
|
||||
|
||||
data List t = Cons t (List t) | End
|
||||
|
||||
instance Mappable List where
|
||||
map :: (i -> o) -> List i -> List o
|
||||
map _ End = End
|
||||
map f (Cons head tail) = Cons (f head) (map f tail)
|
||||
|
||||
instance Zippable List where
|
||||
zip :: (l -> r -> o) -> List l -> List r -> List o
|
||||
zip _ _ End = End
|
||||
zip _ End _ = End
|
||||
zip f (Cons lhead ltail) (Cons rhead rtail) =
|
||||
Cons (f lhead rhead) (zip f ltail rtail)
|
||||
|
||||
instance Add (List e) (List e) (List e) where
|
||||
add End r = r
|
||||
add (Cons head tail) r = Cons head (add tail r)
|
||||
|
||||
|
||||
65
examples/vs_haskell/typeclasses.orc
Normal file
65
examples/vs_haskell/typeclasses.orc
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-- 1
|
||||
define Add $L $R $O
|
||||
as $L -> $R -> $O
|
||||
|
||||
$left:2... + $right:1... =1000=> add ($left...) ($right...)
|
||||
|
||||
-- 2
|
||||
define Mappable $C:type -> type
|
||||
as @I. @O. (I -> O) -> $C I -> $C O
|
||||
|
||||
-- 3
|
||||
define Zippable $C:type -> type
|
||||
as @:Mappable $C.
|
||||
@L. @R. @O. (L -> R -> O) -> $C L -> $C R -> $C O
|
||||
|
||||
-- 4
|
||||
define Default $T:type as $T
|
||||
|
||||
|
||||
-- 5
|
||||
impl
|
||||
@C:Type -> Type. @L. @R. @O.
|
||||
@:(Zippable C). @:(Add L R O).
|
||||
Add (C L) (C R) (C O)
|
||||
by elementwiseAdd
|
||||
via zip add
|
||||
|
||||
-- 6
|
||||
define List $E as Y \r. Option t[ $E, r ]
|
||||
|
||||
impl Mappable List
|
||||
via \f.\list. categorise (
|
||||
(Y \repeat. \opt. match opt {
|
||||
Some t[head, tail] =>
|
||||
Some t[f head, repeat tail];
|
||||
None => None;
|
||||
}) (generalise list)
|
||||
)
|
||||
|
||||
impl Zippable List
|
||||
via \f.\l.\r. categorise (
|
||||
Y \repeat.\lopt.\ropt. do {
|
||||
bind t[lhead, ltail] <- lopt;
|
||||
bind t[rhead, rtail] <- ropt;
|
||||
t[f lhead rhead, repeat ltail rtail]
|
||||
}
|
||||
) (generalise l) (generalise r)
|
||||
|
||||
impl @T. Add (List T) (List T) (List T)
|
||||
by concatListAdd over elementwiseAdd
|
||||
via \l.\r.categorise Y \repeat.\l. (
|
||||
match l (
|
||||
Some t[head, tail] =>
|
||||
Some t[head, repeat tail];
|
||||
None => (generalise r)
|
||||
)
|
||||
) (generalise l)
|
||||
|
||||
Reference in New Issue
Block a user