3 people like it.

AI

Tiny robot simulate, it's available to run on tryfs.net (but not on iPad).

 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: 
module String =
    let join (v:seq<string>) s = System.String.Join(s, v)
    let equalsOrdinalIgnoreCase a b = System.String.Equals(a, b, System.StringComparison.OrdinalIgnoreCase)

module Union =
    open Microsoft.FSharp.Reflection
    let getName (x:'a) = match FSharpValue.GetUnionFields(x, typeof<'a>) with case, _ -> case.Name
    let getInfo v t = FSharpType.GetUnionCases t |> Seq.tryFind (fun i -> String.equalsOrdinalIgnoreCase i.Name v)
    let makeUnion<'a> v = FSharpValue.MakeUnion(v, null) :?> 'a
    let parse<'a> v :'a option =
        let i = getInfo v typeof<'a>
        if i = None then None else Some <| makeUnion i.Value
//    let getCaseNames<'ty> () = FSharpType.GetUnionCases(typeof<'ty>) |> Array.map (fun info -> info.Name)

type Descriptor = { Name : string; Description : string }
let descriptor = { Name = null; Description = null }
let environment = { Name = "Environment"; Description= "The simulate world is all of white, white space, white planet. Color, odor and position mean nothing here." }

module AI =
    module Language =
        let localize v culture = if v = "Hello World" && culture = "zh-CN" then "您好" else v

    type Stat = Idle | Walking | Closed
    type Conversation = ``Hello world`` | ``Started walking`` | ``Already walking`` | ``Stoped walking`` | ``Just idle`` | Bye
    type Operator = { Culture : string }
    let defaultOperator = { Culture = "en-US" }

    let part = { descriptor with Description = "It's a part, I don't know how to use it." }
    type Machine = { Model : string; Parts : seq<Descriptor> }
    type R = { Name : string; Stat : Stat; Operator : Operator; LastConversation : string; Body : Machine  }
    let defaultMachine = { Model = "Simulate model 1130"; Parts = [ { part with Name = "Eyes" }; { part with Name = "Nose" }; { part with Name = "Feet"; Description = "Use feet for walking." } ] }
    let defaultAI = { Name = "AI"; Stat = Idle; Operator = defaultOperator; LastConversation = "Initialized."; Body = defaultMachine }

    let say v m = { m with LastConversation = Language.localize (Union.getName v + ".") m.Operator.Culture }
    let create operator = say ``Hello world`` { defaultAI with Operator = operator }
    let close m = say Bye { m with Stat = Closed }
    let walk m = if m.Stat = Idle then say ``Started walking`` { m with Stat = Walking } else say ``Already walking`` m
    let stop m = if m.Stat = Walking then say ``Stoped walking`` { m with Stat = Idle } else say ``Just idle`` m
    
    module Knowledge =
        let d = [ "I", "I am AI."; "Programmer", "Programmer is a_a."; "Operator", "You are operator." ]
        let s m = seq {
            for i in d -> { Name = fst i; Description = snd i }
            yield { Name = "Stat"; Description = Union.getName m.Stat }
            yield { Name = "Body"; Description = m.Body.Model }
            yield { Name = "Parts"; Description = sprintf "%s." <| String.join (m.Body.Parts |> Seq.map (fun i -> i.Name)) ", " }
            for i in m.Body.Parts -> i
            yield { Name = "Actions"; Description = "Walk, Stop, Close." } }
        let get v m =
            let r = s m |> Seq.tryFind (fun p -> String.equalsOrdinalIgnoreCase p.Name v)
            if r = None then "I don't understand." else r.Value.Description
        let describe m = sprintf "%s\n%s\n%s\nI have these knowledge: %s.\nI can do these actions: %s\n" (get "I" m) (get "Programmer" m)  (get "Operator" m) (String.join (s m |> Seq.map (fun i -> i.Name)) ", ") (get "Actions" m)

    let name = "AI"
    let body = defaultMachine
    let knowledge v m =  { m with LastConversation = Knowledge.get v m }
    let describe m = Knowledge.describe m

open AI
let talk f =
    let r = f
    printfn "%s: %s" r.Name r.LastConversation
    r
