mirror of
https://github.com/github/semantic.git
synced 2024-12-22 22:31:36 +03:00
Add FDoc.RecursionSchemes and anamorphism example
This commit is contained in:
parent
154f88532e
commit
ab4b56dcd8
33
src/FDoc/RecursionSchemes.hs
Normal file
33
src/FDoc/RecursionSchemes.hs
Normal file
@ -0,0 +1,33 @@
|
||||
{-# LANGUAGE DataKinds, ScopedTypeVariables, TypeFamilies, TypeOperators #-}
|
||||
module FDoc.RecursionSchemes where
|
||||
|
||||
import Data.Record
|
||||
import Range
|
||||
import Category
|
||||
import Term
|
||||
import Syntax
|
||||
import Prologue
|
||||
import Prelude
|
||||
import Data.Functor.Foldable hiding (ListF)
|
||||
import FDoc.Term
|
||||
|
||||
data NewField = NewField deriving (Show)
|
||||
|
||||
{-
|
||||
Anamorphism
|
||||
|
||||
ana :: (a -> Base t a) -- ^ a (Base t)-coalgebra
|
||||
-> a -- ^ seed
|
||||
-> t -- ^ resulting fixed point
|
||||
|
||||
Anamorphism as a recursion scheme "builds up" a recursive structure.
|
||||
Anamorphisms work by using a coalgebra, which maps a seed value to a fixed point structure.
|
||||
|
||||
The example below adds a new field to the `Record` fields.
|
||||
|
||||
-}
|
||||
|
||||
indexedTermAna :: [leaf] -> Term (Syntax leaf) (Record '[NewField, Range, Category])
|
||||
indexedTermAna childrenLeaves = ana coalgebra (indexedTerm childrenLeaves)
|
||||
where
|
||||
coalgebra term = (NewField .: (extract term)) :< (unwrap term)
|
Loading…
Reference in New Issue
Block a user