Skip to content

Commit 6a33d68

Browse files
author
Sjoerd Visscher
committed
Use names i/o bids in generated JSON and Haskell
1 parent 06b3aa0 commit 6a33d68

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/InferType.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ inferType ctx tm = { term, bounds, matches: matches <> typeToMatches, errors }
7575
replaceInferredType bounds $
7676
(acc <> step <> empty { bounds = bounds, matches = [Matched matches] })
7777
{ type = Ty (a <#> replaceBoxed bounds) (c <#> replaceBoxed bounds) }
78-
tmWithDecl = tm # traverse (\{ bid, box } -> Map.lookup bid ctx # maybe (Left $ "Undeclared name: " <> show bid) \decl -> Right { bid, box, decl: decl.type })
78+
tmWithDecl = tm # traverse (\{ bid, box } -> Map.lookup bid ctx # maybe (Left $ "Undeclared name: " <> show bid) \decl -> Right { bid, box, decl: decl.type, name: decl.name })
7979
fatTerm = tmWithDecl # either (const (Fix (Ann empty TUnit))) (reannotateFix alg)
8080
typeToMatches = case (getAnn fatTerm).type of Ty l r -> [Unmatched Valid Input l, Unmatched Valid Output r]
8181
{ bounds, matches, errors } = getAnn fatTerm

src/Model.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ derive instance eqValidity :: Eq Validity
162162

163163
data Matches a = Matched (Array (Validity /\ a /\ a)) | Unmatched Validity Side (Array a)
164164

165-
type TypedTerm bv bid = Fix (Ann (Ty String) TermF) { bid :: bid, box :: Box, decl :: TypeDecl bv }
165+
type TypedTerm bv bid = Fix (Ann (Ty String) TermF) { bid :: bid, box :: Box, decl :: TypeDecl bv, name :: String }
166166

167167

168168
class FlipDir bv where

src/Output/Haskell.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import Model
1717

1818

1919
haskellCode :: String -> TypedTerm String String -> String
20-
haskellCode name tm = case haskellCode' tm of
20+
haskellCode fnName tm = case haskellCode' tm of
2121
{ 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 = tm # foldMap \{ decl, bid } -> case decl of
24-
Gen ty -> Map.singleton bid ty
23+
gens = tm # foldMap \{ decl, name } -> case decl of
24+
Gen ty -> Map.singleton name ty
2525
otherwise -> Map.empty
26-
args = if length gens > 0 then name <> " " <> toLower (intercalate " " $ Map.keys gens) <> " = " else ""
27-
typeDecl = name <>
26+
args = if length gens > 0 then fnName <> " " <> toLower (intercalate " " $ Map.keys gens) <> " = " else ""
27+
typeDecl = fnName <>
2828
"\n :: Arrow arr" <>
2929
"\n => " <> intercalate "\n -> " (showTy <$> Map.values gens) <>
3030
"\n -> " <> showTy (getAnn tm) <>
@@ -39,7 +39,7 @@ haskellEmpty = { i: liftF [], o: liftF [], code: "returnA" }
3939
haskellCode' :: TypedTerm String String -> HaskellCode
4040
haskellCode' = foldFix \(Ann _ f) -> alg f where
4141
alg TUnit = haskellEmpty
42-
alg (TBox { bid, decl }) = case decl of
42+
alg (TBox { name, decl }) = case decl of
4343
Perm perm -> perm # foldMapWithIndex (\i p -> ["a" <> show i] /\ ["a" <> show (p - 1)]) #
4444
\(i /\ o) -> { i: liftF i, o: liftF o, code: arr (tuple i) (tuple o) }
4545
Spider _ l r -> { i: liftF i, o: liftF o, code: arr (tuple i) out }
@@ -52,7 +52,7 @@ haskellCode' = foldFix \(Ann _ f) -> alg f where
5252
Gen (Ty i o) ->
5353
{ i: liftF $ foldMapWithIndex (\j n -> [toLower n <> show j]) i
5454
, o: liftF $ foldMapWithIndex (\j n -> [toLower n <> show j]) o
55-
, code: toLower bid }
55+
, code: name }
5656
Cup -> { i: liftF [], o: liftF ["a0", "a1"], code: "cup" }
5757
Cap -> { i: liftF ["a0", "a1"], o: liftF [], code: "cap" }
5858
alg (TC ts) = ts # unsnoc # maybe haskellEmpty (\{ init, last } -> foldr compose last init) # mapCode braced

src/Output/JSON.purs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@ json = foldFix alg >>> stringify where
1414
alg (Ann _ TUnit) = encodeJson { type: "unit" }
1515
alg (Ann _ (TC terms)) = encodeJson { type: "compose", terms }
1616
alg (Ann _ (TT terms)) = encodeJson { type: "tensor", terms }
17-
alg (Ann (Ty l r) (TBox { bid, decl: Gen _ })) = encodeJson
17+
alg (Ann (Ty l r) (TBox { name, decl: Gen _ })) = encodeJson
1818
{ type: "generator"
19-
, name: bid
19+
, name
2020
, inputTypes: l
2121
, outputTypes: r }
22-
alg (Ann (Ty l _) (TBox { bid, decl: Perm p }))
22+
alg (Ann (Ty l _) (TBox { name, decl: Perm p }))
2323
| take (length p) [1,2,3,4,5,6,7,8,9] == p = encodeJson
2424
{ type: "identity"
25-
, name: bid
25+
, name
2626
, typeParams: l }
2727
| otherwise = encodeJson
2828
{ type: "permutation"
29-
, name: bid
29+
, name
3030
, permutation: p
3131
, typeParams: l }
32-
alg (Ann (Ty l r) (TBox { bid, decl: Spider c ni no })) = encodeJson
32+
alg (Ann (Ty l r) (TBox { name, decl: Spider c ni no })) = encodeJson
3333
{ type: "spider"
34-
, name: bid
34+
, name
3535
, inputs: ni
3636
, outputs: no
3737
, color: show c
3838
, typeParam: l <> r # head
3939
}
40-
alg (Ann (Ty l r) (TBox { bid, decl: Cup })) = encodeJson
40+
alg (Ann (Ty l r) (TBox { name, decl: Cup })) = encodeJson
4141
{ type: "cup"
42-
, name: bid
42+
, name
4343
, typeParam: l <> r # head }
44-
alg (Ann (Ty l r) (TBox { bid, decl: Cap })) = encodeJson
44+
alg (Ann (Ty l r) (TBox { name, decl: Cap })) = encodeJson
4545
{ type: "cap"
46-
, name: bid
46+
, name
4747
, typeParam: l <> r # head }

0 commit comments

Comments
 (0)