let create o =
    let r = talk <| create o
    printfn "%s" <| describe r
    r
//let create = talk <| create { defaultOperator with Culture = "zh-CN" }
type Command = Walk | Stop | Exit | Close
let proc m v =
    talk <|
        match Union.parse v with
        | Some c ->
            match c with
            | Walk -> walk m
            | Stop -> stop m
            | Exit | Close -> close m
        | None -> knowledge v m
let sim o =
    async {
        let tracks = Seq.unfold (fun state -> if state.Stat = Closed then None else Some(state, proc state (System.Console.ReadLine()))) (create o)
        ignore <| Seq.length tracks }

Async.StartImmediate <| sim defaultOperator
module String

from Microsoft.FSharp.Core
val join : v:seq<string> -> s:string -> string

Full name: Script.String.join
val v : seq<string>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val s : string
namespace System
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit
System.String.Join(separator: string, values: System.Collections.Generic.IEnumerable<string>) : string
System.String.Join<'T>(separator: string, values: System.Collections.Generic.IEnumerable<'T>) : string
System.String.Join(separator: string, [<System.ParamArray>] values: obj []) : string
System.String.Join(separator: string, [<System.ParamArray>] value: string []) : string
System.String.Join(separator: string, value: string [], startIndex: int, count: int) : string
val equalsOrdinalIgnoreCase : a:string -> b:string -> bool

Full name: Script.String.equalsOrdinalIgnoreCase
val a : string
val b : string
System.String.Equals(a: string, b: string) : bool
System.String.Equals(a: string, b: string, comparisonType: System.StringComparison) : bool
type StringComparison =
  | CurrentCulture = 0
  | CurrentCultureIgnoreCase = 1
  | InvariantCulture = 2
  | InvariantCultureIgnoreCase = 3
  | Ordinal = 4
  | OrdinalIgnoreCase = 5

Full name: System.StringComparison
field System.StringComparison.OrdinalIgnoreCase = 5
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
val getName : x:'a -> string

Full name: Script.Union.getName
val x : 'a
type FSharpValue =
  static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetRecordField : record:obj * info:PropertyInfo -> obj
  static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetTupleField : tuple:obj * index:int -> obj
  static member GetTupleFields : tuple:obj -> obj []
  static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
  static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
  static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
  static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
  static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
  ...

Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.GetUnionFields : value:obj * unionType:System.Type * ?allowAccessToPrivateRepresentation:bool -> UnionCaseInfo * obj []
static member FSharpValue.GetUnionFields : value:obj * unionType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> UnionCaseInfo * obj []
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
val case : UnionCaseInfo
property UnionCaseInfo.Name: string
val getInfo : v:string -> t:System.Type -> UnionCaseInfo option

Full name: Script.Union.getInfo
val v : string
val t : System.Type
type FSharpType =
  static member GetExceptionFields : exceptionType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetFunctionElements : functionType:Type -> Type * Type
  static member GetRecordFields : recordType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetTupleElements : tupleType:Type -> Type []
  static member GetUnionCases : unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo []
  static member IsExceptionRepresentation : exceptionType:Type * ?bindingFlags:BindingFlags -> bool
  static member IsFunction : typ:Type -> bool
  static member IsModule : typ:Type -> bool
  static member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> bool
  static member IsTuple : typ:Type -> bool
  ...

Full name: Microsoft.FSharp.Reflection.FSharpType
static member FSharpType.GetUnionCases : unionType:System.Type * ?allowAccessToPrivateRepresentation:bool -> UnionCaseInfo []
static member FSharpType.GetUnionCases : unionType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> UnionCaseInfo []
module Seq

