Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extracted the `fix` method from `FunC` to `RecFunC`, as suggested by …

…Sean Leather.
  • Loading branch information...
commit 4e237a9200978091cbc876403a03a56955815f0f 1 parent 0d71095
@tomlokhorst authored
View
4 src/Generic/Control/Function.hs
@@ -13,9 +13,11 @@ class NameC j where
class NameC j => FunC j where
lam :: (j a -> j b) -> j (a -> b)
- fix :: (j (a -> b) -> j (a -> b)) -> j (a -> b)
app :: j (a -> b) -> j a -> j b
+class FunC j => RecFunC j where
+ fix :: (j (a -> b) -> j (a -> b)) -> j (a -> b)
+
instance FunC j => Category j (->) where
id a = "id" `named` (lam (\i -> i) `app` a)
(.) f g a = lam f `app` (lam g `app` a)
View
18 src/Generic/Data/List.hs
@@ -28,35 +28,35 @@ instance (BoolC j, ListC j, Ord j a) => Ord j [a] where
(\x xs' -> list false (\y ys' -> x <= y || xs' <= ys') ys)
xs
-instance (FunC j, ListC j) => Functor j [] where
+instance (RecFunC j, ListC j) => Functor j [] where
fmap f = foldr (\a r -> f a `cons` r) nil
singleton :: ListC j => j a -> j [a]
singleton a = a `cons` nil
-foldr :: (FunC j, ListC j) => (j a -> j b -> j b) -> j b -> j [a] -> j b
+foldr :: (RecFunC j, ListC j) => (j a -> j b -> j b) -> j b -> j [a] -> j b
foldr f b xs = fix (\r -> lam (list b (\y ys -> f y (r `app` ys)))) `app` xs
-replicate :: (ListC j, Eq j a, BoolC j, FunC j, Num j a) => j a -> j b -> j [b]
+replicate :: (Eq j a, BoolC j, RecFunC j, ListC j, Num j a) => j a -> j b -> j [b]
replicate n a = fix (\r -> lam (\y -> bool nil (a `cons` (r `app` (y - 1))) (y == 0))) `app` n
-(++) :: (FunC j, ListC j) => j [a] -> j [a] -> j [a]
+(++) :: (RecFunC j, ListC j) => j [a] -> j [a] -> j [a]
xs ++ ys = foldr cons ys xs
-genericLength :: (FunC j, ListC j, Num j a) => j [b] -> j a
+genericLength :: (RecFunC j, ListC j, Num j a) => j [b] -> j a
genericLength = foldr (\_ -> (+1)) 0
-sum :: (FunC j, ListC j, Num j a) => j [a] -> j a
+sum :: (RecFunC j, ListC j, Num j a) => j [a] -> j a
sum = foldr (+) 0
-filter :: (ListC j, BoolC j, FunC j) => (j a -> j Bool) -> j [a] -> j [a]
+filter :: (BoolC j, RecFunC j, ListC j) => (j a -> j Bool) -> j [a] -> j [a]
filter p = foldr (\x xs -> bool xs (x `cons` xs) (p x)) nil
-reverse :: (FunC j, ListC j) => j [a] -> j [a]
+reverse :: (RecFunC j, ListC j) => j [a] -> j [a]
reverse l = rev `app` l `app` nil
where
rev = fix (\r -> lam (\xs -> lam (\a -> list a (\y ys -> r `app` ys `app` (y `cons` a)) xs)))
-and :: (BoolC j, FunC j, ListC j) => j [Bool] -> j Bool
+and :: (BoolC j, RecFunC j, ListC j) => j [Bool] -> j Bool
and = foldr (&&) true
View
2  src/Generic/Data/Maybe.hs
@@ -33,6 +33,6 @@ instance (FunC j, MaybeC j) => Functor j Maybe where
fromMaybe :: MaybeC j => j a -> j (Maybe a) -> j a
fromMaybe d m = maybe d (\a -> a) m
-catMaybes :: (FunC j, ListC j, MaybeC j) => j [Maybe a] -> j [a]
+catMaybes :: (RecFunC j, ListC j, MaybeC j) => j [Maybe a] -> j [a]
catMaybes = foldr (\a b -> maybe nil singleton a ++ b) nil
View
4 src/Lang/Haskell.hs
@@ -20,9 +20,11 @@ type instance H (a -> b) = H a -> H b
instance FunC Haskell where
lam f = Hs (\x -> runHaskell (f (Hs x)))
- fix f = f (fix f)
app (Hs f) (Hs x) = Hs (f x)
+instance RecFunC Haskell where
+ fix f = f (fix f)
+
type instance H Bool = P.Bool
instance BoolC Haskell where
View
4 src/Lang/JavaScript.hs
@@ -19,9 +19,11 @@ instance NameC (Val JS) where
instance FunC (Val JS) where
lam f = Lam f
- fix f = fun1 "fix" (\[v] -> "fix = arguments.callee, " ++ v ++ "(function (i) { return fix(" ++ v ++ ")(i) })") (lam f)
app f x = App f x
+instance RecFunC (Val JS) where
+ fix f = fun1 "fix" (\[v] -> "fix = arguments.callee, " ++ v ++ "(function (i) { return fix(" ++ v ++ ")(i) })") (lam f)
+
instance BoolC (Val JS) where
false = Con "false"
true = Con "true"
Please sign in to comment.
Something went wrong with that request. Please try again.