Skip to content

Commit

Permalink
feat: Add deinline transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 21, 2022
1 parent 337115e commit c28a3bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions schema.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ library
Data.Schema
Data.Schema.Builder
Data.Schema.C
Data.Schema.Deinline
Data.Schema.Pretty
Data.Schema.Type
build-depends:
Expand Down
19 changes: 19 additions & 0 deletions src/Data/Schema/Deinline.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Data.Schema.Deinline (deinline) where

import Control.Arrow (first)
import Data.Fix (Fix (..), foldFix)
import Data.List (nub)
import Data.Schema (Schema, SchemaF (..), Type (..))

deinline :: Schema -> Schema
deinline = fst . foldFix getTypes

getTypes :: SchemaF (Schema, [Schema]) -> (Schema, [Schema])
getTypes (Atom ty) = (Fix . Atom $ ty, [])
getTypes (Sum dt@(Just (_, ty)) cons) = (Fix . Atom . TyName $ ty, Fix (Sum dt (map fst cons)) : concatMap snd cons)
getTypes (Prod fields) = (Fix . Prod . map fst $ fields, concatMap snd fields)
getTypes (Field name ty) = first (Fix . Field name) ty
getTypes (Con name ty) = first (Fix . Con name) ty
getTypes (List ty) = first (Fix . List) ty
getTypes (Schema mods) = (Fix . Schema . nub . concatMap snd $ mods, [])
getTypes x = error $ "unhandled: " <> show x

0 comments on commit c28a3bc

Please sign in to comment.