File size: 14,658 Bytes
a10b189 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFunctor #-}
{-|
Module : Assertica.Surface.AST
Description : Surface syntax abstract syntax tree (intermediate representation)
Copyright : (c) 2026 Ahmad Ali Parr
License : BSL-1.0
The surface AST represents the intermediate form after parsing Haskell-like syntax
but before elaboration into the core AST. It preserves source locations for error
reporting and supports syntactic sugar that the elaborator will expand.
DESIGN PRINCIPLES:
1. Explicit source location tracking on all nodes
2. Surface-level syntactic sugar (qualified names, infix operators)
3. Minimal implicit argument information (explicit where possible)
4. Clear separation from core AST
-}
module Assertica.Surface.AST
( -- * Source location
SourceLoc (..)
, Located (..)
-- * Surface names
, SurfaceIdent
, SurfaceQName
-- * Surface syntax terms
, SurfaceTerm (..)
, SurfaceType (..)
, SurfacePattern (..)
, SurfaceClause (..)
-- * Propositions and assertions
, SurfaceProposition (..)
, SurfaceAssertion (..)
, SurfaceProof (..)
-- * Type and data declarations
, SurfaceDecl (..)
, SurfaceDataCon (..)
-- * Constants
, SurfaceConstant (..)
-- * Pretty printing
, prettySurfaceTerm
, prettySurfaceType
, prettySurfaceProposition
) where
import Data.Text (Text)
import qualified Data.Text as T
import GHC.Generics (Generic)
import Data.List (intercalate)
-- ============================================================================
-- SOURCE LOCATION TRACKING
-- ============================================================================
{-| Source location for error reporting.
-}
data SourceLoc = SourceLoc
{ slFile :: String -- ^ File path
, slLine :: Int -- ^ Line number (1-indexed)
, slCol :: Int -- ^ Column number (1-indexed)
}
deriving (Eq, Show, Generic)
{-| A value with source location information.
-}
data Located a = Located
{ locValue :: a
, locLoc :: SourceLoc
}
deriving (Eq, Show, Functor, Traversable, Foldable, Generic)
-- ============================================================================
-- SURFACE NAMES
-- ============================================================================
{-| A simple identifier (e.g., "x", "join", "True")
-}
type SurfaceIdent = Text
{-| A qualified name (e.g., "Prelude.Bool" or just "Bool")
Parsed as segments separated by dots.
-}
type SurfaceQName = [Text]
-- ============================================================================
-- SURFACE CONSTANTS
-- ============================================================================
{-| Surface-level constants.
-}
data SurfaceConstant
= IntLit Integer -- ^ Integer literal: 42, -5
| BoolLit Bool -- ^ Boolean literal: True, False
| StringLit String -- ^ String literal: "hello"
| UnitLit -- ^ Unit: ()
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE TYPES
-- ============================================================================
{-| Surface-level types.
Supports the same level of expressiveness as core types but with syntax sugar.
-}
data SurfaceType
= STyVar SurfaceIdent -- ^ Type variable: a, b
| STyConst SurfaceQName [SurfaceType] -- ^ Named type: Int, List a
| STyFun SurfaceType SurfaceType -- ^ Function type: a -> b
| STyForall SurfaceIdent (Maybe SurfaceType) SurfaceType
-- ^ Forall: forall x. T or forall x : A. B
| STyTuple [SurfaceType] -- ^ Tuple type: (a, b, c)
| STyApp SurfaceType SurfaceType -- ^ Type application
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE PATTERNS
-- ============================================================================
{-| Surface-level patterns for pattern matching.
-}
data SurfacePattern
= SPVar SurfaceIdent -- ^ Variable pattern: x
| SPConstr SurfaceQName [SurfacePattern] -- ^ Constructor pattern: Cons x xs
| SPWildcard -- ^ Wildcard pattern: _
| SPLit SurfaceConstant -- ^ Literal pattern: 42, True
| SPTuple [SurfacePattern] -- ^ Tuple pattern: (x, y, z)
deriving (Eq, Show, Generic)
{-| A clause in a case expression: pattern -> body
-}
data SurfaceClause = SurfaceClause
{ scPattern :: Located SurfacePattern
, scBody :: Located SurfaceTerm
}
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE TERMS
-- ============================================================================
{-| Surface-level terms.
Includes syntactic sugar like infix operators, lambda with type annotations, etc.
-}
data SurfaceTerm
= STVar SurfaceIdent -- ^ Variable: x, join
| STConst SurfaceConstant -- ^ Constant: 42, True, "hello"
| STLam [SurfaceIdent] (Maybe SurfaceType) (Located SurfaceTerm)
-- ^ Lambda: \x -> e or \x : A -> e or \(x y) : A -> e
| STApp (Located SurfaceTerm) [(Located SurfaceTerm)]
-- ^ Application: f x y z (func + list of args)
| STInfix (Located SurfaceTerm) SurfaceIdent (Located SurfaceTerm)
-- ^ Infix application: a + b (converted to "+" a b)
| STLet SurfaceIdent (Maybe SurfaceType) (Located SurfaceTerm) (Located SurfaceTerm)
-- ^ Let binding: let x = e1 in e2
| STCase (Located SurfaceTerm) [SurfaceClause] (Maybe (Located SurfaceTerm))
-- ^ Case expression with optional default
| STAnn (Located SurfaceTerm) SurfaceType -- ^ Type annotation: e : T
| STForall SurfaceIdent (Maybe SurfaceType) (Located SurfaceTerm)
-- ^ Dependent function: forall x : A . body
| STTuple [Located SurfaceTerm] -- ^ Tuple: (e1, e2, e3)
| STList [Located SurfaceTerm] -- ^ List: [e1, e2, e3]
| STConstr SurfaceQName [Located SurfaceTerm]
-- ^ Data constructor: Just 5, Cons x xs
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE PROPOSITIONS
-- ============================================================================
{-| Surface-level propositions (logical claims).
-}
data SurfaceProposition
= SPEq (Located SurfaceTerm) (Located SurfaceTerm)
-- ^ Equality: a == b
| SPForall SurfaceIdent (Maybe SurfaceType) (Located SurfaceProposition)
-- ^ Forall: forall x : A. P x
| SPExists SurfaceIdent (Maybe SurfaceType) (Located SurfaceProposition)
-- ^ Exists: exists x : A. P x
| SPAnd (Located SurfaceProposition) (Located SurfaceProposition)
-- ^ Conjunction: P /\ Q
| SPOr (Located SurfaceProposition) (Located SurfaceProposition)
-- ^ Disjunction: P \/ Q
| SPImpl (Located SurfaceProposition) (Located SurfaceProposition)
-- ^ Implication: P -> Q
| SPNot (Located SurfaceProposition) -- ^ Negation: not P
| SPTrue -- ^ True: True
| SPFalse -- ^ False: False
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE PROOFS
-- ============================================================================
{-| Surface-level proofs.
-}
data SurfaceProof
= SPRefl -- ^ Reflexivity: refl or by_refl
| SPSymm (Located SurfaceProof) -- ^ Symmetry: symm P
| SPTrans (Located SurfaceProof) (Located SurfaceProof)
-- ^ Transitivity: trans P1 P2
| SPIntro [SurfaceIdent] (Located SurfaceProof)
-- ^ Intro: fun x => P
| SPVar SurfaceIdent -- ^ Proof variable
| SPOpaque SurfaceQName -- ^ Opaque proof (axiom/oracle)
| SPByName SurfaceIdent -- ^ Named proof strategy: by_refl, by_simp
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE ASSERTIONS
-- ============================================================================
{-| Surface-level assertion declaration.
-}
data SurfaceAssertion = SurfaceAssertion
{ saName :: Located SurfaceIdent
, saProp :: Located SurfaceProposition
, saProof :: Maybe (Located SurfaceProof)
}
deriving (Eq, Show, Generic)
-- ============================================================================
-- SURFACE DECLARATIONS
-- ============================================================================
{-| Surface-level data constructor in a data type declaration.
-}
data SurfaceDataCon = SurfaceDataCon
{ sdcName :: SurfaceIdent
, sdcArgs :: [SurfaceType]
}
deriving (Eq, Show, Generic)
{-| Surface-level declarations (type, data, function, assertion, proof, typeclass).
-}
data SurfaceDecl
= SDeclType SurfaceIdent SurfaceType -- ^ Type alias: type T = A
| SDeclData SurfaceIdent [SurfaceIdent] [SurfaceDataCon]
-- ^ Data: data T a = C1 | C2 ...
| SDeclFunc SurfaceIdent (Maybe SurfaceType) [(Located SurfaceTerm)]
-- ^ Function: f : A -> B; f x = body
| SDeclAssertion SurfaceAssertion -- ^ Assertion: assert name : prop
| SDeclProof SurfaceIdent SurfaceProposition (Located SurfaceProof)
-- ^ Proof: proof name : prop := proof_term
deriving (Eq, Show, Generic)
-- ============================================================================
-- PRETTY PRINTING
-- ============================================================================
prettySurfaceTerm :: Located SurfaceTerm -> String
prettySurfaceTerm (Located term _) = prettySurfaceTermRaw term
prettySurfaceTermRaw :: SurfaceTerm -> String
prettySurfaceTermRaw = \case
STVar x -> T.unpack x
STConst c -> prettySurfaceConstant c
STLam xs mty body ->
let ty_str = case mty of
Nothing -> ""
Just ty -> " : " ++ prettySurfaceType ty
in "\\" ++ unwords (map T.unpack xs) ++ ty_str ++ " -> " ++ prettySurfaceTerm body
STApp func args ->
"(" ++ prettySurfaceTerm func ++ " " ++ unwords (map prettySurfaceTerm args) ++ ")"
STInfix l op r ->
"(" ++ prettySurfaceTerm l ++ " " ++ T.unpack op ++ " " ++ prettySurfaceTerm r ++ ")"
STLet x mty e1 e2 ->
let ty_str = case mty of
Nothing -> ""
Just ty -> " : " ++ prettySurfaceType ty
in "let " ++ T.unpack x ++ ty_str ++ " = " ++ prettySurfaceTerm e1 ++ " in " ++ prettySurfaceTerm e2
STCase e clauses def ->
"case " ++ prettySurfaceTerm e ++ " of { " ++
intercalate "; " [prettySurfacePattern (locValue (scPattern c)) ++ " -> " ++ prettySurfaceTerm (scBody c) | c <- clauses] ++
(case def of Nothing -> ""; Just d -> "; _ -> " ++ prettySurfaceTerm d) ++ " }"
STAnn e ty -> "(" ++ prettySurfaceTerm e ++ " : " ++ prettySurfaceType ty ++ ")"
STForall x mty body ->
let ty_str = case mty of
Nothing -> ""
Just ty -> " : " ++ prettySurfaceType ty
in "forall " ++ T.unpack x ++ ty_str ++ " . " ++ prettySurfaceTerm body
STTuple es -> "(" ++ intercalate ", " (map prettySurfaceTerm es) ++ ")"
STList es -> "[" ++ intercalate ", " (map prettySurfaceTerm es) ++ "]"
STConstr qn args ->
let qn_str = intercalate "." (map T.unpack qn)
in qn_str ++ if null args then "" else " " ++ unwords (map prettySurfaceTerm args)
prettySurfacePattern :: SurfacePattern -> String
prettySurfacePattern = \case
SPVar x -> T.unpack x
SPConstr qn pats ->
let qn_str = intercalate "." (map T.unpack qn)
in qn_str ++ if null pats then "" else " " ++ unwords (map prettySurfacePattern pats)
SPWildcard -> "_"
SPLit c -> prettySurfaceConstant c
SPTuple ps -> "(" ++ intercalate ", " (map prettySurfacePattern ps) ++ ")"
prettySurfaceType :: SurfaceType -> String
prettySurfaceType = \case
STyVar x -> T.unpack x
STyConst qn tys ->
let qn_str = intercalate "." (map T.unpack qn)
in if null tys
then qn_str
else qn_str ++ " " ++ unwords (map prettySurfaceType tys)
STyFun a b -> "(" ++ prettySurfaceType a ++ " -> " ++ prettySurfaceType b ++ ")"
STyForall x mty body ->
let ty_str = case mty of
Nothing -> ""
Just ty -> " : " ++ prettySurfaceType ty
in "forall " ++ T.unpack x ++ ty_str ++ " . " ++ prettySurfaceType body
STyTuple tys -> "(" ++ intercalate ", " (map prettySurfaceType tys) ++ ")"
STyApp a b -> "(" ++ prettySurfaceType a ++ " " ++ prettySurfaceType b ++ ")"
prettySurfaceConstant :: SurfaceConstant -> String
prettySurfaceConstant = \case
IntLit n -> show n
BoolLit b -> show b
StringLit s -> show s
UnitLit -> "()"
prettySurfaceProposition :: Located SurfaceProposition -> String
prettySurfaceProposition (Located prop _) = prettySurfacePropositionRaw prop
prettySurfacePropositionRaw :: SurfaceProposition -> String
prettySurfacePropositionRaw = \case
SPEq l r -> "(" ++ prettySurfaceTerm l ++ " == " ++ prettySurfaceTerm r ++ ")"
SPForall x mty body ->
let ty_str = case mty of
Nothing -> ""
Just ty -> " : " ++ prettySurfaceType ty
in "forall " ++ T.unpack x ++ ty_str ++ " . " ++ prettySurfaceProposition body
SPExists x mty body ->
let ty_str = case mty of
Nothing -> ""
Just ty -> " : " ++ prettySurfaceType ty
in "exists " ++ T.unpack x ++ ty_str ++ " . " ++ prettySurfaceProposition body
SPAnd p1 p2 -> "(" ++ prettySurfaceProposition p1 ++ " /\\ " ++ prettySurfaceProposition p2 ++ ")"
SPOr p1 p2 -> "(" ++ prettySurfaceProposition p1 ++ " \\/ " ++ prettySurfaceProposition p2 ++ ")"
SPImpl p1 p2 -> "(" ++ prettySurfaceProposition p1 ++ " -> " ++ prettySurfaceProposition p2 ++ ")"
SPNot p -> "not (" ++ prettySurfaceProposition p ++ ")"
SPTrue -> "True"
SPFalse -> "False"
|