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

Add initial support for pre-defined rules #9

Merged
merged 26 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9f4c9ac
Fix bytes token rule in grammar
aabounegm Dec 25, 2023
c3d3cb8
Add an explicit export list
aabounegm Dec 25, 2023
e20b57a
Add a draft implementation for the first rule
aabounegm Dec 25, 2023
329d782
Fix the implementation of the first rules
aabounegm Dec 28, 2023
70d57e4
Stop normalizing objects that are values of Nu
aabounegm Dec 28, 2023
802360a
Treat program as a formation when normalizing
aabounegm Dec 28, 2023
ac64050
Update tests
aabounegm Dec 28, 2023
59d4e42
Fix the hex representation of data bytes
aabounegm Dec 28, 2023
b296cdd
Change the test Yaml format to an array of tests
aabounegm Dec 28, 2023
5511306
Merge branch 'master' into rule-1
aabounegm Jan 10, 2024
c4f6a16
Update syntax in phi unit tests
aabounegm Jan 10, 2024
17f46ee
Revert "Fix bytes token rule in grammar"
deemp Jan 10, 2024
03ce7c1
Merge branch '21-fix-grammar' into rule-1
deemp Jan 10, 2024
fdc66c9
feat: explain \nu
deemp Jan 10, 2024
1b05862
Merge branch 'master' into rule-1
aabounegm Jan 11, 2024
13f019b
Make test groups objects, not arrays
fizruk Jan 11, 2024
ece7fbd
Update Syntax.cf
fizruk Jan 11, 2024
cb9d91b
Implement applyRules
fizruk Jan 11, 2024
c5d45f5
Fix imports/exports for the build
fizruk Jan 11, 2024
c5bf9c5
add: string-interpolate package
deemp Jan 12, 2024
2cac3f2
refactor: move rules from phi-paper
deemp Jan 12, 2024
06cd141
upd: tests
deemp Jan 12, 2024
655019e
upd: cabal file
deemp Jan 12, 2024
fcd7bb9
refactor: code to run rules unit tests
deemp Jan 12, 2024
8b0ab4e
fix: rm redundant input
deemp Jan 12, 2024
2494f11
fix: add -Werror flag to match CI
deemp Jan 12, 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 eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ library
, base >=4.7 && <5
, directory
, filepath
, mtl
, yaml
default-language: Haskell2010

Expand All @@ -76,6 +77,7 @@ executable normalize-phi
, directory
, eo-phi-normalizer
, filepath
, mtl
, yaml
default-language: Haskell2010

Expand Down Expand Up @@ -103,5 +105,6 @@ test-suite eo-phi-normalizer-test
, filepath
, hspec
, hspec-discover
, mtl
, yaml
default-language: Haskell2010
2 changes: 1 addition & 1 deletion eo-phi-normalizer/grammar/EO/Phi/Syntax.cf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ separator Binding "," ;
Phi. Attribute ::= "φ" ; -- decoratee object
Rho. Attribute ::= "ρ" ; -- parent object
Sigma. Attribute ::= "σ" ; -- home object
VTX. Attribute ::= "ν" ; -- ???
VTX. Attribute ::= "ν" ; -- an object that represents the unique identifier of the containing object
Label. Attribute ::= LabelId ;
Alpha. Attribute ::= AlphaIndex ;

Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
- directory
- filepath
- yaml
- mtl

ghc-options:
- -Wall
Expand Down
73 changes: 61 additions & 12 deletions eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}

module Language.EO.Phi.Normalize where
module Language.EO.Phi.Normalize (
normalizeObject,
normalize,
peelObject,
unpeelObject,
) where

import Control.Monad.State
import Data.Maybe (fromMaybe)
import Numeric (showHex)

import Control.Monad (forM)
import Language.EO.Phi.Syntax.Abs

data Context = Context
{ globalObject :: [Binding]
, thisObject :: [Binding]
, totalNuCount :: Int
}

