SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive...
-
Upload
kerry-jefferson -
Category
Documents
-
view
220 -
download
3
Transcript of SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive...
![Page 1: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/1.jpg)
SPARK 0.8
David Morley, Nov 2005
![Page 2: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/2.jpg)
Core Data Types
• Includes values most often used by SPARK• Primitive
– Integer, e.g. 1– Float, e.g., 1.0– String, e.g., “one”– Symbol, e.g., one– Variable designator, e.g., $var (new in 0.8)
• Compound– Structure, e.g., (foo 1 2 3) - has functor symbol foo– List, e.g., [1 2 3]
![Page 3: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/3.jpg)
SPARK-L Source
• Represented by core data values (cf. Lisp, Prolog)
• SPARKL source to generate a value usually different from the value:– (+ 1 2) 3, ⇒ (- 4 1) 3, ⇒ 3 3⇒– [(+ 1 2) 3] [3 3]⇒– (@ “one”) one⇒– (@@ “foo” 1 2 3) (foo 1 2 3)⇒– (@@ “bar” (+ 1 1) (+ 2 2)) (bar 2 4)⇒
![Page 4: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/4.jpg)
Backquote and comma
• Used for easy construction of basic values (cf. Lisp)
• Backquote ` prevents evaluation– `one one⇒– `(foo 1 2 3) (foo 1 2 3)⇒
• Comma , performs evaluation within backquote– `(bar ,(+ 1 1) ,(+ 2 2)) (bar 2 4)⇒
![Page 5: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/5.jpg)
File
• Contains– Package statement– Identifier declarations– Import statements– Export statements– Require statements– Facts to be loaded
• Every identifier (unquoted symbol or functor) used in the file must have a visible declaration.– Declarations in the file are visible– Imported declarations are visible– Any declaration visible to the package is visible
![Page 6: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/6.jpg)
Package
• A Package is– A “bucket” that accumulates declarations, imports,
exports from files– A means of communicating declarations between files
• Files can use identifiers from their package• Files import identifiers from other packages into their package• Files export identifiers from their package
• Unlike old concept of module:– Not a fixed set of files– New files can add identifiers to a package
![Page 7: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/7.jpg)
Specifying the Package of a File
• “package: P”• For backward compatibility:
– “module: P” = “package: P”– If no package statement, package = filename
![Page 8: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/8.jpg)
File-Local Names
• Not all declarations are shared with the package
• Identifiers starting with underscore are only visible in the file– _foo, _LocalPredicate, _Procedure1
• It is conceivable (but no syntax exists) to import an identifier foo as a different identifer _foo. That import would not be visible to the package.
![Page 9: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/9.jpg)
Identifiers and Symbols• Identifers are local names
– interpreted with respect to a file
• To distinguish identifiers in different files we associate a symbol with every identifier:For identifiers declared in file F in package P:– Id maps to symbol P.Id– _Id maps to symbol F._Id
• When an identifier declared as a functor appears instead as a symbol, it evaluates to the corresponding symbol:– Given {defpredicate (Id $x)} {defpredicate (_Id $y)}– Id P.Id, ⇒ _Id F._Id (cf. ⇒ `Id Id and ⇒ `_Id _Id)⇒– Useful for passing as an argument to applyfun, etc.– Less need for “(qent Id)”
![Page 10: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/10.jpg)
Importing from a package
• Q. When you say “importfrom: P” which files do you get declarations from?
• Ans. All files in the transitive closure of the “requires” relation that are in package P.– F requires F’ if F contains requires: F’– F requires P if F contains importfrom: P …– F requires P if F is in package P– P requires F if F has the same name as P
![Page 11: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/11.jpg)
Usage
• SPARKL source expressions can be used in different ways
• Each way designated by a usage character:+ evaluate term [set: $x (+ 1 2)]- match term (= [$x $y] [1 2])s solve predicate [context: (P $x) ]u update predicate [conclude: (P 7) ]r retract predicate [retract: (P 4) ]…
• SPARKL source processing assigns a usage to each expression in source
![Page 12: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/12.jpg)
Mode declaration for a functor
• Specifies argument usages• Used for static mode checking of
action/predicate usages before file is loaded• Prefix inputs with “+”, outputs with “-”
– {defaction (act +$x $y -$z)}
In any use of this action:• $x must be evaluable (I.e., is an input)• $y need only be matchable (I.e., can be an output)• $z must not be evaluable (I.e, must be output)
![Page 13: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/13.jpg)
Formal parameters• Richer formal parameters for:
– defprocedure cues– task closure parameters– pred closure parameters
• Parameter may be one of:– +X - at start, match X to the actual parameter value– -X - at end, match actual parameter to the value of X– $x - at start, constrain $x to match actual parameter
• Examples:– +[$x $y] - match input parameter to [$x $y]
• Less need for Ground
![Page 14: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/14.jpg)
Accessing Core Valuesfrom Python
• See Python module spark.internal.parse.basicvalues• Core data types are called: String, Float, Integer, Symbol,
Variable, Structure, List– Symbol(“foo”), List((1,2,3)), isinstance(x,List)
• Structure args/List elements access:– myStruct[0], len(myStrict), myList[1:2]
• Structure functor access:– myStruct.functor
• Symbol name access:– mySym.name or str(mySym)
• Construct Structure:– Structure(functorSym, argsSequence) or functorSym.structure(arg0,
arg1, …)
![Page 15: SPARK 0.8 David Morley, Nov 2005. Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,](https://reader036.fdocuments.net/reader036/viewer/2022082505/56649cdf5503460f949a8f73/html5/thumbnails/15.jpg)
Random Bits
• “precondition: X” is now optional
• No distinction between task modifiers and basic tasks– [context: X do: Y] is a special case of using
[…] for sequential composition (technically contiguous: rather than seq: )