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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
------------------------------------------------------------------------------ --- Library to support meta-programming in Curry. --- --- This library contains a definition for representing FlatCurry programs --- in Curry (type "Prog") and an I/O action to read Curry programs and --- transform them into this representation (function "readFlatCurry"). --- --- @author Michael Hanus --- @version March 2001 ------------------------------------------------------------------------------ module Flat where import Char import FileGoodies (getFileInPath) import FilePath (takeFileName, (</>), (<.>)) import System.CurryPath import System.FrontendExec ------------------------------------------------------------------------------ -- Definition of data types for representing FlatCurry programs: -- ============================================================= --- Data type for representing a Curry module in the intermediate form. --- A value of this data type has the form --- <CODE> --- (Prog modname imports typedecls functions opdecls translation_table) --- </CODE> --- where modname: name of this module, --- imports: list of modules names that are imported, --- typedecls, opdecls, functions, translation_table: see below type Prog = Expr --- The data type for representing type variables. --- They are represented by (TVar i) where i is a type variable index. type TVarIndex = Int --- Data type for representing definitions of algebraic data types. --- <PRE> --- A data type definition of the form --- --- data t x1...xn = ...| c t1....tkc |... --- --- is represented by the FlatCurry term --- --- (Type t [i1,...,in] [...(Cons c kc [t1,...,tkc])...]) --- --- where each ij is the index of the type variable xj --- --- Note: the type variable indices are unique inside each type declaration --- and are usually numbered from 0 --- --- Thus, a data type declaration consists of the name of the data type, --- a list of type parameters and a list of constructor declarations. --- </PRE> data TypeDecl = Type String [TVarIndex] [ConsDecl] deriving Eq --- A constructor declaration consists of the name and arity of the --- constructor and a list of the argument types of the constructor. data ConsDecl = Cons String Int [TypeExpr] deriving Eq --- Data type for type expressions. --- A type expression is either a type variable, a function type, --- or a type constructor application. --- --- Note: the names of the predefined type constructors are --- "Int", "Float", "Bool", "Char", "IO", "Success", --- "()" (unit type), "(,...,)" (tuple types), "[]" (list type) data TypeExpr = TVar TVarIndex -- type variable | FuncType TypeExpr TypeExpr -- function type t1->t2 | TCons String [TypeExpr] -- type constructor application deriving Eq --- Data type for operator declarations. --- An operator declaration "fix p n" in Curry corresponds to the --- FlatCurry term (Op n fix p). data OpDecl = Op String Fixity Int deriving Eq --- Data types for the different choices for the fixity of an operator. data Fixity = InfixOp | InfixlOp | InfixrOp deriving (Eq, Show) --- Data types for representing object variables. --- Object variables occurring in expressions are represented by (Var i) --- where i is a variable index. type VarIndex = Int --- Data type for representing function declarations. --- <PRE> --- A function declaration in FlatCurry is a term of the form --- --- (Func name arity type (Rule [i_1,...,i_arity] e)) --- --- and represents the function "name" with definition --- --- name :: type --- name x_1...x_arity = e --- --- where each i_j is the index of the variable x_j --- --- Note: the variable indices are unique inside each function declaration --- and are usually numbered from 0 --- --- External functions are represented as (Func name arity type (External s)) --- where s is the external name associated to this function. --- --- Thus, a function declaration consists of the name, arity, type, and rule. --- </PRE> data FuncDecl = Func String Int TypeExpr Rule deriving Eq --- A rule is either a list of formal parameters together with an expression --- or an "External" tag. data Rule = Rule [VarIndex] Expr | External String deriving Eq --- Data type for classifying case expressions. --- Case expressions can be either flexible or rigid in Curry. data CaseType = Rigid | Flex -- type of a case expression deriving (Eq, Show) --- Data type for classifying combinations --- (i.e., a function/constructor applied to some arguments). data CombType = FuncCall -- a call to a function | ConsCall -- a call with a constructor at the top | PartCall -- a partial application (i.e., FuncCall or -- ConsCall with some arguments missing) deriving (Eq, Show) --- Data types for representing expressions. data Expr = Var VarIndex -- variable (represented by unique index) | Lit Literal -- literal (Integer/Float/Char constant) | Comb CombType String [Expr] -- application (f e1 ... en) of function/ -- constructor f with n<=arity(f) | Apply Expr Expr -- application (e1 e2) | Constr [VarIndex] Expr -- constraint: let x1,...,xn free in e | Or Expr Expr -- disjunction of e1 e2 | Case CaseType Expr [BranchExpr] -- case (rigid or flex) | Choice Expr -- committed choice | GuardedExpr [VarIndex] Expr Expr -- guarded expression | SQ Expr | Prog String [String] [TypeDecl] [FuncDecl] [OpDecl] [Translation] deriving Eq {- The latter guarded expression represents conditional right-hand sides, possibly containing extra variables, i.e., (GuardedExpr [i1,...,in] c e) represents "| c = e where x1,...,xn free" i.e., c is always a constraint and each ij is the index of the variable xj. Remarks: 1. if-then-else expressions are represented as function calls: (if e1 then e2 else e3) is represented as (Comb FuncCall "if_then_else" [e1,e2,e3]) 2. Functions with evaluation annotation "choice" are represented by a rule whose right-hand side is enclosed in a "Choice". Furthermore, all rules of the original definition must be represented by GuardedExpr after pattern matching. Example: m eval choice m [] y = y m x [] = x is translated into: Rule [0,1] (Choice (Or (Case Rigid (Var 0) [(Pattern (Ident "[]") [] (GuardedExpr [] (Comb FuncCall "success" []) (Var 1)))] ) (Case Rigid (Var 1) [(Pattern (Ident "[]") [] (GuardedExpr [] (Comb FuncCall "success" []) (Var 0)))] )) Operational meaning of (Choice e): evaluate e with local search spaces and commit to the first (GuardedExpr [...] c ge) in e whose constraint c is satisfied -} --- Data types for representing branches in a case expressions. --- <PRE> --- Branches "(c x1...xn) -> e" in case expressions are represented as --- --- (Branch (Pattern c [i1,...,in]) e) --- --- where each ij is the index of the pattern variable xj, or as --- --- (Branch (LPattern (Intc i)) e) --- --- for integers as branch patterns (similarly for other literals --- like float or character constants). --- </PRE> data BranchExpr = Branch Pattern Expr deriving Eq --- Data type for representing patterns in case expressions. data Pattern = Pattern String [VarIndex] | LPattern Literal deriving Eq --- Data type for representing literals occurring in an expression --- or case branch. It is either an integer, a float, or a character constant. data Literal = Intc Int | Floatc Float | Charc Char deriving Eq --- Data type for translating external into internal names. --- Each module contains a translation table to translate the --- external names (visible to the user) into internal names (used in --- the implementation). Usually, the internal names are prefixed by --- the name of the module (except for the prelude). Thus, the translation --- table is a list of elements of the form --- <CODE>(Trans name internal_name)</CODE>. data Translation = Trans String String deriving Eq ------------------------------------------------------------------------------ --- I/O action which parses a Curry program and returns the corresponding --- FlatCurry program. --- Thus, the argument is the file name without suffix ".curry" --- (or ".lcurry") and the result is a FlatCurry term representing this --- program. readFlatCurry :: String -> IO Prog readFlatCurry progfile = readFlatCurryWithParseOptions progfile (setQuiet True defaultParams) --- I/O action which reads a FlatCurry program from a file --- with respect to some parser options. --- This I/O action is used by the standard action <CODE>readFlatCurry</CODE>. --- It is currently predefined only in Curry2Prolog. --- @param progfile - the program file name (without suffix ".curry") --- @param options - parameters passed to the front end readFlatCurryWithParseOptions :: String -> FrontendParams -> IO Prog readFlatCurryWithParseOptions progname options = do mbsrc <- lookupModuleSourceInLoadPath progname case mbsrc of Nothing -> do -- no source file, try to find FlatCurry file in load path: loadpath <- getLoadPathForModule progname filename <- getFileInPath (flatCurryFileName (takeFileName progname)) [""] loadpath readFlatCurryFile filename Just (dir,_) -> do callFrontendWithParams FCY options progname readFlatCurryFile (flatCurryFileName (dir </> takeFileName progname)) --- Transforms a name of a Curry program (with or without suffix ".curry" --- or ".lcurry") into the name of the file containing the --- corresponding FlatCurry program. flatCurryFileName :: String -> String flatCurryFileName prog = inCurrySubdir (stripCurrySuffix prog) <.> "fcy" --- Transforms a name of a Curry program (with or without suffix ".curry" --- or ".lcurry") into the name of the file containing the --- corresponding FlatCurry program. flatCurryIntName :: String -> String flatCurryIntName prog = inCurrySubdir (stripCurrySuffix prog) <.> "fint" --- I/O action which reads a FlatCurry program from a file in ".fcy" format. --- In contrast to `readFlatCurry`, this action does not parse --- a source program. Thus, the argument must be the name of an existing --- file (with suffix ".fcy") containing a FlatCurry program in ".fcy" --- format and the result is a FlatCurry term representing this program. --- It is currently predefined only in Curry2Prolog. readFlatCurryFile :: String -> IO Expr --Prog readFlatCurryFile fname = prim_readFlatCurryFile $## fname prim_readFlatCurryFile :: String -> IO Expr --Prog prim_readFlatCurryFile external ------------------------------------------------------------------------------ --- Splits an internal name into a pair of module name and name in local --- module. --- @param intname - the internal name of a (type) constructor or function --- occurring in the FlatCurry program --- @return (mod,lname) where mod is the module name and --- lname the local name of the parameter in this module splitFlatModName :: String -> (String,String) splitFlatModName name | isAlpha (head name) = let (modname,rname) = break (=='.') name in if rname=="" then ("Prelude",name) else (modname,tail rname) | otherwise = ("Prelude",name) ------------------------------------------------------------------------------ |