Manipulating Atoms in Haskell

From OpenCog
Jump to: navigation, search


This tutorial will go through a bunch of scheme shell examples in Haskell. It would be good if the Scheme Tutorial was complete prior to this one. For a more detail overview take a look at Haskell Bindings for OpenCog .

To create libraries that can be integrated into a large OpenCog Project take a look at Creating a shared Haskell Lib


Making some atoms (Haskell)

Make sure you have the AtomSpace installed (not just built) before continuing.

1. Starting an interactive Haskell Session: First we have to go to the Haskell Directory in the AtomSpace Repo to find the library. Then we enter the interactive Session with 'stack ghci --extra-lib-dirs=/usr/local/lib/opencog' And lastely we simplify the prompt to not include all the models.

$ cd /path/to/atomspace/opencog/haskell
$ stack ghci --extra-lib-dirs=/usr/local/lib/opencog/
opencog-atomspace- configure (lib)
Configuring opencog-atomspace-
opencog-atomspace- initial-build-steps (lib)
Configuring GHCi with the following packages: opencog-atomspace
GHCi, version 8.0.1:  :? for help
[1 of 9] Compiling OpenCog.AtomSpace.Env ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Env.hs, interpreted )
[2 of 9] Compiling OpenCog.AtomSpace.Types ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Types.hs, interpreted )
[3 of 9] Compiling OpenCog.AtomSpace.Sugar ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Sugar.hs, interpreted )
[4 of 9] Compiling OpenCog.AtomSpace.Internal ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Internal.hs, interpreted )
[5 of 9] Compiling OpenCog.AtomSpace.CUtils ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/CUtils.hs, interpreted )
[6 of 9] Compiling OpenCog.AtomSpace.Api ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Api.hs, interpreted )
[7 of 9] Compiling OpenCog.AtomSpace.Query ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Query.hs, interpreted )
[8 of 9] Compiling OpenCog.AtomSpace.Utils ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace/Utils.hs, interpreted )
[9 of 9] Compiling OpenCog.AtomSpace ( /home/roman/OpenCog/atomspace/opencog/haskell/OpenCog/AtomSpace.hs, interpreted )
Ok, modules loaded: OpenCog.AtomSpace.Utils, OpenCog.AtomSpace, OpenCog.AtomSpace.Types, OpenCog.AtomSpace.Internal, OpenCog.AtomSpace.Api, OpenCog.AtomSpace.Env, OpenCog.AtomSpace.Sugar, OpenCog.AtomSpace.Query, OpenCog.AtomSpace.CUtils.
Loaded GHCi configuration from /tmp/ghci6478/ghci-script
*OpenCog.AtomSpace.Utils OpenCog.AtomSpace OpenCog.AtomSpace.Api OpenCog.AtomSpace.CUtils OpenCog.AtomSpace.Env OpenCog.AtomSpace.Internal OpenCog.AtomSpace.Query OpenCog.AtomSpace.Sugar OpenCog.AtomSpace.Types OpenCog.AtomSpace.Utils> :set prompt "\ESC[34mλ> \ESC[m"

2. Initializing the AtomSpace: We now create a new AtomSpace with the newAtomSpace wich takes the parent (Mabye AtomSpace) as a para.

λ> as <- newAtomSpace Nothing 

3. Creating Atoms: The Atom types looks as follows:

data Atom = Link Type [Atom] TruthVal | Node Type Name TruthVal
type Type = String
type Name = String

We can use that as follows to create Atoms and then insert them into the AtomSpace:

λ> let node = Node "ConceptNode" "TestNode1" noTv
λ> node
Node "ConceptNode" "TestNode1" (SimpleTV {tvMean = 1.0, tvConfidence = 0.0})
λ> let node2 = Node "ConceptNode" "TestNode2" noTv
λ> let link = Link "SetLink" [node,node2] noTv
λ> link
Link "SetLink" [Node "ConceptNode" "TestNode1" (SimpleTV {tvMean = 1.0, tvConfidence = 0.0}),Node "ConceptNode" "TestNode2" (SimpleTV {tvMean = 1.0, tvConfidence = 0.0})] (SimpleTV {tvMean = 1.0, tvConfidence = 0.0})
λ> mhandle <- as <: insertAndGetHandle link
λ> mhandle 
Just 0x00007f0ff40062e0

5. Querying the atomspace:

λ> let num1 = Node "NumberNode" "2" noTv 
λ> let num2 = Node "NumberNode" "3" noTv 
λ> let add = Link "PlusLink" [num1,num2] noTv
λ> as <: insert add
λ> as <: execute add
Just (Node "NumberNode" "5.000000" (SimpleTV {tvMean = 1.0, tvConfidence = 0.0}))



Maintained by: (edit (Roman))