Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core rewrite #21

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3a3bcf1
Add Node and Port types with related functions and data
KubEF Sep 13, 2024
5ae3549
Add Reducer
KubEF Sep 13, 2024
03490a2
pin fourmolu version in CI
KubEF Sep 13, 2024
f22318b
Delete Example.Project and add simpliest test for Core.Node
KubEF Sep 14, 2024
142fb67
Delete usless comments and complement useful ones
KubEF Sep 15, 2024
076670f
Add loader
KubEF Sep 15, 2024
e8c6d4e
Fix fourmolu to process $ correctly
KubEF Sep 15, 2024
396c345
Add lenses and make some style changes
KubEF Sep 15, 2024
f20525b
Add tests for isActive and isPortToLoad functions
KubEF Sep 16, 2024
47cdb69
Replace elemIndex to elem in markAllInnerEdges
KubEF Sep 17, 2024
c42efc4
Add tests for selectAddresToLoad function
KubEF Sep 17, 2024
957dd8b
Replace repeat to def in handle function
KubEF Sep 17, 2024
69b3439
Add tests for markAllInnerEdges function
KubEF Sep 17, 2024
77a1365
[WIP]Add layout of memory manager
KubEF Sep 18, 2024
beb03b7
Redesign Address and add LocalNode
KubEF Oct 1, 2024
cb4f812
Add updateRam function
KubEF Oct 1, 2024
9204022
Add IdOfPort
KubEF Oct 21, 2024
28948dc
Add more types and getter port function
KubEF Oct 28, 2024
fcc12c1
Add memory manager
KubEF Oct 28, 2024
ee84822
Fully redesign reducer
KubEF Oct 28, 2024
dfd0921
Comment irrelevant (all) tests
KubEF Oct 28, 2024
503c9a1
Change default json formatter
KubEF Oct 28, 2024
6c44b6a
Add simple vector-based key-value storage
KubEF Nov 7, 2024
8c684c2
Replace Address to AddressNum in Loader and unify LoadedNode with Loc…
KubEF Nov 7, 2024
f1d32ac
Rework memory manager update to minimize usage of RAM
KubEF Nov 7, 2024
833d147
Rename Map functions
KubEF Nov 8, 2024
9e0c858
Merge foldls
KubEF Nov 8, 2024
98f2046
Delete updateRam
KubEF Nov 15, 2024
d4f2574
Refactor and some doc changes
KubEF Nov 15, 2024
6db019e
Minor refactoring
KubEF Nov 15, 2024
27f82a3
Simplified reducer
KubEF Nov 15, 2024
52030f6
Add handmade agents
KubEF Nov 15, 2024
1918777
Write concrete handmade reduction rule
KubEF Nov 15, 2024
fa9b564
Add some doctests in memory manager
KubEF Nov 18, 2024
4afd346
Weakened Index to Unsigned constraint
KubEF Nov 18, 2024
fa5f3be
Update simple lambda reduction rule
KubEF Nov 18, 2024
ed75dd2
Rewrite indexToUnsigned via resize and bitCoerce
KubEF Nov 18, 2024
c8d17af
Add skipping svg files in pre-commit
KubEF Nov 19, 2024
2564570
Fix doctests
KubEF Nov 25, 2024
9fce015
Rename agent constructors
KubEF Nov 28, 2024
3547c90
Rework Port and Node, add Connection type
KubEF Dec 8, 2024
3893e18
Split MemoryManager functionality
KubEF Dec 8, 2024
3262a04
Replace Edge, ActivePair and Delta type
KubEF Dec 8, 2024
7ee225d
Replace types in MemoryManager modules
KubEF Dec 8, 2024
be705c9
Rewrite addresses allocated to Vec of Maybe
KubEF Dec 8, 2024
164643d
Add giveActiveAddressNumber function
KubEF Dec 9, 2024
43c3e6f
Fix the case when necessary amount of memory is zero
KubEF Dec 9, 2024
01db43e
Add necessary loading functions
KubEF Dec 9, 2024
9e46b32
Rewrite Reducer
KubEF Dec 9, 2024
b685955
[WIP] Add core function
KubEF Dec 9, 2024
2ca782d
Fix pedantic build
KubEF Dec 9, 2024
1240556
Add specific reduce rules for simple lambdas
KubEF Dec 9, 2024
63aa4ef
Add example of initial net
KubEF Dec 9, 2024
462d8d5
Add functions to export list for doctests
KubEF Dec 9, 2024
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"editor.rulers": [
120
]
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
}
}
1 change: 1 addition & 0 deletions fourmolu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ reexports:
- "module Relude exports Relude.Print"
- "module Relude exports Relude.String"
- "module Relude.String exports Relude.String.Reexport"
- "module Clash.Prelude exports Clash.HaskellPrelude"
11 changes: 9 additions & 2 deletions lamagraph-core/lamagraph-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ common common-options

-- clash-prelude will set suitable version bounds for the plugins
clash-prelude >= 1.8.1 && < 1.10,
lens,
ghc-typelits-natnormalise,
ghc-typelits-extra,
ghc-typelits-knownnat
Expand All @@ -85,7 +86,12 @@ library
import: common-options
hs-source-dirs: src
exposed-modules:
Example.Project
Core.Node
Core.Reducer
Core.MemoryManager
Core.Map
Core.Concrete.ReduceRulesLambda
INet.Net
default-language: Haskell2010

