@@ -6,22 +6,29 @@ import Control.Monad.Free (Free, liftF, wrap)
66import Data.Array (intercalate , singleton , replicate , uncons )
77import Data.Foldable (foldMap , foldl , length )
88import Data.FoldableWithIndex (foldMapWithIndex )
9- import Data.Set as Set
9+ import Data.Map as Map
1010import Data.Maybe (maybe )
11- import Data.String (toLower , contains )
12- import Data.String.Pattern (Pattern (..))
11+ import Data.String (toLower , contains , replaceAll , Pattern (..), Replacement (..))
1312import Data.Traversable (mapAccumL )
1413import Data.Tuple.Nested ((/\))
1514
1615import Common
1716import Model
1817
19- haskellCode :: TypedTerm String String -> String
20- haskellCode tm = case haskellCode' tm of
21- { i, o, code } -> args <> arr (tuple $ foldMap singleton i) (showNested i) `comp` code `comp` arr (showNested o) (tuple $ foldMap singleton o)
18+
19+ haskellCode :: String -> TypedTerm String String -> String
20+ haskellCode name tm = case haskellCode' tm of
21+ { i, o, code } -> typeDecl <> args <> indent (" \n " <> arr (tuple $ foldMap singleton i) (showNested i) `comp` code `comp` arr (showNested o) (tuple $ foldMap singleton o))
2222 where
23- gens = foldMap (\{ decl, bid } -> if isGen decl then Set .singleton bid else Set .empty) tm
24- args = if length gens > 0 then " \\ " <> toLower (intercalate " " gens) <> " -> " else " "
23+ gens = tm # foldMap \{ decl, bid } -> case decl of
24+ Gen ty -> Map .singleton bid ty
25+ otherwise -> Map .empty
26+ args = if length gens > 0 then name <> " " <> toLower (intercalate " " $ Map .keys gens) <> " = " else " "
27+ typeDecl = name <>
28+ " \n :: Arrow arr" <>
29+ " \n => " <> intercalate " \n -> " (showTy <$> Map .values gens) <>
30+ " \n -> " <> showTy (getAnn tm) <>
31+ " \n "
2532
2633
2734type HaskellCode = { i :: Free Array String , o :: Free Array String , code :: String }
@@ -41,42 +48,43 @@ haskellCode' = foldFix \(Ann _ f) -> alg f where
4148 o = 0 ..< r <#> \n -> " o" <> show n
4249 out = if l == 1
4350 then tuple (replicate r " i0" )
44- else " let o = mconcat [" <> intercalate " , " i <> " ] in " <> tuple (replicate r " o" )
51+ else " let o = merge [" <> intercalate " , " i <> " ] in " <> tuple (replicate r " o" )
4552 Gen (Ty i o) ->
4653 { i: liftF $ foldMapWithIndex (\j n -> [toLower n <> show j]) i
4754 , o: liftF $ foldMapWithIndex (\j n -> [toLower n <> show j]) o
4855 , code: toLower bid }
49- Cup -> haskellEmpty -- TODO
50- Cap -> haskellEmpty
51- alg (TC ts) = ts # uncons # maybe haskellEmpty \{ head, tail } -> foldl compose head tail
56+ Cup -> { i: liftF [] , o: liftF [ " a0 " , " a1 " ], code: " cup " }
57+ Cap -> { i: liftF [ " a0 " , " a1 " ], o: liftF [] , code: " cap " }
58+ alg (TC ts) = ts # uncons # maybe haskellEmpty ( \{ head, tail } -> foldl compose head tail) # mapCode braced
5259 where
53- compose l r = { i: l.i, o: r.o, code : braced $ l.code `comp` arr (showNested l.o) (showNested i') `comp` r.code }
60+ compose l r = { i: l.i, o: r.o, code : l.code `comp` arr (showNested l.o) (showNested i') `comp` r.code }
5461 where
5562 os = foldMap singleton l.o
5663 i' = mapAccumL accum os r.i # _.value
5764 accum os' _ = uncons os' # maybe { accum: [] , value: " _" } \{ head, tail } -> { accum: tail, value: head }
5865 alg (TT ts) = foldMapWithIndex f ts # g
5966 where
60- f j { i, o, code } =
61- [{ i: map (\n -> n <> " _" <> show j) i
62- , o: map (\n -> n <> " _" <> show j) o
63- , code
64- }]
65- g l = uncons l # maybe haskellEmpty \{ head, tail } -> foldl tensor head tail
66- tensor :: HaskellCode -> HaskellCode -> HaskellCode
67+ f j { i, o, code } = [{ i: map (\n -> n <> " _" <> show j) i, o: map (\n -> n <> " _" <> show j) o, code }]
68+ g l = uncons l # maybe haskellEmpty (\{ head, tail } -> foldl tensor head tail)
6769 tensor l r =
6870 { i: wrap [l.i, r.i]
6971 , o: wrap [l.o, r.o]
70- , code: " ( " <> l.code <> " *** " <> r.code <> " ) "
72+ , code: braced ( l.code <> " *** " <> r.code)
7173 }
7274
75+ mapCode :: (String -> String ) -> HaskellCode -> HaskellCode
76+ mapCode f { i, o, code } = { i, o, code: f code }
77+
7378comp :: String -> String -> String
7479comp " returnA" r = r
7580comp l " returnA" = l
76- comp l r = l <> " >>> " <> r
81+ comp l r = l <> " \n >>> " <> r
82+
83+ indent :: String -> String
84+ indent = replaceAll (Pattern " \n " ) (Replacement " \n " )
7785
7886braced :: String -> String
79- braced s = if contains (Pattern " " ) s then " (" <> s <> " )" else s
87+ braced s = if contains (Pattern " " ) s then " (" <> ( if contains ( Pattern " \n " ) s then indent s else s) <> " )" else s
8088
8189arr :: String -> String -> String
8290arr ls rs = if ls == rs then " returnA" else " arr (\\ " <> ls <> " -> " <> rs <> " )"
@@ -87,3 +95,6 @@ tuple ss = "(" <> intercalate ", " ss <> ")"
8795
8896showNested :: Free Array String -> String
8997showNested = foldFree tuple identity
98+
99+ showTy :: Ty String -> String
100+ showTy (Ty l r) = " arr " <> tuple l <> " " <> tuple r
0 commit comments