AS_BASIC_CSL.hs revision 5815bdce64892624676670501231b62f3d534898
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
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederMaintainer : Ewaryst.Schulz@dfki.de
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederStability : experimental
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian MaederPortability : portable
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederThis file contains the abstract syntax for CSL as well as pretty printer for it.
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
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederimport qualified Data.Map as Map
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
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-- | A simple operator constructor from given operator id and arguments
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian MaedermkPredefOp :: OPNAME -> [EXPRESSION] -> EXPRESSION
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian MaedermkPredefOp n el = Op (OpId n) [] el nullRange
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder-- | operator symbol declaration
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving Show
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | variable symbol declaration
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata VAR_ITEM = Var_item [Id.Token] Domain Id.Range
27912d626bf179b82fcb337077e5cd9653bb71cfChristian Maeder deriving Show
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian Maedernewtype BASIC_SPEC = Basic_spec [AS_Anno.Annoted (BASIC_ITEM)]
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder deriving Show
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maederdata GroundConstant = GCI APInt | GCR APFloat deriving (Eq, Ord, Show)
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)
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-- | Extended Parameter Datatype
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maederdata EXTPARAM = EP Id.Token String APInt deriving (Eq, Ord, Show)
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maederdata OPNAME = OP_mult -- arithmetic operators
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder | OP_div | OP_plus | OP_minus | OP_neg | OP_pow
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 -- special CAS operators
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder | OP_maximize | OP_factor
230aa1e8c53fcaffd75814c7d86bd37c8012596aChristian Maeder | OP_divide | OP_factorize | OP_int | OP_rlqe | OP_simplify | OP_solve
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder -- comparison predicates
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder | OP_neq | OP_lt | OP_leq | OP_eq | OP_gt | OP_geq | OP_convergence
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder -- boolean constants and connectives
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder | OP_false | OP_true | OP_not | OP_and | OP_or | OP_impl
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder -- quantifiers
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder | OP_ex | OP_all
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder deriving (Eq, Ord)
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maederinstance Show OPNAME where
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder OP_neq -> "!="
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_mult -> "*"
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder OP_plus -> "+"
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maeder OP_minus -> "-"
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder OP_neg -> "-"
7c57322afb6342e5cc8b1fdc96050b707407fc61Christian Maeder OP_div -> "/"
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder OP_leq -> "<="
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 Maederdata OPID = OpId OPNAME | OpString String deriving (Eq, Ord)
9348e8460498ddfcd9da11cd8b5794c06023e004Christian Maederinstance Show OPID where
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder show (OpId n) = show n
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder show (OpString s) = s
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder-- | Datatype for expressions
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maederdata EXPRESSION =
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 | Double APFloat Id.Range
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder deriving (Eq, Ord, Show)
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-- | 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-- | symbol for identifiers
16e124196c6b204769042028c74f533509c9b5d3Christian Maedernewtype SYMB = Symb_id Id.Token
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder -- pos: colon
16e124196c6b204769042028c74f533509c9b5d3Christian Maeder deriving (Show, Eq)
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-- | 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)
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 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-- Pretty Printing;
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
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
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
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 ]
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
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder case Map.lookup arit oim of
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder Just x -> Right x
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder case Map.lookup (-1) oim of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Just x -> Right x
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> Left True
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> Left False
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
d4146229cf85928342dfd25ec8b579a7feb0d381Christian Maeder case Map.lookup arit oim of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Just x -> Right x
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
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 case Map.lookup arit oim of
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder Just x -> bind x
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder _ -> error $ "lookupBindInfo: no opinfo for " ++ show op
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian MaederlookupBindInfo (OpString _) _ = Nothing
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! "
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 MaederprintCMD (Cond l) = vcat (map (uncurry printCase) l) $+$ text "end"
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 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 MaederprintExtparam :: EXTPARAM -> Doc
d4146229cf85928342dfd25ec8b579a7feb0d381Christian MaederprintExtparam (EP p op i) =
0be2d0cfd911d313e7e941edbc77f95052c8c19bChristian Maeder pretty p <> text op <> (text $ if op == "-|" then "" else show i)
72b9099aeec0762bae4546db3bc4b48721027bf4Christian MaederprintExtparams :: [EXTPARAM] -> Doc
599766906b25938d5b184febd19b8e0bbe623e7bChristian MaederprintExtparams [] = empty
599766906b25938d5b184febd19b8e0bbe623e7bChristian MaederprintExtparams l = brackets $ sepByCommas $ map printExtparam l
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 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