1
+ {-# LANGUAGE BlockArguments #-}
1
2
{-# LANGUAGE GADTs #-}
2
3
3
4
-- | The Primer API.
@@ -63,7 +64,7 @@ import Control.Monad.Zip (MonadZip)
63
64
import Data.Map qualified as Map
64
65
import Data.Text qualified as T
65
66
import ListT qualified (toList )
66
- import Optics (ifoldr , (^.) )
67
+ import Optics (ifoldr , over , traverseOf , view , (^.) )
67
68
import Primer.App (
68
69
App ,
69
70
EditAppM ,
@@ -107,10 +108,12 @@ import Primer.Core (
107
108
Type ,
108
109
Type' (.. ),
109
110
defAST ,
110
- defType ,
111
111
moduleNamePretty ,
112
112
unLocalName ,
113
+ _typeMeta ,
114
+ _typeMetaLens ,
113
115
)
116
+ import Primer.Core qualified as Core
114
117
import Primer.Database (
115
118
OffsetLimit ,
116
119
Page ,
@@ -146,6 +149,7 @@ import Primer.JSON (
146
149
)
147
150
import Primer.Module (moduleDefsQualified , moduleName , moduleTypesQualified )
148
151
import Primer.Name (Name , unName )
152
+ import Primer.Primitives (primDefType )
149
153
import StmContainers.Map qualified as StmMap
150
154
151
155
-- | The API environment.
@@ -480,11 +484,20 @@ viewProg p =
480
484
, editable = e
481
485
, types = fst <$> Map. assocs (moduleTypesQualified m)
482
486
, defs =
483
- ( \ (n , d) ->
487
+ ( \ (name , d) ->
484
488
Def
485
- { name = n
486
- , type_ = viewTreeType $ defType d
489
+ { name
487
490
, term = viewTreeExpr . astDefExpr <$> defAST d
491
+ , type_ =
492
+ case d of
493
+ Core. DefAST d' -> viewTreeType $ astDefType d'
494
+ Core. DefPrim d' -> viewTreeType' $ labelNodes $ primDefType d'
495
+ where
496
+ labelNodes =
497
+ flip evalState (0 :: Int ) . traverseOf _typeMeta \ () -> do
498
+ n <- get
499
+ put $ n + 1
500
+ pure $ " primtype_" <> show d' <> " _" <> show n
488
501
}
489
502
)
490
503
<$> Map. assocs (moduleDefsQualified m)
@@ -668,7 +681,12 @@ viewTreeExpr e0 = case e0 of
668
681
669
682
-- | Similar to 'viewTreeExpr', but for 'Type's
670
683
viewTreeType :: Type -> Tree
671
- viewTreeType t0 = case t0 of
684
+ viewTreeType = viewTreeType' . over _typeMeta (show . view _id)
685
+
686
+ -- | Like 'viewTreeType', but with the flexibility to accept arbitrary textual node identifiers,
687
+ -- rather than using the type's numeric IDs.
688
+ viewTreeType' :: Type' Text -> Tree
689
+ viewTreeType' t0 = case t0 of
672
690
TEmptyHole _ ->
673
691
Tree
674
692
{ nodeId
@@ -682,7 +700,7 @@ viewTreeType t0 = case t0 of
682
700
{ nodeId
683
701
, flavor = FlavorTHole
684
702
, body = NoBody
685
- , childTrees = [viewTreeType t]
703
+ , childTrees = [viewTreeType' t]
686
704
, rightChild = Nothing
687
705
}
688
706
TCon _ n ->
@@ -698,7 +716,7 @@ viewTreeType t0 = case t0 of
698
716
{ nodeId
699
717
, flavor = FlavorTFun
700
718
, body = NoBody
701
- , childTrees = [viewTreeType t1, viewTreeType t2]
719
+ , childTrees = [viewTreeType' t1, viewTreeType' t2]
702
720
, rightChild = Nothing
703
721
}
704
722
TVar _ n ->
@@ -714,15 +732,15 @@ viewTreeType t0 = case t0 of
714
732
{ nodeId
715
733
, flavor = FlavorTApp
716
734
, body = NoBody
717
- , childTrees = [viewTreeType t1, viewTreeType t2]
735
+ , childTrees = [viewTreeType' t1, viewTreeType' t2]
718
736
, rightChild = Nothing
719
737
}
720
738
TForall _ n k t ->
721
739
Tree
722
740
{ nodeId
723
741
, flavor = FlavorTForall
724
742
, body = TextBody $ withKindAnn $ unName $ unLocalName n
725
- , childTrees = [viewTreeType t]
743
+ , childTrees = [viewTreeType' t]
726
744
, rightChild = Nothing
727
745
}
728
746
where
@@ -733,7 +751,7 @@ viewTreeType t0 = case t0 of
733
751
KType -> identity
734
752
_ -> (<> (" :: " <> show k))
735
753
where
736
- nodeId = show $ t0 ^. _id
754
+ nodeId = t0 ^. _typeMetaLens
737
755
738
756
showGlobal :: GlobalName k -> Text
739
757
showGlobal n = moduleNamePretty (qualifiedModule n) <> " ." <> unName (baseName n)
0 commit comments