Difference between revisions of "Stdlib"

From Predictive Chemistry
Jump to: navigation, search
(Created page with "<source lang="haskell"> /* Stdenv v 0.4 * * To be run once, create some useful standard objects, and save * as a commit structure (for later loading into stdenv-s). * * …")
 
(The Easy Way)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
  +
== The Easy Way ==
  +
You can load up the whole standard environment using the builtin set_com primitive. One exception are IO functions, which must be explicitly asked for (because of obvious security issues). Currently, you also have to manually
  +
add infix rules for user-defined functions, like so
  +
<source lang="haskell">
  +
set_com (open "0b21518a6bed97b38745615071c91f976c0c1a96")
  +
sleep = Prim "89010250203552d538a6981e396d648999f2a62d"
  +
infixr $ 0
  +
infixl >> 1
  +
infixl >>= 1
  +
infixl >>= 1
  +
infixr =<< 1
  +
infixr $ 0
  +
infixl ++ 4
  +
infixl !% 5
  +
-- these are for match expressions
  +
infixl // 0
  +
infixl $$ 1
  +
infixl @ 2
  +
</source>
  +
  +
If you want to extend your stdenv, you can use get_com to get the current working commit.
  +
  +
commit =<< get_com
  +
  +
Use caution with get_com, since storing its return value leads to a recursive object and in infinite loop during evaluation. This will be fixed in the future by dis-allowing writes to commit objects (using an O(log n) functional re-write instead).
  +
  +
== The Hard Way ==
 
<source lang="haskell">
 
<source lang="haskell">
 
/* Stdenv v 0.4
 
/* Stdenv v 0.4
Line 6: Line 33:
 
*
 
*
 
* Create some useful primitives from the base commands,
 
* Create some useful primitives from the base commands,
* 'typeof', 'commit', 'load_prim', and 'load_base'
 
  +
* 'typeof' = λ (t:⋆) (x:t) → t -- compilation step not yet complete (and presently skipped)
  +
* 'commit' : {} -> IO(string)
  +
* 'open' : string -> {} -- retrieve a previously committed dir.
  +
* 'Prim' : string -> ? -- outputs a prim
  +
* 'Base' : string -> ⋆ -- outputs the type of a base object
 
*/
 
*/
   
Line 22: Line 53:
 
>>= = applyIO
 
>>= = applyIO
 
=<< = flip (>>=)
 
=<< = flip (>>=)
  +
>> = λ a b → applyIO a (const b)
 
infixl >>= 1
 
infixl >>= 1
 
infixr =<< 1
 
infixr =<< 1
  +
infixl >> 1
   
 
-- $ : <a,b : 'kind> -> (a -> b) -> a -> b
 
-- $ : <a,b : 'kind> -> (a -> b) -> a -> b
Line 62: Line 95:
 
-- Save current project as a commit.
 
-- Save current project as a commit.
 
-- commit_proj "stdenv-0.2"
 
-- commit_proj "stdenv-0.2"
  +
  +
unApply = Prim "1572ad8f02e44c195ee3cfff433f0fa81df75268"
   
 
-- string cat
 
-- string cat
++ = Prim "b1c9a203f71d2e5bdc4ee2378de3ab2fdb1d8352"
+
++ = Prim "04abdf66335ce0504c9e7a1707445e2f700fc185"
 
-- string slice
 
-- string slice
 
!% = Prim "992740f62d4ca2f2ca9f6f6cf9274d8b9626a7c2"
 
!% = Prim "992740f62d4ca2f2ca9f6f6cf9274d8b9626a7c2"
Line 71: Line 106:
 
infix !% 5
 
infix !% 5
   
  +
sleep = Prim "89010250203552d538a6981e396d648999f2a62d"
 
</source>
 
</source>

Latest revision as of 16:49, 22 October 2014

The Easy Way

You can load up the whole standard environment using the builtin set_com primitive. One exception are IO functions, which must be explicitly asked for (because of obvious security issues). Currently, you also have to manually add infix rules for user-defined functions, like so <source lang="haskell"> set_com (open "0b21518a6bed97b38745615071c91f976c0c1a96") sleep = Prim "89010250203552d538a6981e396d648999f2a62d" infixr $ 0 infixl >> 1 infixl >>= 1 infixl >>= 1 infixr =<< 1 infixr $ 0 infixl ++ 4 infixl !% 5 -- these are for match expressions infixl // 0 infixl $$ 1 infixl @ 2 </source>

If you want to extend your stdenv, you can use get_com to get the current working commit.

 commit =<< get_com

Use caution with get_com, since storing its return value leads to a recursive object and in infinite loop during evaluation. This will be fixed in the future by dis-allowing writes to commit objects (using an O(log n) functional re-write instead).

The Hard Way

<source lang="haskell"> /* Stdenv v 0.4

*
*  To be run once, create some useful standard objects, and save
* as a commit structure (for later loading into stdenv-s). 
*
*   Create some useful primitives from the base commands,
* 'typeof' =  λ (t:⋆) (x:t) →  t -- compilation step not yet complete (and presently skipped)
* 'commit' : {} -> IO(string)
* 'open' : string -> {} -- retrieve a previously committed dir.
* 'Prim' : string -> ? -- outputs a prim
* 'Base' : string -> ⋆ -- outputs the type of a base object
*/

-- Test types Nat : ⋆ Z : Nat Succ : Nat → Nat

-- Simple functions id = λ x → x seq = λ a b → b const = λ a b → a flip = λ f a b → f b a

>>= = applyIO =<< = flip (>>=) >> = λ a b → applyIO a (const b) infixl >>= 1 infixr =<< 1 infixl >> 1

-- $ : <a,b : 'kind> -> (a -> b) -> a -> b $ : (a -> b) -> a -> b $ = fun f x -> f x infixr $ 0

IO : (a : 'kind) -> 'kind ST : (a : 'kind) (r : 'kind) -> 'kind

rm := Prim =<< commit {

        name = "rm",
        ptype = type String -> IO(Nil),
        n=1, extn = 1, -- IO prim
        API = "",
        reduce = "
  1. include <ast.h>
  2. include <cli.h>

Ast *reduce(prim_cb_t *cb, struct Prim *p) {

   struct Environ *e = (struct Environ *)cb->st;
   if(p->t[0]->type != TBase || p->t[0]->base->type != TString) {
       printf(\"rm() called on non-string\\n\");
       return mkNil();
   }
   p9remove(e, p->t[0]->base->name);
   return mkNil();

}", }

/* More work to do: unitIO applyIO unitST applyST

rm pwd ls cd open gc_stat

  • /

-- Save current project as a commit. -- commit_proj "stdenv-0.2"

unApply = Prim "1572ad8f02e44c195ee3cfff433f0fa81df75268"

-- string cat ++ = Prim "04abdf66335ce0504c9e7a1707445e2f700fc185" -- string slice !% = Prim "992740f62d4ca2f2ca9f6f6cf9274d8b9626a7c2" strlen = Prim "2081afb248e480a8f6ca6220156c69d7ac938ef8" infixl ++ 4 infix !% 5

sleep = Prim "89010250203552d538a6981e396d648999f2a62d" </source>