-- Builds the executable 'clash', with lamagraph-core project in scope
Expand Down Expand Up @@ -123,7 +129,8 @@ test-suite test-library
ghc-options: -threaded
main-is: unittests.hs
other-modules:
Tests.Example.Project
Tests.Core.Node
NodeGenerate
build-depends:
lamagraph-core,
QuickCheck,
Expand Down
40 changes: 40 additions & 0 deletions lamagraph-core/src/Core/Concrete/ReduceRulesLambda.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{- | All modules from Concrete are
1a. Compiled from some dsl in separate file
1b. Compiled from embedded dsl in INet module
2. Written by hands for tests
-}
module Core.Concrete.ReduceRulesLambda where
Copy link
Member

Choose a reason for hiding this comment

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

Not Concrete, but Example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My idea that in common case this module will generate automatically by Template Haskell and some dsl. It is just example for now, but something more complex in the future


import Clash.Prelude
import Control.Lens
import Core.MemoryManager
import Core.Node
import Core.Reducer
import INet.Net

(|><|) ::
-- forall portsNumber nodesNumber edgesNumber.
-- (KnownNat portsNumber, KnownNat nodesNumber, KnownNat edgesNumber, edgesNumber ~ portsNumber) =>
-- Node portsNumber ->
-- Node portsNumber ->
-- ReduceRuleResult nodesNumber edgesNumber portsNumber
Node 2 ->
Node 2 ->
ReduceRuleResult 0 2 2
lNode |><| rNode = case (lNode ^. nodeType, rNode ^. nodeType) of
(Apply, Abs) -> applyToLambdaRule lNode rNode
(Abs, Apply) -> applyToLambdaRule lNode rNode
Copy link
Member

Choose a reason for hiding this comment

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

Should it be applyToLambdaRule rNode lNode?

_ -> error "There is no rule for this active pair in the reduction rules"
where
applyToLambdaRule n1 n2 =
let arisingNodes = def
portToEdgeEnd p = case p ^. nodeAddress of
Nothing -> error "Port must to be connected"
Just addr -> case addr of
ActualAddress addrNum -> EdgeEnd addrNum (p ^. portConnectedToId)
LocalAddress addrNum -> EdgeEnd addrNum (p ^. portConnectedToId) -- Maybe this is should be more complicated
portsToEdgeEnds node = map (maybe (error "All Ports must to be presented") portToEdgeEnd) (node ^. secondaryPorts)
lE = portsToEdgeEnds n1
rE = portsToEdgeEnds n2
arisingEdges = zipWith (\l r -> Just $ Edge l r) lE (reverse rE)
in ReduceRuleResult arisingEdges arisingNodes
21 changes: 21 additions & 0 deletions lamagraph-core/src/Core/Loader.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Core.Loader where

import Clash.Prelude
import Core.Node

-- | Get `Node` by his `AddressNumber` from RAM. Actually, preparing to reducer work.
loader ::
( KnownDomain dom
, HiddenClockResetEnable dom
, KnownNat numberOfPorts
) =>
(Signal dom AddressNumber -> Signal dom (Node numberOfPorts)) ->
Signal dom (Maybe AddressNumber) ->
Signal dom (Maybe (LoadedNode numberOfPorts))
loader ram mbAddressNumberToLoad =
mkLoadedNode <$> mbNode <*> mbAddressNumberToLoad
where
mkLoadedNode node address = LoadedNode <$> node <*> address
mbNode = case sequenceA mbAddressNumberToLoad of
Nothing -> pure Nothing
Just n -> sequenceA $ Just (ram n)
70 changes: 70 additions & 0 deletions lamagraph-core/src/Core/Map.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Eta reduce" #-}

module Core.Map (Map, find, insertWith, insert) where

import Clash.Prelude
import Core.Node

{- $setup
>>> import Clash.Prelude
>>> import Core.Node
-}

{- | key-value store based on `Vec`. Key is `AddressNumber`, it has linear time to find or change element.
But iterate (such as map or fold) by value and key is easy.
It assumed that `size` is small
-}
type Map (size :: Nat) v = (Vec size (Maybe (AddressNumber, Maybe v)))

find ::
Copy link
Member

Choose a reason for hiding this comment

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

Can it be done using fold? It should be more efficient. I hope.

(KnownNat size) =>
Map size v ->
AddressNumber ->
Maybe v
find dict key = case dict of
Nil -> Nothing
(Just (k, v) `Cons` t) -> if k == key then v else find t key
(Nothing `Cons` t) -> find t key

{- | Update or insert (by applying function to `Nothing`) value by the key.

TODO: fix @id@ case. If @func@ is isomorphic to @id@ then @insertWith@ works incorrect
-}
insertWith ::
(KnownNat size, Eq v) =>
Map size v ->
(Maybe v -> Maybe v) ->
AddressNumber ->
Map size v
insertWith dict func key = if dict /= updated then updated else inserted
where
updated = update key func dict
inserted = insert key (func Nothing) dict

-- | Try to update (by applying the function) value by key. It does nothing if there is no value by the key in the `Map`
update ::
AddressNumber ->
(Maybe v -> Maybe v) ->
Map size v ->
Map size v
update key func dict = map (fmap updateIfKeysEquals) dict
where
updateIfKeysEquals (k, v) = if k == key then (k, func v) else (k, v)

{- | Insert key-value pair in the free space

>>> insert 1 (Just 1) (def :: Map 2 AddressNumber)
Just (1,Just 1) :> Nothing :> Nil
-}
insert ::
AddressNumber ->
Maybe v ->
Map size v ->
Map size v
insert key value dict = case dict of
h `Cons` t -> case h of
Nothing -> Just (key, value) `Cons` t
Just _ -> h `Cons` insert key value t
Nil -> error "All addresses are written"
Loading