from Microsoft.FSharp.Collections
val tryFind : predicate:('T -> bool) -> source:seq<'T> -> 'T option

Full name: Microsoft.FSharp.Collections.Seq.tryFind
val i : UnionCaseInfo
Multiple items
module String

from Script

--------------------
module String

from Microsoft.FSharp.Core
val makeUnion : v:UnionCaseInfo -> 'a

Full name: Script.Union.makeUnion
val v : UnionCaseInfo
static member FSharpValue.MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?allowAccessToPrivateRepresentation:bool -> obj
static member FSharpValue.MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:System.Reflection.BindingFlags -> obj
val parse : v:string -> 'a option

Full name: Script.Union.parse
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
val i : UnionCaseInfo option
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
property Option.Value: UnionCaseInfo
type Descriptor =
  {Name: string;
   Description: string;}

Full name: Script.Descriptor
Descriptor.Name: string
Descriptor.Description: string
val descriptor : Descriptor

Full name: Script.descriptor
val environment : Descriptor

Full name: Script.environment
val localize : v:string -> culture:string -> string

Full name: Script.AI.Language.localize
val culture : string
type Stat =
  | Idle
  | Walking
  | Closed

Full name: Script.AI.Stat
union case Stat.Idle: Stat
union case Stat.Walking: Stat
union case Stat.Closed: Stat
type Conversation =
  | ( Hello world )
  | ( Started walking )
  | ( Already walking )
  | ( Stoped walking )
  | ( Just idle )
  | Bye

Full name: Script.AI.Conversation
union case Conversation.Bye: Conversation
type Operator =
  {Culture: string;}

Full name: Script.AI.Operator
Operator.Culture: string
val defaultOperator : Operator

Full name: Script.AI.defaultOperator
val part : Descriptor

Full name: Script.AI.part
type Machine =
  {Model: string;
   Parts: seq<Descriptor>;}

Full name: Script.AI.Machine
Machine.Model: string
Machine.Parts: seq<Descriptor>
type R =
  {Name: string;
   Stat: Stat;
   Operator: Operator;
   LastConversation: string;
   Body: Machine;}

Full name: Script.AI.R
R.Name: string
Multiple items
R.Stat: Stat

--------------------
type Stat =
  | Idle
  | Walking
  | Closed

Full name: Script.AI.Stat
Multiple items
R.Operator: Operator

--------------------
type Operator =
  {Culture: string;}

Full name: Script.AI.Operator
R.LastConversation: string
R.Body: Machine
val defaultMachine : Machine

Full name: Script.AI.defaultMachine
val defaultAI : R

Full name: Script.AI.defaultAI
val say : v:'a -> m:R -> R

Full name: Script.AI.say
val v : 'a
val m : R
module Language

from Script.AI
module Union

from Script
R.Operator: Operator
val create : operator:Operator -> R

Full name: Script.AI.create
val operator : Operator
val close : m:R -> R

Full name: Script.AI.close
val walk : m:R -> R

Full name: Script.AI.walk
R.Stat: Stat
val stop : m:R -> R

Full name: Script.AI.stop
val d : (string * string) list

Full name: Script.AI.Knowledge.d
val s : m:R -> seq<Descriptor>

Full name: Script.AI.Knowledge.s
val i : string * string
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val i : Descriptor
val get : v:string -> m:R -> string

Full name: Script.AI.Knowledge.get
val r : Descriptor option
val p : Descriptor
property Option.Value: Descriptor
val describe : m:R -> string

Full name: Script.AI.Knowledge.describe
val name : string

Full name: Script.AI.name
val body : Machine

Full name: Script.AI.body
val knowledge : v:string -> m:R -> R

Full name: Script.AI.knowledge
module Knowledge

from Script.AI
val describe : m:R -> string

Full name: Script.AI.describe
module AI

from Script
val talk : f:R -> R

Full name: Script.talk
val f : R
val r : R
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val create : o:Operator -> R

Full name: Script.create
val o : Operator
type Command =
  | Walk
  | Stop
  | Exit
  | Close

Full name: Script.Command
union case Command.Walk: Command
union case Command.Stop: Command
union case Command.Exit: Command
union case Command.Close: Command
val proc : m:R -> v:string -> R

Full name: Script.proc
val c : Command
val sim : o:Operator -> Async<unit>

Full name: Script.sim
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val tracks : seq<R>
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
val state : R
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.ReadLine() : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.StartImmediate : computation:Async<unit> * ?cancellationToken:System.Threading.CancellationToken -> unit

More information

Link:http://fssnip.net/cR
Posted:12 years ago
Author:a_a
Tags: ai