{"id":308591,"date":"2020-08-17T15:01:22","date_gmt":"2020-08-17T15:01:22","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=308591"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=308591","title":{"rendered":"\u0422\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043d\u0430 Haskell"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/515442\/\">\n<p>\u0422\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u2014 \u0438\u0437\u0432\u0435\u0447\u043d\u0430\u044f \u0442\u0435\u043c\u0430. \u041e \u043d\u0443\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0445 \u0441\u043f\u043e\u0440\u044f\u0442, \u0430 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0441 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u043e\u0432 <a href=\"https:\/\/www.idris-lang.org\/\" rel=\"nofollow\">\u043f\u0438\u0448\u0443\u0442 \u0446\u0435\u043b\u044b\u0435 \u044f\u0437\u044b\u043a\u0438<\/a>. \u041c\u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043d\u0430 Haskell \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u043b \u0431\u044b \u0432\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044f\u043c \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u0415\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438\u043b\u0438 \u043e\u043d\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435_\u043d\u0443\u0436\u043d\u044b? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u0412\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0447\u0435\u0433\u043e-\u0442\u043e (\u0447\u0442\u043e \u043d\u0435\u043f\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c!) \u043d\u0435\u0442 \u2014 \u044d\u0442\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u043c\u0443. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c..<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"ozhidanie\">\u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u043c \u0436\u0435 \u0434\u0435\u043b\u043e\u043c \u0432\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442\u0441\u044f (\u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043c\u043d\u0435) \u0441\u0442\u0430\u0442\u044c\u044f \u043e <a href=\"https:\/\/www.parsonsmatt.org\/2017\/04\/26\/basic_type_level_programming_in_haskell.html\" rel=\"nofollow\">type level haskell<\/a>, \u0433\u0434\u0435, \u0441 \u043f\u043e\u043c\u043e\u0448\u044c\u044e <code>DataKinds<\/code>, <code>GADTs<\/code>, <code>KindSignatures<\/code> (\u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u0433\u0434\u0435 \u0438 \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u2014 \u043d\u0438\u0436\u0435) \u0432\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0438\u043d\u0434\u0443\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430, \u0430 \u0437\u0430 \u043d\u0438\u043c\u0438 \u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u044b, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u0438\u043d\u043e\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">data Nat = Zero | Succ Nat  data Vector (n :: Nat) a where   (:|) :: a -&gt; Vector n a -&gt; Vector ('Succ n) a   Nil :: Vector 'Zero a  infixr 3 :|  <\/code><\/pre>\n<p>  <\/p>\n<p><code>KindSignatures<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e <code>n<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043b\u044e\u0431\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0441 kind&#8217;\u043e\u043c <code>*<\/code> (\u043a\u0430\u043a \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>\u0430<\/code> \u0432 \u044d\u0442\u043e\u043c \u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435), \u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u0438\u043f\u0430 Nat, \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u043c \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0442\u0438\u043f\u043e\u0432. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c <code>DataKinds<\/code>. <code>GADTs<\/code> \u0436\u0435 \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043c\u043e\u0433 \u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u0442\u0438\u043f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>Nil<\/code> \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442 \u0438\u043c\u0435\u043d\u043d\u043e Vector \u0434\u043b\u0438\u043d\u044b <code>Zero<\/code>, \u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>:|<\/code> \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442 \u043a \u0432\u0435\u043a\u0442\u043e\u0440\u0443 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0442\u0438\u043f\u0430 <code>a<\/code> \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u044e, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u0441\u043e\u0441\u043b\u0430\u043b\u0441\u044f \u0432\u044b\u0448\u0435 \u0438\u043b\u0438 Haskell Wiki.<\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u0436\u0435. \u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">newtype Matrix (m :: Nat) (n :: Nat) a = Matrix (Vector m (Vector n a))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u044d\u0442\u043e \u0434\u0430\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">&gt;&gt;&gt; :t Matrix $ (1 :| Nil) :| Nil Matrix $ (1 :| Nil) :| Nil :: Num a =&gt; Matrix ('Succ 'Zero) ('Succ 'Zero) a  &gt;&gt;&gt; :t Matrix $ (1 :| 2 :| Nil) :| (3 :| 4 :| Nil) :| Nil Matrix $ (1 :| 2 :| Nil) :| (3 :| 4 :| Nil) :| Nil   :: Num a =&gt; Matrix ('Succ ('Succ 'Zero)) ('Succ ('Succ 'Zero)) a<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0443\u0436\u0435 \u0441\u0435\u0439\u0447\u0430\u0441 \u043b\u0435\u0437\u0443\u0442 \u0438\u0437\u043e \u0432\u0441\u0435\u0445 \u0449\u0435\u043b\u0435\u0439, \u043d\u043e \u0436\u0438\u0442\u044c \u0441 \u043d\u0438\u043c\u0438 \u043c\u043e\u0436\u043d\u043e, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c. <\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u043a\u0430 \u0441 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u043c\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043e\u043d\u0438 \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u0434\u0430\u0432\u0430\u0439\u0442\u0435, \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u043c\u043d\u043e\u0436\u0438\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043d\u0430 \u0447\u0438\u0441\u043b\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">(*|) :: Num a =&gt; a -&gt; Matrix m n a -&gt; Matrix m n a (*|) n = fmap (n *)  -- \u0423\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043d\u0430 \u0447\u0438\u0441\u043b\u043e \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 fmap -- \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u043e\u0434\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043a\u0430\u043a \u0444\u0443\u043d\u043a\u0442\u043e\u0440  instance Functor (Matrix m n) where   fmap f (Matrix vs) = Matrix $ fmap f &lt;$&gt; vs  instance Functor (Vector n) where   fmap f (v :| vs) = (f v) :| (fmap f vs)   fmap _ Nil = Nil <\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">&gt;&gt;&gt; :t fmap (+1) (1 :| 2 :| Nil) fmap (+1) (1 :| 2 :| Nil)   :: Num b =&gt; Vector ('Succ ('Succ 'Zero)) b  &gt;&gt;&gt; fmap  (+1) (1 :| 2 :| Nil) 2 :| 3 :| Nil  \u03bb &gt; :t 5 *| Matrix ((1 :| 2 :| Nil) :| ( 3 :| 4 :| Nil) :| Nil) 5 *| Matrix ((1 :| 2 :| Nil) :| ( 3 :| 4 :| Nil) :| Nil)   :: Num a =&gt; Matrix ('Succ ('Succ 'Zero)) ('Succ ('Succ 'Zero)) a  \u03bb &gt; 5 *| Matrix ((1 :| 2 :| Nil) :| ( 3 :| 4 :| Nil) :| Nil) Matrix 5 :| 10 :| Nil :| 15 :| 20 :| Nil :| Nil<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421 \u0442\u0435\u043c \u0436\u0435 \u0443\u0441\u043f\u0435\u0445\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u0440\u0438\u0446:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">zipVectorWith :: (a -&gt; b -&gt; c) -&gt; Vector n a -&gt; Vector n b -&gt; Vector n c zipVectorWith f (l:|ls) (v:|vs) = f l v :| (zipVectorWith f ls vs) zipVectorWith _ Nil Nil = Nil  (|+|) :: Num a =&gt; Matrix m n a -&gt; Matrix m n a -&gt; Matrix m n a (|+|) (Matrix l) (Matrix r) = Matrix $ zipVectorWith (zipVectorWith (+)) l r<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043e\u0432\u0435\u0440\u0442\u0430\u0439\u043f\u0438\u043d\u0433\u0430 \u0432\u044b\u043b\u0435\u0437\u0443\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e: \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0430\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0438\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c, \u0432\u0437\u044f\u0432 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0442\u0440\u0430\u043d\u043f\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\"> -- transpose               :: [[a]] -&gt; [[a]] -- transpose []             = [] -- transpose ([]   : xss)   = transpose xss -- transpose ((x:xs) : xss) = (x : [h | (h:_) &lt;- xss]) : transpose (xs : [ t | (_:t) &lt;- xss])  transposeMatrix :: Vector m (Vector n a) -&gt; Vector n (Vector m a) transposeMatrix Nil = Nil transposeMatrix ((x:|xs):|xss) = (x :| (fmap headVec xss)) :| (transposeMatrix (xs :| fmap tailVec xss)) <\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043b\u0438\u0447\u043d\u043e, \u043d\u043e GHC \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0438\u043d\u0430\u0447\u0435 (\u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e).<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">    \u2022 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 \u2018transposeMatrix\u2019         at Text.hs:42:17-19       \u2018n\u2019 is a rigid type variable bound by         the type signature for:           transposeMatrix :: forall (m :: Nat) (n :: Nat) a.                              Vector m (Vector n a) -&gt; 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)     \u2022 In the expression: Nil       In an equation for \u2018transposeMatrix\u2019: transposeMatrix Nil = Nil     \u2022 Relevant bindings include         transposeMatrix :: Vector m (Vector n a) -&gt; Vector n (Vector m a)           (bound at Text.hs:42:1)    |    | transposeMatrix Nil = Nil    |<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442? \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u0437 \u043f\u043e\u0441\u044b\u043b\u043a\u0438 <strong>m \u0435\u0441\u0442\u044c Zero<\/strong> \u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 <strong>n \u0435\u0441\u0442\u044c Zero<\/strong>.<br \/>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043c\u044b \u0431\u044b \u043c\u043e\u0433\u043b\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043de <code>Nil<\/code>, \u0430 \u0432\u0435\u043a\u0442\u043e\u0440 <code>Nil<\/code>&#8216;\u043e\u0432 \u0434\u043b\u0438\u043d\u044b <code>n<\/code>. \u0422\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0442\u0438\u043f <code>n<\/code> \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0447\u0435\u0433\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u043c, \u0432\u0435\u0434\u044c <code>n<\/code> \u0441\u043e\u0442\u0440\u0451\u0442\u0441\u044f \u0438 \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.<\/p>\n<p>  <\/p>\n<h1 id=\"realnost\">\u0420\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h1>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u0436, \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0440\u0430\u0437\u0432\u0430\u043b\u0438\u043b\u0430\u0441\u044c, \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0447\u0442\u043e-\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0435, \u0447\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043d\u0430 \u0442\u0438\u043f\u0430\u0445. \u0412 Haskell \u043d\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0442 \u0441\u043b\u043e\u0432\u0430 \u0441\u043e\u0432\u0441\u0435\u043c.<\/p>\n<p>  <\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u044f\u043d\u043a\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e-\u0441\u0442\u0430\u0440\u0438\u043d\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043f\u0438\u0441\u043a\u043e\u0432. \u041d\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u0435\u0433\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u043c? <\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435 \u0432 Haskell \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0434\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f: <a href=\"https:\/\/hackage.haskell.org\/package\/linear\" rel=\"nofollow\">linear<\/a> \u0438 <a href=\"https:\/\/github.com\/bolt12\/laop\" rel=\"nofollow\">laop<\/a>, \u0438 \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>linear \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439<\/li>\n<li>laop \u0441\u0442\u0440\u043e\u0438\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0438\u0437 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0443\u043f\u0430\u0441\u0442\u044c \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 <\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c linear \u0438 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c laop. \u041d\u043e \u043a\u0430\u043a? \u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u043c \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u0438, \u043d\u043e \u043e\u043d \u0438\u043c\u0435\u0435\u0442 \u043e\u0434\u043d\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u0435\u043b\u0438\u043a\u0430\u0442\u043d\u043e \u0443\u043c\u0430\u043b\u0447\u0438\u0432\u0430\u043b\u043e\u0441\u044c: \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u043a\u0442\u043e\u0440\u044b \u0447\u0440\u0435\u0437\u043c\u0435\u0440\u043d\u043e \u0432\u0435\u0440\u0431\u043e\u0437\u043d\u043e, \u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u043e\u0431\u0435\u0449\u0430\u044e\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u043a\u0443\u0447\u0438 Succ \u0438 Zero:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">Matrix $ (1 :| 2 :| 3 :| 4 :| Nil) :| (5 :| 6 :| 7 :| 8 :| Nil) :| (9 :| 10 :| 11 :| Nil) :| Nil<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"haskell\">    \u2022 Couldn't match type \u2018'Zero\u2019 with \u2018'Succ 'Zero\u2019       Expected type: Vector                        ('Succ 'Zero) (Vector ('Succ ('Succ ('Succ ('Succ 'Zero)))) a)         Actual type: Vector                        ('Succ 'Zero) (Vector ('Succ ('Succ ('Succ 'Zero))) a)     \u2022 In the second argument of \u2018(:|)\u2019, namely         \u2018(9 :| 10 :| 11 :| Nil) :| Nil\u2019       In the second argument of \u2018(:|)\u2019, namely         \u2018(5 :| 6 :| 7 :| 8 :| Nil) :| (9 :| 10 :| 11 :| Nil) :| Nil\u2019       In the second argument of \u2018($)\u2019, namely         \u2018(1 :| 2 :| 3 :| 4 :| Nil)            :| (5 :| 6 :| 7 :| 8 :| Nil) :| (9 :| 10 :| 11 :| Nil) :| Nil\u2019<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0438 \u0448\u0438\u0440\u0438\u043d\u044b \u043c\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u043c\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c GHC, \u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0447\u0442\u043e-\u0442\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u0440\u0443\u0433\u043e\u0435. \u041d\u043e \u0447\u0442\u043e?<\/p>\n<p>  <\/p>\n<h2 id=\"template-haskell\">Template Haskell<\/h2>\n<p>  <\/p>\n<p>TemplateHaskell (TH) \u2014 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u044f\u0437\u044b\u043a\u0430, \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u0442\u0430-\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430. \u0412\u0435\u0441\u044c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0434\u0430\u043b\u0435\u0435 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>  <\/p>\n<p>\u0417\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 \u0431\u044b\u043b \u0432\u0437\u044f\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/learn_matlab\/matrices-and-arrays.html\" rel=\"nofollow\">matlab<\/a>: <\/p>\n<p>  <\/p>\n<pre><code class=\"matlab\">v = [1 2 3] m = [1 2 3; 4 5 6; 7 8 10]<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0444\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">matrix := line; line | line line := unit units units := unit | \u03b5 unit := var | num | inside_brackets<\/code><\/pre>\n<p>  <\/p>\n<p>\u0413\u0434\u0435 <\/p>\n<p>  <\/p>\n<ul>\n<li>var \u2014 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0438\u043b\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0431\u0435\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/li>\n<li>num \u2014 \u0447\u0438\u0441\u043b\u043e (\u0446\u0435\u043b\u043e\u0435 \u0438\u043b\u0438 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439)<\/li>\n<li>inside_brackets \u2014 \u043b\u044e\u0431\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Haskell \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043a\u043e\u0431\u043e\u043a <code>(<\/code> <code>)<\/code>. \u0414\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 Haskell \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 haskell-src-exts \u0438 haskell-src-meta<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0418 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0430\u0440\u0441\u0435\u0440 (\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u043d\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u0430\u0445!). \u041f\u043e\u043b\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/JuniorGarbageCollector\/QLinear\/blob\/master\/src\/Internal\/Quasi\/Matrix\/Parser.hs\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">matrix :: Parser [[Exp]] matrix = (line `sepBy` char ';') &lt;* eof  line :: Parser [Exp] line = spaces &gt;&gt; unit `endBy1` spaces  unit :: Parser Exp unit = (var &lt;|&gt; num &lt;|&gt; inBrackets) &gt;&gt;= toExpr<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0443\u0442 Exp \u2014 \u043a\u0443\u0441\u043e\u043a AST \u044f\u0437\u044b\u043a\u0430 Haskell, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u043e\u0432 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u043e\u0440\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443, \u0430 \u0441\u0430\u043c\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u0432 \u0442\u0438\u043f \u043c\u0430\u0442\u0440\u0438\u0446\u044b (\u0412 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043b\u0438\u0448\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043d\u0430\u0448\u0435 AST \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439).<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u0432\u0435\u0434\u0451\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 c \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u0440\u044b\u0442 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439,<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">data Matrix (m :: Nat) (n :: Nat) a where   Matrix :: forall m n a. (Int, Int) -&gt; ![[a]] -&gt; Matrix m n a<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442 \u043d\u0430\u043c AST \u0441 \u0433\u043e\u0442\u043e\u0432\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">expr :: Parser.Parser [[Exp]] -&gt; String -&gt; Q Exp expr parser source = do -- parser \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 matrix \u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043d\u0438\u043f\u043f\u0435\u0442\u0430   -- \u043f\u0430\u0440\u0441\u0438\u043c \u0441\u044b\u0440\u043e\u0439 \u0442\u0435\u043a\u0441\u0442 \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0438   let (matrix, (m, n)) = unwrap $ parse source parser    -- \u0441\u0442\u0440\u043e\u0438\u043c AST   let sizeType = LitT . NumTyLit   -- \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c TypeApplication \u0434\u043b\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0442\u0438\u043f\u043e\u0432, \u0430 \u0442\u0438\u043f \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0445\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0443\u043f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443   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 -&gt; Parser.Parser [[a]] -&gt; Either [String] ([[a]], (Integer, Integer)) parse source parser = do   matrix &lt;- Parser.parse parser &quot;QLinear&quot; source -- \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0441\u044b\u0440\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430   size &lt;- checkSize matrix -- \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439   pure (matrix, size)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0432\u0441\u0435\u0433\u043e \u043d\u0438\u0447\u0435\u0433\u043e: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438\u0437 \u0433\u043e\u0442\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u0434\u0430 QuasiQuoter<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">matrix :: QuasiQuoter matrix =   QuasiQuoter     { quoteExp = expr Parser.matrix,       quotePat = notDefined &quot;Pattern&quot;,       quoteType = notDefined &quot;Type&quot;,       quoteDec = notDefined &quot;Declaration&quot;     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f! \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">&gt;&gt;&gt; :set -XTemplateHaskell -XDataKinds -XQuasiQuotes -XTypeApplications &gt;&gt;&gt; :t [matrix| 1 2; 3 4 |] [matrix| 1 2; 3 4 |] :: Num _ =&gt; Matrix 2 2 _  &gt;&gt;&gt; :t [matrix| 1 2; 3 4 5 |] &lt;interactive&gt;:1:1: error:     \u2022 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 &quot; 1 2; 3 4 5 &quot;     \u2022 In the quasi-quotation: [matrix| 1 2; 3 4 5 |]  &gt;&gt;&gt; :t [matrix| (length . show); (+1) |] [matrix| (length . show); (+1) |] :: Matrix 2 1 (Int -&gt; Int)<\/code><\/pre>\n<p>  <\/p>\n<p>TH \u0434\u0430\u043b \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u044f\u0437\u044b\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043e\u0434\u043d\u0438\u0445 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u0445 \u044f \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u043a\u0440\u043ec \u0434\u043b\u044f \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432. \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043d\u0438\u0436\u0435, \u0430 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c \u043d\u0430 <a href=\"https:\/\/github.com\/JuniorGarbageCollector\/QLinear\" rel=\"nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a> \u0438 <a href=\"https:\/\/hackage.haskell.org\/package\/qlinear\" rel=\"nofollow\">\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043d\u0430 hackage<\/a><\/p>\n<p>  <\/p>\n<pre><code class=\"haskell\">&gt;&gt;&gt; [operator| (x, y) =&gt; (y, x) |] [0,1] [1,0] &gt;&gt;&gt; [operator| (x, y) =&gt; (2 * x, y + x) |] ~*~ [vector| 3 4 |] [6] [7]<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"vyvody\">\u0412\u044b\u0432\u043e\u0434\u044b<\/h1>\n<p>  <\/p>\n<p>\u0420\u0430\u0431\u043e\u0447\u0438\u0435 \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043d\u0430 Haskell \u043d\u0435 \u0442\u043e, \u0447\u0442\u043e \u0431\u044b \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u043b\u0438 \u043c\u043e\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c\u0438? \u0421\u043a\u043e\u0440\u0435\u0435 \u043d\u0435\u0442. \u041e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0438 \u0433\u0438\u0431\u043a\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043e\u0442\u0434\u0430\u043d\u0430 \u043d\u0430 \u043e\u0442\u043a\u0443\u043f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 (\u0442\u043e \u0435\u0441\u0442\u044c \u043c\u043d\u0435), \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442. <\/p>\n<p>  <\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e \u044d\u0442\u043e \u0438\u043b\u0438 \u043f\u043b\u043e\u0445\u043e, \u043d\u043e \u0432 \u0437\u043e\u043e\u043f\u0430\u0440\u043a\u0435 \u043c\u0430\u0442\u0440\u0438\u0446 \u043d\u0430 \u0445\u0430\u0441\u043a\u0435\u043b\u0435 \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u0410 \u0432\u044b \u0440\u0435\u0448\u0430\u0439\u0442\u0435: \u043d\u0443\u0436\u043d\u044b \u0438\u043b\u0438 \u043d\u0438\u043d\u0443\u0436\u043d\u044b.<\/p>\n<p>  <\/p>\n<p>\u0414\u0443\u0431\u043b\u0438\u0440\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/JuniorGarbageCollector\/QLinear\" rel=\"nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a> \u0438 <a href=\"https:\/\/hackage.haskell.org\/package\/qlinear\" rel=\"nofollow\">hackage<\/a><\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f, \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0443\u043b\u043b\u0440\u0435\u043a\u0432\u0435\u0441\u0442\u044b, \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/515442\/\"> https:\/\/habr.com\/ru\/post\/515442\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/515442\/\">\n<p>\u0422\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u2014 \u0438\u0437\u0432\u0435\u0447\u043d\u0430\u044f \u0442\u0435\u043c\u0430. \u041e \u043d\u0443\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0445 \u0441\u043f\u043e\u0440\u044f\u0442, \u0430 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0441 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u043e\u0432 <a href=\"https:\/\/www.idris-lang.org\/\" rel=\"nofollow\">\u043f\u0438\u0448\u0443\u0442 \u0446\u0435\u043b\u044b\u0435 \u044f\u0437\u044b\u043a\u0438<\/a>. \u041c\u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043d\u0430 Haskell \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u043b \u0431\u044b \u0432\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044f\u043c \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u0415\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438\u043b\u0438 \u043e\u043d\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435_\u043d\u0443\u0436\u043d\u044b? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u0412\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0447\u0435\u0433\u043e-\u0442\u043e (\u0447\u0442\u043e \u043d\u0435\u043f\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c!) \u043d\u0435\u0442 \u2014 \u044d\u0442\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u043c\u0443. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-308591","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=308591"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308591\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=308591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=308591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=308591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}