data MyInt where
MkMyInt :: Int -> MyInt
a :: MyInt
a = MkMyInt 42
extractInt :: MyInt -> Int
extractInt (MkMyInt n) = n
data Employee where
MkEmployee
:: String -- name
-> Int -- age
-> Double -- salary
-> Employee
data Shape where
Circle :: Double {- radius -} -> Shape
Rectangle :: Double -> Double -> Shape
area :: Shape -> Double
area (Circle r) = pi * r^2
area (Rectangle x y) = x * y
data Value where
Scalar :: Double -> Value
String :: String -> Value
Array :: Vector Value -> Value
Dict :: HashTable Value -> Value
Func :: (Value -> Value) -> Value
square :: Double -> Double
square x = x^2
x = square "foo" -- compile-time error
vs
square :: Value -> Value
square (Scalar x) = Scalar (x^2)
square _ = throw DynamicTypeException
x = square (String "foo") -- run-time error
enum TAG { SCALAR, STRING, ARRAY, DICT, FUNC };
struct dyn_t {
enum TAG tag;
union {
scalar_t scalar;
string_t string;
array_t array;
dict_t dict;
func_t func;
} payload;
};
sub square($) {
my $x = shift;
return $x ** 2;
}
my @y = (1,2);
print square(@y);
sub square($) {
my $x = shift;
return $$x ** 2;
}
my @y = (1,2);
print square(\@y);
data Value a where
MkValue :: a -> Value a
a :: Value Int
a = MkValue 42
extractInt :: Value Int -> Int
extractInt (MkValue n) = n
data Value where
MkValue :: a -> Value
a :: Value
a = MkValue 42
extractInt :: Value -> Int
extractInt (MkValue n) = n
data Value where
MkValue :: a -> Value
a :: Value
a = MkValue 42
extractInt :: Value -> Int
extractInt (MkValue n) = unsafeCoerce n
data Tag where
IntTag :: Tag
StringTag :: Tag
data Value where
MkValue :: Tag -> a -> Value
a :: Value
a = MkValue IntTag 42
extractInt :: Value -> Int
extractInt (MkValue IntTag n) = unsafeCoerce n
extractInt _ = throw DynamicTypeException
data Tag a where
IntTag :: Tag Int
StringTag :: Tag String
data Value where
MkValue :: Tag a -> a -> Value
a :: Value
a = MkValue IntTag 42
extractInt :: Value -> Int
extractInt (MkValue IntTag n) = n
extractInt _ = throw DynamicTypeException
data Counter where
MkCounter
:: u
-> (u -> Counter) -- increment
-> (u -> Int) -- get value
-> Counter
data Set a where
MkSet
:: s
-> (s -> [a]) -- elements
-> (s -> Set a -> Set a) -- union
-> Set a
data Set a where
MkSet
:: s
-> (s -> [a]) -- elements
-> (s -> s -> Set a) -- union
-> Set a
data Set a where
MkSet
:: (s -> [a]) -- elements
-> (s -> s -> s) -- union
-> s -- empty
-> (a -> s) -- singleton
-> Set a
data Iterator a where
End :: Iterator a
Focus
:: u
-> (u -> a) -- current
-> (u -> Iterator a) -- next
-> Iterator a
data Iterator a where
End :: Iterator a
Focus
:: u
-> (u -> (a, Iterator a))
-> Iterator a
data Iterator a where
End :: Iterator a
Focus
:: (a, Iterator a)
-> Iterator a
data Iterator a where
End :: Iterator a
Focus
:: a
-> Iterator a
-> Iterator a
data List a where
Nil :: Iterator a
Cons
:: a
-> List a
-> List a