Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ml.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source-repository head

library
exposed-modules:
MLambda.Differentiable
MLambda.Index
MLambda.Linear
MLambda.Matrix
Expand Down Expand Up @@ -55,6 +56,7 @@ library
, singletons-base
, template-haskell
, vector
, vinyl
default-language: GHC2024

test-suite ml-test
Expand Down Expand Up @@ -94,6 +96,7 @@ test-suite ml-test
, tasty-hunit
, template-haskell
, vector
, vinyl
default-language: GHC2024

benchmark ml-bench
Expand Down Expand Up @@ -131,4 +134,5 @@ benchmark ml-bench
, tasty-bench
, template-haskell
, vector
, vinyl
default-language: GHC2024
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:
- singletons
- singletons-base
- primitive
- vinyl
# - ghc-typelits-natnormalise

ghc-options:
Expand Down
63 changes: 63 additions & 0 deletions src/MLambda/Differentiable.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- |
-- Module : MLambda.Differentiable
-- Description : Implements differentiation machinery.
-- Copyright : (c) neclitoris, 2026
-- License : BSD-3-Clause
-- Maintainer : nas140301@gmail.com
-- Stability : experimental
-- Portability : portable
--
-- This module contains definition of `Differentiable` type class.
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE PatternSynonyms #-}

Check warning on line 12 in src/MLambda/Differentiable.hs

View workflow job for this annotation

GitHub Actions / lint

Warning in module MLambda.Differentiable: Unused LANGUAGE pragma ▫︎ Found: "{-# LANGUAGE PatternSynonyms #-}"
module MLambda.Differentiable
( Functional(..)
, Differentiable(..)
, Matmul(..)
) where

import MLambda.Index
import MLambda.Matrix
import MLambda.NDArr
import MLambda.TypeLits

import Data.Kind
import Data.List.Singletons
import Data.Vinyl
import Numeric.Netlib.Class

import Prelude hiding (Floating)


type family ArgsL (i :: [[Natural]]) e :: [Type] where
ArgsL '[] e = '[]
ArgsL (x ': xs) e = NDArr x e : ArgsL xs e

type family Args (i :: [[Natural]]) e :: Type where

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут бтв тоже просто type вместо type family можно

Args i e = Rec At (Fins @(ArgsL i e) (ArgsL i e))

type family Fun (i :: [[Natural]]) (o :: [Natural]) e :: Type where

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно просто type Fun i o e = ... вместо тайпфэмили

Fun i o e = Args i e -> NDArr o e

class Functional f i o e where
($$) :: f -> Fun i o e

class Functional f i o e => Differentiable f i o e where
d :: f -> Args i e -> Index o -> Args i e
Comment thread
TurtlePU marked this conversation as resolved.

data Matmul = Matmul

instance (KnownNat m, KnownNat n, KnownNat k, Floating e) => Functional Matmul '[[m,n], [n,k]] '[m,k] e where
_ $$ (At a :& At b :& RNil) = a `cross` b

instance (1 <= m, 1 <= n, 1 <= k, KnownNat m, KnownNat n, KnownNat k, Floating e) => Differentiable Matmul '[[m,n], [n,k]] '[m,k] e where
d _ (At a :& At b :& RNil) (i :. j) = At a' :& At b' :& RNil
where
a' = fromIndex \(k :. l) -> if k == i then b `at` (l :. j) else 0
b' = fromIndex \(k :. l) -> if l == j then a `at` (i :. k) else 0

data (:.:) f1 f2 = f1 :.: f2

instance (Functional f1 i1 o1 e, Functional f2 (o1 : i2) o2 e, i ~ i1 ++ i2) => Functional (f1 :.: f2) i o2 e where

Check warning on line 61 in src/MLambda/Differentiable.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12.2 on ubuntu-latest

• Redundant constraints: (Functional f1 i1 o1 e,

@TurtlePU TurtlePU Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему не просто

(Functional f i j e, Functional g j k e) => Functional (f :.: g) i k e

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В текущей постановке тебе нужно как-то прокинуть в этот инстанс свидетель разделения i ~ i1 ++ i2, по которому в рантайме рекорд можно будет разбить на две части

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потому что кайнды не сходятся. В конце концов, хочется иметь отображение из многих тензоров в один (таковым является, например, матмул), а тут j одновременно и как параметр, и как результат.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тогда, видимо, в качестве первого шага в композиции нужно класть список "функций", а не одну функцию. Либо сорить вспомогательной штукой которая переставляет входные аргументы

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Либо делать так, чтобы "функция" могла возвращать много тензоров, а не один, но это без гетерогенных индексов не выражается

(f1 :.: f2) $$ r = undefined

Check warning on line 62 in src/MLambda/Differentiable.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12.2 on ubuntu-latest

Defined but not used: ‘r’

Check warning on line 62 in src/MLambda/Differentiable.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12.2 on ubuntu-latest

Defined but not used: ‘f2’

Check warning on line 62 in src/MLambda/Differentiable.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12.2 on ubuntu-latest

Defined but not used: ‘f1’

32 changes: 32 additions & 0 deletions src/MLambda/TypeLits.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE RequiredTypeArguments #-}
{-# LANGUAGE StandaloneKindSignatures #-}

-- |
-- Module : MLambda.TypeLits
Expand All @@ -22,11 +23,16 @@ module MLambda.TypeLits
, Peano
, RNat (..)
, RPNat (..)
, Fin (..)
, Fins
, At(..)
, type (!)
, ReifiedNat
, rnat
, rpnat
) where

import Data.Kind
import Data.Proxy (Proxy (Proxy))
import GHC.TypeError (ErrorMessage (..), TypeError)
import GHC.TypeNats hiding (natVal)
Expand Down Expand Up @@ -67,6 +73,32 @@ data RPNat n where
RPZ :: RPNat PZ
RPS :: RPNat n -> RPNat (PS n)

-- | Finite list index.
type Fin :: [k] -> Type
data Fin (l :: [k :: Type]) where
FZ :: Fin (x : xs)
FS :: Fin xs -> Fin (x : xs)

type (!) :: forall l -> Fin l -> Type
type family (!) (l :: [k :: Type]) (i :: Fin l) where
(x ': _) ! FZ = x
(_ ': xs) ! (FS i) = xs ! i

type At :: forall {k} {l :: [k]} . Fin l -> Type
data At (i :: Fin l) where
At :: l ! i -> At i

type Map :: forall k l . (k -> l) -> [k] -> [l]
type family Map f l where
Map _ '[] = '[]
Map f (x ': xs) = f x ': Map f xs

-- | Creates a list of indices into a type-level list.
type Fins :: forall {k} (l :: [k]) . [k] -> [Fin l]

@TurtlePU TurtlePU Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, тут лучше

type Fins :: forall {k}. forall (l :: [k]) -> [Fin l]

если такое компилируется

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Или просто forall l -> [Fin l], как в сигнатуре для (!)

type family Fins (l :: [k :: Type]) where
Fins '[] = '[]
Fins (x ': xs) = FZ ': Map FS (Fins xs)

-- | A stronger variant of 'KnownNat' which enables induction on type-level naturals.
class ReifiedNat n where
rnat0 :: RNat n
Expand Down
Loading