ุชุนุชุจุฑ ุงูู ุตูููุงุช ุงูุขู ูุฉ ู ู ุงูููุน ู ูุถูุนูุง ุฏุงุฆู ูุง. ูุชุฌุงุฏููู ุญูู ู ุฏู ู ูุงุกู ุชูุง ุ ููุชู ูุชุงุจุฉ ูุบุงุช ูุงู ูุฉ ูุชูููุฐ ููุงุฆู ุจุทูู ุนูู ู ุณุชูู ุงูููุน . ููุฏ ุฃุฏูุดูู ุงูุบุฑูุจ ุฃูู ูุง ููุฌุฏ ุญุชู ุงูุขู ู ุชุบูุฑ ูู ูุงุณูู ููุจู ุงูู ุนุงููุฑ ุงูุนููุงููุฉ ููุฑุงุญุฉ ูุงูุฃู ุงู. ูู ููุงู ุฃู ุฃุณุจุงุจ ูููุต ุงูู ูุชุจุงุช ุงูุฌุงูุฒุฉ ุฃู ุฃููุง ููุณุช ุถุฑูุฑูุฉุ ุฏุนููุง ูููู ุฐูู.
ุฃุถู ู ุทุฑููุฉ ูููู ุณุจุจ ุนุฏู ูุฌูุฏ ุดูุก ู ุง (ูุงูุฐู ูุฌุจ ุฃู ูููู ุจุงูุชุฃููุฏ!) ูู ู ุญุงููุฉ ุงูููุงู ุจุฐูู ุจููุณู. ููุฌุฑุจ ..
ุชููุน
ุฃูู ุดูุก ูุชุจุงุฏุฑ ุฅูู ุงูุฐูู (ุนูู ุงูุฃูู ุจุงููุณุจุฉ ูู) ู
ู ุงูู
ุงุฏุฉ ุนูู ู
ู ูุงุณูู ููุน ู
ุณุชูู ุ ุญูุซุ ุจู
ุณุงุนุฏุฉ DataKindsุ GADTsุ KindSignatures(ูุตูุง ู
ูุฌุฒุง ู
ู ุฃูู ููู
ุงุฐุง ูุชู
ุงุณุชุฎุฏุงู
ูุง - ุฃุฏูุงู) ูุชู
ุฅุฏุฎุงู ุงูุฃุนุฏุงุฏ ุงูุทุจูุนูุฉ ุงูุงุณุชูุฑุงุฆูุ ูุงูุชู ุชูู ูุฑุงุกูุง ููุงููุงุช ู
ุนูู
ุงุช ุทูู:
data Nat = Zero | Succ Nat
data Vector (n :: Nat) a where
(:|) :: a -> Vector n a -> Vector ('Succ n) a
Nil :: Vector 'Zero a
infixr 3 :|
KindSignaturesูุณุชุฎุฏู
ููุฅุดุงุฑุฉ ุฅูู ุฃูู nูุฏ ูุง ูููู ุฃู ููุน ูู ููุน *(ู
ุซู ู
ุนูู
ุฉ ูู ููุณ ุงูู
ุซุงู) ุ ูููู ููู
ุฉ ู
ู ุงูููุน Nat ู
ุฑููุนุฉ ุฅูู ู
ุณุชูู ุงูุฃููุงุน. ูุฐุง ู
ู
ูู ุจุงูุงู
ุชุฏุงุฏ DataKinds. GADTsููุงู ุญุงุฌุฉ ุฅูููุง ุญุชู ูู
ูู ููู
ูุดุฆ ุงูุชุฃุซูุฑ ุนูู ููุน ุงูููู
ุฉ. ูู ุญุงูุชูุง ุ ุณูููุดุฆ Nilุงูู
ููุดุฆ ู
ุชุฌู ุงูุทูู ุจุงูุถุจุท Zeroุ ูุณูููู
ุงูู
ููุดุฆ :|ุจุฅุฑูุงู ุนูุตุฑ ููุน ุจุงูู
ุชุฌู ูู ุงููุณูุทุฉ ุงูุซุงููุฉ aููุฒูุฏ ุงูุญุฌู
ุจู
ูุฏุงุฑ ูุงุญุฏ. ููุญุตูู ุนูู ูุตู ุฃูุซุฑ ุชูุตููุงู ูุตุญุฉ ุ ูู
ููู ูุฑุงุกุฉ ุงูู
ูุงูุฉ ุงูุชู ุฃุดุฑุช ุฅูููุง ุฃุนูุงู ุฃู Haskell Wiki.
ู ุงุฐุง. ูุจุฏู ุฃู ูุฐุง ูู ู ุง ูุญุชุงุฌู. ูุจูู ููุท ูุฏุฎูู ุงูู ุตูููุฉ:
newtype Matrix (m :: Nat) (n :: Nat) a = Matrix (Vector m (Vector n a))
ููุฐุง ุณูุนู ู ุฃูุถูุง:
>>> :t Matrix $ (1 :| Nil) :| Nil
Matrix $ (1 :| Nil) :| Nil :: Num a => Matrix ('Succ 'Zero) ('Succ 'Zero) a
>>> :t Matrix $ (1 :| 2 :| Nil) :| (3 :| 4 :| Nil) :| Nil
Matrix $ (1 :| 2 :| Nil) :| (3 :| 4 :| Nil) :| Nil
:: Num a => Matrix ('Succ ('Succ 'Zero)) ('Succ ('Succ 'Zero)) a
ู ุดุงูู ูุฐุง ุงูููุฌ ุชุธูุฑ ุจุงููุนู ู ู ุฌู ูุน ุงูุดููู ุ ููู ูู ููู ุงูุชุนุงูุด ู ุนูุง ุ ูุณููุงุตู.
, , , , , :
(*|) :: Num a => a -> Matrix m n a -> Matrix m n a
(*|) n = fmap (n *)
-- fmap
--
instance Functor (Matrix m n) where
fmap f (Matrix vs) = Matrix $ fmap f <$> vs
instance Functor (Vector n) where
fmap f (v :| vs) = (f v) :| (fmap f vs)
fmap _ Nil = Nil
, :
>>> :t fmap (+1) (1 :| 2 :| Nil)
fmap (+1) (1 :| 2 :| Nil)
:: Num b => Vector ('Succ ('Succ 'Zero)) b
>>> fmap (+1) (1 :| 2 :| Nil)
2 :| 3 :| Nil
ฮป > :t 5 *| Matrix ((1 :| 2 :| Nil) :| ( 3 :| 4 :| Nil) :| Nil)
5 *| Matrix ((1 :| 2 :| Nil) :| ( 3 :| 4 :| Nil) :| Nil)
:: Num a => Matrix ('Succ ('Succ 'Zero)) ('Succ ('Succ 'Zero)) a
ฮป > 5 *| Matrix ((1 :| 2 :| Nil) :| ( 3 :| 4 :| Nil) :| Nil)
Matrix 5 :| 10 :| Nil :| 15 :| 20 :| Nil :| Nil
:
zipVectorWith :: (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c
zipVectorWith f (l:|ls) (v:|vs) = f l v :| (zipVectorWith f ls vs)
zipVectorWith _ Nil Nil = Nil
(|+|) :: Num a => Matrix m n a -> Matrix m n a -> Matrix m n a
(|+|) (Matrix l) (Matrix r) = Matrix $ zipVectorWith (zipVectorWith (+)) l r
: , , . , :
-- transpose :: [[a]] -> [[a]]
-- transpose [] = []
-- transpose ([] : xss) = transpose xss
-- transpose ((x:xs) : xss) = (x : [h | (h:_) <- xss]) : transpose (xs : [ t | (_:t) <- xss])
transposeMatrix :: Vector m (Vector n a) -> Vector n (Vector m a)
transposeMatrix Nil = Nil
transposeMatrix ((x:|xs):|xss) = (x :| (fmap headVec xss)) :| (transposeMatrix (xs :| fmap tailVec xss))
, GHC ( ).
โข Could not deduce: n ~ 'Zero
from the context: m ~ 'Zero
bound by a pattern with constructor:
Nil :: forall a. Vector 'Zero a,
in an equation for โtransposeMatrixโ
at Text.hs:42:17-19
โnโ is a rigid type variable bound by
the type signature for:
transposeMatrix :: forall (m :: Nat) (n :: Nat) a.
Vector m (Vector n a) -> Vector n (Vector m a)
at Text.hs:41:1-79
Expected type: Vector n (Vector m a)
Actual type: Vector 'Zero (Vector m a)
โข In the expression: Nil
In an equation for โtransposeMatrixโ: transposeMatrix Nil = Nil
โข Relevant bindings include
transposeMatrix :: Vector m (Vector n a) -> Vector n (Vector m a)
(bound at Text.hs:42:1)
|
| transposeMatrix Nil = Nil
|
? , m Zero n Zero.
, , e Nil, Nil' n. n , , n .
, , - , . Haskell , .
- . . ?
- linear
- laop
linear laop. ? , , : , Succ Zero:
Matrix $ (1 :| 2 :| 3 :| 4 :| Nil) :| (5 :| 6 :| 7 :| 8 :| Nil) :| (9 :| 10 :| 11 :| Nil) :| Nil
โข Couldn't match type โ'Zeroโ with โ'Succ 'Zeroโ
Expected type: Vector
('Succ 'Zero) (Vector ('Succ ('Succ ('Succ ('Succ 'Zero)))) a)
Actual type: Vector
('Succ 'Zero) (Vector ('Succ ('Succ ('Succ 'Zero))) a)
โข In the second argument of โ(:|)โ, namely
โ(9 :| 10 :| 11 :| Nil) :| Nilโ
In the second argument of โ(:|)โ, namely
โ(5 :| 6 :| 7 :| 8 :| Nil) :| (9 :| 10 :| 11 :| Nil) :| Nilโ
In the second argument of โ($)โ, namely
โ(1 :| 2 :| 3 :| 4 :| Nil)
:| (5 :| 6 :| 7 :| 8 :| Nil) :| (9 :| 10 :| 11 :| Nil) :| Nilโ
, GHC, - . ?
Template Haskell
TemplateHaskell (TH) โ , -, , . .
v = [1 2 3]
m = [1 2 3; 4 5 6; 7 8 10]
:
matrix := line; line | line
line := unit units
units := unit | ฮต
unit := var | num | inside_brackets
- var โ
- num โ ( )
- inside_brackets โ Haskell
(). Haskell haskell-src-exts haskell-src-meta
matrix :: Parser [[Exp]]
matrix = (line `sepBy` char ';') <* eof
line :: Parser [Exp]
line = spaces >> unit `endBy1` spaces
unit :: Parser Exp
unit = (var <|> num <|> inBrackets) >>= toExpr
Exp โ AST Haskell, , ( AST ).
c , ,
data Matrix (m :: Nat) (n :: Nat) a where
Matrix :: forall m n a. (Int, Int) -> ![[a]] -> Matrix m n a
, AST
expr :: Parser.Parser [[Exp]] -> String -> Q Exp
expr parser source = do -- parser matrix
--
let (matrix, (m, n)) = unwrap $ parse source parser
-- AST
let sizeType = LitT . NumTyLit
-- TypeApplication , ,
let constructor = foldl AppTypeE (ConE 'Matrix) [sizeType m, sizeType n, WildCardT]
let size = TupE $ map (LitE . IntegerL) [m, n]
let value = ListE $ map ListE $ matrix
pure $ foldl AppE constructor [size, value]
parse :: String -> Parser.Parser [[a]] -> Either [String] ([[a]], (Integer, Integer))
parse source parser = do
matrix <- Parser.parse parser "QLinear" source --
size <- checkSize matrix --
pure (matrix, size)
: QuasiQuoter
matrix :: QuasiQuoter
matrix =
QuasiQuoter
{ quoteExp = expr Parser.matrix,
quotePat = notDefined "Pattern",
quoteType = notDefined "Type",
quoteDec = notDefined "Declaration"
}
! :
>>> :set -XTemplateHaskell -XDataKinds -XQuasiQuotes -XTypeApplications
>>> :t [matrix| 1 2; 3 4 |]
[matrix| 1 2; 3 4 |] :: Num _ => Matrix 2 2 _
>>> :t [matrix| 1 2; 3 4 5 |]
<interactive>:1:1: error:
โข Exception when trying to run compile-time code:
All lines must be the same length
CallStack (from HasCallStack):
error, called at src/Internal/Quasi/Quasi.hs:9:19 in qlnr-0.1.2.0-82f1f55c:Internal.Quasi.Quasi
Code: template-haskell-2.15.0.0:Language.Haskell.TH.Quote.quoteExp
matrix " 1 2; 3 4 5 "
โข In the quasi-quotation: [matrix| 1 2; 3 4 5 |]
>>> :t [matrix| (length . show); (+1) |]
[matrix| (length . show); (+1) |] :: Matrix 2 1 (Int -> Int)
TH , c . , hackage
>>> [operator| (x, y) => (y, x) |]
[0,1]
[1,0]
>>> [operator| (x, y) => (2 * x, y + x) |] ~*~ [vector| 3 4 |]
[6]
[7]
Haskell , . ? . , ( ), .
, . : .
ุฑูุงุจุท ู ูุฑุฑุฉ ุฅูู ู ุณุชูุฏุน ู hackage
ุงูุชุนูููุงุช ูุงูุงูุชุฑุงุญุงุช ูุทูุจุงุช ุงูุณุญุจ ู ุฑุญุจ ุจูุง