lookupBinding :: Attribute -> [Binding] -> Maybe Object
Expand All @@ -18,27 +27,67 @@ lookupBinding a (AlphaBinding a' object : bindings)
| otherwise = lookupBinding a bindings
lookupBinding _ _ = Nothing

isNu :: Binding -> Bool
isNu (AlphaBinding VTX _) = True
isNu (EmptyBinding VTX) = True
isNu _ = False

-- | Normalize an input 𝜑-program.
normalize :: Program -> Program
normalize (Program bindings) = Program (map (normalizeBindingWith context) bindings)
normalize (Program bindings) = evalState (Program . objectBindings <$> normalizeObject (Formation bindings)) context
where
context =
Context
{ globalObject = bindings
, thisObject = bindings
, totalNuCount = nuCount bindings
}
nuCount binds = count isNu binds + sum (map (sum . map (nuCount . objectBindings) . values) binds)
count = (length .) . filter
values (AlphaBinding _ obj) = [obj]
values _ = []
objectBindings (Formation bs) = bs
objectBindings _ = []

normalizeBindingWith :: Context -> Binding -> Binding
normalizeBindingWith context = \case
AlphaBinding a object -> AlphaBinding a (normalizeObjectWith context object)
binding -> binding
rule1 :: Object -> State Context Object
rule1 (Formation bindings) = do
normalizedBindings <- forM bindings $ \case
AlphaBinding a object
| a /= VTX ->
aabounegm marked this conversation as resolved.
Show resolved Hide resolved
do
object' <- rule1 object
pure (AlphaBinding a object')
b -> pure b
finalBindings <-
if not $ any isNu normalizedBindings
then do
nus <- gets totalNuCount
modify (\c -> c{totalNuCount = totalNuCount c + 1})
let pad s = (if even (length s) then "" else "0") ++ s
let insertDashes s
| length s <= 2 = s ++ "-"
| otherwise =
let go = \case
[] -> []
[x] -> [x]
[x, y] -> [x, y, '-']
(x : y : xs) -> x : y : '-' : go xs
in go s
let dataObject = Formation [DeltaBinding $ Bytes $ insertDashes $ pad $ showHex nus ""]
pure (AlphaBinding VTX dataObject : normalizedBindings)
else do
pure normalizedBindings
pure (Formation finalBindings)
rule1 object = pure object

normalizeObjectWith :: Context -> Object -> Object
normalizeObjectWith Context{..} object =
normalizeObject :: Object -> State Context Object
normalizeObject object = do
this <- gets thisObject
case object of
ThisDispatch a ->
fromMaybe object (lookupBinding a thisObject)
_ -> object
-- Rule 1
obj@(Formation _) -> rule1 obj
ThisDispatch a -> pure $ fromMaybe object (lookupBinding a this)
_ -> pure object

-- | Split compound object into its head and applications/dispatch actions.
peelObject :: Object -> PeeledObject
Expand Down
4 changes: 2 additions & 2 deletions eo-phi-normalizer/test/Language/EO/PhiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ spec :: Spec
spec = do
tests <- runIO (allPhiTests "test/eo/phi/")
describe "Normalizer" $ do
forM_ tests $ \PhiTest{..} -> do
forM_ tests $ mapM_ $ \PhiTest{..} -> do
it name $ do
Phi.printTree (Phi.normalize input) `shouldBe` Phi.printTree normalized
describe "Prettify" $ do
forM_ tests $ \PhiTest{..} -> do
forM_ tests $ mapM_ $ \PhiTest{..} -> do
it name $ do
Phi.printTree input `shouldBe` dropWhileEnd isSpace prettified
2 changes: 1 addition & 1 deletion eo-phi-normalizer/test/Test/EO/Phi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data PhiTest = PhiTest
}
deriving (Generic, FromJSON)

allPhiTests :: FilePath -> IO [PhiTest]
allPhiTests :: FilePath -> IO [[PhiTest]]
allPhiTests dir = do
paths <- listDirectory dir
forM (sort paths) $ \path ->
Expand Down
7 changes: 0 additions & 7 deletions eo-phi-normalizer/test/eo/phi/normal-1.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions eo-phi-normalizer/test/eo/phi/normal-2.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions eo-phi-normalizer/test/eo/phi/rule-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- name: 'Program in normal form (1)'
input: |
{}
normalized: |
{ ν ↦ ⟦ Δ ⤍ 00- ⟧ }
prettified: |
{ }

- name: 'Program in normal form (2)'
input: |
{ φ ↦ ⟦ φ ↦ ⟦ φ ↦ ⟦ ⟧ ⟧ ⟧ }
normalized: |
{ ν ↦ ⟦ Δ ⤍ 03- ⟧, φ ↦ ⟦ ν ↦ ⟦ Δ ⤍ 02- ⟧, φ ↦ ⟦ ν ↦ ⟦ Δ ⤍ 01- ⟧, φ ↦ ⟦ ν ↦ ⟦ Δ ⤍ 00- ⟧ ⟧ ⟧ ⟧ }
prettified: |
{ φ ↦ ⟦ φ ↦ ⟦ φ ↦ ⟦ ⟧ ⟧ ⟧ }
7 changes: 7 additions & 0 deletions eo-phi-normalizer/test/eo/phi/rule-5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: 'Simple static attribute reference'
input: |
{ φ ↦ ⟦ ⟧ , a ↦ ξ.φ }
normalized: |
{ ν ↦ ⟦ Δ ⤍ 01- ⟧, φ ↦ ⟦ ν ↦ ⟦ Δ ⤍ 00- ⟧ ⟧, a ↦ ξ.φ }
prettified: |
{ φ ↦ ⟦ ⟧, a ↦ ξ.φ }
10 changes: 0 additions & 10 deletions eo-phi-normalizer/test/eo/phi/test.yaml

This file was deleted.