-
Notifications
You must be signed in to change notification settings - Fork 459
Abstract actions over terms #300
Changes from all commits
c0ecbb1
e9fc612
de5de5f
937df79
1bc2511
09f95d6
a43e947
44b0614
7259059
f092a30
f9c20bc
371e0c0
11091cf
785d523
152e94a
0f8da48
598dce7
05e0086
5de3d14
a97e42a
5c6df16
cca474b
7aaaa64
02f46c6
13e3788
5e1b21c
7101733
a060a15
6f5e2ff
19ac4d2
e727b3c
197ffdd
41a61e3
8c6bf14
12a07c2
2f77374
0cc79bb
97400fb
c6cfa94
897304c
bf26428
9d4b7ce
51dbc97
50105fd
3c37459
aab52d5
235b2e8
7b256bf
f9e05fc
f644a45
2034359
8184742
4843d44
fdb6d96
6050f10
05103d8
64c2287
bc13823
972653a
868bde8
cc77d7b
bda1269
c0278a9
bf14a4f
b401da6
8fc85e8
7aa36de
377d824
0c5342a
68ea7e1
5fe4b1d
e5685f9
f08ac25
fea2338
08247c7
bc62f32
c847990
ab72710
9b1c99f
8af4158
7647102
1fdecb0
9f68d5b
7312997
eb922e7
714aac1
7d93f39
ca2f8a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,11 +30,11 @@ import Prologue | |
| type ComparabilityRelation syntax ann1 ann2 = forall a b. TermF syntax ann1 a -> TermF syntax ann2 b -> Bool | ||
|
|
||
| rws :: (Foldable syntax, Functor syntax, Diffable syntax) | ||
| => ComparabilityRelation syntax (FeatureVector, ann) (FeatureVector, ann) | ||
| -> (Term syntax (FeatureVector, ann) -> Term syntax (FeatureVector, ann) -> Bool) | ||
| -> [Term syntax (FeatureVector, ann)] | ||
| -> [Term syntax (FeatureVector, ann)] | ||
| -> EditScript (Term syntax (FeatureVector, ann)) (Term syntax (FeatureVector, ann)) | ||
| => ComparabilityRelation syntax (FeatureVector, ann1) (FeatureVector, ann2) | ||
| -> (Term syntax (FeatureVector, ann1) -> Term syntax (FeatureVector, ann2) -> Bool) | ||
| -> [Term syntax (FeatureVector, ann1)] | ||
| -> [Term syntax (FeatureVector, ann2)] | ||
| -> EditScript (Term syntax (FeatureVector, ann1)) (Term syntax (FeatureVector, ann2)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use distinct type parameters for the annotations on either side of the diffing process to guarantee we don’t get them crossed. |
||
| rws _ _ as [] = This <$> as | ||
| rws _ _ [] bs = That <$> bs | ||
| rws canCompare _ [a] [b] = if canCompareTerms canCompare a b then [These a b] else [That b, This a] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| {-# LANGUAGE GeneralizedNewtypeDeriving, TypeOperators, UndecidableInstances #-} | ||
| {-# LANGUAGE FunctionalDependencies, GeneralizedNewtypeDeriving, TypeOperators, UndecidableInstances #-} | ||
| module Diffing.Interpreter | ||
| ( diffTerms | ||
| , diffTermPair | ||
| , DiffTerms(..) | ||
| , stripDiff | ||
| ) where | ||
|
|
||
|
|
@@ -16,30 +16,33 @@ import Prologue | |
|
|
||
| -- | Diff two à la carte terms recursively. | ||
| diffTerms :: (Diffable syntax, Eq1 syntax, Hashable1 syntax, Traversable syntax) | ||
| => Term syntax ann | ||
| -> Term syntax ann | ||
| -> Diff.Diff syntax ann ann | ||
| => Term syntax ann1 | ||
| -> Term syntax ann2 | ||
| -> Diff.Diff syntax ann1 ann2 | ||
| diffTerms t1 t2 = stripDiff (fromMaybe (Diff.replacing t1' t2') (run (runNonDetOnce (runDiff (algorithmForTerms t1' t2'))))) | ||
| where (t1', t2') = ( defaultFeatureVectorDecorator t1 | ||
| , defaultFeatureVectorDecorator t2) | ||
|
|
||
| -- | Strips the head annotation off a diff annotated with non-empty records. | ||
| stripDiff :: Functor syntax | ||
| => Diff.Diff syntax (FeatureVector, ann) (FeatureVector, ann) | ||
| -> Diff.Diff syntax ann ann | ||
| => Diff.Diff syntax (FeatureVector, ann1) (FeatureVector, ann2) | ||
| -> Diff.Diff syntax ann1 ann2 | ||
| stripDiff = bimap snd snd | ||
|
|
||
| -- | Diff a 'These' of terms. | ||
| diffTermPair :: (Diffable syntax, Eq1 syntax, Hashable1 syntax, Traversable syntax) => These (Term syntax ann) (Term syntax ann) -> Diff.Diff syntax ann ann | ||
| diffTermPair = these Diff.deleting Diff.inserting diffTerms | ||
| class DiffTerms term diff | diff -> term, term -> diff where | ||
| -- | Diff a 'These' of terms. | ||
| diffTermPair :: These (term ann1) (term ann2) -> diff ann1 ann2 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abstracting diffing over the term & diff types allows us to express the operation over à la carte & precise terms uniformly (when precise terms support diffing). |
||
|
|
||
| instance (Diffable syntax, Eq1 syntax, Hashable1 syntax, Traversable syntax) => DiffTerms (Term syntax) (Diff.Diff syntax) where | ||
| diffTermPair = these Diff.deleting Diff.inserting diffTerms | ||
|
|
||
|
|
||
| -- | Run an 'Algorithm' to completion in an 'Alternative' context using the supplied comparability & equivalence relations. | ||
| runDiff :: Algorithm | ||
| (Term syntax (FeatureVector, ann)) | ||
| (Term syntax (FeatureVector, ann)) | ||
| (Diff.Diff syntax (FeatureVector, ann) (FeatureVector, ann)) | ||
| (DiffC (Term syntax (FeatureVector, ann)) (Term syntax (FeatureVector, ann)) (Diff.Diff syntax (FeatureVector, ann) (FeatureVector, ann)) m) | ||
| (Term syntax (FeatureVector, ann1)) | ||
| (Term syntax (FeatureVector, ann2)) | ||
| (Diff.Diff syntax (FeatureVector, ann1) (FeatureVector, ann2)) | ||
| (DiffC (Term syntax (FeatureVector, ann1)) (Term syntax (FeatureVector, ann2)) (Diff.Diff syntax (FeatureVector, ann1) (FeatureVector, ann2)) m) | ||
| result | ||
| -> m result | ||
| runDiff = runDiffC . runAlgorithm | ||
|
|
@@ -57,8 +60,8 @@ instance ( Alternative m | |
| , Traversable syntax | ||
| ) | ||
| => Carrier | ||
| (Diff (Term syntax (FeatureVector, ann)) (Term syntax (FeatureVector, ann)) (Diff.Diff syntax (FeatureVector, ann) (FeatureVector, ann)) :+: sig) | ||
| (DiffC (Term syntax (FeatureVector, ann)) (Term syntax (FeatureVector, ann)) (Diff.Diff syntax (FeatureVector, ann) (FeatureVector, ann)) m) where | ||
| (Diff (Term syntax (FeatureVector, ann1)) (Term syntax (FeatureVector, ann2)) (Diff.Diff syntax (FeatureVector, ann1) (FeatureVector, ann2)) :+: sig) | ||
| (DiffC (Term syntax (FeatureVector, ann1)) (Term syntax (FeatureVector, ann2)) (Diff.Diff syntax (FeatureVector, ann1) (FeatureVector, ann2)) m) where | ||
| eff (L op) = case op of | ||
| Diff t1 t2 k -> runDiff (algorithmForTerms t1 t2) <|> pure (Diff.replacing t1 t2) >>= k | ||
| Linear (Term (In ann1 f1)) (Term (In ann2 f2)) k -> Diff.merge (ann1, ann2) <$> tryAlignWith (runDiff . diffThese) f1 f2 >>= k | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| {-# LANGUAGE AllowAmbiguousTypes, ConstraintKinds, GADTs, RankNTypes, ScopedTypeVariables, TypeFamilies, TypeOperators #-} | ||
| module Parsing.Parser | ||
| ( Parser(..) | ||
| , SomeTerm(..) | ||
| , withSomeTerm | ||
| , SomeAnalysisParser(..) | ||
| , SomeASTParser(..) | ||
| , someASTParser | ||
|
|
@@ -112,8 +110,7 @@ data Parser term where | |
| -> Parser (Term (Sum syntaxes) Loc) | ||
| -- | A parser for 'Markdown' using cmark. | ||
| MarkdownParser :: Parser (Term (TermF [] CMarkGFM.NodeType) (Node Markdown.Grammar)) | ||
| -- | An abstraction over parsers when we don’t know the details of the term type. | ||
| SomeParser :: ApplyAll typeclasses syntax => Parser (Term syntax ann) -> Parser (SomeTerm typeclasses ann) | ||
|
|
||
|
|
||
| -- | Apply all of a list of typeclasses to all of a list of functors using 'Apply'. Used by 'someParser' to constrain all of the language-specific syntax types to the typeclasses in question. | ||
| type family ApplyAll (typeclasses :: [(* -> *) -> Constraint]) (syntax :: * -> *) :: Constraint where | ||
|
|
@@ -168,12 +165,6 @@ precisePythonParser :: Parser (Py.Term Loc) | |
| precisePythonParser = UnmarshalParser tree_sitter_python | ||
|
|
||
|
|
||
| data SomeTerm typeclasses ann where | ||
| SomeTerm :: ApplyAll typeclasses syntax => Term syntax ann -> SomeTerm typeclasses ann | ||
|
|
||
| withSomeTerm :: (forall syntax . ApplyAll typeclasses syntax => Term syntax ann -> a) -> SomeTerm typeclasses ann -> a | ||
| withSomeTerm with (SomeTerm term) = with term | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only |
||
|
|
||
| -- | A parser for producing specialized (tree-sitter) ASTs. | ||
| data SomeASTParser where | ||
| SomeASTParser :: (Bounded grammar, Enum grammar, Show grammar) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| {-# LANGUAGE DerivingVia, RankNTypes, ScopedTypeVariables #-} | ||
| module Rendering.TOC | ||
| ( renderToCDiff | ||
| , renderToCTerm | ||
| , diffTOC | ||
| , Summaries(..) | ||
| , TOCSummary(..) | ||
|
|
@@ -143,15 +142,6 @@ renderToCDiff blobs = uncurry Summaries . bimap toMap toMap . List.partition isV | |
| diffTOC :: (Foldable f, Functor f) => Diff f (Maybe Declaration) (Maybe Declaration) -> [TOCSummary] | ||
| diffTOC = fmap entrySummary . dedupe . tableOfContentsBy declaration | ||
|
|
||
| renderToCTerm :: (Foldable f, Functor f) => Blob -> Term f (Maybe Declaration) -> Summaries | ||
| renderToCTerm b@Blob{..} = uncurry Summaries . bimap toMap toMap . List.partition isValidSummary . termToC | ||
| where | ||
| toMap [] = mempty | ||
| toMap as = Map.singleton (T.pack (blobPath b)) (toJSON <$> as) | ||
|
|
||
| termToC :: (Foldable f, Functor f) => Term f (Maybe Declaration) -> [TOCSummary] | ||
| termToC = fmap (recordSummary "unchanged") . termTableOfContentsBy declaration | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused. |
||
|
|
||
| -- The user-facing category name | ||
| toCategoryName :: Declaration -> T.Text | ||
| toCategoryName declaration = case declaration of | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing was using this.