AS_BASIC_CSL.hs revision 5815bdce64892624676670501231b62f3d534898
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder{- |
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederModule : $Header$
5ba323da9f037264b4a356085e844889aedeac23Christian MaederDescription : Abstract syntax for CSL
c58a94c44b76b072ace930f2126c889c0b64cb2aChristian MaederCopyright : (c) Dominik Dietrich, Ewaryst Schulz, DFKI Bremen 2010
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu ProdescuLicense : GPLv2 or higher, see LICENSE.txt
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederMaintainer : Ewaryst.Schulz@dfki.de
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederStability : experimental
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian MaederPortability : portable
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederThis file contains the abstract syntax for CSL as well as pretty printer for it.
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-}
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maedermodule CSL.AS_BASIC_CSL
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder ( EXPRESSION (..) -- datatype for numerical expressions (e.g. polynomials)
950e053ba55ac9c7d9c26a1ab48bd00202b29511Christian Maeder , EXTPARAM (..) -- datatype for extended parameters (e.g. [I=0])
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder , BASIC_ITEM (..) -- Items of a Basic Spec
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder , BASIC_SPEC (..) -- Basic Spec
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder , SYMB_ITEMS (..) -- List of symbols
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder , SYMB (..) -- Symbols
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder , SYMB_MAP_ITEMS (..) -- Symbol map
dc6b48bb46df8e56da3491c98476e6da0d1d5d1dChristian Maeder , SYMB_OR_MAP (..) -- Symbol or symbol map
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder , OPNAME (..) -- predefined operator names
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder , OPID (..) -- identifier for operators
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder , OP_ITEM (..) -- operator declaration
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder , VAR_ITEM (..) -- variable declaration
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder , Domain (..) -- domains for variable declarations
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder , GroundConstant (..) -- constants for domain formation
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder , CMD (..) -- Command datatype
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder , mkOp -- Simple Operator constructor
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder , mkPredefOp -- Simple Operator constructor for predefined ops
f26a1fc3851297e6483cf3fb56e9c0967b8f8b13Christian Maeder , OpInfo (..) -- Type for Operator information
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder , BindInfo (..) -- Type for Binder information
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder , operatorInfo -- Operator information for pretty printing
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- and static analysis
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder , operatorInfoMap -- allows efficient lookup of ops by printname
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder , operatorInfoNameMap -- allows efficient lookup of ops by opname
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder , lookupOpInfoForStatAna
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder , lookupBindInfo
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder , APInt, APFloat -- arbitrary precision numbers
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder ) where
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maederimport Common.Id as Id
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederimport Common.Doc
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maederimport Common.DocUtils
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederimport Common.AS_Annotation as AS_Anno
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederimport qualified Data.Map as Map
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian Maeder
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- Arbitrary precision numbers
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maedertype APInt = Integer
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- TODO: use an arbitrary precision float here:
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian Maeder-- The use of Other floats (such as Double) requires an instance for
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder-- ShATermConvertible in Common.ATerm.ConvInstances
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maedertype APFloat = Double
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder-- | A simple operator constructor from given operator name and arguments
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian MaedermkOp :: String -> [EXPRESSION] -> EXPRESSION
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian MaedermkOp s el = Op (OpString s) [] el nullRange
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder
54ff63bb3b23ef18efbdc51b053a2ca6f348329aChristian Maeder-- | A simple operator constructor from given operator id and arguments
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaedermkPredefOp :: OPNAME -> [EXPRESSION] -> EXPRESSION
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian MaedermkPredefOp n el = Op (OpId n) [] el nullRange
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder-- | operator symbol declaration
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maederdata OP_ITEM = Op_item [Id.Token] Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving Show
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | variable symbol declaration
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata VAR_ITEM = Var_item [Id.Token] Domain Id.Range
27912d626bf179b82fcb337077e5cd9653bb71cfChristian Maeder deriving Show
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian Maedernewtype BASIC_SPEC = Basic_spec [AS_Anno.Annoted (BASIC_ITEM)]
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder deriving Show
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata GroundConstant = GCI APInt | GCR APFloat deriving (Eq, Ord, Show)
f26a1fc3851297e6483cf3fb56e9c0967b8f8b13Christian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder-- | A finite set or an interval. True = closed, False = opened
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata Domain = Set [GroundConstant]
5ba323da9f037264b4a356085e844889aedeac23Christian Maeder | IntVal (GroundConstant, Bool) (GroundConstant, Bool)
5ba323da9f037264b4a356085e844889aedeac23Christian Maeder deriving (Eq, Ord, Show)
5ba323da9f037264b4a356085e844889aedeac23Christian Maeder
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder-- | basic items: an operator or variable declaration or an axiom
16e124196c6b204769042028c74f533509c9b5d3Christian Maederdata BASIC_ITEM =
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder Op_decl OP_ITEM
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder | Var_decls [VAR_ITEM]
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder | Axiom_item (AS_Anno.Annoted CMD)
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder deriving Show
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | Extended Parameter Datatype
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maederdata EXTPARAM = EP Id.Token String APInt deriving (Eq, Ord, Show)
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maederdata OPNAME = OP_mult -- arithmetic operators
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder | OP_div | OP_plus | OP_minus | OP_neg | OP_pow
f26a1fc3851297e6483cf3fb56e9c0967b8f8b13Christian Maeder
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder -- roots, trigonometric and other operators
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder | OP_fthrt | OP_sqrt
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder | OP_abs | OP_max | OP_min
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder | OP_cos | OP_sin | OP_tan | OP_Pi
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder -- special CAS operators
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder | OP_maximize | OP_factor
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder | OP_divide | OP_factorize | OP_int | OP_rlqe | OP_simplify | OP_solve
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder -- comparison predicates
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder | OP_neq | OP_lt | OP_leq | OP_eq | OP_gt | OP_geq | OP_convergence
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder -- boolean constants and connectives
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | OP_false | OP_true | OP_not | OP_and | OP_or | OP_impl
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- quantifiers
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder | OP_ex | OP_all
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder deriving (Eq, Ord)
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maederinstance Show OPNAME where
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder show x =
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder case x of
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder OP_neq -> "!="
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_mult -> "*"
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder OP_plus -> "+"
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_minus -> "-"
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder OP_neg -> "-"
7c57322afb6342e5cc8b1fdc96050b707407fc61Christian Maeder OP_div -> "/"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_lt -> "<"
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder OP_leq -> "<="
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder OP_eq -> "="
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_gt -> ">"
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_geq -> ">="
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder OP_Pi -> "Pi"
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_pow -> "^"
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder OP_abs -> "abs"
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_all -> "all"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_and -> "and"
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder OP_convergence -> "convergence"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_cos -> "cos"
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian Maeder OP_divide -> "divide"
31242f7541fd6ef179e4eb5be7522ddf54ae397bChristian Maeder OP_ex -> "ex"
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder OP_factor -> "factor"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_factorize -> "factorize"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_fthrt -> "fthrt"
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder OP_impl -> "impl"
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian Maeder OP_int -> "int"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_max -> "max"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_maximize -> "maximize"
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder OP_min -> "min"
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_not -> "not"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_or -> "or"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_rlqe -> "rlqe"
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian Maeder OP_simplify -> "simplify"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder OP_sin -> "sin"
5ba323da9f037264b4a356085e844889aedeac23Christian Maeder OP_solve -> "solve"
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder OP_sqrt -> "sqrt"
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder OP_tan -> "tan"
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder OP_false -> "False"
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder OP_true -> "True"
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maederdata OPID = OpId OPNAME | OpString String deriving (Eq, Ord)
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maederinstance Show OPID where
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder show (OpId n) = show n
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder show (OpString s) = s
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | Datatype for expressions
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maederdata EXPRESSION =
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Var Id.Token
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder -- token instead string Id vs Token:
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder | Op OPID [EXTPARAM] [EXPRESSION] Id.Range
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder -- TODO: don't need them anymore, they should be removed soon
c9892acbf03a509d874ac6d79b9a2cb09042e0dcChristian Maeder | List [EXPRESSION] Id.Range
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder -- this means interval (interval
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | Interval APFloat APFloat Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | Int APInt Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | Double APFloat Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving (Eq, Ord, Show)
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder-- TODO: add Range-support to this type
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata CMD = Ass EXPRESSION EXPRESSION
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | Cmd String [EXPRESSION]
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian Maeder | Sequence [CMD] -- program sequence
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | Cond [(EXPRESSION, [CMD])]
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | Repeat EXPRESSION [CMD] -- constraint, statements
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving (Show, Eq, Ord)
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | symbol lists for hiding
b49276c9f50038e0bd499ad49f7bd6444566a834Christian Maederdata SYMB_ITEMS = Symb_items [SYMB] Id.Range
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder -- pos: SYMB_KIND, commas
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder deriving (Show, Eq)
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder-- | symbol for identifiers
16e124196c6b204769042028c74f533509c9b5d3Christian Maedernewtype SYMB = Symb_id Id.Token
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder -- pos: colon
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder deriving (Show, Eq)
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder-- | symbol maps for renamings
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maederdata SYMB_MAP_ITEMS = Symb_map_items [SYMB_OR_MAP] Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- pos: SYMB_KIND, commas
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving (Show, Eq)
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | symbol map or renaming (renaming then denotes the identity renaming)
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata SYMB_OR_MAP = Symb SYMB
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder | Symb_map SYMB SYMB Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- pos: "|->"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving (Show, Eq)
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata BindInfo = BindInfo { bindingVarPos :: [Int] -- ^ argument positions of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- binding variables
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder , boundBodyPos :: [Int] -- ^ argument positions of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- bound terms
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder } deriving (Eq, Ord, Show)
facf15c975d25ca5d31d8f84bf48f09d1d951ad6Christian Maeder
facf15c975d25ca5d31d8f84bf48f09d1d951ad6Christian Maederdata OpInfo = OpInfo { prec :: Int -- ^ precedence between 0 and 9
facf15c975d25ca5d31d8f84bf48f09d1d951ad6Christian Maeder , infx :: Bool -- ^ True = infix
facf15c975d25ca5d31d8f84bf48f09d1d951ad6Christian Maeder , arity :: Int -- ^ the operator arity
facf15c975d25ca5d31d8f84bf48f09d1d951ad6Christian Maeder , opname :: OPNAME -- ^ The actual operator name
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder , bind :: Maybe BindInfo -- ^ More info for binders
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder } deriving (Eq, Ord, Show)
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder-- Pretty Printing;
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maederinstance Pretty Domain where
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder pretty = printDomain
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maederinstance Pretty OP_ITEM where
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder pretty = printOpItem
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederinstance Pretty VAR_ITEM where
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder pretty = printVarItem
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maederinstance Pretty BASIC_SPEC where
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder pretty = printBasicSpec
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maederinstance Pretty BASIC_ITEM where
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder pretty = printBasicItems
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederinstance Pretty EXTPARAM where
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder pretty = printExtparam
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maederinstance Pretty EXPRESSION where
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder pretty = printExpression
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maederinstance Pretty SYMB_ITEMS where
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder pretty = printSymbItems
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maederinstance Pretty SYMB where
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder pretty = printSymbol
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maederinstance Pretty SYMB_MAP_ITEMS where
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder pretty = printSymbMapItems
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maederinstance Pretty SYMB_OR_MAP where
de2f13b8310de00ca228385b1530660e036054c2Christian Maeder pretty = printSymbOrMap
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maederinstance Pretty CMD where
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder pretty = printCMD
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder-- | Mapping of operator names to arity-'OpInfo'-maps (an operator may
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder-- behave differently for different arities).
f8c07dc6526e0134d66885d461a30abadc2c6038Christian MaederoperatorInfoMap :: Map.Map String (Map.Map Int OpInfo)
afa6848d579d235c9677e1ab477916df8e5ae11aChristian MaederoperatorInfoMap = foldl f Map.empty operatorInfo
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder where f m oi = Map.insertWith Map.union (show $ opname oi)
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder (Map.fromList [(arity oi, oi)]) m
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian Maeder-- | Same as operatorInfoMap but with keys of type OPNAME instead of String
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian MaederoperatorInfoNameMap :: Map.Map OPNAME (Map.Map Int OpInfo)
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian MaederoperatorInfoNameMap = foldl f Map.empty operatorInfo
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian Maeder where f m oi = Map.insertWith Map.union (opname oi)
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian Maeder (Map.fromList [(arity oi, oi)]) m
d976ba42e9d48c289f9c73147669c7e57b7aa98eChristian Maeder
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder-- | Mapping of operator names to arity-'OpInfo'-maps (an operator may
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder-- behave differently for different arities).
f8c07dc6526e0134d66885d461a30abadc2c6038Christian MaederoperatorInfo :: [OpInfo]
afa6848d579d235c9677e1ab477916df8e5ae11aChristian MaederoperatorInfo =
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder let -- arity (-1 means flex), precedence, infix
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder toSgl n i p fx = OpInfo p fx i n Nothing
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder toSglBind n i p fx bv bb =
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder OpInfo p fx i n $ Just $ BindInfo [bv] [bb]
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder -- arityflex simple ops
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder aflex s = toSgl s (-1) 0 False
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder -- arity0 simple ops
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder a0 s = toSgl s 0 0 False
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder -- arity1 simple ops
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder a1 s = toSgl s 1 0 False
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder -- arity2 simple ops
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder a2 s = toSgl s 2 0 False
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder -- arity2 binder
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder a2bind bv bb s = toSglBind s 2 0 False bv bb
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder -- arity2 infix with precedence
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder a2i p s = toSgl s 2 p True
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder in map a0 [ OP_Pi, OP_true, OP_false ]
31a189d4cff554f78407cdc422480e84e99a6ec6Christian Maeder ++ map a1 [ OP_neg, OP_cos, OP_sin, OP_tan, OP_sqrt, OP_fthrt, OP_abs
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder , OP_simplify, OP_rlqe, OP_factor, OP_factorize ]
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder ++ map (a2i 2) [ OP_ex, OP_all, OP_and, OP_or, OP_impl ]
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder ++ map (a2i 3) [ OP_eq, OP_gt, OP_leq, OP_geq, OP_neq, OP_lt]
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder ++ map (a2i 4) [ OP_plus, OP_minus]
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder ++ map (a2i 5) [OP_div, OP_mult]
f8c07dc6526e0134d66885d461a30abadc2c6038Christian Maeder ++ map (a2i 6) [OP_pow]
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder ++ map a2 [ OP_int, OP_divide, OP_solve, OP_convergence ]
afa6848d579d235c9677e1ab477916df8e5ae11aChristian Maeder ++ map aflex [ OP_min, OP_max ]
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder ++ map (a2bind 1 0) [ OP_maximize ]
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | For the given name and arity we lookup an 'OpInfo', where arity=-1
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder-- means flexible arity. If an operator is registered for the given
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder-- string but not for the arity we return: Left True.
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder-- This function is designed for the lookup of operators in not statically
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- analyzed terms. For statically analyzed terms use lookupOpInfo.
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederlookupOpInfoForStatAna :: String -- ^ operator name
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -> Int -- ^ operator arity
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -> Either Bool OpInfo
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai CodesculookupOpInfoForStatAna op arit =
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder case Map.lookup op operatorInfoMap of
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder Just oim ->
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder case Map.lookup arit oim of
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder Just x -> Right x
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder Nothing ->
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder case Map.lookup (-1) oim of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Just x -> Right x
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> Left True
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> Left False
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | For the given name and arity we lookup an 'OpInfo', where arity=-1
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder-- means flexible arity. If an operator is registered for the given
2faad0c99d17a6ef53a464864caccbb20cf48409Christian Maeder-- string but not for the arity we return: Left True.
2faad0c99d17a6ef53a464864caccbb20cf48409Christian MaederlookupOpInfo :: OPID -- ^ operator id
2faad0c99d17a6ef53a464864caccbb20cf48409Christian Maeder -> Int -- ^ operator arity
2faad0c99d17a6ef53a464864caccbb20cf48409Christian Maeder -> Either Bool OpInfo
9348e8460498ddfcd9da11cd8b5794c06023e004Christian MaederlookupOpInfo (OpId op) arit =
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder case Map.lookup op operatorInfoNameMap of
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder Just oim ->
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder case Map.lookup arit oim of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Just x -> Right x
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder Nothing ->
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder case Map.lookup (-1) oim of
599766906b25938d5b184febd19b8e0bbe623e7bChristian Maeder Just x -> Right x
599766906b25938d5b184febd19b8e0bbe623e7bChristian Maeder _ -> Left True
599766906b25938d5b184febd19b8e0bbe623e7bChristian Maeder _ -> error $ "lookupOpInfo: no opinfo for " ++ show op
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederlookupOpInfo (OpString _) _ = Left False
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder-- | For the given name and arity we lookup an 'BindInfo', where arity=-1
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- means flexible arity.
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederlookupBindInfo :: OPID -- ^ operator name
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -> Int -- ^ operator arity
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder -> Maybe BindInfo
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederlookupBindInfo (OpId op) arit =
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder case Map.lookup op operatorInfoNameMap of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder Just oim ->
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder case Map.lookup arit oim of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Just x -> bind x
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder _ -> Nothing
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> error $ "lookupBindInfo: no opinfo for " ++ show op
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian MaederlookupBindInfo (OpString _) _ = Nothing
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian MaederprintCMD :: CMD -> Doc
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaederprintCMD (Ass c def)
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder = printExpression c <+> text ":=" <+> printExpression def
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederprintCMD c@(Cmd s exps) -- TODO: remove the case := later
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder | s==":=" = error $ "printCMD: use Ass for assignment representation! "
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder ++ show c
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | s=="constraint" = printExpression (exps !! 0)
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder | otherwise = (text s) <> (parens (sepByCommas (map printExpression exps)))
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederprintCMD (Repeat e stms) =
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder text "re" <> (text "peat" $+$ vcat (map ((text "." <+>) . printCMD) stms))
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder $+$ text "until" <+> printExpression e
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederprintCMD (Sequence stms) =
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder text "se" <> (text "quence" $+$ vcat (map ((text "." <+>) . printCMD) stms))
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder $+$ text "end"
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederprintCMD (Cond l) = vcat (map (uncurry printCase) l) $+$ text "end"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederprintCase :: EXPRESSION -> [CMD] -> Doc
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederprintCase e l = text "ca"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder <> (text "se" <+> printExpression e <> text ":"
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder $+$ vcat (map ((text "." <+>) . printCMD) l))
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaedergetPrec :: EXPRESSION -> Int
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaedergetPrec (Op s _ exps _)
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | length exps == 0 = 8 -- check maximum given prec in operatorInfo,
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder -- this value must be higher
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder | otherwise =
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder case lookupOpInfo s $ length exps of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Right oi -> prec oi
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder Left True -> error $
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder concat [ "getPrec: registered operator ", show s, " used "
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder , "with non-registered arity ", show $ length exps ]
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> 0
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaedergetPrec _ = 9
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederprintExtparam :: EXTPARAM -> Doc
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederprintExtparam (EP p op i) =
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder pretty p <> text op <> (text $ if op == "-|" then "" else show i)
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder
72b9099aeec0762bae4546db3bc4b48721027bf4Christian MaederprintExtparams :: [EXTPARAM] -> Doc
599766906b25938d5b184febd19b8e0bbe623e7bChristian MaederprintExtparams [] = empty
599766906b25938d5b184febd19b8e0bbe623e7bChristian MaederprintExtparams l = brackets $ sepByCommas $ map printExtparam l
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder
599766906b25938d5b184febd19b8e0bbe623e7bChristian MaederprintInfix :: EXPRESSION -> Doc
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaederprintInfix e@(Op s _ exps _) =
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- we mustn't omit the space between the operator and its arguments for text-
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder-- operators such as "and", "or", but it would be good to omit it for "+-*/"
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder (if (outerprec<=(getPrec (exps!!0))) then (printExpression $ (exps !! 0))
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder else (parens (printExpression $ (exps !! 0))))
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder <+> text (show s) <+> (if outerprec<= getPrec (exps!!1)
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder then printExpression $ exps !! 1
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder else parens (printExpression $ exps !! 1))
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder where outerprec = getPrec e
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederprintInfix _ = error "printInfix: Impossible case"
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder
72b9099aeec0762bae4546db3bc4b48721027bf4Christian MaederprintExpression :: EXPRESSION -> Doc
72b9099aeec0762bae4546db3bc4b48721027bf4Christian MaederprintExpression (Var token) = text $ "$" ++ tokStr token
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaederprintExpression e@(Op s epl exps _)
b38e57295a9ba3a4de9719171dcff2d9f3b554cdChristian Maeder | length exps == 0 = text (show s) <> printExtparams epl
b38e57295a9ba3a4de9719171dcff2d9f3b554cdChristian Maeder | otherwise =
b38e57295a9ba3a4de9719171dcff2d9f3b554cdChristian Maeder let asPrfx = text (show s) <> printExtparams epl
b38e57295a9ba3a4de9719171dcff2d9f3b554cdChristian Maeder <> parens (sepByCommas $ map printExpression exps)
b38e57295a9ba3a4de9719171dcff2d9f3b554cdChristian Maeder in case lookupOpInfo s $ length exps of
Right oi
| infx oi -> printInfix e
| otherwise -> asPrfx
_ -> asPrfx
printExpression (List exps _) = sepByCommas (map printExpression exps)
printExpression (Int i _) = text (show i)
printExpression (Double d _) = text (show d)
printExpression (Interval l r _) =
brackets $ sepByCommas $ map (text . show) [l, r]
printOpItem :: OP_ITEM -> Doc
printOpItem (Op_item tokens _) =
text "operator" <+> sepByCommas (map pretty tokens)
printVarItem :: VAR_ITEM -> Doc
printVarItem (Var_item vars dom _) =
hsep [sepByCommas $ map pretty vars, text "in", pretty dom]
printDomain :: Domain -> Doc
printDomain (Set l) = braces $ sepByCommas $ map printGC l
printDomain (IntVal (c1, b1) (c2, b2)) =
hcat [ getIBorder True b1, sepByCommas $ map printGC [c1, c2]
, getIBorder False b2]
getIBorder :: Bool -> Bool -> Doc
getIBorder False False = lbrack
getIBorder True True = lbrack
getIBorder _ _ = rbrack
printGC :: GroundConstant -> Doc
printGC (GCI i) = text (show i)
printGC (GCR d) = text (show d)
printBasicSpec :: BASIC_SPEC -> Doc
printBasicSpec (Basic_spec xs) = vcat $ map pretty xs
printBasicItems :: BASIC_ITEM -> Doc
printBasicItems (Axiom_item x) = pretty x
printBasicItems (Op_decl x) = pretty x
printBasicItems (Var_decls x) = text "vars" <+> (sepBySemis $ map pretty x)
printSymbol :: SYMB -> Doc
printSymbol (Symb_id sym) = pretty sym
printSymbItems :: SYMB_ITEMS -> Doc
printSymbItems (Symb_items xs _) = fsep $ map pretty xs
printSymbOrMap :: SYMB_OR_MAP -> Doc
printSymbOrMap (Symb sym) = pretty sym
printSymbOrMap (Symb_map source dest _) =
pretty source <+> mapsto <+> pretty dest
printSymbMapItems :: SYMB_MAP_ITEMS -> Doc
printSymbMapItems (Symb_map_items xs _) = fsep $ map pretty xs
-- Instances for GetRange
instance GetRange OP_ITEM where
getRange = Range . rangeSpan
rangeSpan x = case x of
Op_item a b -> joinRanges [rangeSpan a, rangeSpan b]
instance GetRange VAR_ITEM where
getRange = Range . rangeSpan
rangeSpan x = case x of
Var_item a _ b -> joinRanges [rangeSpan a, rangeSpan b]
instance GetRange BASIC_SPEC where
getRange = Range . rangeSpan
rangeSpan x = case x of
Basic_spec a -> joinRanges [rangeSpan a]
instance GetRange BASIC_ITEM where
getRange = Range . rangeSpan
rangeSpan x = case x of
Op_decl a -> joinRanges [rangeSpan a]
Var_decls a -> joinRanges [rangeSpan a]
Axiom_item a -> joinRanges [rangeSpan a]
instance GetRange CMD where
getRange = Range . rangeSpan
rangeSpan (Ass c def) = joinRanges (map rangeSpan [c, def])
rangeSpan (Cmd _ exps) = joinRanges (map rangeSpan exps)
-- parsing guruantees l <> null
rangeSpan (Repeat c l) = joinRanges [rangeSpan c, rangeSpan $ head l]
-- parsing guruantees l <> null
rangeSpan (Sequence l) = rangeSpan $ head l
rangeSpan (Cond l) = rangeSpan $ head l
instance GetRange SYMB_ITEMS where
getRange = Range . rangeSpan
rangeSpan (Symb_items a b) = joinRanges [rangeSpan a, rangeSpan b]
instance GetRange SYMB where
getRange = Range . rangeSpan
rangeSpan (Symb_id a) = joinRanges [rangeSpan a]
instance GetRange SYMB_MAP_ITEMS where
getRange = Range . rangeSpan
rangeSpan (Symb_map_items a b) = joinRanges [rangeSpan a, rangeSpan b]
instance GetRange SYMB_OR_MAP where
getRange = Range . rangeSpan
rangeSpan x = case x of
Symb a -> joinRanges [rangeSpan a]
Symb_map a b c -> joinRanges [rangeSpan a, rangeSpan b, rangeSpan c]
instance GetRange EXPRESSION where
getRange = Range . rangeSpan
rangeSpan x = case x of
Var token -> joinRanges [rangeSpan token]
Op _ _ exps a -> joinRanges $ [rangeSpan a] ++ (map rangeSpan exps)
List exps a -> joinRanges $ [rangeSpan a] ++ (map rangeSpan exps)
Int _ a -> joinRanges [rangeSpan a]
Double _ a -> joinRanges [rangeSpan a]
Interval _ _ a -> joinRanges [rangeSpan a]