From ab4b56dcd8a24e757ef6534f4321c62e01eb0c95 Mon Sep 17 00:00:00 2001 From: Rick Winfrey Date: Thu, 17 Nov 2016 16:17:09 -0600 Subject: [PATCH] Add FDoc.RecursionSchemes and anamorphism example --- src/FDoc/RecursionSchemes.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/FDoc/RecursionSchemes.hs diff --git a/src/FDoc/RecursionSchemes.hs b/src/FDoc/RecursionSchemes.hs new file mode 100644 index 000000000..7ee53dbe8 --- /dev/null +++ b/src/FDoc/RecursionSchemes.hs @@ -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)